modelcontextprotocol / modelcontextprotocol/python-sdk

FastMCP server death on client HEAD calls

未關閉
#1,269 6 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

bug P2 ready for work
主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 1 小時
30 天內合併 PR
31

描述

Initial Checks
Description

I am using streamable HTTP for MCP. I see HTTP HEAD calls in the log which killing the server. I expect the server to be resilient to poor user behavior.

My code:
https://github.com/deephaven/deephaven-mcp/tree/main/src/deephaven_mcp/mcp_docs_server

Example logs:

2025-08-13 09:29:26.178 PDT
[2025-08-13 16:29:26,179] INFO: [mcp_docs_server:app_lifespan] MCP docs server ready and yielding context
2025-08-13 09:29:26.179 PDT
INFO: 169.254.169.126:65146 - "HEAD /mcp HTTP/1.1" 405 Method Not Allowed
2025-08-13 09:29:26.179 PDT
[2025-08-13 16:29:26,180] INFO: Terminating session: None
2025-08-13 09:29:26.183 PDT
[2025-08-13 16:29:26,180] ERROR: Error in message router
2025-08-13 09:29:26.183 PDT
Traceback (most recent call last):
  File "/app/.venv/lib/python3.12/site-packages/mcp/server/streamable_http.py", line 831, in message_router
    async for session_message in write_stream_reader:
  File "/app/.venv/lib/python3.12/site-packages/anyio/abc/_streams.py", line 41, in __anext__
    return await self.receive()

2025-08-13 09:29:26.183 PDT
^^^^^^^^^^^^^^^^^^^^
2025-08-13 09:29:26.183 PDT
File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 111, in receive
2025-08-13 09:29:26.183 PDT
return self.receive_nowait()
2025-08-13 09:29:26.183 PDT
^^^^^^^^^^^^^^^^^^^^^
2025-08-13 09:29:26.183 PDT
File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 93, in receive_nowait
2025-08-13 09:29:26.183 PDT
raise ClosedResourceError
2025-08-13 09:29:26.183 PDT
anyio.ClosedResourceError
2025-08-13 09:29:26.183 PDT
[2025-08-13 16:29:26,184] INFO: [mcp_docs_server:app_lifespan] Context manager exiting normally
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,184] INFO: [mcp_docs_server:app_lifespan] Final memory usage: 109.02 MB
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,184] INFO: [mcp_docs_server:app_lifespan] Final CPU percent: 0.0%
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,184] INFO: [mcp_docs_server:app_lifespan] Final open file descriptors: 11
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,185] INFO: [mcp_docs_server:app_lifespan] shutdown active asyncio tasks: 3
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,185] WARNING: [mcp_docs_server:app_lifespan] Running tasks during shutdown: 3
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,185] WARNING: [mcp_docs_server:app_lifespan] Running task 1: mcp.server.streamable_http_manager.StreamableHTTPSessionManager._handle_stateless_request.<locals>.run_stateless_server
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,185] WARNING: [mcp_docs_server:app_lifespan] Running task 2: Task-2
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,185] WARNING: [mcp_docs_server:app_lifespan] Running task 3: mcp.server.fastmcp.server.FastMCP.run_streamable_http_async
2025-08-13 09:29:26.184 PDT
[2025-08-13 16:29:26,185] INFO: [mcp_docs_server:app_lifespan] shutdown active threads: 1
2025-08-13 09:29:26.185 PDT
[2025-08-13 16:29:26,185] INFO: [mcp_docs_server:app_lifespan] MCP docs server shutting down

My attempts to work around this problem are in this PR. You would need to look at the whole history of what I tried. I included different middleware changes, increasing versions, etc.
https://github.com/deephaven/deephaven-mcp/pull/89

Example Code
My code:
https://github.com/deephaven/deephaven-mcp/tree/main/src/deephaven_mcp/mcp_docs_server
Python & MCP Python SDK
The build can be found at https://github.com/deephaven/deephaven-mcp/blob/main/ops/docker/mcp-docs/Dockerfile.  It is python 3.12 with the latest mcp (version >= 1.12.4).

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

閱讀 mcp/server/streamable_http.py,尤其是 message_router,並追蹤 traceback 中描述的 FastMCP.run_streamable_http_async 進入點。針對 streamable HTTP endpoint 重現一個 HEAD 請求,檢視 deephaven-mcp PR #89 中嘗試過的 middleware 與版本變更,並驗證 server 仍在執行,且不會因意外的 ClosedResourceError shutdown 而停止。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api, backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。