Fix Windows CRLF in stdio server TextIOWrapper by flobo3 · Pull Request #2497 · modelcontextprotocol/python-sdk · GitHub
Skip to content

Fix Windows CRLF in stdio server TextIOWrapper#2497

Open
flobo3 wants to merge 1 commit intomodelcontextprotocol:mainfrom
flobo3:fix/stdio-crlf-windows
Open

Fix Windows CRLF in stdio server TextIOWrapper#2497
flobo3 wants to merge 1 commit intomodelcontextprotocol:mainfrom
flobo3:fix/stdio-crlf-windows

Conversation

@flobo3
Copy link
Copy Markdown

@flobo3 flobo3 commented Apr 23, 2026

Summary

stdio_server() creates TextIOWrapper around sys.stdin.buffer and sys.stdout.buffer without specifying newline="". On Windows, the default newline=None causes \n\r\n translation on stdout and universal newline mode on stdin, violating the NDJSON wire format used by the MCP spec (which specifies LF-only line endings).

Fix

Add newline="" to both TextIOWrapper calls. newline="" disables translation while still operating in text mode, ensuring consistent \n-only output across all platforms.

Testing

  • Existing tests pass (they use StringIO/BytesIO which don't go through the TextIOWrapper default path)
  • Verified the fix produces correct b'...\n' output on Windows 11 with Python 3.12

Fixes #2433

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