The best way to control Claude Code, Codex and more from any computer or phone
| 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 | |
| 10 | describe("AcpMcpOverAcpBridge", () => { |
| 11 | it.effect("forwards authenticated MCP requests and closes the negotiated session", () => |
| 12 | Effect.gen(function* () { |
| 13 | const initializeStarted = Promise.withResolvers<void>(); |
| 14 | const initializeResponse = Promise.withResolvers<void>(); |
| 15 | const listQueued = Promise.withResolvers<void>(); |
| 16 | const deleteStarted = Promise.withResolvers<void>(); |
| 17 | const deleteResponse = Promise.withResolvers<void>(); |
| 18 | const requests: Array<{ |
| 19 | readonly method: string; |
| 20 | readonly headers: Headers; |
| 21 | readonly body: unknown; |
| 22 | }> = []; |
| 23 | let deletes = 0; |
| 24 | const bridge = yield* makeAcpMcpOverAcpBridge({ |
| 25 | endpoint: "http://127.0.0.1:1/mcp", |
| 26 | authorization: "Bearer bridge-test", |
| 27 | allocateConnectionId: Effect.succeed("connection-1"), |
| 28 | fetchImplementation: async (_url, init) => { |
| 29 | const method = init?.method ?? "GET"; |
| 30 | const headers = new Headers(init?.headers); |
| 31 | const body = init?.body === undefined ? null : JSON.parse(String(init.body)); |
| 32 | requests.push({ method, headers, body }); |
| 33 | if (method === "DELETE") { |
| 34 | if (++deletes > 1) return new Response(null, { status: 204 }); |
| 35 | deleteStarted.resolve(); |
| 36 | await deleteResponse.promise; |
| 37 | return new Response(null, { status: 503 }); |
| 38 | } |
| 39 | if (body === null || typeof body !== "object") return new Response(null, { status: 202 }); |
| 40 | const request = body as { readonly id?: unknown; readonly method?: unknown }; |
| 41 | if (request.method === "initialize") { |
| 42 | initializeStarted.resolve(); |
| 43 | await initializeResponse.promise; |
| 44 | } |
| 45 | if (request.method === "notifications/initialized") { |
| 46 | return new Response(null, { status: 202 }); |
| 47 | } |
| 48 | return new Response( |
| 49 | JSON.stringify({ |
| 50 | jsonrpc: "2.0", |
| 51 | id: request.id, |
| 52 | result: |
| 53 | request.method === "initialize" |
| 54 | ? { protocolVersion: "2025-06-18", capabilities: {}, serverInfo: {} } |
| 55 | : { tools: [] }, |
| 56 | }), |
| 57 | { |
| 58 | headers: { "content-type": "application/json", "mcp-session-id": "session-42" }, |
| 59 | }, |
| 60 | ); |
| 61 | }, |
| 62 | }); |
| 63 | |
| 64 | const connected = yield* bridge.connect({ serverId: "t3-code" }); |
| 65 | expect(connected).toEqual({ connectionId: "connection-1" }); |
| 66 | const initialize = yield* bridge |
| 67 | .message({ connectionId: connected.connectionId, method: "initialize" }) |
| 68 | .pipe(Effect.forkChild); |
| 69 | yield* Effect.promise(() => initializeStarted.promise); |
| 70 | const list = yield* Effect.gen(function* () { |
| 71 | listQueued.resolve(); |
| 72 | return yield* bridge.message({ |
| 73 | connectionId: connected.connectionId, |
| 74 | method: "tools/list", |
| 75 | }); |
| 76 | }).pipe(Effect.forkChild); |
| 77 | yield* Effect.promise(() => listQueued.promise); |
| 78 | initializeResponse.resolve(); |
| 79 | expect(yield* Fiber.join(initialize)).toMatchObject({ protocolVersion: "2025-06-18" }); |
| 80 | expect(yield* Fiber.join(list)).toEqual({ tools: [] }); |
| 81 | yield* bridge.notification({ |
| 82 | connectionId: connected.connectionId, |
| 83 | method: "notifications/initialized", |
| 84 | }); |
| 85 | // Calls during a close are refused; a rejected close restores the connection for a retry. |
| 86 | const rejected = yield* bridge |
| 87 | .disconnect({ connectionId: connected.connectionId }) |
| 88 | .pipe(Effect.flip, Effect.forkChild); |
| 89 | yield* Effect.promise(() => deleteStarted.promise); |
| 90 | const closing = yield* bridge |
| 91 | .message({ connectionId: connected.connectionId, method: "tools/list" }) |
| 92 | .pipe(Effect.flip); |
| 93 | expect(closing.message).toContain("Unknown MCP-over-ACP connection"); |
| 94 | deleteResponse.resolve(); |
| 95 | expect((yield* Fiber.join(rejected)).message).toContain("HTTP 503"); |
| 96 | yield* bridge.disconnect({ connectionId: connected.connectionId }); |
| 97 | expect(deletes).toBe(2); |
| 98 | |
| 99 | expect(requests[0]?.headers.get("authorization")).toBe("Bearer bridge-test"); |
| 100 | expect(requests[1]?.headers.get("mcp-session-id")).toBe("session-42"); |
| 101 | expect(requests[1]?.headers.get("mcp-protocol-version")).toBe("2025-06-18"); |
| 102 | expect(requests.at(-1)?.method).toBe("DELETE"); |
| 103 | expect(requests.at(-1)?.headers.get("mcp-session-id")).toBe("session-42"); |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | it.effect("aborts an in-flight HTTP request when the ACP call is interrupted", () => |
| 108 | Effect.gen(function* () { |
| 109 | const started = Promise.withResolvers<AbortSignal>(); |
| 110 | const bridge = yield* makeAcpMcpOverAcpBridge({ |
| 111 | endpoint: "http://127.0.0.1:1/mcp", |
| 112 | authorization: "Bearer bridge-test", |
| 113 | allocateConnectionId: Effect.succeed("connection-1"), |
| 114 | fetchImplementation: (_url, init) => { |
| 115 | if (!init?.signal) throw new Error("Missing cancellation signal"); |
| 116 | started.resolve(init.signal); |
| 117 | return new Promise<Response>(() => {}); |
| 118 | }, |
| 119 | }); |
| 120 | const connected = yield* bridge.connect({ serverId: "t3-code" }); |
| 121 | const request = yield* bridge |
| 122 | .message({ connectionId: connected.connectionId, method: "tools/list" }) |
| 123 | .pipe(Effect.forkChild); |
| 124 | const signal = yield* Effect.promise(() => started.promise); |
| 125 | yield* Fiber.interrupt(request); |
| 126 | expect(signal.aborted).toBe(true); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | it.effect("answers a call while a long call on the same connection is pending", () => |
| 131 | Effect.gen(function* () { |
| 132 | const waitStarted = Promise.withResolvers<void>(); |
| 133 | const waitResponse = Promise.withResolvers<Response>(); |
| 134 | const reply = (id: unknown, result: unknown) => |
| 135 | new Response(JSON.stringify({ jsonrpc: "2.0", id, result }), { |
| 136 | headers: { "content-type": "application/json", "mcp-session-id": "session-42" }, |
| 137 | }); |
| 138 | const bridge = yield* makeAcpMcpOverAcpBridge({ |
| 139 | endpoint: "http://127.0.0.1:1/mcp", |
| 140 | authorization: "Bearer bridge-test", |
| 141 | allocateConnectionId: Effect.succeed("connection-1"), |
| 142 | fetchImplementation: (_url, init) => { |
| 143 | const request = JSON.parse(String(init?.body)) as { |
| 144 | readonly id?: unknown; |
| 145 | readonly method?: unknown; |
| 146 | }; |
| 147 | if (request.method === "initialize") { |
| 148 | return Promise.resolve(reply(request.id, { protocolVersion: "2025-06-18" })); |
| 149 | } |
| 150 | if (request.method === "tools/call") { |
| 151 | waitStarted.resolve(); |
| 152 | return waitResponse.promise; |
| 153 | } |
| 154 | return Promise.resolve(reply(request.id, { tools: [] })); |
| 155 | }, |
| 156 | }); |
| 157 | const { connectionId } = yield* bridge.connect({ serverId: "t3-code" }); |
| 158 | yield* bridge.message({ connectionId, method: "initialize" }); |
| 159 | |
| 160 | const wait = yield* bridge |
| 161 | .message({ connectionId, method: "tools/call", params: { name: "t3_thread_wait" } }) |
| 162 | .pipe(Effect.forkChild); |
| 163 | yield* Effect.promise(() => waitStarted.promise); |
| 164 | expect(yield* bridge.message({ connectionId, method: "tools/list" })).toEqual({ tools: [] }); |
| 165 | |
| 166 | waitResponse.resolve(reply(2, { content: [] })); |
| 167 | expect(yield* Fiber.join(wait)).toEqual({ content: [] }); |
| 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({ |
| 233 | endpoint: "http://127.0.0.1:1/mcp", |
| 234 | authorization: "Bearer bridge-test", |
| 235 | allocateConnectionId: Effect.succeed("connection-1"), |
| 236 | fetchImplementation: () => Promise.resolve(new Response(null, { status: 202 })), |
| 237 | }); |
| 238 | |
| 239 | expect(yield* bridge.connect({ serverId: "other" }).pipe(Effect.flip)).toMatchObject({ |
| 240 | _tag: "AcpMcpOverAcpError", |
| 241 | }); |
| 242 | expect( |
| 243 | yield* bridge.message({ connectionId: "missing", method: "tools/list" }).pipe(Effect.flip), |
| 244 | ).toMatchObject({ _tag: "AcpMcpOverAcpError" }); |
| 245 | |
| 246 | const connected = yield* bridge.connect({ serverId: "t3-code" }); |
| 247 | const failure = yield* bridge |
| 248 | .message({ |
| 249 | connectionId: connected.connectionId, |
| 250 | method: "tools/call", |
| 251 | params: { payload: "x".repeat(8 * 1024 * 1024) }, |
| 252 | }) |
| 253 | .pipe(Effect.flip); |
| 254 | expect(failure.message).toContain("exceeds 8 MiB"); |
| 255 | }), |
| 256 | ); |
| 257 | }); |