Describe the bug
Stateless mode with Streamable-HTTP leaks memory. The StreamableHTTPSessionManager stateless server task group never exits and the _task list grows with each request leading to always increasing memory usage.
Expected behavior
Stateless server exits after handling the request freeing up the resources
Steps to reproduce
- Create minimal MCP server:
frommcp.server.fastmcpimportFastMCPmcp=FastMCP("StatelessServer", stateless_http=True)
@mcp.tool()deftask_size() ->int:
returnlen(mcp.session_manager._task_group._tasks)
if__name__=="__main__":
mcp.run(transport="streamable-http")- Create minimal MCP client:
importasynciofromfastmcpimportClientfromfastmcp.client.transportsimportStreamableHttpTransportfrommcp.typesimportCallToolResultasyncdefexecute_mcp_tool_call():
transport=StreamableHttpTransport("http://localhost:8000/mcp/")
asyncwithClient(transport) asclient:
tools=awaitclient.list_tools()
print(tools)
result=awaitclient.call_tool("task_size")
print(result)
asyncio.run(execute_mcp_tool_call())- Do multiple requests
- Tool will return the size of pending tasks in session manager task group and it is always increasing:
[TextContent(type='text', text='23', annotations=None)]
[TextContent(type='text', text='27', annotations=None)]
[TextContent(type='text', text='31', annotations=None)]
Describe the bug
Stateless mode with Streamable-HTTP leaks memory. The
StreamableHTTPSessionManagerstateless server task group never exits and the_tasklist grows with each request leading to always increasing memory usage.Expected behavior
Stateless server exits after handling the request freeing up the resources
Steps to reproduce