Open
Description
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:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("StatelessServer", stateless_http=True)
@mcp.tool()
def task_size() -> int:
return len(mcp.session_manager._task_group._tasks)
if __name__ == "__main__":
mcp.run(transport="streamable-http")
- Create minimal MCP client:
import asyncio
from fastmcp import Client
from fastmcp.client.transports import StreamableHttpTransport
from mcp.types import CallToolResult
async def execute_mcp_tool_call():
transport = StreamableHttpTransport("http://localhost:8000/mcp/")
async with Client(transport) as client:
tools = await client.list_tools()
print(tools)
result = await client.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)]
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
To triage