Skip to content

Commit 1a443ae

Browse files
authored
fix(core): frame MCP stdio messages without the SDK read buffer (#60694)
1 parent 208ea0e commit 1a443ae

2 files changed

Lines changed: 177 additions & 31 deletions

File tree

‎packages/core/src/mcp/stdio.ts‎

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * as McpStdio from "./stdio.js"
22

3-
import { ReadBuffer, serializeMessage, type JSONRPCMessage, type Transport } from "@modelcontextprotocol/client"
4-
import { Cause, Duration, Effect, Fiber, Option, Queue, Scope, Stream } from "effect"
3+
import { deserializeMessage, serializeMessage, type JSONRPCMessage, type Transport } from "@modelcontextprotocol/client"
4+
import { Cause, Duration, Effect, Fiber, Option, Queue, Result, Scope, Stream } from "effect"
55
import { ChildProcess } from "effect/process"
66
import type { ChildProcessHandle } from "effect/process/ChildProcessSpawner"
77
import { Environment } from "../environment/index.js"
@@ -48,11 +48,13 @@ export const make = Effect.fnUntraced(function* (options: Options) {
4848
// Outgoing frames are queued rather than written to `handle.stdin` directly: the sink closes the
4949
// stream it is run with, and stdin must stay open across the whole session.
5050
const outgoing = yield* Queue.bounded<string, Cause.Done>(OUTGOING_CAPACITY)
51-
const buffer = new ReadBuffer()
5251
const state: { phase: "ready" | "starting" | "open" | "closed"; handle?: ChildProcessHandle } = { phase: "ready" }
5352
let startup: Promise<void> | undefined
5453
let closing: Promise<void> | undefined
55-
let trailingBytes = 0
54+
// Bytes of the unterminated line, kept as chunk views and joined once its newline arrives.
55+
let pending: Array<Uint8Array> = []
56+
let pendingBytes = 0
57+
const decoder = new TextDecoder()
5658
let stderr = ""
5759

5860
const stop = Effect.fnUntraced(function* (handle: ChildProcessHandle) {
@@ -66,13 +68,15 @@ export const make = Effect.fnUntraced(function* (options: Options) {
6668
(closing ??= Effect.runPromise(
6769
Effect.gen(function* () {
6870
state.phase = "closed"
71+
pending = []
72+
pendingBytes = 0
6973
Queue.endUnsafe(outgoing)
7074
if (startup) yield* Effect.promise(() => startup!.catch(() => undefined))
7175
const handle = state.handle
7276
if (!handle) return
7377
state.handle = undefined
7478
yield* stop(handle)
75-
}).pipe(Effect.ensuring(Queue.shutdown(outgoing)), Effect.ensuring(Effect.sync(() => buffer.clear()))),
79+
}).pipe(Effect.ensuring(Queue.shutdown(outgoing))),
7680
))
7781

7882
const transport: Transport = {
@@ -114,32 +118,44 @@ export const make = Effect.fnUntraced(function* (options: Options) {
114118
close,
115119
}
116120

117-
const deliver = (chunk: Uint8Array) =>
118-
Effect.gen(function* () {
119-
for (const byte of chunk) {
120-
trailingBytes = byte === 10 ? 0 : trailingBytes + 1
121-
if (trailingBytes > MAX_FRAME_BYTES) return yield* Effect.fail(new Error("MCP stdio frame exceeded 16 MiB"))
122-
}
123-
buffer.append(Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength))
124-
while (true) {
125-
// `undefined` means the frame failed to parse: the buffer has already advanced past it, so
126-
// keep draining. `null` means the buffer holds no complete frame yet.
127-
const message = yield* Effect.try({
128-
try: () => buffer.readMessage(),
129-
catch: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
130-
}).pipe(
131-
Effect.catch((error) =>
132-
Effect.sync(() => {
133-
transport.onerror?.(error)
134-
return undefined
135-
}),
136-
),
137-
)
138-
if (message === undefined) continue
139-
if (message === null) return
140-
transport.onmessage?.(message)
141-
}
121+
const deliver = (chunk: Uint8Array) => {
122+
let start = 0
123+
let newline = chunk.indexOf(10)
124+
while (newline !== -1 && state.phase !== "closed") {
125+
const line = chunk.subarray(start, newline)
126+
const size = pendingBytes + line.length
127+
if (size > MAX_FRAME_BYTES) return oversized()
128+
receive(pending.length === 0 ? line : Buffer.concat([...pending, line], size))
129+
pending = []
130+
pendingBytes = 0
131+
start = newline + 1
132+
newline = chunk.indexOf(10, start)
133+
}
134+
if (start === chunk.length || state.phase === "closed") return
135+
pendingBytes += chunk.length - start
136+
if (pendingBytes > MAX_FRAME_BYTES) return oversized()
137+
// A view is enough: the runtime hands out a fresh buffer for every pipe read.
138+
pending.push(start === 0 ? chunk : chunk.subarray(start))
139+
}
140+
141+
const receive = (line: Uint8Array) => {
142+
const text = decoder.decode(line.at(-1) === 13 ? line.subarray(0, -1) : line)
143+
if (text.trim() === "") return
144+
const message = Result.try({
145+
try: () => deserializeMessage(text),
146+
catch: (cause) => new Error(`Invalid MCP stdio message from ${options.server}: ${cause}`, { cause }),
142147
})
148+
if (Result.isFailure(message)) return transport.onerror?.(message.failure)
149+
transport.onmessage?.(message.success)
150+
}
151+
152+
// Closing fails every pending request at once rather than leaving the one this line answered waiting.
153+
const oversized = () => {
154+
transport.onerror?.(
155+
new Error(`MCP server ${options.server} wrote more than 16 MiB to stdout without a message boundary`),
156+
)
157+
void close()
158+
}
143159

144160
const startOutput = (handle: ChildProcessHandle) =>
145161
Effect.gen(function* () {
@@ -158,7 +174,7 @@ export const make = Effect.fnUntraced(function* (options: Options) {
158174
)
159175

160176
yield* Effect.forkScoped(
161-
Stream.runForEach(handle.stdout, deliver).pipe(
177+
Stream.runForEach(handle.stdout, (chunk) => Effect.sync(() => deliver(chunk))).pipe(
162178
Effect.tapCause((cause) =>
163179
Effect.sync(() => {
164180
const error = Cause.squash(cause)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { expect, test } from "bun:test"
2+
import type { JSONRPCMessage } from "@modelcontextprotocol/client"
3+
import { Environment } from "@opencode/core/environment/index"
4+
import { McpStdio } from "@opencode/core/mcp/stdio"
5+
import { Deferred, Effect, Layer, Sink, Stream } from "effect"
6+
import { ChildProcessSpawner } from "effect/process"
7+
import { ExitCode, makeHandle, ProcessId } from "effect/process/ChildProcessSpawner"
8+
9+
const encoder = new TextEncoder()
10+
const MiB = 1024 * 1024
11+
const after = encoder.encode('{"jsonrpc":"2.0","method":"after"}\n')
12+
13+
test("receives a frame between 10 and 16 MiB split into pipe-sized chunks", async () => {
14+
// 12 MiB sits between the SDK read buffer's 10 MiB default and our 16 MiB cap.
15+
const output = await read(chunks(frame(12 * MiB), 64 * 1024))
16+
17+
expect(output.errors).toEqual([])
18+
expect(output.messages).toHaveLength(1)
19+
expect(output.messages[0]).toMatchObject({ jsonrpc: "2.0", id: 1 })
20+
})
21+
22+
test("frames CRLF, blank lines, several messages per chunk and split UTF-8", async () => {
23+
const bytes = encoder.encode(
24+
[
25+
'{"jsonrpc":"2.0","method":"first","params":{"text":"héllo 🙂"}}\r\n',
26+
"\n",
27+
" \r\n",
28+
"not json\n",
29+
'{"jsonrpc":"2.0","method":"second"}\n{"jsonrpc":"2.0","method":"third"}\r\n',
30+
].join(""),
31+
)
32+
33+
for (const size of [bytes.length, 1, 3]) {
34+
const output = await read(chunks(bytes, size))
35+
expect(output.messages).toEqual([
36+
{ jsonrpc: "2.0", method: "first", params: { text: "héllo 🙂" } },
37+
{ jsonrpc: "2.0", method: "second" },
38+
{ jsonrpc: "2.0", method: "third" },
39+
])
40+
expect(output.errors).toEqual([expect.stringContaining("Invalid MCP stdio message")])
41+
}
42+
})
43+
44+
test("accepts a frame of exactly 16 MiB", async () => {
45+
// Frame sizes include the newline.
46+
const output = await read(chunks(frame(16 * MiB + 1), 64 * 1024))
47+
48+
expect(output.errors).toEqual([])
49+
expect(output.messages).toEqual([expect.objectContaining({ id: 1, result: expect.anything() })])
50+
})
51+
52+
test("closes the connection on a line one byte over 16 MiB", async () => {
53+
// stdout stays open until the process is killed, so finishing at all means the transport closed
54+
// itself, which is what fails the client's pending requests.
55+
const output = await read([...chunks(frame(16 * MiB + 2), 64 * 1024), after], { open: true })
56+
57+
expect(output.errors).toEqual(["MCP server framing wrote more than 16 MiB to stdout without a message boundary"])
58+
expect(output.messages).toEqual([])
59+
})
60+
61+
function frame(size: number) {
62+
const prefix = '{"jsonrpc":"2.0","id":1,"result":{"text":"'
63+
const suffix = '"}}\n'
64+
return encoder.encode(prefix + "x".repeat(size - prefix.length - suffix.length) + suffix)
65+
}
66+
67+
function chunks(bytes: Uint8Array, size: number) {
68+
return Array.from({ length: Math.ceil(bytes.length / size) }, (_, index) =>
69+
bytes.subarray(index * size, (index + 1) * size),
70+
)
71+
}
72+
73+
/** Runs the transport against a process whose stdout yields `output`, then ends or, with `open`, waits to be killed. */
74+
async function read(output: ReadonlyArray<Uint8Array>, options?: { open: boolean }) {
75+
const killed = Deferred.makeUnsafe<void>()
76+
const messages: Array<JSONRPCMessage> = []
77+
const errors: Array<string> = []
78+
const driver = Environment.makeMemoryDriver()
79+
const environment = Layer.succeed(
80+
Environment.Service,
81+
Environment.Service.of({
82+
files: Environment.makeFiles(driver),
83+
spawner: ChildProcessSpawner.make(() =>
84+
Effect.succeed(
85+
makeHandle({
86+
pid: ProcessId(1),
87+
exitCode: Effect.succeed(ExitCode(0)),
88+
isRunning: Effect.succeed(false),
89+
kill: () => Deferred.succeed(killed, undefined),
90+
stdin: Sink.drain,
91+
stdout: Stream.fromIterable(output).pipe(
92+
Stream.concat(
93+
options?.open ? Stream.fromEffect(Deferred.await(killed)).pipe(Stream.drain) : Stream.empty,
94+
),
95+
),
96+
stderr: Stream.empty,
97+
all: Stream.empty,
98+
getInputFd: () => Sink.drain,
99+
getOutputFd: () => Stream.empty,
100+
unref: Effect.succeed(Effect.void),
101+
}),
102+
),
103+
),
104+
}),
105+
)
106+
107+
await Effect.runPromise(
108+
Effect.scoped(
109+
Effect.gen(function* () {
110+
const transport = yield* McpStdio.make({
111+
server: "framing",
112+
command: "unused",
113+
args: [],
114+
cwd: import.meta.dir,
115+
environment: {},
116+
})
117+
const closed = new Promise<void>((resolve) => (transport.onclose = resolve))
118+
transport.onmessage = (message) => messages.push(message)
119+
// stdout ending reports the exit; only framing errors matter here.
120+
transport.onerror = (error) => {
121+
if (!error.message.startsWith("MCP server process exited")) errors.push(error.message)
122+
}
123+
yield* Effect.promise(() => transport.start())
124+
yield* Effect.promise(() => closed)
125+
}).pipe(Effect.provide(environment)),
126+
),
127+
)
128+
129+
return { messages, errors }
130+
}

0 commit comments

Comments
 (0)