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

Open Beginner friendly
#1,933 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
backend

Research direction

Start in src/mcp/server/stdio.py around the wrapper setup at the linked lines, then run the minimal FastMCP reproduction from the issue. Confirm that closing the stdio wrappers no longer closes the process's real standard streams, and verify that the final print succeeds after the server exits.

Written by the indexing model from the issue text.

Description

bug good first issue P3
Initial Checks
Description

Hi! I ran into an issue where running the server with transport="stdio" causes subsequent stdio operations to fail after the server exits.

Minimal reproduction
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Demo")
mcp.run(transport="stdio")
print("?")

When using Ctrl+D to exit the server, the final print raises:

Traceback (most recent call last):
  File "...", line 5, in <module>
    print("?")
ValueError: I/O operation on closed file.

I’m not sure if this is intended behavior though.

Likely cause

In stdio.py, stdio is wrapped like this:
https://github.com/modelcontextprotocol/python-sdk/blob/5301298225968ce0fa8ae62870f950709da14dc6/src/mcp/server/stdio.py#L44-L47

When these wrappers are closed, they also close the sys.stdin.buffer / sys.stdout.buffer.

Proposed fix
    if not stdin:
        stdin_fd = os.dup(sys.stdin.fileno())
        stdin_bin = os.fdopen(stdin_fd, "rb", closefd=True)
        stdin = anyio.wrap_file(TextIOWrapper(stdin_bin, encoding="utf-8"))
    if not stdout:
        stdout_fd = os.dup(sys.stdout.fileno())
        stdout_bin = os.fdopen(stdout_fd, "wb", closefd=True)
        stdout = anyio.wrap_file(TextIOWrapper(stdout_bin, encoding="utf-8"))
Python & MCP Python SDK
Python 3.10.15
mcp==1.25.0
Dominant language
Python
Stars
24.3k
Forks
4k
Avg merge
1d 19m
Merged PRs (30d)
29

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelcontextprotocol/python-sdk

All issues in modelcontextprotocol/python-sdk

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.