Closed as not planned
Closed as not planned
Description
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
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
Labels
No labels