Skip to content

Commit 0ab5563

Browse files
committed
fix(server): discard failed ACP MCP disconnect bodies
1 parent 89f4f81 commit 0ab5563

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎apps/server/src/mcp/AcpMcpOverAcpBridge.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,71 @@ describe("AcpMcpOverAcpBridge", () => {
6767
}),
6868
);
6969

70+
it.effect("discards the disconnect response body before checking HTTP status", () =>
71+
Effect.gen(function* () {
72+
const cases = [
73+
{ status: 500, expectError: true },
74+
{ status: 404, expectError: false },
75+
{ status: 200, expectError: false },
76+
] as const;
77+
78+
for (const [index, testCase] of cases.entries()) {
79+
let cancelled = false;
80+
const bridge = yield* makeAcpMcpOverAcpBridge({
81+
endpoint: "http://127.0.0.1:1/mcp",
82+
authorization: "Bearer bridge-test",
83+
allocateConnectionId: Effect.succeed(`connection-disconnect-${index}`),
84+
fetchImplementation: async (_url, init) => {
85+
const method = init?.method ?? "GET";
86+
if (method === "DELETE") {
87+
return new Response(
88+
new ReadableStream({
89+
cancel() {
90+
cancelled = true;
91+
},
92+
}),
93+
{ status: testCase.status },
94+
);
95+
}
96+
const body = init?.body === undefined ? null : JSON.parse(String(init.body));
97+
const request = body as { readonly id?: unknown };
98+
return new Response(
99+
JSON.stringify({
100+
jsonrpc: "2.0",
101+
id: request.id,
102+
result: { protocolVersion: "2025-06-18", capabilities: {}, serverInfo: {} },
103+
}),
104+
{
105+
headers: { "content-type": "application/json", "mcp-session-id": "session-42" },
106+
},
107+
);
108+
},
109+
});
110+
111+
const connected = yield* bridge.connect({ serverId: "t3-code" });
112+
yield* bridge.message({
113+
connectionId: connected.connectionId,
114+
method: "initialize",
115+
});
116+
const disconnect = bridge.disconnect({ connectionId: connected.connectionId });
117+
if (testCase.expectError) {
118+
expect(yield* disconnect.pipe(Effect.flip)).toMatchObject({
119+
_tag: "AcpMcpOverAcpError",
120+
message: `T3 Code MCP endpoint rejected disconnect with HTTP ${testCase.status}.`,
121+
});
122+
} else {
123+
expect(yield* disconnect).toEqual({});
124+
}
125+
expect(cancelled).toBe(true);
126+
expect(
127+
yield* bridge
128+
.message({ connectionId: connected.connectionId, method: "tools/list" })
129+
.pipe(Effect.flip),
130+
).toMatchObject({ _tag: "AcpMcpOverAcpError" });
131+
}
132+
}),
133+
);
134+
70135
it.effect("aborts an in-flight HTTP request when the ACP call is interrupted", () =>
71136
Effect.gen(function* () {
72137
const started = Promise.withResolvers<AbortSignal>();

‎apps/server/src/mcp/AcpMcpOverAcpBridge.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,16 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
164164
}),
165165
catch: bridgeError,
166166
});
167+
yield* Effect.promise(
168+
() => response.body?.cancel().catch(() => undefined) ?? Promise.resolve(),
169+
);
167170
if (!response.ok && response.status !== 404) {
168171
return yield* Effect.fail(
169172
new AcpMcpOverAcpError(
170173
`T3 Code MCP endpoint rejected disconnect with HTTP ${response.status}.`,
171174
),
172175
);
173176
}
174-
yield* Effect.promise(
175-
() => response.body?.cancel().catch(() => undefined) ?? Promise.resolve(),
176-
);
177177
}
178178
return {};
179179
});

0 commit comments

Comments
 (0)