Skip to content

Commit 2e9897e

Browse files
authored
[v1.x] fix: handle non-UTF-8 bytes in stdio server stdin (#2303)
1 parent f8d98b6 commit 2e9897e

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
@@ -44,7 +44,7 @@ async def stdio_server(
4444
# python is platform-dependent (Windows is particularly problematic), so we
4545
# re-wrap the underlying binary stream to ensure UTF-8.
4646
if not stdin:
47-
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8"))
47+
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="replace"))
4848
if not stdout:
4949
stdout = anyio.wrap_file(TextIOWrapper(sys.stdout.buffer, encoding="utf-8"))
5050

@@ -63,7 +63,7 @@ async def stdin_reader():
6363
async for line in stdin:
6464
try:
6565
message = types.JSONRPCMessage.model_validate_json(line)
66-
except Exception as exc: # pragma: no cover
66+
except Exception as exc:
6767
await read_stream_writer.send(exc)
6868
continue
6969

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] == JSONRPCMessage(root=JSONRPCRequest(jsonrpc="2.0", id=3, method="ping"))
6163
assert received_responses[1] == JSONRPCMessage(root=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 == JSONRPCMessage(root=valid)

0 commit comments

Comments
 (0)