Skip to content

Commit 1105291

Browse files
fix(acp): close the MCP session of an initialize still in flight at dispose
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5c043a0 commit 1105291

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,45 @@ describe("AcpMcpOverAcpBridge", () => {
357357
}).pipe(Effect.scoped),
358358
);
359359

360+
it.effect("closes the session of an initialize still in flight when disposed", () =>
361+
Effect.gen(function* () {
362+
const started = Promise.withResolvers<void>();
363+
const respond = Promise.withResolvers<void>();
364+
const deleted: Array<string | null> = [];
365+
const bridge = yield* makeAcpMcpOverAcpBridge({
366+
endpoint: "http://127.0.0.1:1/mcp",
367+
authorization: "Bearer bridge-test",
368+
allocateConnectionId: Effect.succeed("connection-1"),
369+
fetchImplementation: async (_url, init) => {
370+
if (init?.method === "DELETE") {
371+
deleted.push(new Headers(init.headers).get("mcp-session-id"));
372+
return new Response(null, { status: 204 });
373+
}
374+
const request = JSON.parse(String(init?.body)) as { readonly id?: unknown };
375+
started.resolve();
376+
await respond.promise;
377+
return new Response(
378+
JSON.stringify({
379+
jsonrpc: "2.0",
380+
id: request.id,
381+
result: { protocolVersion: "2025-06-18" },
382+
}),
383+
{ headers: { "content-type": "application/json", "mcp-session-id": "session-1" } },
384+
);
385+
},
386+
});
387+
388+
const initialize = yield* bridge
389+
.message({ serverId: "t3-code", requestId: "mcp-1", method: "initialize" })
390+
.pipe(Effect.forkChild);
391+
yield* Effect.promise(() => started.promise);
392+
yield* bridge.dispose;
393+
respond.resolve();
394+
yield* Fiber.join(initialize);
395+
expect(deleted).toEqual(["session-1"]);
396+
}).pipe(Effect.scoped),
397+
);
398+
360399
it.effect("closes the session of a replaced initialize even when that initialize fails", () =>
361400
Effect.gen(function* () {
362401
const firstStarted = Promise.withResolvers<void>();

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
311311
(connectionId) => disconnect({ connectionId }).pipe(Effect.ignore),
312312
{ discard: true },
313313
),
314-
).pipe(Effect.andThen(Effect.suspend(() => close(serverConnection)).pipe(Effect.ignore))),
314+
).pipe(
315+
Effect.andThen(
316+
Effect.suspend(() => {
317+
serverConnection.retired = true;
318+
return close(serverConnection);
319+
}).pipe(Effect.ignore),
320+
),
321+
),
315322
};
316323
});

0 commit comments

Comments
 (0)