TypeScript
fix(server): ACP MCP bridge no longer splits messages on U+2028/U+2029 (#30719)
TheGuyWithHATeee9ba1d3d67
diff --git a/apps/server/src/mcp/AcpMcpStdioBridge.test.ts b/apps/server/src/mcp/AcpMcpStdioBridge.test.tsindex 5d07f1a9362..3887a0368b9 100644--- a/apps/server/src/mcp/AcpMcpStdioBridge.test.ts+++ b/apps/server/src/mcp/AcpMcpStdioBridge.test.ts@@ -9,8 +9,10 @@ import { vi } from "vite-plus/test";import { mcpToolSession, runAcpMcpCliFastPath, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";-function makeHarness(responder: (request: Request) => Promise<Response> | Response) {- const input = new NodeStream.PassThrough();+function makeHarness(+ responder: (request: Request) => Promise<Response> | Response,+ input = new NodeStream.PassThrough(),+) {const written: Array<string> = [];const requests: Array<{ readonly headers: Headers; readonly body: string }> = [];const bridge = runAcpMcpStdioBridge({@@ -163,6 +165,36 @@ describe("AcpMcpStdioBridge", () => {}),);+ it.effect("keeps U+2028 and U+2029 inside a message instead of splitting it", () =>+ Effect.gen(function* () {+ const { input, written, requests, bridge } = makeHarness(+ () => Response.json({ jsonrpc: "2.0", id: 4, result: { ok: true } }),+ // Object mode keeps the two writes as separate chunks instead of one buffered read.+ new NodeStream.PassThrough({ objectMode: true }),+ );+ const fiber = yield* Effect.forkChild(bridge);+ // JSON.stringify leaves both separators unescaped, as MCP stdio clients send them.+ const message = JSON.stringify({+ jsonrpc: "2.0",+ id: 4,+ method: "tools/call",+ params: { name: "echo", arguments: { text: "a\u2028b\u2029c\u{1f30d}" } },+ });++ // Split inside the emoji's UTF-8 bytes and leave the last line unterminated, as stdin may.+ const bytes = Buffer.from(message);+ const split = bytes.indexOf(Buffer.from("\u{1f30d}")) + 2;+ input.write(bytes.subarray(0, split));+ input.end(bytes.subarray(split));+ yield* Fiber.join(fiber);++ expect(requests.map((request) => request.body)).toEqual([message]);+ expect(written.map((chunk) => JSON.parse(chunk))).toEqual([+ { jsonrpc: "2.0", id: 4, result: { ok: true } },+ ]);+ }),+ );+it.effect("forwards SSE messages before the HTTP response closes", () =>Effect.gen(function* () {let resolveResponseController!: (diff --git a/apps/server/src/mcp/AcpMcpStdioBridge.ts b/apps/server/src/mcp/AcpMcpStdioBridge.tsindex 4f12497be5d..485f85e1837 100644--- a/apps/server/src/mcp/AcpMcpStdioBridge.ts+++ b/apps/server/src/mcp/AcpMcpStdioBridge.ts@@ -1,7 +1,5 @@// Relay JSON-RPC verbatim; schema decoding would reject foreign payloads.-// @effect-diagnostics nodeBuiltinImport:off globalJsonInEffect:off-import * as NodeReadline from "node:readline";-+// @effect-diagnostics globalJsonInEffect:offimport * as Cause from "effect/Cause";import * as Effect from "effect/Effect";import * as Option from "effect/Option";@@ -320,8 +318,12 @@ export function runAcpMcpStdioBridge(options: AcpMcpStdioBridgeOptions): Effect.return yield* launch(task);});- const reader = NodeReadline.createInterface({ input: options.input });- yield* Stream.runForEach(Stream.fromAsyncIterable(reader, bridgeError), dispatch).pipe(+ // node:readline also splits on U+2028/U+2029, which JSON.stringify leaves raw inside strings.+ yield* Stream.fromAsyncIterable(options.input, bridgeError).pipe(+ Stream.map((chunk) => (typeof chunk === "string" ? Buffer.from(chunk) : chunk)),+ Stream.decodeText(),+ Stream.splitLines,+ Stream.runForEach(dispatch),Effect.ignoreCause,);yield* Fiber.awaitAll(running);