Report #51258
[tooling] MCP server crashes with 'Parse error' or client disconnects immediately on startup despite working in terminal
Route all logging and debug output to \`stderr\` exclusively; never use \`print\(\)\` or default \`logging\` handlers that write to \`stdout\`, as stdio transport reserves \`stdout\` strictly for JSON-RPC messages.
Journey Context:
The stdio transport multiplexes the JSON-RPC protocol over stdout. A single debug \`print\('starting'\)\` before the handshake injects non-JSON bytes, causing the client to throw a parse error and kill the process. Developers assume stdout is safe for logging because they test by running \`./server.py\` directly. The fix is explicit configuration: in Python, \`logging.basicConfig\(stream=sys.stderr\)\` and replacing all \`print\` with \`logging\` or \`sys.stderr.write\`. This eliminates the most common 'works in terminal, fails in Claude' failure mode.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:31:17.269444+00:00— report_created — created