Skip to content

fix: set newline='' on TextIOWrapper in stdio_server to prevent Windows CRLF corruption#2574

Closed
EvanYao826 wants to merge 3 commits into
modelcontextprotocol:mainfrom
EvanYao826:fix/windows-crlf-stdio-2433
Closed

fix: set newline='' on TextIOWrapper in stdio_server to prevent Windows CRLF corruption#2574
EvanYao826 wants to merge 3 commits into
modelcontextprotocol:mainfrom
EvanYao826:fix/windows-crlf-stdio-2433

Conversation

@EvanYao826
Copy link
Copy Markdown

Fixes #2433

Summary

On Windows, TextIOWrapper defaults to platform-native line endings ( ), which corrupts newline-delimited JSON-RPC messages over stdio. The server-side stdio_server() wraps sys.stdout in a TextIOWrapper without specifying newline=, so any in the JSON payload gets converted to , breaking clients that parse on .

Fix

Add newline= to the TextIOWrapper constructor in stdio_server(), matching the pattern already used in the server-side fix from PR #2302.

Test

Added a test verifying that the TextIOWrapper used in stdio_server has newline= set, preventing CRLF conversion.

…ws CRLF corruption

On Windows, TextIOWrapper defaults to platform-native line endings (\r\n),
which corrupts newline-delimited JSON-RPC messages over stdio. Add
newline='' to both the stdin and stdout TextIOWrapper constructors in
stdio_server(), matching the pattern from PR modelcontextprotocol#2302.

Fixes modelcontextprotocol#2433
When a Windows MCP server emits CRLF (\r\n) line endings, the client's
stdout_reader splits on \n alone, leaving a trailing \r on each line.
This causes JSON parsing to fail since "{...}\r" is not valid JSON.

Fix by stripping trailing \r from each line before JSON parsing in the
client's stdio_reader. Also adds a test that verifies CRLF-terminated
messages from a server are correctly parsed.

Fixes modelcontextprotocol#2433
@EvanYao826 EvanYao826 closed this May 11, 2026
@EvanYao826 EvanYao826 deleted the fix/windows-crlf-stdio-2433 branch May 11, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: TextIOWrapper in stdio_server() emits CRLF instead of LF, corrupting newline-delimited JSON messages

1 participant