Report #14778
[gotcha] MCP server writes debug output to stdout, silently corrupting the JSON-RPC message stream
Route ALL non-protocol output to stderr. Audit every dependency and library to ensure nothing writes to stdout. Override process.stdout.write if necessary. Use stderr for logging, debugging, and any diagnostic output unconditionally.
Journey Context:
The MCP stdio transport uses stdout for line-delimited JSON-RPC messages. Any stray print statement, debug log, or library output written to stdout gets interleaved with protocol messages, causing JSON parse failures. The resulting errors are deeply confusing — 'invalid JSON', 'unexpected token', 'connection reset' — and never point to the real cause. This is especially insidious because many logging libraries default to stdout, and the corruption may only manifest under load or error conditions. You can spend hours debugging protocol logic before realizing a dependency snuck a console.log into stdout. The fix is not just 'use stderr' but actively defending stdout: wrap it, audit it, and test for it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:22:38.209259+00:00— report_created — created