Skip to content

Fix stdio_server closing process stdio#2505

Open
GitAashishG wants to merge 2 commits intomodelcontextprotocol:mainfrom
GitAashishG:fix-1933-stdio-does-not-close-real-stdio
Open

Fix stdio_server closing process stdio#2505
GitAashishG wants to merge 2 commits intomodelcontextprotocol:mainfrom
GitAashishG:fix-1933-stdio-does-not-close-real-stdio

Conversation

@GitAashishG
Copy link
Copy Markdown

Summary

  • duplicate stdin/stdout file descriptors before wrapping them in stdio_server
  • close only the duplicated async wrappers during teardown so process stdio stays usable
  • add a regression test covering post-shutdown writes to sys.stdout

Testing

  • uv run --frozen pytest tests/server/test_stdio.py
  • uv run --frozen ruff check src/mcp/server/stdio.py tests/server/test_stdio.py

Closes #1933

- Widen `_wrap_stdio_text_stream`'s parameter type from `TextIOWrapper`
  to `typing.TextIO` so `sys.stdin`/`sys.stdout` (typed `TextIO` in
  typeshed) pass type checking.
- Add a regression test using `io.BytesIO`-backed streams to cover the
  `AttributeError`/`UnsupportedOperation` fallback path (needed to
  keep coverage at 100%).
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.

Using transport="stdio" closes real stdio, causing ValueError after server exits

1 participant