Title: streamable-http client: no size bound before JSONRPCMessage.model_validate_json — one large server message can OOM the client · Issue #3330 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: streamable-http client: no size bound before JSONRPCMessage.model_validate_json — one large server message can OOM the client · Issue #3330 · modelcontextprotocol/python-sdk
X Title: streamable-http client: no size bound before JSONRPCMessage.model_validate_json — one large server message can OOM the client · Issue #3330 · modelcontextprotocol/python-sdk
Description: Summary StreamableHTTPTransport parses every inbound SSE event and JSON response body with JSONRPCMessage.model_validate_json / model_validate_json(content) and no size bound. Because pydantic validation of a large JSON document allocate...
Open Graph Description: Summary StreamableHTTPTransport parses every inbound SSE event and JSON response body with JSONRPCMessage.model_validate_json / model_validate_json(content) and no size bound. Because pydantic vali...
X Description: Summary StreamableHTTPTransport parses every inbound SSE event and JSON response body with JSONRPCMessage.model_validate_json / model_validate_json(content) and no size bound. Because pydantic vali...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3330
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"streamable-http client: no size bound before JSONRPCMessage.model_validate_json — one large server message can OOM the client","articleBody":"### Summary\n\n`StreamableHTTPTransport` parses every inbound SSE event and JSON response body with `JSONRPCMessage.model_validate_json` / `model_validate_json(content)` and no size bound. Because pydantic validation of a large JSON document allocates several times the wire size in live Python objects, a single oversized message from a server can exhaust the client's memory. There is no hook to inspect or reject a message before it is parsed.\n\nWe hit this in production: a client connected to ~30 MCP servers, one of which answered `tools/list` with a **21.9 MB** single SSE event. Measured with `tracemalloc` at the parse site:\n\n```\n#1 663.4 MB in 9,957,044 blocks\n mcp/client/streamable_http.py:217\n message = JSONRPCMessage.model_validate_json(sse.data)\n#2 46.9 MB in 3 blocks\n httpx_sse/_decoders.py:61 (whole event buffered as one string)\n#3 46.9 MB in 2 blocks\n httpx_sse/_decoders.py:120 (copy from slicing)\n```\n\nRoughly a 7× amplification of the wire size in live objects, transient but concurrent with other parses. A trivial request that triggered only catalogue loading peaked at **2.35 GB** RSS from a 56 MB baseline; heavier concurrent work reached **4.2 GB**. Removing that one server dropped the same request's peak to **456 MB**. Versions: `mcp` 1.27.1, Python 3.14.\n\n### Why a client-side bound is needed\n\nThe client cannot know in advance that a server will return a huge payload, and a misbehaving or misconfigured server should not be able to OOM its client. Today the only outcome is process death with nothing identifying the responsible server — the failure surfaces as an unexplained kill rather than an actionable error.\n\n### What we did as a workaround, and why it was awkward\n\nWe wrapped `StreamableHTTPTransport._handle_sse_event` and `_handle_json_response` to measure `sse.data` / the response body and reject anything over a configured cap before parsing. Two things made this harder than expected, and both seem worth addressing upstream:\n\n1. **Raising from the handler is not viable.** Every call site wraps it in `except Exception: logger.debug(...)` and then reconnects with `Last-Event-ID`, which replays the same oversized payload. The raise is swallowed and the pending request hangs.\n2. **Delivering a bare `Exception` on the read stream does not fail the request either.** In `BaseSession._receive_loop`, `isinstance(message, Exception)` routes to `_handle_incoming`, which for the default message handler is a no-op; only a `JSONRPCResponse`/`JSONRPCError` matching `_response_streams[request_id]` completes `send_request`, and that awaits with `timeout=None`. To fail the request we had to synthesize a `JSONRPCError` and recover the request id from the raw payload with a regex, since parsing it is exactly what we were trying to avoid.\n\n### Suggested improvements\n\n- An optional client-side maximum message size (constructor argument and/or environment variable) enforced **before** `model_validate_json`, on both the SSE and `application/json` paths.\n- When it trips, fail the corresponding pending request with a distinct error identifying the endpoint and the observed size, rather than letting the reconnect path replay the payload.\n- Failing that, a documented hook to inspect a raw message before parsing would let clients implement this without patching private methods.\n- Independently: `_handle_sse_event` returning `False` for a parse failure causes the caller to treat the stream as ended and reconnect with `Last-Event-ID`; for a deterministic failure (such as a payload that will always be too large, or malformed JSON) this retries something that cannot succeed.\n\nHappy to open a PR if a maintainer indicates the preferred shape (constructor arg vs. env var vs. pre-parse hook).\n","author":{"url":"https://github.com/aartaria","@type":"Person","name":"aartaria"},"datePublished":"2026-08-18T08:15:17Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3330"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3f10b36e-0d34-9ef3-82d7-e99457ac494e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8660:1148D3:14EFC26:1AC2059:6B0130FE |
| html-safe-nonce | d17e43ff387d903464c6ab433167f2ef4a211328dfc4a8568660fa09cd67dda3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjYwOjExNDhEMzoxNEVGQzI2OjFBQzIwNTk6NkIwMTMwRkUiLCJ2aXNpdG9yX2lkIjoiMjgxODM0NDI3ODU3MTQzMDE0MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9c71a51f972bf19ea1bb2bafd13d16949664e3cc10a37fabcaebc0bd88348c8e |
| hovercard-subject-tag | issue:5179664911 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3330/issue_layout |
| twitter:image | https://opengraph.githubassets.com/33b4386eae4fb86916afb8de5b21ab8eeb49c1b5647dfe18ab1d7be330834de0/modelcontextprotocol/python-sdk/issues/3330 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/33b4386eae4fb86916afb8de5b21ab8eeb49c1b5647dfe18ab1d7be330834de0/modelcontextprotocol/python-sdk/issues/3330 |
| og:image:alt | Summary StreamableHTTPTransport parses every inbound SSE event and JSON response body with JSONRPCMessage.model_validate_json / model_validate_json(content) and no size bound. Because pydantic vali... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aartaria |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width