Skip to content

Fix stdio_server from closing process stdio handles#2142

Open
aframedelta wants to merge 1 commit intomodelcontextprotocol:mainfrom
aframedelta:fix-1933-stdio-no-close
Open

Fix stdio_server from closing process stdio handles#2142
aframedelta wants to merge 1 commit intomodelcontextprotocol:mainfrom
aframedelta:fix-1933-stdio-no-close

Conversation

@aframedelta
Copy link

Summary

  • prevent stdio_server() from closing underlying process stdio handles
  • add a regression test that verifies stdio remains usable after server shutdown

Why

Closes #1933.

stdio_server() wraps sys.stdin.buffer / sys.stdout.buffer in a TextIOWrapper. When the async wrapper is closed, the underlying process stdio can be closed too, causing subsequent print() calls to fail with ValueError: I/O operation on closed file.

Changes

  • introduce _NonClosingTextIOWrapper in mcp.server.stdio
  • use it for default process stdio wrapping
  • add test_stdio_server_does_not_close_process_stdio regression test

Validation

  • . .venv/bin/activate && pytest tests/server/test_stdio.py -q
    • 2 passed

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