diff --git a/package-lock.json b/package-lock.json index 4d87da6f..0bc8d70a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.111.0", "license": "Apache-2.0", "dependencies": { - "@agentclientprotocol/sdk": "2.11.0", + "@agentclientprotocol/sdk": "2.12.0", "@anthropic-ai/claude-agent-sdk": "0.3.331", "diff": "9.0.0", "zod": "4.6.7" @@ -38,9 +38,9 @@ } }, "node_modules/@agentclientprotocol/sdk": { - "version": "2.11.0", - "resolved": "https://registry.npmjs.org/@agentclientprotocol/sdk/-/sdk-2.11.0.tgz", - "integrity": "sha512-B4MQTHjaP11lsc82XTm4rHcHl7CP3jfjAappFIXTs3gYwTTHdq4HtxSFOCKZSWD46FtW2Q9uF7wveKC62wEIvg==", + "version": "2.12.0", + "resolved": "https://registry.npmjs.org/@agentclientprotocol/sdk/-/sdk-2.12.0.tgz", + "integrity": "sha512-nSkamfY4Lw9/AVBU5G5V9p82ScXhANDMBybQwcPH/9+euUtZm04/VFUMaLj4gLFEXxQEy+QP1VQiKgSRbYVIPg==", "license": "Apache-2.0", "peerDependencies": { "@modelcontextprotocol/server": "^2.4.0", diff --git a/package.json b/package.json index 5220b26b..adc47884 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "node": ">=22" }, "dependencies": { - "@agentclientprotocol/sdk": "2.11.0", + "@agentclientprotocol/sdk": "2.12.0", "@anthropic-ai/claude-agent-sdk": "0.3.331", "diff": "9.0.0", "zod": "4.6.7" diff --git a/src/mcp-servers.ts b/src/mcp-servers.ts index 05ef0805..95958c92 100644 --- a/src/mcp-servers.ts +++ b/src/mcp-servers.ts @@ -495,7 +495,12 @@ export class McpOverAcp { /** * Resolves when a request of the tool use `toolUseId` need wait no longer * for the announcement of its agent's child session: {@link release} ended - * the wait, or {@link announcementWaitMs} passed. It never rejects. + * the wait, {@link announcementWaitMs} passed, or `abandoned` aborted. It + * never rejects. + * + * `abandoned` is the bridge's signal for the request. It aborts when Claude + * Code closes the request's HTTP connection or the ACP connection closes. + * The bridge then sends nothing, whatever the request names. * * Nothing that an announcement needs waits for the request. The hook that * recorded the tool use has answered. The session reads Claude Code's @@ -503,12 +508,14 @@ export class McpOverAcp { * to that stream before it ran the subagent's tool. A replay ends without * the request too. */ - private held(toolUseId: string): Promise { + private held(toolUseId: string, abandoned: AbortSignal): Promise { + if (abandoned.aborted) return Promise.resolve(); return new Promise((resolve) => { const waiters = this.waiting.get(toolUseId) ?? new Set<() => void>(); this.waiting.set(toolUseId, waiters); const end = () => { clearTimeout(timer); + abandoned.removeEventListener("abort", end); waiters.delete(end); if (waiters.size === 0 && this.waiting.get(toolUseId) === waiters) { this.waiting.delete(toolUseId); @@ -518,6 +525,7 @@ export class McpOverAcp { const timer = setTimeout(end, this.announcementWaitMs); // The request's HTTP connection keeps the process alive, not this timer. timer.unref(); + abandoned.addEventListener("abort", end, { once: true }); waiters.add(end); }); } @@ -547,6 +555,7 @@ export class McpOverAcp { private attribution({ serverId, params, + signal, }: McpHttpRequestInfo): McpHttpRequestAttribution | undefined | Promise { const meta = params?._meta; @@ -557,7 +566,7 @@ export class McpOverAcp { if (typeof toolUseId !== "string") return undefined; const now = this.named(toolUseId, serverId); if (now !== "unannounced") return now; - return this.held(toolUseId).then(() => { + return this.held(toolUseId, signal).then(() => { const then = this.named(toolUseId, serverId); return then === "unannounced" ? undefined : then; }); diff --git a/src/tests/helpers.ts b/src/tests/helpers.ts index 82ad2a93..1b6ef717 100644 --- a/src/tests/helpers.ts +++ b/src/tests/helpers.ts @@ -207,6 +207,8 @@ const CLAUDE_CODE_MCP_META = { * headers that mirror its version, method and tool name. A `tools/call` has * the id of its tool use (`toolUseId`) and a progress token in `_meta`. * `headers` replace the config's own, which carry the server's bearer token. + * `abandoned` closes the request's connection, as Claude Code does when it + * gives a request up. The promise then rejects. * * Resolves with the HTTP status and the JSON-RPC messages of the response: * the one message of a JSON body, or every message of an event stream. It @@ -218,6 +220,7 @@ export function claudeCodeMcpRequest( params: Record = {}, headers: Record | undefined = config.headers, toolUseId = "toolu_01", + abandoned?: AbortSignal, ): Promise<{ status: number; messages: Array> }> { const meta = method === "tools/call" @@ -234,6 +237,7 @@ export function claudeCodeMcpRequest( config.url, { method: "POST", + signal: abandoned, headers: { ...headers, accept: "application/json, text/event-stream", @@ -267,7 +271,7 @@ export function claudeCodeMcpRequest( /** * The message of the JSON-RPC error (`-33002`) that the HTTP bridge of - * `@agentclientprotocol/sdk` 2.11.0 answers Claude Code with, when the client + * `@agentclientprotocol/sdk` 2.12.0 answers Claude Code with, when the client * rejected the `mcp/message` request with `-32602` and the message * `Invalid params: `. Claude Code 2.1.330 passes it on as it is: as * the `error` of a PostToolUseFailure hook, and as the tool's result, which diff --git a/src/tests/mcp-servers.test.ts b/src/tests/mcp-servers.test.ts index 8fe868e5..6da79aff 100644 --- a/src/tests/mcp-servers.test.ts +++ b/src/tests/mcp-servers.test.ts @@ -337,12 +337,15 @@ describe("McpOverAcp", () => { agentId: agent?.id, agentType: agent?.type, }); - /** Claude Code sends `method` for the tool use `id`: what its `mcp/message` request named. */ - const request = async (id: string, method = "tools/call") => { - await claudeCodeMcpRequest(config, method, { name: "echo" }, undefined, id); + /** + * Claude Code sends `method` for the tool use `id`: what its `mcp/message` + * request named. It rejects when `abandoned` closes the request first. + */ + const request = async (id: string, method = "tools/call", abandoned?: AbortSignal) => { + await claudeCodeMcpRequest(config, method, { name: "echo" }, undefined, id, abandoned); return named.at(-1); }; - return { mcp, tools, hook, request }; + return { mcp, tools, hook, request, named }; } type RootSession = Awaited>; @@ -548,6 +551,21 @@ describe("McpOverAcp", () => { const held = mcp as unknown as Record<"toolUses" | "waiting", Map>; expect([held.toolUses.size, held.waiting.size]).toEqual([0, 0]); }); + + it("stops waiting, and sends nothing, when Claude Code gives the request up", async () => { + const { mcp, hook, request, named } = await rootSession(UNKNOWN_AGENT); + await hook("PreToolUse", "toolu_01", { id: "agent-1" }); + const abandoned = new AbortController(); + + const requested = request("toolu_01", "tools/call", abandoned.signal); + await vi.waitFor(() => expect(waiting(mcp).size).toBe(1)); + abandoned.abort(); + + await expect(requested).rejects.toThrow(); + // Not after the minute that the request would wait for an announcement. + await vi.waitFor(() => expect(waiting(mcp).size).toBe(0)); + expect(named).toEqual([]); + }); }); it.each<{ ended: string; end: (session: RootSession) => Promise }>([