Skip to content

**Memory Leak** in MCP Server – will be a memory leak until OOM (Out of Memory) occurs #1076

Description

@MadanMaram

Initial Checks

Description

As part of our usage of FastMCP and Python MCP SDK, we observed a memory leak in the MCP server. When the client continuously calls a simple tool (e.g., a greet function), the server's memory usage steadily increases over time and does not get released, eventually leading to an Out of Memory (OOM) condition.

This issue is reproducible both in the fastmcp library and the MCP SDK.

Issue

Here is the Logs:Memory will keep increasing
memory_log_pythonmcp.txt

Example Code

Server.py----------------------------------------------------frommcp.server.fastmcpimportFastMCP# Create an MCP servermcp=FastMCP("Demo Server")
# Add a simple tool@mcp.tool()defgreet(name: str) ->str:
"""Return a greeting message."""returnf"Hello, {name}!"if__name__=="__main__":
# Run the server with streamable HTTP transportmcp.run(transport="streamable-http")
--------------------------------------------------client.py((withawhileloop---------------------------------------------------importasynciofrommcp.client.streamable_httpimportstreamablehttp_clientfrommcp.client.sessionimportClientSessionasyncdefcall_tool(name: str):
# Connect to the MCP server using streamable HTTPasyncwithstreamablehttp_client("http://127.0.0.1:8000/mcp") as (read, write, _):
asyncwithClientSession(read, write) assession:
# Initialize the connectionawaitsession.initialize()
# Call the 'greet' toolresult=awaitsession.call_tool("greet", {"name": name})
print(result)
asyncdefmain():
whileTrue:
awaitcall_tool("Bob")
if__name__=="__main__":
asyncio.run(main())
--------------------------------------------

Python & MCP Python SDK

Environment:
Python: 3.12.1
MCP Python SDK: 1.10.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions