Title: fix: default encoding_error_handler to replace in stdio_client for UTF-8 resilience by Maanik23 · Pull Request #2553 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix: default encoding_error_handler to replace in stdio_client for UTF-8 resilience by Maanik23 · Pull Request #2553 · modelcontextprotocol/python-sdk
X Title: fix: default encoding_error_handler to replace in stdio_client for UTF-8 resilience by Maanik23 · Pull Request #2553 · modelcontextprotocol/python-sdk
Description: Fixes #2454 Summary The stdio_client transport crashes when the spawned child process writes invalid UTF-8 bytes to stdout. The transport decodes child stdout with encoding_error_handler="strict" by default, 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. Asymmetry with server side 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 Keep the transport alive so subsequent valid messages can still be processed The client side was still using "strict", creating an inconsistency. Changes src/mcp/client/stdio.py Changed StdioServerParameters.encoding_error_handler default from "strict" to "replace" Updated docstring to explain the rationale Changed logger.exception to logger.warning for JSON parse failures (avoids noisy full tracebacks for expected validation errors on malformed input) Removed # pragma: no cover from the now-reachable exception handling path tests/client/test_stdio.py Added test_stdio_client_invalid_utf8_resilience — spawns a child that writes \xff\xfe\n (invalid UTF-8) followed by a valid JSON-RPC message, then asserts: First item from read stream is an Exception (JSON validation failure on the U+FFFD-replaced line) Second item is a valid SessionMessage with the expected payload Backward compatibility Users who explicitly set encoding_error_handler="strict" retain the old behavior The default change is safe: any code consuming the read stream already handles Exception items (the type signature is MemoryObjectReceiveStream[SessionMessage | Exception]) No new dependencies
Open Graph Description: Fixes #2454 Summary The stdio_client transport crashes when the spawned child process writes invalid UTF-8 bytes to stdout. The transport decodes child stdout with encoding_error_handler="stri...
X Description: Fixes #2454 Summary The stdio_client transport crashes when the spawned child process writes invalid UTF-8 bytes to stdout. The transport decodes child stdout with encoding_error_handler="...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2553
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:91d5ef78-0d20-0f4c-08f6-db89563615cb |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 8730:27DFD6:2282F11:2E0F60D:6A5D7502 |
| html-safe-nonce | 449091f6bc0d3672b84ef96c88ea15ff2f89099a93b6dda2ca4c23282d642a71 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NzMwOjI3REZENjoyMjgyRjExOjJFMEY2MEQ6NkE1RDc1MDIiLCJ2aXNpdG9yX2lkIjoiNzA3NTc3ODY2NzEwOTc3NDU5NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3da7655736c1ab94546cb3db9ab0168f34e8b3ea3eea8b6c2e033017ce93e3bf |
| hovercard-subject-tag | pull_request:3643429857 |
| 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/2553/files |
| twitter:image | https://avatars.githubusercontent.com/u/46487935?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/46487935?s=400&v=4 |
| og:image:alt | Fixes #2454 Summary The stdio_client transport crashes when the spawned child process writes invalid UTF-8 bytes to stdout. The transport decodes child stdout with encoding_error_handler="stri... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width