Fix stdio MCP server: redirect logging to stderr to prevent protocol corruption #40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The stdio MCP server was failing with "Failed to parse initialize response from stdio MCP server" errors because log messages were being written to stdout, corrupting the JSON-RPC protocol stream.
In stdio transport mode, stdout must be reserved exclusively for MCP JSON-RPC messages, but the logging system was mixing log entries with protocol messages:
This caused MCP clients to fail parsing responses, breaking the initialization handshake.
Solution
Modified the logging system to redirect all log output to stderr in stdio mode while preserving stdout logging for web/websocket modes:
init_loggingfunction to accept ause_stderrparameter that controls output destinationAfter Fix
The stdio transport now produces clean JSON-RPC protocol messages on stdout:
All log messages are properly redirected to stderr, ensuring MCP clients can successfully parse initialize responses and complete the handshake.
Testing
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.