The best way to control Claude Code, Codex and more from any computer or phone
fix(acp): MCP bridge disconnect gives up on an endpoint that never answers (#38237)
Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 17, 2026 at 18:35 UTC
b46edac5c1993b0c2cf721a5ffd3909709bcf7c0
2 files changed
+74
-1
packages/provider-acp/src/server/mcpOverAcpBridge.test.ts
+62
@@ -1,6 +1,9 @@
1
import { describe, expect, it } from "@effect/vitest";
2
import * as Effect from "effect/Effect";
3
import * as Fiber from "effect/Fiber";
4
+import * as Exit from "effect/Exit";
5
+import * as Scope from "effect/Scope";
6
+import * as TestClock from "effect/testing/TestClock";
7
8
import { makeAcpMcpOverAcpBridge } from "./mcpOverAcpBridge.ts";
9
@@ -165,6 +168,65 @@ describe("AcpMcpOverAcpBridge", () => {
168
}),
169
);
170
171
+ it.effect("closes all MCP sessions within one disconnect timeout during runtime cleanup", () =>
172
+ Effect.gen(function* () {
173
+ const deleteStarted = Promise.withResolvers<void>();
174
+ const deleteResponse = Promise.withResolvers<Response>();
175
+ const deleteSignals: AbortSignal[] = [];
176
+ let nextConnectionId = 0;
177
+ const bridge = yield* makeAcpMcpOverAcpBridge({
178
+ endpoint: "http://127.0.0.1:1/mcp",
179
+ authorization: "Bearer bridge-test",
180
+ allocateConnectionId: Effect.sync(() => `connection-${++nextConnectionId}`),
181
+ fetchImplementation: (_url, init) => {
182
+ if (init?.method === "DELETE") {
183
+ if (!init.signal) throw new Error("Missing cancellation signal");
184
+ deleteSignals.push(init.signal);
185
+ deleteStarted.resolve();
186
+ return deleteResponse.promise;
187
+ }
188
+ const request = JSON.parse(String(init?.body)) as { readonly id: number };
189
+ return Promise.resolve(
190
+ new Response(
191
+ JSON.stringify({
192
+ jsonrpc: "2.0",
193
+ id: request.id,
194
+ result: { protocolVersion: "2025-06-18" },
195
+ }),
196
+ {
197
+ headers: {
198
+ "content-type": "application/json",
199
+ "mcp-session-id": `session-${nextConnectionId}`,
200
+ },
201
+ },
202
+ ),
203
+ );
204
+ },
205
+ });
206
+ for (let index = 0; index < 16; index++) {
207
+ const { connectionId } = yield* bridge.connect({ serverId: "t3-code" });
208
+ yield* bridge.message({ connectionId, method: "initialize" });
209
+ }
210
+ const scope = yield* Scope.make();
211
+ yield* Scope.addFinalizer(scope, bridge.dispose);
212
+ const closing = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild);
213
+ yield* Effect.gen(function* () {
214
+ yield* Effect.promise(() => deleteStarted.promise);
215
+ yield* TestClock.adjust("5 seconds");
216
+ expect(closing.pollUnsafe(), "All sessions must close within one timeout").toBeDefined();
217
+ expect(deleteSignals).toHaveLength(16);
218
+ expect(deleteSignals.every((signal) => signal.aborted)).toBe(true);
219
+ }).pipe(
220
+ Effect.ensuring(
221
+ Effect.gen(function* () {
222
+ deleteResponse.resolve(new Response(null, { status: 204 }));
223
+ yield* Fiber.join(closing);
224
+ }),
225
+ ),
226
+ );
227
+ }),
228
+ );
229
+
230
it.effect("rejects unknown servers, connections, and oversized messages", () =>
231
Effect.gen(function* () {
232
const bridge = yield* makeAcpMcpOverAcpBridge({
packages/provider-acp/src/server/mcpOverAcpBridge.ts
+12
-1
@@ -10,6 +10,8 @@ import { responsePayloads } from "@t3tools/shared/mcpResponsePayloads";
10
11
const MAX_CONNECTIONS = 16;
12
const MAX_MESSAGE_BYTES = 8 * 1024 * 1024;
13
+// Runtime teardown waits on disconnect, so an endpoint that never answers must not hold it.
14
+const DISCONNECT_TIMEOUT = "5 seconds";
15
16
interface JsonRpcEnvelope {
17
readonly id?: unknown;
@@ -207,6 +209,15 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
209
() => response.body?.cancel().catch(() => undefined) ?? Promise.resolve(),
210
);
211
}).pipe(
212
+ Effect.timeoutOrElse({
213
+ duration: DISCONNECT_TIMEOUT,
214
+ orElse: () =>
215
+ Effect.fail(
216
+ new AcpMcpOverAcpError({
217
+ message: "T3 Code MCP endpoint did not answer disconnect.",
218
+ }),
219
+ ),
220
+ }),
221
// Restore a session that failed to close, so a retry or dispose can close it.
222
Effect.onError(() => Effect.sync(() => connections.set(request.connectionId, connection))),
223
);
@@ -272,7 +283,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
283
Effect.forEach(
284
[...connections.keys()],
285
(connectionId) => disconnect({ connectionId }).pipe(Effect.ignore),
275
- { discard: true },
286
+ { concurrency: "unbounded", discard: true },
287
),
288
),
289
};