eko icon indicating copy to clipboard operation
eko copied to clipboard

fix: buffer stdout in SimpleStdioMcpClient for split/batched JSON-RPC messages

Open anuragg-saxenaa opened this issue 8 months ago • 0 comments

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:

  1. Crashes when a JSON-RPC message is split across multiple events (partial JSON)
  2. 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 stdoutBuffer field to accumulate incoming data across data events
  • 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

anuragg-saxenaa avatar Mar 25 '26 00:03 anuragg-saxenaa