v1.30.0: normal streamable-HTTP session close logs "Error in standalone SSE writer" (anyio.ClosedResourceError) at ERROR
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 mcp/server/streamable_http.py 开始,重点查看 standalone_sse_writer() 和 StreamableHTTPServerTransport.terminate(),然后追踪 DELETE 清理如何关闭流。使用提供的客户端和服务器示例重现问题,并检查会话关闭时的服务器日志。完成标准是客户端正常终止时不再产生 ERROR traceback,并且有一个涵盖流已关闭情况的回归测试。
由索引模型根据 Issue 内容生成。
描述
Initial Checks
- I confirm that I'm using the newest release of my line (the latest 1.x, 1.30.0)
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Release line
1.x (maintenance line, v1.x branch)
Description
With 1.30.0 on the server side, closing a stateful streamable-HTTP session the normal way logs an ERROR with a full traceback:
ERROR mcp.server.streamable_http: Error in standalone SSE writer
Traceback (most recent call last):
File ".../mcp/server/streamable_http.py", line 723, in standalone_sse_writer
async for event_message in standalone_stream_reader:
File ".../anyio/abc/_streams.py", line 37, in __anext__
return await self.receive()
File ".../anyio/streams/memory.py", line 141, in receive
raise ClosedResourceError from None
anyio.ClosedResourceError
Here, closing the normal way means the client's streamablehttp_client context exits and sends DELETE. Nothing actually fails: the calls succeed and the server keeps serving. But every session that ends puts an ERROR traceback in the server log, and that buries real errors.
Which side and which version: it depends only on the server's SDK version. I crossed the two versions:
| server SDK | client SDK | ERROR records on close |
|---|---|---|
| 1.29.1 | 1.29.1 | 0 |
| 1.30.0 | 1.30.0 | 1 |
| 1.30.0 | 1.29.1 | 1 |
| 1.29.1 | 1.30.0 | 0 |
What seems to happen (from reading the 1.30.0 source):
- The
DELETErunsStreamableHTTPServerTransport.terminate(). terminate()calls_clean_up_memory_streams(GET_STREAM_KEY).- That closes the receive stream
standalone_sse_writeris still iterating, so the iteration raisesClosedResourceErrorinstead of ending. - The writer's
except Exception: logger.exception("Error in standalone SSE writer")reports it.
1.30.0 moved the GET handler's stream cleanup into a finally, which looks like what changed the shutdown order.
Expected: a client-initiated session termination is not logged at ERROR. Either the writer stops cleanly when its stream is closed by terminate(), or ClosedResourceError during termination is treated as a normal end, logged at DEBUG at most.
We work around it in our server with a logging.Filter on mcp.server.streamable_http that demotes exactly this record (that message plus ClosedResourceError) to DEBUG.
Example Code
"""Prints 1 ERROR record on mcp 1.30.0, 0 on 1.29.1."""
import asyncio, logging, socket, threading, time
import uvicorn
from mcp.client.session import ClientSession
from mcp.client.streamable_http import streamablehttp_client
from mcp.server.fastmcp import FastMCP
errors = []
class Collect(logging.Handler):
def emit(self, record):
if record.levelno >= logging.ERROR:
errors.append(record)
logging.getLogger("mcp.server.streamable_http").addHandler(Collect())
server = FastMCP("repro")
@server.tool()
def echo(text: str) -> str:
return text
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
port = s.getsockname()[1]
uv = uvicorn.Server(uvicorn.Config(server.streamable_http_app(),
host="127.0.0.1", port=port, log_level="warning"))
threading.Thread(target=uv.run, daemon=True).start()
while not uv.started:
time.sleep(0.05)
async def main():
async with streamablehttp_client(f"http://127.0.0.1:{port}/mcp") as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
await session.list_tools()
await asyncio.sleep(1) # let the server process the DELETE
asyncio.run(main())
uv.should_exit = True
print(len(errors), "ERROR record(s):",
[(r.getMessage(), type(r.exc_info[1]).__name__) for r in errors])
# 1.30.0: 1 ERROR record(s): [('Error in standalone SSE writer', 'ClosedResourceError')]
# 1.29.1: 0 ERROR record(s): []
Python & MCP Python SDK
Python 3.12.8, Linux
mcp 1.30.0 (server side; 1.29.1 does not show it)
anyio 4.16.0, starlette 1.10.0, sse-starlette 3.7.0, uvicorn 0.54.0
- 主要语言
- Python
- 星标
- 25k
- 派生
- 4.2k
- 平均合并
- 10 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/python-sdk 的其他 Issue
-
v2
难度 1/5 1 小时以内 新手友好度 86/100
modelcontextprotocol/python-sdk#3978 · 2 条评论 ·
维护者通常 1 天内回复
-
Client serves cached tools after its context exits with an exception可能已有人在做 @jonxsong 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
modelcontextprotocol/python-sdk#3968 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
modelcontextprotocol/python-sdk#3957 ·
维护者通常 1 天内回复
-
v2
难度 1/5 1 小时以内 新手友好度 91/100
modelcontextprotocol/python-sdk#3934 · 2 条评论 ·
维护者通常 1 天内回复
-
bug v1
难度 2/5 1-3 小时 新手友好度 79/100
modelcontextprotocol/python-sdk#3929 · 2 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/python-sdk 的全部 Issue
相似的 Issue
-
auto-reported bug
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
-
backend bug
难度 2/5 1-3 小时 新手友好度 86/100
nolte/kamerplanter#3193 ·
维护者通常 1 天内回复
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwise可能已有人在做 @abdulwaarith0 今天认领。 未关闭needs-triage
难度 2/5 1-3 小时 新手友好度 84/100
MakazhanAlpamys/Soup#3690 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复