Skip to content

Commit 1a2244f

Browse files
authored
fix: handle non-UTF-8 bytes in stdio server stdin (#2302)
1 parent 75a80b6 commit 1a2244f

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/mcp/server/stdio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def stdio_server(stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.
3939
# python is platform-dependent (Windows is particularly problematic), so we
4040
# re-wrap the underlying binary stream to ensure UTF-8.
4141
if not stdin:
42-
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8"))
42+
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="replace"))
4343
if not stdout:
4444
stdout = anyio.wrap_file(TextIOWrapper(sys.stdout.buffer, encoding="utf-8"))
4545

@@ -58,7 +58,7 @@ async def stdin_reader():
5858
async for line in stdin:
5959
try:
6060
message = types.jsonrpc_message_adapter.validate_json(line, by_name=False)
61-
except Exception as exc: # pragma: no cover
61+
except Exception as exc:
6262
await read_stream_writer.send(exc)
6363
continue
6464

tests/server/test_stdio.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import io
2+
import sys
3+
from io import TextIOWrapper
24

35
import anyio
46
import pytest
@@ -59,3 +61,34 @@ async def test_stdio_server():
5961
assert len(received_responses) == 2
6062
assert received_responses[0] == JSONRPCRequest(jsonrpc="2.0", id=3, method="ping")
6163
assert received_responses[1] == JSONRPCResponse(jsonrpc="2.0", id=4, result={})
64+
65+
66+
@pytest.mark.anyio
67+
async def test_stdio_server_invalid_utf8(monkeypatch: pytest.MonkeyPatch):
68+
"""Non-UTF-8 bytes on stdin must not crash the server.
69+
70+
Invalid bytes are replaced with U+FFFD, which then fails JSON parsing and
71+
is delivered as an in-stream exception. Subsequent valid messages must
72+
still be processed.
73+
"""
74+
# \xff\xfe are invalid UTF-8 start bytes.
75+
valid = JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")
76+
raw_stdin = io.BytesIO(b"\xff\xfe\n" + valid.model_dump_json(by_alias=True, exclude_none=True).encode() + b"\n")
77+
78+
# Replace sys.stdin with a wrapper whose .buffer is our raw bytes, so that
79+
# stdio_server()'s default path wraps it with errors='replace'.
80+
monkeypatch.setattr(sys, "stdin", TextIOWrapper(raw_stdin, encoding="utf-8"))
81+
monkeypatch.setattr(sys, "stdout", TextIOWrapper(io.BytesIO(), encoding="utf-8"))
82+
83+
with anyio.fail_after(5):
84+
async with stdio_server() as (read_stream, write_stream):
85+
await write_stream.aclose()
86+
async with read_stream: # pragma: no branch
87+
# First line: \xff\xfe -> U+FFFD U+FFFD -> JSON parse fails -> exception in stream
88+
first = await read_stream.receive()
89+
assert isinstance(first, Exception)
90+
91+
# Second line: valid message still comes through
92+
second = await read_stream.receive()
93+
assert isinstance(second, SessionMessage)
94+
assert second.message == valid

0 commit comments

Comments
 (0)