Title: fix(client): tolerate invalid UTF-8 from server stdout in stdio_client by Bartok9 · Pull Request #2873 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix(client): tolerate invalid UTF-8 from server stdout in stdio_client by Bartok9 · Pull Request #2873 · modelcontextprotocol/python-sdk
X Title: fix(client): tolerate invalid UTF-8 from server stdout in stdio_client by Bartok9 · Pull Request #2873 · modelcontextprotocol/python-sdk
Description: Summary Default StdioServerParameters.encoding_error_handler to "replace" so a server emitting malformed UTF-8 no longer crashes the client transport. Malformed bytes now surface as an in-stream JSON parse error and the transport stays alive for subsequent valid messages. Motivation Closes #2454. stdio_client() decoded child stdout with encoding_error_handler="strict". When a spawned server writes invalid UTF-8 bytes to stdout, the UnicodeDecodeError raised during TextReceiveStream iteration escapes the decode loop's except clauses and tears down the transport task group — it surfaces as an ExceptionGroup out of the context manager instead of being delivered as a normal in-stream parse error. The issue itself points at the analogous server-side hardening (#2302, errors="replace" on stdin), which deliberately chose to: replace invalid bytes with U+FFFD, let JSON validation fail on the malformed line, and keep the transport alive. This change brings the client to parity: defaulting encoding_error_handler to "replace" means the bad line fails JSON-RPC validation and is delivered as an Exception via _parse_line, while later valid messages still come through. Verification uv run pytest tests/client/test_stdio.py -q — 34 passed, 1 skipped uv run pytest tests/client/ tests/interaction/transports/test_stdio.py -q — 238 passed, 1 skipped, 1 xfailed (no regression from the default change) New regression test test_invalid_utf8_mid_session_surfaces_as_an_in_stream_exception fails without the fix (5s task-group hang) and passes with it. Manual repro from the issue (\xff\xfe\n followed by a valid ping) crashed the task group on main with an ExceptionGroup(UnicodeDecodeError, ...); with the fix it surfaces a ValidationError then reads the following valid message. ruff format --check + ruff check + pyright clean on both changed files. Notes Salvages the intent of the stale, now-conflicting #2456 by @shaun0927 (same root cause + default flip), rebuilt cleanly against current main after the transport was refactored. The decode/drain plumbing moved, so this is a fresh implementation on the new code path with a mid-session regression test (distinct from the existing test_invalid_utf8_flushed_by_a_dying_server_does_not_break_shutdown, which only covers the raw-bytes shutdown drain).
Open Graph Description: Summary Default StdioServerParameters.encoding_error_handler to "replace" so a server emitting malformed UTF-8 no longer crashes the client transport. Malformed bytes now surface as an i...
X Description: Summary Default StdioServerParameters.encoding_error_handler to "replace" so a server emitting malformed UTF-8 no longer crashes the client transport. Malformed bytes now surface...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2873
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:bb6af536-615e-53d9-c2cd-cf4a277f322c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AF64:D2231:38BFF:4B35B:6A615624 |
| html-safe-nonce | c4d84f4c98badef10299294b9ba799ffb9bdf4ad1513d678f7b134f5a2716333 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjY0OkQyMjMxOjM4QkZGOjRCMzVCOjZBNjE1NjI0IiwidmlzaXRvcl9pZCI6IjE4NzAyOTU4NjY0NzkzMDIxODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 979212db7d1ae422b236b105c4a040db2391c4abbad7bfc672cb2e2d4d357fba |
| hovercard-subject-tag | pull_request:3865492699 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2873/files |
| twitter:image | https://avatars.githubusercontent.com/u/259807879?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/259807879?s=400&v=4 |
| og:image:alt | Summary Default StdioServerParameters.encoding_error_handler to "replace" so a server emitting malformed UTF-8 no longer crashes the client transport. Malformed bytes now surface as an i... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 35b919fdb8e6752d2ed95a144e761ca5e924557563e2ed0a5ebf6c4430cdeeb6 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 1e22b08b60ccb67c3791f565bed248565260e611 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width