inspector/test-servers/src/modern-tasks.ts
Cliff Hall 4defa32ac6
chore: close a batch of quality-gate & tidy gaps (#1785 #1787 #1789 #1790 #1767) (#1792)
* chore: close a batch of quality-gate & tidy gaps (#1785 #1787 #1789 #1790 #1767)

Bundles five small, related tidy/gate items into one PR:

- #1785: delete the dead src/types/navigation.ts InspectorTab (0 importers;
  duplicated/drifted from utils/inspectorTabs InspectorTabId). src/types/
  now holds only ambient .d.ts stubs.
- #1787: add src/hooks/** to the web coverage `include` (it was silently
  outside the ≥90 gate) and add tests for the two untested hooks,
  useServerJsonImport and useImportClientConfig, bringing every hook to
  ≥90 on lines/statements/functions/branches.
- #1789: widen clients/{cli,tui,launcher} format/format:check to their
  top-level configs; add root format:shared / format:check:shared covering
  test-servers/src/**, vitest.shared.mts, and the repo-root eslint.config.js.
- #1790: pin prettier to an exact version (3.8.4, dropping the ^3.8.1
  caret) in all five package.json files so the format gate's verdict can't
  shift with an in-range patch bump; lockfiles synced.
- #1767: its root-file format+lint gap is closed here (format:shared +
  a new lint:shared via a second files block in the root eslint.config.js;
  web/server/ was already gated in #1788). Its larger cli/tui __tests__
  typecheck piece (76 type errors) is split out to #1791.

Prettier-normalized the newly-covered files (pure formatting). No runtime
behavior change. `npm run ci` green.

Closes #1785
Closes #1787
Closes #1789
Closes #1790
Closes #1767

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5

* chore: address PR #1792 review findings

- #1789 gap: widen clients/web format to include .storybook and clients/
  launcher format to include scripts/; reformat the 3 .storybook files
  (were non-conforming under the pinned prettier 3.8.4). Audited all 885
  tracked source files — now fully prettier-clean.
- Export VALIDATE_DEBOUNCE_MS / COLLAPSE_DELAY_MS / HIGHLIGHT_DURATION_MS
  from useServerJsonImport and use them in the test instead of hardcoded
  300/1000/250 (so a tuned constant can't silently desync the test).
- Assert the symmetric renameErrors["alpha"] alongside ["beta"] so a
  one-directional collision-check regression fails.
- Comment the blank-rename test as exercising a UI-unreachable defensive
  branch (canImport gates submit).
- Un-stale the two specification/v2_ux_interfaces_plan.md references to the
  now-removed navigation.ts InspectorTab.

npm run ci green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5

* docs: sync AGENTS.md format-surface description with the widened globs (#1792 review r2)

1ce3b64 added .storybook to clients/web's format glob and scripts/ to
clients/launcher's, but AGENTS.md:222 still described the pre-fix scope.
Bring it in line and note the non-recursive top-level glob is why nested
dirs are named explicitly. Docs only.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5

* chore: handle PR #1792 round-3 follow-ups in-place (format-coverage guard, uniform globs, theme gate)

Rather than defer these to tickets, fold them into this PR:

- Obs 2: widen every client + root format glob to a uniform
  *.{ts,tsx,mts,cts,js,jsx,mjs,cjs} so a future .mts/.cjs/.tsx top-level
  file can't silently slip the gate.
- Obs 1: add scripts/verify-format-coverage.mjs (wired into `validate`) —
  it parses the format:check globs out of every package.json, enumerates
  all tracked source files, and fails listing any not covered by a glob.
  The durable, standing version of #1789's one-shot audit; negative-tested.
- Obs 3: bring src/theme/** under the coverage gate (21/23 files were
  already ≥90 transitively; add Paper.test.tsx for the one laggard,
  Paper.ts). src/App.tsx (~4.5k lines, ~42% branches) is converted from a
  silent omission into an explicit, documented exclusion (comment on the
  coverage `include` + AGENTS.md) — gating it is a dedicated effort, not a
  whitelist tweak.

npm run ci green (the new guard reports 885/885 tracked source files gated).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5

* chore: address PR #1792 round-4 findings (guard hardening, docs, exclusion list)

- Harden verify-format-coverage: only harvest format:check globs from
  scripts transitively reachable from `validate` (via npm run refs), so an
  unwired/renamed format:check no longer counts as coverage. Negative-tested
  — renaming clients/web format:check to an unreferenced name now fails the
  guard (627 files flagged) instead of passing.
- Name src/main.tsx and src/index.ts (browser + bin bootstraps) alongside
  src/App.tsx in the coverage-include comment and AGENTS.md, so the
  documented exclusion list is complete rather than claiming App.tsx is the
  only one.
- Doc sync: document verify:format-coverage (AGENTS.md + README script table
  + scripts/ layout line), fix the `validate` ordering (verify:format-
  coverage now runs before validate:core) in AGENTS.md and README, and
  update the quoted globs to the uniform *.{ts,tsx,mts,cts,js,jsx,mjs,cjs} /
  test-servers *.{ts,tsx,mts,cts}.

npm run ci green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5

* chore: address PR #1792 round-5 findings (airtight guard + doc/config sync)

- verify-format-coverage: assert the root `validate` chain invokes each
  client's `validate` (cd <dir> && npm run validate), closing the last
  reachability edge — a client dropped from the root chain now fails the
  guard even though its own globs still exist. Negative-tested.
- eslint.config.js: widen the shared `files` block to
  test-servers/src/**/*.{ts,tsx,mts,cts} to match the widened lint:shared
  glob, so a future .mts/.cts there is lint-gated, not just prettier-gated.
- AGENTS.md: scope the "uniform extension set" claim to client globs
  (core stays {ts,tsx}, shared {ts,tsx,mts,cts}), with verify:format-coverage
  as the backstop — removing the self-contradiction with the line below.

npm run ci green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-26 10:43:11 -04:00

418 lines
14 KiB
TypeScript

/**
* Modern (2026-07-28) Tasks extension server — SEP-2663
* (`io.modelcontextprotocol/tasks`).
*
* SDK v2 removed all built-in tasks support, and the redesigned extension is a
* different wire shape than the deleted 2025-11-25 runtime (`ttlMs`/`pollIntervalMs`,
* a polymorphic `DetailedTask` inlining `result`/`error`/`inputRequests`,
* `tasks/get` polling, a new `tasks/update`, no `tasks/list`, no blocking
* `tasks/result`). This module wires a minimal, poll-driven modern task runtime
* by hand so an Inspector connecting with **Protocol Era = Modern** can exercise
* the full flow: a task-augmented `tools/call` returns a `CreateTaskResult`
* (`resultType: "task"`); the client polls `tasks/get`; an `input_required` task
* surfaces an embedded elicitation the client answers via `tasks/update`; and
* the completed task inlines its `CallToolResult`.
*
* The runtime is a single shared `Map` so it survives across the stateless
* modern leg's per-request server instances (each `createMcpHandler` request
* builds a fresh `McpServer`, but they share one runtime via the config).
*/
import * as z from "zod/v4";
import type { RequestHandler } from "express";
import type { McpServer } from "@modelcontextprotocol/server";
import type { ToolDefinition } from "./composable-test-server.js";
/** SEP-2133 extension identifier for the redesigned Tasks extension. */
export const TASKS_EXTENSION_KEY = "io.modelcontextprotocol/tasks";
/** Names of the modern task-augmented tools whose `tools/call` returns a task. */
export const MODERN_TASK_TOOL_NAMES = new Set([
"modern_task",
"modern_input_task",
// Never completes — stays `input_required` on every poll, so a client keeps
// being re-prompted. Used to exercise the client's input-round cap.
"modern_loop_task",
]);
const DEFAULT_TTL_MS = 60_000;
const DEFAULT_POLL_INTERVAL_MS = 500;
/** `modern_task` reports `working` for this many `tasks/get` polls before completing. */
const SIMPLE_WORKING_POLLS = 2;
type ModernTaskStatus =
| "working"
| "input_required"
| "completed"
| "failed"
| "cancelled";
interface ModernTaskEntry {
taskId: string;
kind: "simple" | "input" | "loop";
status: ModernTaskStatus;
createdAt: string;
lastUpdatedAt: string;
args: Record<string, unknown>;
/** Remaining `working` polls for the simple task before it completes. */
pollsRemaining: number;
/** Set once the client answered the embedded elicitation via `tasks/update`. */
inputSatisfied?: boolean;
/** The `inputResponses` the client submitted, echoed back in the result. */
inputResponses?: Record<string, unknown>;
}
/** The embedded elicitation an `input_required` modern task surfaces. Shaped as
* a standalone `elicitation/create` request so the client's pending-request UI
* (reused from the MRTR path) renders it and returns an `ElicitResult`. */
function confirmInputRequests(): Record<string, unknown> {
return {
confirm: {
method: "elicitation/create",
params: {
message: "Approve this task before it continues?",
requestedSchema: {
type: "object",
properties: {
approved: {
type: "boolean",
title: "Approved",
description: "Whether to proceed with the task",
},
},
required: ["approved"],
},
},
},
};
}
function nowIso(): string {
return new Date().toISOString();
}
function newTaskId(): string {
return (
globalThis.crypto?.randomUUID?.() ??
`task-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`
);
}
/**
* Shared, in-memory modern task runtime. One instance is created per modern
* server config and reused across the stateless leg's per-request servers, so a
* task created by one `tools/call` request is visible to a later `tasks/get`.
*/
export class ModernTaskRuntime {
private tasks = new Map<string, ModernTaskEntry>();
/** Handle a task-augmented `tools/call`: durably create the task and return a
* `CreateTaskResult` (`resultType: "task"`) seed. */
createTask(
toolName: string,
args: Record<string, unknown>,
): Record<string, unknown> {
const now = nowIso();
const kind: ModernTaskEntry["kind"] =
toolName === "modern_input_task"
? "input"
: toolName === "modern_loop_task"
? "loop"
: "simple";
const entry: ModernTaskEntry = {
taskId: newTaskId(),
kind,
status: "working",
createdAt: now,
lastUpdatedAt: now,
args,
pollsRemaining: SIMPLE_WORKING_POLLS,
};
this.tasks.set(entry.taskId, entry);
return {
resultType: "task",
...this.project(entry),
statusMessage: "The operation is now in progress.",
};
}
/** Serve `tasks/get`: advance the scripted lifecycle and return a
* `DetailedTask` (`resultType: "complete"`). Throws on an unknown id. */
getTask(taskId: string): Record<string, unknown> {
const entry = this.requireTask(taskId);
this.advance(entry);
return { resultType: "complete", ...this.project(entry) };
}
/** Serve `tasks/update`: record the answered `inputResponses` and ack empty. */
updateTask(
taskId: string,
inputResponses: Record<string, unknown> | undefined,
): Record<string, unknown> {
const entry = this.requireTask(taskId);
entry.inputSatisfied = true;
if (inputResponses) entry.inputResponses = inputResponses;
this.touch(entry);
return { resultType: "complete" };
}
/** Serve `tasks/cancel`: move the task to `cancelled` and ack empty. */
cancelTask(taskId: string): Record<string, unknown> {
const entry = this.requireTask(taskId);
if (
entry.status !== "completed" &&
entry.status !== "failed" &&
entry.status !== "cancelled"
) {
entry.status = "cancelled";
this.touch(entry);
}
return { resultType: "complete" };
}
private requireTask(taskId: string): ModernTaskEntry {
const entry = this.tasks.get(taskId);
if (!entry) throw new Error(`Unknown taskId: ${taskId}`);
return entry;
}
private touch(entry: ModernTaskEntry): void {
entry.lastUpdatedAt = nowIso();
}
/** Move a task one step along its scripted lifecycle on each poll. */
private advance(entry: ModernTaskEntry): void {
if (
entry.status === "completed" ||
entry.status === "failed" ||
entry.status === "cancelled"
) {
return;
}
if (entry.kind === "simple") {
if (entry.pollsRemaining > 0) {
entry.pollsRemaining -= 1;
} else {
entry.status = "completed";
}
} else if (entry.kind === "loop") {
// Never advances: stays input_required every poll (ignores tasks/update),
// so a client is re-prompted indefinitely — exercises its round cap.
entry.status = "input_required";
} else {
// input task: request input, then complete once the client has answered.
entry.status = entry.inputSatisfied ? "completed" : "input_required";
}
this.touch(entry);
}
/** Project the internal entry onto the wire `DetailedTask`, inlining the
* status-specific member (`result` / `inputRequests`). */
private project(entry: ModernTaskEntry): Record<string, unknown> {
const base: Record<string, unknown> = {
taskId: entry.taskId,
status: entry.status,
createdAt: entry.createdAt,
lastUpdatedAt: entry.lastUpdatedAt,
ttlMs: DEFAULT_TTL_MS,
};
// The simple task advertises a poll interval; the input task omits it so a
// client falls back to its own default cadence (both paths exercised).
if (entry.kind === "simple") {
base.pollIntervalMs = DEFAULT_POLL_INTERVAL_MS;
}
if (entry.status === "input_required") {
base.inputRequests = confirmInputRequests();
}
if (entry.status === "completed") {
base.result = this.completedResult(entry);
}
return base;
}
/** The `CallToolResult` a completed task inlines (SEP-2663: no `tasks/result`). */
private completedResult(entry: ModernTaskEntry): Record<string, unknown> {
const detail =
entry.kind === "input"
? `input=${JSON.stringify(entry.inputResponses ?? {})}`
: `args=${JSON.stringify(entry.args ?? {})}`;
return {
content: [
{
type: "text",
text: `Modern task ${entry.taskId} completed (${detail}).`,
},
],
isError: false,
};
}
}
/** The two plain tools whose `tools/call` the modern task handlers intercept.
* Their handlers are placeholders — {@link wireModernTaskHandlers} routes these
* names to the runtime before the SDK handler runs. */
export function createModernTaskTools(): ToolDefinition[] {
const placeholder: ToolDefinition["handler"] = async () => ({
content: [
{ type: "text" as const, text: "Task tool must be invoked as a task." },
],
isError: true,
});
return [
{
name: "modern_task",
description:
"Create a modern (SEP-2663) task that reports progress over a few polls then completes.",
inputSchema: {
message: z
.string()
.optional()
.describe("Text echoed back in the completed task result."),
},
handler: placeholder,
},
{
name: "modern_input_task",
description:
"Create a modern task that pauses at input_required and completes after tasks/update.",
inputSchema: {},
handler: placeholder,
},
{
name: "modern_loop_task",
description:
"Create a modern task that never completes — stays input_required every poll, so the client's round cap trips.",
inputSchema: {},
handler: placeholder,
},
];
}
interface RawHandlerHost {
_requestHandlers: Map<
string,
(request: unknown, ctx: unknown) => Promise<unknown>
>;
}
interface ToolsCallRequest {
params: { name: string; arguments?: Record<string, unknown> };
}
interface TaskMethodRequest {
params: {
taskId: string;
inputResponses?: Record<string, unknown>;
};
}
/**
* Wire the modern task methods by hand onto an `McpServer`:
* - override `tools/call` so a modern task tool returns a `CreateTaskResult`
* (`resultType: "task"`) and durably creates the task; ordinary tools fall
* through to the SDK handler;
* - register raw `tasks/get` / `tasks/update` / `tasks/cancel` handlers backed
* by the shared runtime. There is deliberately **no** `tasks/list` and **no**
* `tasks/result` (SEP-2663 removed both).
*
* The `tools/call` override is installed into the private handler registry (not
* via `setRequestHandler`) so the `CreateTaskResult` skips the `Server`'s
* `tools/call` result-schema validation — the same seam the legacy task server
* uses. This goes away when the SDK models the tasks extension natively.
*/
export function wireModernTaskHandlers(
mcpServer: McpServer,
runtime: ModernTaskRuntime,
): void {
const lowLevel = mcpServer.server;
// Reach the private `_requestHandlers` map (`RawHandlerHost`) to register a
// raw `tools/call` handler that skips the SDK's result-schema validation, so a
// task-augmented `CreateTaskResult` gets through. Mirrors the client-side
// bypass in `core/mcp/inspectorClient.ts` and the sibling in
// `composable-test-server.ts`; both go away when the SDK models the tasks
// extension natively.
const registry = (lowLevel as unknown as RawHandlerHost)._requestHandlers;
const sdkToolsCall = registry.get("tools/call");
registry.set("tools/call", async (request, ctx) => {
const req = request as ToolsCallRequest;
if (MODERN_TASK_TOOL_NAMES.has(req.params.name)) {
return runtime.createTask(req.params.name, req.params.arguments ?? {});
}
if (!sdkToolsCall) {
throw new Error("tools/call handler is not initialized");
}
return sdkToolsCall(request, ctx);
});
registry.set("tasks/get", async (request) => {
const req = request as TaskMethodRequest;
return runtime.getTask(req.params.taskId);
});
registry.set("tasks/update", async (request) => {
const req = request as TaskMethodRequest;
return runtime.updateTask(req.params.taskId, req.params.inputResponses);
});
registry.set("tasks/cancel", async (request) => {
const req = request as TaskMethodRequest;
return runtime.cancelTask(req.params.taskId);
});
}
const MODERN_TASK_METHODS = new Set([
"tasks/get",
"tasks/update",
"tasks/cancel",
]);
interface TaskRpcBody {
jsonrpc?: string;
id?: unknown;
method?: string;
params?: { taskId?: string; inputResponses?: Record<string, unknown> };
}
/**
* Express middleware that answers modern `tasks/get` / `tasks/update` /
* `tasks/cancel` POSTs DIRECTLY, before the SDK's `createMcpHandler` sees them.
* SDK v2's modern leg era-gates inbound spec methods, so `tasks/*` (spec-method
* names removed from the 2026-07-28 era) would be answered `-32601 Method not
* found` by the handler — a conformant server can't serve the extension through
* the SDK. Intercepting at the HTTP layer is how the test server serves the
* extension anyway (mirrors the `specErrorInjector` seam). `tasks` task
* creation still flows through `tools/call` (a modern method) inside the handler.
*/
export function createModernTaskInterceptor(
getRuntime: () => ModernTaskRuntime,
): RequestHandler {
return (req, res, next) => {
const body = req.body as TaskRpcBody | undefined;
const method = body?.method;
if (!method || !MODERN_TASK_METHODS.has(method)) {
next();
return;
}
const runtime = getRuntime();
const id = body?.id ?? null;
const taskId = body?.params?.taskId ?? "";
try {
let result: Record<string, unknown>;
if (method === "tasks/get") {
result = runtime.getTask(taskId);
} else if (method === "tasks/update") {
result = runtime.updateTask(taskId, body?.params?.inputResponses);
} else {
result = runtime.cancelTask(taskId);
}
res.json({ jsonrpc: "2.0", id, result });
} catch (err) {
res.json({
jsonrpc: "2.0",
id,
error: { code: -32602, message: (err as Error).message },
});
}
};
}