Skip to content

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

Closed as not planned
@MadanMaram

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
----------------------------------------------------
from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo Server")

# Add a simple tool
@mcp.tool()
def greet(name: str) -> str:
    """Return a greeting message."""
    return f"Hello, {name}!"

if __name__ == "__main__":
    # Run the server with streamable HTTP transport
    mcp.run(transport="streamable-http")

--------------------------------------------------
client.py((with a while loop---------------------------------------------------
import asyncio
from mcp.client.streamable_http import streamablehttp_client
from mcp.client.session import ClientSession

async def call_tool(name: str):
    # Connect to the MCP server using streamable HTTP
    async with streamablehttp_client("http://127.0.0.1:8000/mcp") as (read, write, _):
        async with ClientSession(read, write) as session:
            # Initialize the connection
            await session.initialize()
            # Call the 'greet' tool
            result = await session.call_tool("greet", {"name": name})
            print(result)

async def main():
    while True:
        await call_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