Skip to content

Commit 41606fa

Browse files
fix(acp): an initialize keeps the connection it opened while the old session closes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0671723 commit 41606fa

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

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

Lines changed: 46 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("keeps each initialize on its own session while the replaced one is closing", () =>
315+
Effect.gen(function* () {
316+
const deleteStarted = Promise.withResolvers<void>();
317+
const deleteResponse = Promise.withResolvers<void>();
318+
let sessions = 0;
319+
const bridge = yield* makeAcpMcpOverAcpBridge({
320+
endpoint: "http://127.0.0.1:1/mcp",
321+
authorization: "Bearer bridge-test",
322+
allocateConnectionId: Effect.succeed("connection-1"),
323+
fetchImplementation: async (_url, init) => {
324+
const sessionId = new Headers(init?.headers).get("mcp-session-id");
325+
if (init?.method === "DELETE") {
326+
if (sessionId === "session-1") {
327+
deleteStarted.resolve();
328+
await deleteResponse.promise;
329+
}
330+
return new Response(null, { status: 204 });
331+
}
332+
const request = JSON.parse(String(init?.body)) as { readonly id?: unknown };
333+
// The MCP endpoint refuses an initialize that carries an existing session.
334+
const outcome =
335+
sessionId === null
336+
? { result: { protocolVersion: "2025-06-18" } }
337+
: { error: { code: -32600, message: "initialize must not include a session" } };
338+
return new Response(JSON.stringify({ jsonrpc: "2.0", id: request.id, ...outcome }), {
339+
status: sessionId === null ? 200 : 400,
340+
headers: {
341+
"content-type": "application/json",
342+
...(sessionId === null ? { "mcp-session-id": `session-${++sessions}` } : {}),
343+
},
344+
});
345+
},
346+
});
347+
const initialize = (requestId: string) =>
348+
bridge.message({ serverId: "t3-code", requestId, method: "initialize" });
349+
const initialized = { result: { protocolVersion: "2025-06-18" } };
350+
351+
yield* initialize("mcp-1");
352+
const waiting = yield* initialize("mcp-2").pipe(Effect.forkChild);
353+
yield* Effect.promise(() => deleteStarted.promise);
354+
expect(yield* initialize("mcp-3")).toEqual(initialized);
355+
deleteResponse.resolve();
356+
expect(yield* Fiber.join(waiting)).toEqual(initialized);
357+
}).pipe(Effect.scoped),
358+
);
359+
314360
it.effect("closes the session of an initialize that a newer initialize replaced in flight", () =>
315361
Effect.gen(function* () {
316362
const firstStarted = Promise.withResolvers<void>();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
139139
const renewServerConnection = Effect.gen(function* () {
140140
const previous = serverConnection;
141141
previous.retired = true;
142-
serverConnection = yield* makeConnection;
142+
const connection = yield* makeConnection;
143+
serverConnection = connection;
143144
yield* close(previous).pipe(Effect.ignore);
144-
return serverConnection;
145+
return connection;
145146
});
146147

147148
// Only initialization needs ordering; negotiated calls may wait independently.

0 commit comments

Comments
 (0)