Report #1641
[gotcha] MCP stdio server hangs or returns garbled responses after adding logging or print statements
Route ALL non-JSON-RPC output to stderr exclusively. In Node.js use console.error\(\) or a stderr logger; in Python use sys.stderr.write\(\) or logging.StreamHandler\(sys.stderr\). Never write to stdout from any code path — including dependencies, unhandled exception tracebacks, or startup banners.
Journey Context:
MCP's stdio transport multiplexes JSON-RPC messages over stdout. Any stray byte on stdout corrupts the framing — the client tries to parse log lines as JSON-RPC and gets parse errors, timeouts, or silently dropped messages. Developers instinctively add console.log/print for debugging, which is exactly what breaks the protocol. The resulting errors are deeply misleading: you see JSON parse failures or connection drops, never 'you logged to the wrong stream.' This is the single most common cause of 'my MCP server doesn't work' reports. The fix is simple but requires auditing every dependency that might touch stdout.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T06:31:38.908229+00:00— report_created — created