Title: fix: handle non-UTF-8 bytes in stdio server stdin by maxisbey · Pull Request #2302 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix: handle non-UTF-8 bytes in stdio server stdin by maxisbey · Pull Request #2302 · modelcontextprotocol/python-sdk
X Title: fix: handle non-UTF-8 bytes in stdio server stdin by maxisbey · Pull Request #2302 · modelcontextprotocol/python-sdk
Description: TextIOWrapper(sys.stdin.buffer, encoding="utf-8") defaults to errors="strict", which raises UnicodeDecodeError when stdin contains bytes that are not valid UTF-8. The exception occurs during async for line in stdin: iteration — outside the JSON-parsing try/except — so it propagates through the task group and terminates the server process. With errors="replace", invalid bytes become U+FFFD. The resulting line then fails JSON validation, which is already caught and sent into the read stream as an exception for the session layer to handle — the same path as any other malformed input. Motivation and Context The MCP spec requires clients to send valid UTF-8 JSON-RPC, and in stdio transport the client spawns the server as its own subprocess — so in practice this only triggers when a client sends protocol-violating input to its own child. But the server process dying on a single bad byte is worse than surfacing a parse error, and the other MCP SDKs (TypeScript, C#, Go, Rust) all handle this gracefully. How Has This Been Tested? Regression test test_stdio_server_invalid_utf8 monkeypatches sys.stdin with a binary buffer containing \xff\xfe\n followed by a valid JSON-RPC message. It verifies the invalid line surfaces as an in-stream exception and the valid message still comes through afterward. Fails on main with ExceptionGroup(UnicodeDecodeError), passes with this fix. Breaking Changes None. Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context Reported by @manthanghasadiya. src/mcp/client/stdio.py has a related issue — StdioServerParameters.encoding_error_handler defaults to "strict", so a buggy server can crash the client the same way. It's configurable so lower priority; worth a follow-up to change the default.
Open Graph Description: TextIOWrapper(sys.stdin.buffer, encoding="utf-8") defaults to errors="strict", which raises UnicodeDecodeError when stdin contains bytes that are not valid UTF-8. The exception ...
X Description: TextIOWrapper(sys.stdin.buffer, encoding="utf-8") defaults to errors="strict", which raises UnicodeDecodeError when stdin contains bytes that are not valid UTF-8...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2302
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:200c141f-1fdd-379c-511b-025c492ecec8 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D156:2E960D:4399E7:5B974D:6A5CC4B6 |
| html-safe-nonce | b5870bc48eb66626e2b42bb3eb032a47e7450f9a3fc4accfa2d1f5ee26ba27a0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTU2OjJFOTYwRDo0Mzk5RTc6NUI5NzREOjZBNUNDNEI2IiwidmlzaXRvcl9pZCI6IjczMDkxMTQ2MzEzMzk4ODAyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d86e28d4dbb7eeddff8da37345a942d857b3834df85b3fac215d40f11a2f9e2c |
| hovercard-subject-tag | pull_request:3410672196 |
| 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/2302/files |
| twitter:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| og:image:alt | TextIOWrapper(sys.stdin.buffer, encoding="utf-8") defaults to errors="strict", which raises UnicodeDecodeError when stdin contains bytes that are not valid UTF-8. The exception ... |
| 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