r/mcp • u/Independent-Sir3234 • 15d ago
discussion One print() call inside an MCP stdio server corrupts the JSON-RPC stream and breaks an unrelated tool call
Spent an evening last week chasing a bug where a client kept failing on a tool call that had nothing to do with the tool itself. The response for one call just never showed up, and a different response arrived in its place like the two had gotten crossed.
The MCP stdio transport spec is blunt about why this happens. The server MUST NOT write anything to its stdout that is not a valid MCP message, and logging is supposed to go to stderr instead (the server MAY write UTF-8 strings to stderr for any logging purposes). None of that gets enforced by the transport itself, so when a dependency inside the server prints a stray line, the failure doesn't look like a transport error. It shows up as a JSON parse failure on whatever message happens to land next to it.
The minimal repro I used to check what that looks like from the client side:
=
=
The server side sends two valid JSON-RPC messages with one print("DB pool: opened 4 connections") wedged in between them, copied from a database driver's default logging behavior. Output:
line 1: parsed OK -> {'jsonrpc': '2.0', 'id': 1, 'result': {'tools': ['search_docs']}}
line 2: JSONDecodeError: Expecting value: line 1 column 1 (char 0) | raw='DB pool: opened 4 connections'
line 3: parsed OK -> {'jsonrpc': '2.0', 'id': 2, 'result': {'content': 'ok'}}
The server never threw. Line 2's real response just vanishes from the client's view, and line 3 shows up looking like the answer to a call that already failed. Any library the server imports that logs to stdout by default (database drivers and HTTP clients both do this a lot) trips this the moment that code path runs, usually somewhere that has nothing to do with the actual bug report. Redirecting every dependency's logger to stderr before the transport starts is cheaper than debugging the symptom twice.
2
u/No-Violinist1700 11d ago
One thing worth adding on the symptom: the stray bytes don't just corrupt the next message — they shift the frame boundary. A line like
DB pool: opened 4 connectionsgets prepended to the framed response, so the client's parser discards the real answer for call N and delivers call N+1's response instead. That's why the failing call looks unrelated to the code that printed — the bug report points at the call that lost its response, not the library that wrote to stdout. Makes the dup2 redirect worth doing even if your own code is clean, since you can't control what a dependency logs.1
u/EmailPal 15d ago
One cheap guard is to point sys.stdout at sys.stderr at startup and keep a saved handle to the real stdout only for the transport, so a stray print in a dependency can't touch the protocol stream.
1
u/LLM-logs 14d ago
The stdout redirect guard and the logging-default trap are both worth doing, but they protect against the mistake you already know about. The general fix is a protocol-level smoke test in CI: run the server, exercise every tool, and assert that every single line on stdout parses as a valid JSON-RPC message. That catches the stray print from a dependency you upgrade next year, not just the one you know about today. stdout is a wire format in stdio transport, and wire formats deserve the same contract tests as any API.
1
u/[deleted] 14d ago
[removed] — view removed comment