Skip to content

Stateless Mode memory leak #756

Description

@algirdasci

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

  1. 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")
  1. 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())
  1. Do multiple requests
  2. 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

Labels

No labels
No labels

Type

No type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions