Title: 2026-07-28 SSE responses share one header list, so in-place middleware appends leak across responses · Issue #3701 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: 2026-07-28 SSE responses share one header list, so in-place middleware appends leak across responses · Issue #3701 · modelcontextprotocol/python-sdk
X Title: 2026-07-28 SSE responses share one header list, so in-place middleware appends leak across responses · Issue #3701 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before openi...
Open Graph Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontex...
X Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/mod...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3701
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"2026-07-28 SSE responses share one header list, so in-place middleware appends leak across responses","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Release line\n\n2.x (current stable)\n\n### Description\n\nOn the 2026-07-28 HTTP entry, `handle_modern_request` starts every SSE response with the module-level `_SSE_HEADERS` list itself:\n\n```python\nawait send({\"type\": \"http.response.start\", \"status\": _OK_STATUS, \"headers\": _SSE_HEADERS})\n```\n\nASGI middleware that adds a header with `message[\"headers\"].append(...)` therefore appends to the SDK's shared list. The header stays there for the life of the process, so every later SSE response carries one more copy, along with the values set for earlier requests. JSON responses aren't affected, because they build their own header list.\n\nWe hit this in production with an observability middleware that appended an `x-correlation-id`. After about 250 SSE responses in one process, our edge proxy's HTTP client rejected the responses as malformed. Only calls that commit to SSE (a notification, or more than 15 s without one) take this path, so fast calls and most test suites never touch it, and a restart hid it for a while.\n\nThe middleware shouldn't mutate a list it doesn't own; Starlette's `MutableHeaders(scope=message)` copies for exactly this reason. Passing `list(_SSE_HEADERS)` would keep one middleware's mistake from turning into process-wide header growth and cross-request header leakage.\n\nExpected: each SSE response gets its own header list.\n\n### Example Code\n\n```python\nimport asyncio\nimport httpx\nfrom mcp.server.mcpserver import Context, MCPServer\nfrom mcp.server.transport_security import TransportSecuritySettings\n\nmcp = MCPServer(\"repro\")\n\n@mcp.tool()\nasync def slow(ctx: Context) -> str:\n await ctx.report_progress(1, 2) # any notification commits the response to SSE\n return \"done\"\n\ninner = mcp.streamable_http_app(\n transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False)\n)\n\nasync def app(scope, receive, send): # middleware that appends in place\n async def send2(msg):\n if msg[\"type\"] == \"http.response.start\":\n msg[\"headers\"].append((b\"x-correlation-id\", b\"abc\"))\n await send(msg)\n await inner(scope, receive, send2)\n\ndef body(i):\n return {\"jsonrpc\": \"2.0\", \"id\": i, \"method\": \"tools/call\",\n \"params\": {\"name\": \"slow\", \"arguments\": {}, \"_meta\": {\n \"progressToken\": i,\n \"io.modelcontextprotocol/protocolVersion\": \"2026-07-28\",\n \"io.modelcontextprotocol/clientInfo\": {\"name\": \"r\", \"version\": \"0\"},\n \"io.modelcontextprotocol/clientCapabilities\": {}}}}\n\nasync def main():\n async with inner.router.lifespan_context(inner):\n async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url=\"http://localhost\") as c:\n for i in range(1, 6):\n r = await c.post(\"/mcp\", json=body(i), headers={\n \"accept\": \"application/json, text/event-stream\",\n \"mcp-protocol-version\": \"2026-07-28\",\n \"mcp-method\": \"tools/call\", \"mcp-name\": \"slow\"})\n print(i, len(r.headers.get_list(\"x-correlation-id\")))\n\nasyncio.run(main())\n```\n\nOutput: `1 1`, `2 2`, `3 3`, `4 4`, `5 5`.\n\nI have a fix ready (`list(_SSE_HEADERS)` plus a regression test in `tests/server/test_streamable_http_modern.py` that fails before and passes after) and would be glad to open the PR if you assign this to me. Open PR #3192 carries the same line, in case that's the better place for it.\n\nAI disclosure: I used Claude Code to find the line, write the repro, and draft this issue. I hit the bug in production and have checked the repro and the fix myself.\n\n### Python & MCP Python SDK\n\n```\nPython 3.13, mcp 2.3.0 (also present on main at 754254c)\n```\n","author":{"url":"https://github.com/mgoldsborough","@type":"Person","name":"mgoldsborough"},"datePublished":"2026-10-13T21:36:41Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3701"}
| 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:3e687401-c42e-0f4c-b10a-024ed7aad70d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B0B6:26ECEB:D1D9:1103F:6B00B72F |
| html-safe-nonce | 2b4c1ed1a4a35b3e99bd80e5837b866a147b2661a822fde74d65c0ba65d909fc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMEI2OjI2RUNFQjpEMUQ5OjExMDNGOjZCMDBCNzJGIiwidmlzaXRvcl9pZCI6IjE0MjkyMDIxMDY3MTUyNTY2MjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 307dc4976ec615d6f046187085845604bb06903c05d9ab5daa7174895d221da0 |
| hovercard-subject-tag | issue:5838107985 |
| 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/3701/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8a3141b12403ebda372ec1307e7f0997f8b3d80647169faff4ea4f5762b5c823/modelcontextprotocol/python-sdk/issues/3701 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8a3141b12403ebda372ec1307e7f0997f8b3d80647169faff4ea4f5762b5c823/modelcontextprotocol/python-sdk/issues/3701 |
| og:image:alt | Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontex... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mgoldsborough |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1d07c25977adfc5ff6acb0a962a9bc168c07afbda6fef765f9761c350c2ae4a2 |
| 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 | 5429cbfc6aca8c7b9a086a3c8f47400346f38cc9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width