2026-07-28 SSE responses share one header list, so in-place middleware appends leak across responses
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
- #3704 來自 @guptaishaan —— 已關閉,未合併
評估
研究方向
從 handle_modern_request 開始,它以模組層級的列表 _SSE_HEADERS 傳送 SSE 回應。檢查為每個回應複製一份是否能修復就地附加造成的洩漏。在撰寫修復之前,先查看已關閉但未合併的 PR #3704,弄清楚它未被合併的原因。完成標準:tests/server/test_streamable_http_modern.py 中的回歸測試在目前的 main 上失敗,並在每個 SSE 回應都擁有自己的標頭列表後通過。
由索引模型根據 Issue 內容生成。
描述
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 opening this issue
Release line
2.x (current stable)
Description
On the 2026-07-28 HTTP entry, handle_modern_request starts every SSE response with the module-level _SSE_HEADERS list itself:
await send({"type": "http.response.start", "status": _OK_STATUS, "headers": _SSE_HEADERS})
ASGI 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.
We 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.
The 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.
Expected: each SSE response gets its own header list.
Example Code
import asyncio
import httpx
from mcp.server.mcpserver import Context, MCPServer
from mcp.server.transport_security import TransportSecuritySettings
mcp = MCPServer("repro")
@mcp.tool()
async def slow(ctx: Context) -> str:
await ctx.report_progress(1, 2) # any notification commits the response to SSE
return "done"
inner = mcp.streamable_http_app(
transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False)
)
async def app(scope, receive, send): # middleware that appends in place
async def send2(msg):
if msg["type"] == "http.response.start":
msg["headers"].append((b"x-correlation-id", b"abc"))
await send(msg)
await inner(scope, receive, send2)
def body(i):
return {"jsonrpc": "2.0", "id": i, "method": "tools/call",
"params": {"name": "slow", "arguments": {}, "_meta": {
"progressToken": i,
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
"io.modelcontextprotocol/clientInfo": {"name": "r", "version": "0"},
"io.modelcontextprotocol/clientCapabilities": {}}}}
async def main():
async with inner.router.lifespan_context(inner):
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://localhost") as c:
for i in range(1, 6):
r = await c.post("/mcp", json=body(i), headers={
"accept": "application/json, text/event-stream",
"mcp-protocol-version": "2026-07-28",
"mcp-method": "tools/call", "mcp-name": "slow"})
print(i, len(r.headers.get_list("x-correlation-id")))
asyncio.run(main())
Output: 1 1, 2 2, 3 3, 4 4, 5 5.
I 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.
AI 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.
Python & MCP Python SDK
Python 3.13, mcp 2.3.0 (also present on main at 754254c)
- 主要語言
- Python
- 星號
- 24.5k
- 分支
- 4k
- 平均合併
- 18 小時 31 分鐘
- 30 天內合併 PR
- 38
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
modelcontextprotocol/python-sdk 的其他 Issue
-
bug v1 v2
難度 2/5 1-3 小時 新手友好度 78/100
modelcontextprotocol/python-sdk#3698 ·
維護者通常 1 天內回覆
-
Streamable HTTP: Content-Type handling is order dependent and accepts invalid values可能已有人在做 @jayzuccarelli 於 2 天前認領。 未關閉bug v1 v2
難度 2/5 1-3 小時 新手友好度 76/100
modelcontextprotocol/python-sdk#3691 · 1 則留言 ·
維護者通常 1 天內回覆
-
Skills: URI traversal validation skips percent-encoded dot-segments可能已有人在做 @Varshith-Kali 於 1 天前認領。 未關閉bug v2
難度 2/5 1-3 小時 新手友好度 78/100
modelcontextprotocol/python-sdk#3685 · 2 則留言 ·
維護者通常 1 天內回覆
-
Type annotation gaps and unhandled FileNotFoundError in src/mcp/cli/cli.py可能重新可做 關聯的 PR 已關閉且未合併。 未關閉enhancement
難度 2/5 1-3 小時 新手友好度 84/100
modelcontextprotocol/python-sdk#3673 · 4 則留言 ·
維護者通常 1 天內回覆
-
Streamable HTTP server rejects a mixed-case Content-Type with 415可能已有人在做 @CRYPTONIKAV 於 4 天前認領。 未關閉bug v1 v2
難度 2/5 1-3 小時 新手友好度 75/100
modelcontextprotocol/python-sdk#3670 · 4 則留言 ·
維護者通常 1 天內回覆
查看 modelcontextprotocol/python-sdk 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 62/100
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 74/100
WingedGuardian/GENesis-AGI#3375 ·
維護者通常 1 天內回覆
-
curriculum documentation quality
難度 2/5 1-3 小時 新手友好度 75/100
githubnext/gh-aw-workshop#4601 ·
維護者通常 1 天內回覆
-
approved Good First Issue scope: docs
難度 1/5 1 小時以內 新手友好度 85/100
hiero-ledger/hiero-sdk-python#2730 · 3 則留言 ·
維護者通常 1 天內回覆