fix: disable newline translation in stdio TextIOWrapper to prevent CRLF on Windows#2552
Open
Maanik23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: disable newline translation in stdio TextIOWrapper to prevent CRLF on Windows#2552Maanik23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Maanik23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
On Windows, TextIOWrapper without newline=" performs universal newline translation (\n -> \r\n on write), corrupting the newline-delimited JSON wire format used by MCP's stdio transport. Add newline=" to both the stdin and stdout TextIOWrapper calls in stdio_server() to disable translation on all platforms. Add a regression test that inspects the raw bytes written to the output buffer and asserts no \r bytes are present. Fixes modelcontextprotocol#2433
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2433
Summary
On Windows,
TextIOWrapperwithoutnewline=""performs universal newline translation (\n→\r\non write), corrupting the newline-delimited JSON wire format used by MCP's stdio transport.Root Cause
mcp/server/stdio.pycreatesTextIOWrapper(sys.stdout.buffer, encoding="utf-8")without specifyingnewline="". On Windows, the defaultnewline=Nonecauses\n→\r\ntranslation, so every JSON-RPC message written to stdout ends with\r\ninstead of\n.The comment on line 43 even acknowledges: "Encoding of stdin/stdout as text streams on python is platform-dependent (Windows is particularly problematic)" — but the fix applied (re-wrap to ensure UTF-8) didn't also fix the newline translation mode.
Changes
src/mcp/server/stdio.pynewline=""to both the stdin and stdoutTextIOWrappercallsnewline=""disables universal newline translation while still operating in text mode\nis emitted on all platforms, matching the NDJSON wire formattests/server/test_stdio.pytest_stdio_server_no_crlf_on_windows— exercises the defaultsys.stdout.buffercode path viamonkeypatch, inspects raw bytes in the output buffer, and asserts no\rbytes are presentVerification
Compatibility
\nwas already preserved)\r\ncorruption without any new dependenciesnewline=""to prevent future issues if a client sends strict LF