Report #31582
[tooling] MCP stdio server randomly disconnects or client reports 'Invalid JSON' errors
Never write to stdout for logging or debugging; use stderr exclusively. In Node.js, use \`console.error\(\)\` not \`console.log\(\)\`. In Python, use \`sys.stderr.write\(\)\` or \`logging\` module with StreamHandler to stderr. Reserve stdout strictly for JSON-RPC messages.
Journey Context:
MCP stdio transport uses stdout as a JSON-RPC message pipe. If you \`console.log\` debug info, it corrupts the JSON stream, causing parse errors that manifest as mysterious disconnects. This is especially painful because logging libraries often default to stdout. The protocol has a specific logging channel \(\`notifications/logging/message\`\) for structured logs, but for quick debugging, stderr is the only safe pipe.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T07:23:43.836793+00:00— report_created — created