stdio_client crashes on malformed UTF-8 from child stdout instead of surfacing parse error
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
從 mcp.client.stdio.stdio_client() 和 tests/client/test_stdio.py 開始;檢查 TextReceiveStream 解碼和讀取串流迭代如何處理編碼錯誤。新增 issue 中描述的 malformed-then-valid 回歸測試,然後執行針對性的測試,確認第一個項目是 Exception,第二個是有效的 SessionMessage,且傳輸仍保持運作。
由索引模型根據 Issue 內容生成。
描述
Summary
mcp.client.stdio.stdio_client() crashes when the spawned child process writes invalid UTF-8 bytes to stdout.
The transport currently decodes child stdout with encoding_error_handler="strict", so malformed bytes raise during TextReceiveStream(...) iteration. That exception escapes the decoding loop and brings down the transport task group instead of surfacing the bad line as an in-stream parse error.
Why this looks like a bug
The SDK already hardened the server side for the analogous case in PR #2302 (fix: handle non-UTF-8 bytes in stdio server stdin). That change explicitly preferred:
- replace invalid bytes with U+FFFD,
- let JSON validation fail on the malformed line, and
- keep the transport alive so subsequent valid messages can still be processed.
The client side still behaves asymmetrically today. A buggy or non-compliant child server can kill the Python client transport with a single malformed line even if the next line is valid JSON-RPC.
That seems inconsistent with the current stdio robustness direction.
Reproduction
A minimal child process that writes one malformed line and then one valid JSON-RPC line:
import sys
import time
sys.stdout.buffer.write(b"\xff\xfe\n")
sys.stdout.buffer.write(b'{"jsonrpc":"2.0","id":1,"method":"ping"}\n')
sys.stdout.buffer.flush()
time.sleep(0.2)
With current stdio_client(...) defaults, the transport raises ExceptionGroup instead of continuing.
Expected behavior
The malformed line should be surfaced as an in-stream parse / validation error, and the next valid JSON-RPC line should still be received.
Observed behavior
The transport task group fails before the valid follow-up message is delivered.
Proposed fix
Match the server-side approach from PR #2302:
- default
StdioServerParameters.encoding_error_handlerto"replace" - continue treating malformed decoded lines as JSON validation failures
- keep the background stdio tasks resilient during early subprocess shutdown / abrupt close
Validation
I reproduced this locally against current main and verified that a minimal patch plus a regression test fixes it.
A focused regression test in tests/client/test_stdio.py can assert:
- first item from the read stream is an
Exception - second item is the valid
SessionMessage
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 7 小時
- 30 天內合併 PR
- 18
環境準備
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
modelcontextprotocol/python-sdk 的其他 Issue
-
v1 v2
難度 2/5 1-3 小時 新手友好度 86/100
modelcontextprotocol/python-sdk#3589 ·
維護者通常 1 天內回覆
-
v1 v2
難度 2/5 1-3 小時 新手友好度 84/100
modelcontextprotocol/python-sdk#3585 ·
維護者通常 1 天內回覆
-
v1 v2
難度 2/5 1-3 小時 新手友好度 70/100
modelcontextprotocol/python-sdk#3578 · 1 則留言 ·
維護者通常 1 天內回覆
-
v1 v2
難度 2/5 1-3 小時 新手友好度 65/100
modelcontextprotocol/python-sdk#3573 · 4 則留言 ·
維護者通常 1 天內回覆
-
v2
難度 2/5 1-3 小時 新手友好度 75/100
modelcontextprotocol/python-sdk#3566 ·
維護者通常 1 天內回覆
查看 modelcontextprotocol/python-sdk 的全部 Issue
相似的 Issue
-
needs triage
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
openvinotoolkit/openvino_notebooks#3665 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 86/100
維護者通常 1 天內回覆
-
docs
難度 2/5 1-3 小時 新手友好度 88/100
維護者通常 1 天內回覆
-
benchmark-gap
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆