11export * 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"
55import { ChildProcess } from "effect/process"
66import type { ChildProcessHandle } from "effect/process/ChildProcessSpawner"
77import { 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 )
0 commit comments