Skip to content

fix: stdio_server emits LF only on Windows stdout (fixes #2433)#2908

Open
kaXianc2-gom wants to merge 1 commit into
modelcontextprotocol:mainfrom
kaXianc2-gom:fix/stdio-windows-crlf
Open

fix: stdio_server emits LF only on Windows stdout (fixes #2433)#2908
kaXianc2-gom wants to merge 1 commit into
modelcontextprotocol:mainfrom
kaXianc2-gom:fix/stdio-windows-crlf

Conversation

@kaXianc2-gom

Copy link
Copy Markdown

Summary

Fix #2433: Windows TextIOWrapper in stdio_server() emits \r\n instead of \n, corrupting newline-delimited JSON messages.

Change

Add newline="" to TextIOWrapper(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 line
  • tests/server/test_stdio.py: +18 lines (new test test_mcpserver_run_stdio_emits_lf_not_crlf)

stdin is intentionally left with the default newline=None so universal-newline behaviour normalises \r\n\n on read.

AI Assistance Disclosure

This contribution was developed with AI assistance (Claude / Anthropic Claude Code). The process was:

  1. Issue triage: AI identified Windows: TextIOWrapper in stdio_server() emits CRLF instead of LF, corrupting newline-delimited JSON messages #2433 as a Windows-specific bug matching our environment
  2. Local sandbox verification: Forked → cloned to local sandbox → created venv → ran full existing test suite (629 passed, 1 skipped)
  3. Fix applied: One-line change to stdio.py (add newline="" parameter)
  4. Cross-validation performed:
    • ✅ Existing tests: 5/5 passed (including 4 legacy tests)
    • ✅ New CRLF-specific test: passes on Windows
    • ✅ Full server test suite: 629/629 passed
    • ✅ Ruff format: no changes needed
    • ✅ Ruff lint: all checks passed
  5. Human review: The change was reviewed and understood before submission

Verification Environment

Why this fix is correct

Scenario Before (buggy) After (fixed)
Write "json\n" on Windows stdout Emits "json\r\n" Emits "json\n"
Read "json\r\n" from Windows stdin Normalises to "json\n" Unchanged ✅
Linux/macOS No impact (newline=None is no-op) No impact (newline="" is no-op)

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>
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