Skip to content
TypeScript

fix(server): abort interrupted ACP MCP fetches (#23955)

Julius Marmingee64a6d4ca84e
diff --git a/apps/server/src/mcp/AcpMcpStdioBridge.test.ts b/apps/server/src/mcp/AcpMcpStdioBridge.test.ts
index 0368c897142..0f1bedc17c6 100644
--- a/apps/server/src/mcp/AcpMcpStdioBridge.test.ts
+++ b/apps/server/src/mcp/AcpMcpStdioBridge.test.ts
@@ -2,10 +2,11 @@
import * as NodeStream from "node:stream";
import { describe, expect, it } from "@effect/vitest";
+import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
-import { runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
+import { mcpToolSession, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
function makeHarness(responder: (request: Request) => Promise<Response> | Response) {
const input = new NodeStream.PassThrough();
@@ -29,6 +30,13 @@ function makeHarness(responder: (request: Request) => Promise<Response> | Respon
return { input, written, requests, bridge };
}
+function pendingFetch(started: Deferred.Deferred<AbortSignal | undefined>) {
+ return (_url: string, init?: RequestInit): Promise<Response> => {
+ Deferred.doneUnsafe(started, Effect.succeed(init?.signal ?? undefined));
+ return new Promise(() => {});
+ };
+}
+
describe("AcpMcpStdioBridge", () => {
it.effect("forwards requests and replays the session id and protocol version", () =>
Effect.gen(function* () {
@@ -282,4 +290,43 @@ describe("AcpMcpStdioBridge", () => {
expect(JSON.parse(written[0]!)).toMatchObject({ id: 7, result: {} });
}),
);
+
+ it.effect("aborts the pending fetch when a tool call is interrupted", () =>
+ Effect.gen(function* () {
+ const started = yield* Deferred.make<AbortSignal | undefined>();
+ const fiber = yield* mcpToolSession({
+ endpoint: "http://127.0.0.1:1/mcp",
+ authorization: "Bearer bridge-test",
+ clientName: "t3-code-acp-cli",
+ fetchImplementation: pendingFetch(started),
+ }).pipe(
+ Effect.flatMap((session) => session.call("orchestrator_capabilities", {})),
+ Effect.scoped,
+ Effect.forkChild,
+ );
+
+ const signal = yield* Deferred.await(started);
+ yield* Fiber.interrupt(fiber);
+ expect(signal?.aborted).toBe(true);
+ }),
+ );
+
+ it.effect("aborts the pending fetch when the stdio bridge is interrupted", () =>
+ Effect.gen(function* () {
+ const started = yield* Deferred.make<AbortSignal | undefined>();
+ const input = new NodeStream.PassThrough();
+ const fiber = yield* runAcpMcpStdioBridge({
+ endpoint: "http://127.0.0.1:1/mcp",
+ authorization: "Bearer bridge-test",
+ input,
+ output: { write: () => undefined },
+ fetchImplementation: pendingFetch(started),
+ }).pipe(Effect.forkChild);
+
+ input.write(`${JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" })}\n`);
+ const signal = yield* Deferred.await(started);
+ yield* Fiber.interrupt(fiber);
+ expect(signal?.aborted).toBe(true);
+ }),
+ );
});
diff --git a/apps/server/src/mcp/AcpMcpStdioBridge.ts b/apps/server/src/mcp/AcpMcpStdioBridge.ts
index 2db36ce5abc..ea9a4c93b00 100644
--- a/apps/server/src/mcp/AcpMcpStdioBridge.ts
+++ b/apps/server/src/mcp/AcpMcpStdioBridge.ts
@@ -92,11 +92,12 @@ export const mcpToolSession = (
message: unknown,
): Effect.fn.Return<ReadonlyArray<unknown>, AcpMcpBridgeError> {
const response = yield* Effect.tryPromise({
- try: () =>
+ try: (signal) =>
fetchImplementation(options.endpoint, {
method: "POST",
headers: { "content-type": "application/json", ...headers() },
body: JSON.stringify(message),
+ signal,
}),
catch: bridgeError,
});
@@ -212,7 +213,7 @@ export function runAcpMcpStdioBridge(options: AcpMcpStdioBridgeOptions): Effect.
)(
function* (line: string, envelope: JsonRpcEnvelope) {
const response = yield* Effect.tryPromise({
- try: () =>
+ try: (signal) =>
fetchImplementation(options.endpoint, {
method: "POST",
headers: {
@@ -223,6 +224,7 @@ export function runAcpMcpStdioBridge(options: AcpMcpStdioBridgeOptions): Effect.
...(protocolVersion === null ? {} : { "mcp-protocol-version": protocolVersion }),
},
body: line,
+ signal,
}),
catch: bridgeError,
});