Open History to see the last commit
1// The bridge intentionally treats MCP JSON-RPC messages as opaque JSON.
2// @effect-diagnostics preferSchemaOverJson:off
3import { describe, expect, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6
7import { makeAcpMcpOverAcpBridge } from "./AcpMcpOverAcpBridge.ts";
8
9describe("AcpMcpOverAcpBridge", () => {
10 it.effect("forwards authenticated MCP requests and closes the negotiated session", () =>
11 Effect.gen(function* () {
12 const requests: Array<{
13 readonly method: string;
14 readonly headers: Headers;
15 readonly body: unknown;
16 }> = [];
17 const bridge = yield* makeAcpMcpOverAcpBridge({
18 endpoint: "http://127.0.0.1:1/mcp",
19 authorization: "Bearer bridge-test",
20 allocateConnectionId: Effect.succeed("connection-1"),
21 fetchImplementation: async (_url, init) => {
22 const method = init?.method ?? "GET";
23 const headers = new Headers(init?.headers);
24 const body = init?.body === undefined ? null : JSON.parse(String(init.body));
25 requests.push({ method, headers, body });
26 if (method === "DELETE") return new Response(null, { status: 204 });
27 if (body === null || typeof body !== "object") return new Response(null, { status: 202 });
28 const request = body as { readonly id?: unknown; readonly method?: unknown };
29 if (request.method === "notifications/initialized") {
30 return new Response(null, { status: 202 });
31 }
32 return new Response(
33 JSON.stringify({
34 jsonrpc: "2.0",
35 id: request.id,
36 result:
37 request.method === "initialize"
38 ? { protocolVersion: "2025-06-18", capabilities: {}, serverInfo: {} }
39 : { tools: [] },
40 }),
41 {
42 headers: { "content-type": "application/json", "mcp-session-id": "session-42" },
43 },
44 );
45 },
46 });
47
48 const connected = yield* bridge.connect({ serverId: "t3-code" });
49 expect(connected).toEqual({ connectionId: "connection-1" });
50 expect(
51 yield* bridge.message({ connectionId: connected.connectionId, method: "initialize" }),
52 ).toMatchObject({ protocolVersion: "2025-06-18" });
53 expect(
54 yield* bridge.message({ connectionId: connected.connectionId, method: "tools/list" }),
55 ).toEqual({ tools: [] });
56 yield* bridge.notification({
57 connectionId: connected.connectionId,
58 method: "notifications/initialized",
59 });
60 yield* bridge.disconnect({ connectionId: connected.connectionId });
61
62 expect(requests[0]?.headers.get("authorization")).toBe("Bearer bridge-test");
63 expect(requests[1]?.headers.get("mcp-session-id")).toBe("session-42");
64 expect(requests[1]?.headers.get("mcp-protocol-version")).toBe("2025-06-18");
65 expect(requests.at(-1)?.method).toBe("DELETE");
66 expect(requests.at(-1)?.headers.get("mcp-session-id")).toBe("session-42");
67 }),
68 );
69
70 it.effect("aborts an in-flight HTTP request when the ACP call is interrupted", () =>
71 Effect.gen(function* () {
72 const started = Promise.withResolvers<AbortSignal>();
73 const bridge = yield* makeAcpMcpOverAcpBridge({
74 endpoint: "http://127.0.0.1:1/mcp",
75 authorization: "Bearer bridge-test",
76 allocateConnectionId: Effect.succeed("connection-1"),
77 fetchImplementation: (_url, init) => {
78 if (!init?.signal) throw new Error("Missing cancellation signal");
79 started.resolve(init.signal);
80 return new Promise<Response>(() => {});
81 },
82 });
83 const connected = yield* bridge.connect({ serverId: "t3-code" });
84 const request = yield* bridge
85 .message({ connectionId: connected.connectionId, method: "tools/list" })
86 .pipe(Effect.forkChild);
87 const signal = yield* Effect.promise(() => started.promise);
88 yield* Fiber.interrupt(request);
89 expect(signal.aborted).toBe(true);
90 }).pipe(Effect.scoped),
91 );
92
93 it.effect("rejects unknown servers, connections, and oversized messages", () =>
94 Effect.gen(function* () {
95 const bridge = yield* makeAcpMcpOverAcpBridge({
96 endpoint: "http://127.0.0.1:1/mcp",
97 authorization: "Bearer bridge-test",
98 allocateConnectionId: Effect.succeed("connection-1"),
99 fetchImplementation: () => Promise.resolve(new Response(null, { status: 202 })),
100 });
101
102 expect(yield* bridge.connect({ serverId: "other" }).pipe(Effect.flip)).toMatchObject({
103 _tag: "AcpMcpOverAcpError",
104 });
105 expect(
106 yield* bridge.message({ connectionId: "missing", method: "tools/list" }).pipe(Effect.flip),
107 ).toMatchObject({ _tag: "AcpMcpOverAcpError" });
108
109 const connected = yield* bridge.connect({ serverId: "t3-code" });
110 const failure = yield* bridge
111 .message({
112 connectionId: connected.connectionId,
113 method: "tools/call",
114 params: { payload: "x".repeat(8 * 1024 * 1024) },
115 })
116 .pipe(Effect.flip);
117 expect(failure.message).toContain("exceeds 8 MiB");
118 }),
119 );
120});