fix: stdio_server emits LF only on Windows stdout (fixes #2433)#2908
Open
kaXianc2-gom wants to merge 1 commit into
Open
fix: stdio_server emits LF only on Windows stdout (fixes #2433)#2908kaXianc2-gom wants to merge 1 commit into
kaXianc2-gom wants to merge 1 commit into
Conversation
Add newline='' to TextIOWrapper for stdout to prevent \n → \r\n translation on Windows, which corrupts newline-delimited JSON messages. The MCP protocol uses \n as the line delimiter. Emitting \r\n is a protocol-level impurity that breaks clients parsing JSON lines. stdin is intentionally left with the default newline=None so that universal-newline behaviour normalises \r\n → \n on read. Fixes modelcontextprotocol#2433 Co-Authored-By: Claude <noreply@anthropic.com>
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.
Summary
Fix #2433: Windows
TextIOWrapperinstdio_server()emits\r\ninstead of\n, corrupting newline-delimited JSON messages.Change
Add
newline=""toTextIOWrapper(sys.stdout.buffer, ...)so the MCP protocol line delimiter (\n) is preserved on Windows.src/mcp/server/stdio.py: +3 lines (1 line of code + 2 comment lines), −1 linetests/server/test_stdio.py: +18 lines (new testtest_mcpserver_run_stdio_emits_lf_not_crlf)stdinis intentionally left with the defaultnewline=Noneso universal-newline behaviour normalises\r\n→\non read.AI Assistance Disclosure
This contribution was developed with AI assistance (Claude / Anthropic Claude Code). The process was:
stdio.py(addnewline=""parameter)Verification Environment
Why this fix is correct
"json\n"on Windows stdout"json\r\n"❌"json\n"✅"json\r\n"from Windows stdin"json\n"✅