Question
Hi! I'm deploying a FastMCP server on AWS Lambda using Mangum and wanted to check if I'm following the recommended deployment pattern.
I created the FastMCP instance globally so that tool registration only happens during Lambda cold starts.
# mcp_server.py
mcp = FastMCP(...)
# lambda_handler.py
from mangum import Mangum
from mcp_server import mcp
app = mcp.streamable_http_app()
handler = Mangum(app)
The first invocation succeeds, but subsequent warm invocations fail with:
StreamableHTTPSessionManager.run() can only be called once per instance
Working approach
The only approach I've found that works consistently is recreating the FastMCP instance for every invocation:
def handler(event, context):
mcp = create_mcp()
app = mcp.streamable_http_app()
return Mangum(app)(event, context)
This resolves the issue, but it also means tool registration and server initialization happen on every request instead of only during cold starts.
Is recreating the FastMCP instance per Lambda invocation the recommended deployment pattern?
Or is there a supported way to reuse a global FastMCP instance across warm Lambda invocations while using Mangum?
If this is expected behavior, I'd also appreciate any guidance on the recommended deployment pattern for FastMCP on AWS Lambda.
Thanks!
Additional Context
Environment:-
- MCP Python SDK: 1.28.1
- Python: 3.12
- Mangum: 0.21.0
- AWS Lambda (Function URL)
- Transport: streamable-http
Question
Hi! I'm deploying a FastMCP server on AWS Lambda using Mangum and wanted to check if I'm following the recommended deployment pattern.
I created the FastMCP instance globally so that tool registration only happens during Lambda cold starts.
The first invocation succeeds, but subsequent warm invocations fail with:
StreamableHTTPSessionManager.run() can only be called once per instance
Working approach
The only approach I've found that works consistently is recreating the FastMCP instance for every invocation:
This resolves the issue, but it also means tool registration and server initialization happen on every request instead of only during cold starts.
Is recreating the FastMCP instance per Lambda invocation the recommended deployment pattern?
Or is there a supported way to reuse a global FastMCP instance across warm Lambda invocations while using Mangum?
If this is expected behavior, I'd also appreciate any guidance on the recommended deployment pattern for FastMCP on AWS Lambda.
Thanks!
Additional Context
Environment:-