eko
eko copied to clipboard
fix: buffer stdout in SimpleStdioMcpClient for split/batched JSON-RPC messages
Fixes #276
What
Adds proper newline-delimited JSON buffering to stdout parsing, replacing the naive single-message-per-data-event assumption.
Why
Node.js stream events do not guarantee message boundaries. The previous implementation called directly on each raw event, which:
- Crashes when a JSON-RPC message is split across multiple events (partial JSON)
- Drops messages when multiple newline-delimited JSON messages arrive in a single event (only the first would be parsed)
This is a well-known pitfall with Node.js child process stdout handling and becomes more likely with larger payloads or rapid-fire MCP tool responses.
Changes
- Added a
stdoutBufferfield to accumulate incoming data acrossdataevents - Split buffered data on newlines, keeping the last (potentially incomplete) segment in the buffer
- Parse each complete line individually with error handling (
try/catch+Log.warn) - Reset buffer on reconnect to prevent stale data from a previous session