Skip to content

Commit e64a6d4

Browse files
juliusmarmingekiiweeclaude
authored
fix(server): abort interrupted ACP MCP fetches (#23955)
Co-authored-by: kiiwee <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 9a63802 commit e64a6d4

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import * as NodeStream from "node:stream";
33

44
import { describe, expect, it } from "@effect/vitest";
5+
import * as Deferred from "effect/Deferred";
56
import * as Effect from "effect/Effect";
67
import * as Fiber from "effect/Fiber";
78

8-
import { runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
9+
import { mcpToolSession, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
910

1011
function makeHarness(responder: (request: Request) => Promise<Response> | Response) {
1112
const input = new NodeStream.PassThrough();
@@ -29,6 +30,13 @@ function makeHarness(responder: (request: Request) => Promise<Response> | Respon
2930
return { input, written, requests, bridge };
3031
}
3132

33+
function pendingFetch(started: Deferred.Deferred<AbortSignal | undefined>) {
34+
return (_url: string, init?: RequestInit): Promise<Response> => {
35+
Deferred.doneUnsafe(started, Effect.succeed(init?.signal ?? undefined));
36+
return new Promise(() => {});
37+
};
38+
}
39+
3240
describe("AcpMcpStdioBridge", () => {
3341
it.effect("forwards requests and replays the session id and protocol version", () =>
3442
Effect.gen(function* () {
@@ -282,4 +290,43 @@ describe("AcpMcpStdioBridge", () => {
282290
expect(JSON.parse(written[0]!)).toMatchObject({ id: 7, result: {} });
283291
}),
284292
);
293+
294+
it.effect("aborts the pending fetch when a tool call is interrupted", () =>
295+
Effect.gen(function* () {
296+
const started = yield* Deferred.make<AbortSignal | undefined>();
297+
const fiber = yield* mcpToolSession({
298+
endpoint: "http://127.0.0.1:1/mcp",
299+
authorization: "Bearer bridge-test",
300+
clientName: "t3-code-acp-cli",
301+
fetchImplementation: pendingFetch(started),
302+
}).pipe(
303+
Effect.flatMap((session) => session.call("orchestrator_capabilities", {})),
304+
Effect.scoped,
305+
Effect.forkChild,
306+
);
307+
308+
const signal = yield* Deferred.await(started);
309+
yield* Fiber.interrupt(fiber);
310+
expect(signal?.aborted).toBe(true);
311+
}),
312+
);
313+
314+
it.effect("aborts the pending fetch when the stdio bridge is interrupted", () =>
315+
Effect.gen(function* () {
316+
const started = yield* Deferred.make<AbortSignal | undefined>();
317+
const input = new NodeStream.PassThrough();
318+
const fiber = yield* runAcpMcpStdioBridge({
319+
endpoint: "http://127.0.0.1:1/mcp",
320+
authorization: "Bearer bridge-test",
321+
input,
322+
output: { write: () => undefined },
323+
fetchImplementation: pendingFetch(started),
324+
}).pipe(Effect.forkChild);
325+
326+
input.write(`${JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" })}\n`);
327+
const signal = yield* Deferred.await(started);
328+
yield* Fiber.interrupt(fiber);
329+
expect(signal?.aborted).toBe(true);
330+
}),
331+
);
285332
});

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ export const mcpToolSession = (
9292
message: unknown,
9393
): Effect.fn.Return<ReadonlyArray<unknown>, AcpMcpBridgeError> {
9494
const response = yield* Effect.tryPromise({
95-
try: () =>
95+
try: (signal) =>
9696
fetchImplementation(options.endpoint, {
9797
method: "POST",
9898
headers: { "content-type": "application/json", ...headers() },
9999
body: JSON.stringify(message),
100+
signal,
100101
}),
101102
catch: bridgeError,
102103
});
@@ -212,7 +213,7 @@ export function runAcpMcpStdioBridge(options: AcpMcpStdioBridgeOptions): Effect.
212213
)(
213214
function* (line: string, envelope: JsonRpcEnvelope) {
214215
const response = yield* Effect.tryPromise({
215-
try: () =>
216+
try: (signal) =>
216217
fetchImplementation(options.endpoint, {
217218
method: "POST",
218219
headers: {
@@ -223,6 +224,7 @@ export function runAcpMcpStdioBridge(options: AcpMcpStdioBridgeOptions): Effect.
223224
...(protocolVersion === null ? {} : { "mcp-protocol-version": protocolVersion }),
224225
},
225226
body: line,
227+
signal,
226228
}),
227229
catch: bridgeError,
228230
});

0 commit comments

Comments
 (0)