mirror of
https://github.com/agentclientprotocol/claude-agent-acp.git
synced 2026-11-29 12:43:01 +00:00
fix: stop holding an MCP request that Claude Code gave up (#1711)
## Summary A subagent's MCP request waits up to 500 ms for the announcement of the subagent's child session. When Claude Code closed the request's HTTP connection during that wait, the adapter kept the request waiting until the tool use ended or the time passed. It now stops at once. `@agentclientprotocol/sdk` 2.12.0 gives the HTTP bridge's attribution resolver the request's `signal`. The signal aborts when the HTTP connection or the ACP connection closes. `McpOverAcp` ends the wait on it. What a request names does not change. The bridge already sent nothing for an abandoned request. ## Changes - `@agentclientprotocol/sdk` 2.11.0 to 2.12.0. - `src/mcp-servers.ts`: the wait for a child session's announcement also ends when the request's signal aborts. - `src/tests/mcp-servers.test.ts`: a request that Claude Code gives up stops waiting and sends nothing. ## Verification - `npm run check` passes. - `npm run test:run`: 4,552 tests passed and 53 were skipped. - The new test fails without the change: the request stays held.
This commit is contained in:
8
package-lock.json
generated
8
package-lock.json
generated
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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<void> {
|
||||
private held(toolUseId: string, abandoned: AbortSignal): Promise<void> {
|
||||
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<McpHttpRequestAttribution | undefined> {
|
||||
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;
|
||||
});
|
||||
|
||||
@@ -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<string, unknown> = {},
|
||||
headers: Record<string, string> | undefined = config.headers,
|
||||
toolUseId = "toolu_01",
|
||||
abandoned?: AbortSignal,
|
||||
): Promise<{ status: number; messages: Array<Record<string, any>> }> {
|
||||
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: <reason>`. 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
|
||||
|
||||
@@ -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<ReturnType<typeof rootSession>>;
|
||||
|
||||
@@ -548,6 +551,21 @@ describe("McpOverAcp", () => {
|
||||
const held = mcp as unknown as Record<"toolUses" | "waiting", Map<string, unknown>>;
|
||||
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<void> }>([
|
||||
|
||||
Reference in New Issue
Block a user