Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/mcp/server/stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ async def run_server():
```
"""

import io
import os
import sys
from contextlib import asynccontextmanager
from io import TextIOWrapper
Expand All @@ -34,14 +36,32 @@ async def stdio_server(stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.
"""Server transport for stdio: this communicates with an MCP client by reading
from the current process' stdin and writing to stdout.
"""
# Purposely not using context managers for these, as we don't want to close
# standard process handles. Encoding of stdin/stdout as text streams on
# python is platform-dependent (Windows is particularly problematic), so we
# re-wrap the underlying binary stream to ensure UTF-8.
# When stdin/stdout are not provided, duplicate the underlying file descriptors
# so that closing the wrappers does not close the real sys.stdin/sys.stdout.
# Encoding of stdin/stdout as text streams on Python is platform-dependent
# (Windows is particularly problematic), so we re-wrap the underlying binary
# stream to ensure UTF-8.
_stdin_wrapper: TextIOWrapper | None = None
_stdout_wrapper: TextIOWrapper | None = None

if not stdin:
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="replace"))
try:
stdin_fd = os.dup(sys.stdin.fileno())
_stdin_wrapper = TextIOWrapper(os.fdopen(stdin_fd, "rb"), encoding="utf-8", errors="replace")
stdin = anyio.wrap_file(_stdin_wrapper)
except (AttributeError, io.UnsupportedOperation):
# sys.stdin has no real fd (e.g. BytesIO in tests) — wrap buffer directly.
# Closing this wrapper also closes the buffer, but that is harmless in
# that context because there is no real fd to leak.
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="replace"))
if not stdout:
stdout = anyio.wrap_file(TextIOWrapper(sys.stdout.buffer, encoding="utf-8"))
try:
stdout_fd = os.dup(sys.stdout.fileno())
_stdout_wrapper = TextIOWrapper(os.fdopen(stdout_fd, "wb"), encoding="utf-8")
stdout = anyio.wrap_file(_stdout_wrapper)
except (AttributeError, io.UnsupportedOperation):
# sys.stdout has no real fd — wrap buffer directly (same reasoning as above).
stdout = anyio.wrap_file(TextIOWrapper(sys.stdout.buffer, encoding="utf-8"))

read_stream_writer, read_stream = create_context_streams[SessionMessage | Exception](0)
write_stream, write_stream_reader = create_context_streams[SessionMessage](0)
Expand Down Expand Up @@ -71,7 +91,13 @@ async def stdout_writer():
except anyio.ClosedResourceError: # pragma: no cover
await anyio.lowlevel.checkpoint()

async with anyio.create_task_group() as tg:
tg.start_soon(stdin_reader)
tg.start_soon(stdout_writer)
yield read_stream, write_stream
try:
async with anyio.create_task_group() as tg:
tg.start_soon(stdin_reader)
tg.start_soon(stdout_writer)
yield read_stream, write_stream
finally:
if _stdout_wrapper is not None:
_stdout_wrapper.close()
if _stdin_wrapper is not None:
_stdin_wrapper.close()
33 changes: 33 additions & 0 deletions tests/server/test_stdio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import io
import os
import sys
import tempfile
from io import TextIOWrapper

import anyio
Expand Down Expand Up @@ -63,6 +65,37 @@ async def test_stdio_server():
assert received_responses[1] == JSONRPCResponse(jsonrpc="2.0", id=4, result={})


@pytest.mark.anyio
async def test_stdio_server_does_not_close_real_stdio(monkeypatch: pytest.MonkeyPatch):
"""stdio_server() must not close the real sys.stdin/sys.stdout after exiting.

Regression test for https://github.com/modelcontextprotocol/python-sdk/issues/1933.
"""
# Substitute sys.stdin/stdout with real file-backed streams so that fileno()
# works and we can verify the fds remain open after the server exits.
with tempfile.TemporaryFile() as tf_in, tempfile.TemporaryFile() as tf_out:
fake_stdin = TextIOWrapper(tf_in, encoding="utf-8")
fake_stdout = TextIOWrapper(tf_out, encoding="utf-8")
monkeypatch.setattr(sys, "stdin", fake_stdin)
monkeypatch.setattr(sys, "stdout", fake_stdout)

real_stdin_fd = sys.stdin.fileno()
real_stdout_fd = sys.stdout.fileno()

with anyio.fail_after(5):
async with stdio_server() as (read_stream, write_stream):
await write_stream.aclose()
await read_stream.aclose()

# os.fstat() raises OSError if the fd has been closed; successful calls
# prove stdio_server() did not close the real process descriptors.
os.fstat(real_stdin_fd)
os.fstat(real_stdout_fd)
# The Python wrappers we set as sys.stdin/stdout must not be closed either.
assert not sys.stdin.closed
assert not sys.stdout.closed


@pytest.mark.anyio
async def test_stdio_server_invalid_utf8(monkeypatch: pytest.MonkeyPatch):
"""Non-UTF-8 bytes on stdin must not crash the server.
Expand Down
Loading