Skip to content

Commit 45b0fe4

Browse files
fix(server): ACP terminal MCP calls answer interleaved server requests (pingdotgg#35805)
1 parent 5ceabb3 commit 45b0fe4

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎apps/server/src/mcp/AcpMcpStdioBridge.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,41 @@ describe("AcpMcpStdioBridge", () => {
392392
}),
393393
);
394394

395+
it.effect("answers a server ping that arrives before the tool result on its SSE stream", () =>
396+
Effect.gen(function* () {
397+
const sent: Array<{ id?: unknown; method?: string; result?: unknown }> = [];
398+
const result = yield* mcpToolSession({
399+
endpoint: "http://127.0.0.1:1/mcp",
400+
authorization: "Bearer bridge-test",
401+
clientName: "t3-code-acp-cli",
402+
fetchImplementation: async (_url, init) => {
403+
if (init?.method === "DELETE") return new Response(null, { status: 204 });
404+
const message = JSON.parse(String(init?.body)) as (typeof sent)[number];
405+
sent.push(message);
406+
if (message.method === "initialize") {
407+
return Response.json(
408+
{ jsonrpc: "2.0", id: message.id, result: { protocolVersion: "2025-06-18" } },
409+
{ headers: { "mcp-session-id": "session-42" } },
410+
);
411+
}
412+
if (message.method !== "tools/call") return new Response(null, { status: 202 });
413+
const ping = { jsonrpc: "2.0", id: "server-ping", method: "ping" };
414+
const reply = { jsonrpc: "2.0", id: message.id, result: { content: [] } };
415+
return new Response(
416+
`data: ${JSON.stringify(ping)}\n\ndata: ${JSON.stringify(reply)}\n\n`,
417+
{ headers: { "content-type": "text/event-stream" } },
418+
);
419+
},
420+
}).pipe(
421+
Effect.flatMap((session) => session.call("orchestrator_capabilities", {})),
422+
Effect.scoped,
423+
);
424+
425+
expect(result).toEqual({ content: [] });
426+
expect(sent).toContainEqual({ jsonrpc: "2.0", id: "server-ping", result: {} });
427+
}),
428+
);
429+
395430
it.effect("aborts the pending fetch when the stdio bridge is interrupted", () =>
396431
Effect.gen(function* () {
397432
const started = yield* Deferred.make<AbortSignal | undefined>();

‎apps/server/src/mcp/AcpMcpStdioBridge.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ function protocolVersionOf(entry: unknown): string | null {
5858
return typeof version === "string" && version.length > 0 ? version : null;
5959
}
6060

61+
function replyToServerRequest(envelope: JsonRpcEnvelope): unknown {
62+
return envelope.method === "ping"
63+
? { jsonrpc: "2.0", id: envelope.id, result: {} }
64+
: { jsonrpc: "2.0", id: envelope.id, error: { code: -32601, message: "Method not found" } };
65+
}
66+
6167
export interface McpToolSessionOptions {
6268
readonly endpoint: string;
6369
readonly authorization: string;
@@ -112,10 +118,16 @@ export const mcpToolSession = (
112118
message: `T3 Code MCP endpoint responded with HTTP ${response.status}.`,
113119
});
114120
}
115-
const payloads = yield* Stream.runCollect(responsePayloads(response, bridgeError));
116-
for (const payload of payloads) {
121+
const payloads: Array<unknown> = [];
122+
yield* Stream.runForEach(responsePayloads(response, bridgeError), (payload) => {
117123
protocolVersion = protocolVersionOf(payload) ?? protocolVersion;
118-
}
124+
const envelope = asEnvelope(payload);
125+
if (envelope?.id !== undefined && typeof envelope.method === "string") {
126+
return send(replyToServerRequest(envelope)).pipe(Effect.asVoid);
127+
}
128+
payloads.push(payload);
129+
return Effect.void;
130+
});
119131
return payloads;
120132
});
121133

0 commit comments

Comments
 (0)