Skip to content

Commit 0671723

Browse files
fix(acp): close the MCP session of an initialize replaced while in flight
Overlapping current-draft initializes left the first session open on the MCP endpoint, which only frees sessions on DELETE. A replaced connection is now retired and closes its session as soon as its in-flight response sets one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c6e6db0 commit 0671723

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎packages/provider-acp/src/server/mcpOverAcpBridge.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,52 @@ describe("AcpMcpOverAcpBridge", () => {
311311
}).pipe(Effect.scoped),
312312
);
313313

314+
it.effect("closes the session of an initialize that a newer initialize replaced in flight", () =>
315+
Effect.gen(function* () {
316+
const firstStarted = Promise.withResolvers<void>();
317+
const firstResponse = Promise.withResolvers<void>();
318+
const deleted: Array<string | null> = [];
319+
let sessions = 0;
320+
const bridge = yield* makeAcpMcpOverAcpBridge({
321+
endpoint: "http://127.0.0.1:1/mcp",
322+
authorization: "Bearer bridge-test",
323+
allocateConnectionId: Effect.succeed("connection-1"),
324+
fetchImplementation: async (_url, init) => {
325+
if (init?.method === "DELETE") {
326+
deleted.push(new Headers(init.headers).get("mcp-session-id"));
327+
return new Response(null, { status: 204 });
328+
}
329+
const request = JSON.parse(String(init?.body)) as { readonly id?: unknown };
330+
const sessionId = `session-${++sessions}`;
331+
if (sessionId === "session-1") {
332+
firstStarted.resolve();
333+
await firstResponse.promise;
334+
}
335+
return new Response(
336+
JSON.stringify({
337+
jsonrpc: "2.0",
338+
id: request.id,
339+
result: { protocolVersion: "2025-06-18" },
340+
}),
341+
{ headers: { "content-type": "application/json", "mcp-session-id": sessionId } },
342+
);
343+
},
344+
});
345+
const initialize = (requestId: string) =>
346+
bridge.message({ serverId: "t3-code", requestId, method: "initialize" });
347+
348+
const first = yield* initialize("mcp-1").pipe(Effect.forkChild);
349+
yield* Effect.promise(() => firstStarted.promise);
350+
yield* initialize("mcp-2");
351+
firstResponse.resolve();
352+
yield* Fiber.join(first);
353+
expect(deleted).toEqual(["session-1"]);
354+
355+
yield* bridge.dispose;
356+
expect(deleted).toEqual(["session-1", "session-2"]);
357+
}).pipe(Effect.scoped),
358+
);
359+
314360
it.effect("closes the current-draft session its notifications ride on when disposed", () =>
315361
Effect.gen(function* () {
316362
const requests: Array<{ readonly method: string; readonly headers: Headers }> = [];
@@ -377,6 +423,11 @@ describe("AcpMcpOverAcpBridge", () => {
377423
expect(yield* bridge.connect({ serverId: "other" }).pipe(Effect.flip)).toMatchObject({
378424
_tag: "AcpMcpOverAcpError",
379425
});
426+
expect(
427+
yield* bridge
428+
.message({ serverId: "other", requestId: "mcp-1", method: "tools/list" })
429+
.pipe(Effect.flip),
430+
).toMatchObject({ message: 'Unknown ACP MCP server "other".' });
380431
expect(
381432
yield* bridge
382433
.message({ connectionId: "missing", method: "tools/list" })

‎packages/provider-acp/src/server/mcpOverAcpBridge.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface Connection {
2323
sessionId: string | null;
2424
protocolVersion: string | null;
2525
nextRequestId: number;
26+
retired: boolean;
2627
}
2728

2829
export class AcpMcpOverAcpError extends Error {
@@ -104,6 +105,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
104105
sessionId: null,
105106
protocolVersion: null,
106107
nextRequestId: 0,
108+
retired: false,
107109
}));
108110
// Current-draft agents address the declared server directly; each initialize opens a new MCP session.
109111
let serverConnection = yield* makeConnection;
@@ -136,6 +138,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
136138

137139
const renewServerConnection = Effect.gen(function* () {
138140
const previous = serverConnection;
141+
previous.retired = true;
139142
serverConnection = yield* makeConnection;
140143
yield* close(previous).pipe(Effect.ignore);
141144
return serverConnection;
@@ -215,6 +218,8 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
215218
for (const payload of payloads) {
216219
connection.protocolVersion = protocolVersionOf(payload) ?? connection.protocolVersion;
217220
}
221+
// An initialize that was replaced while in flight must not leave its new session behind.
222+
if (connection.retired) yield* close(connection).pipe(Effect.ignore);
218223
return payloads;
219224
}).pipe(Effect.mapError(bridgeError)),
220225
);
@@ -244,6 +249,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
244249
),
245250
);
246251
}
252+
connection.sessionId = null;
247253
yield* discardResponseBody(response);
248254
});
249255

0 commit comments

Comments
 (0)