Repository navigation
Replies: 1 comment
|
You can keep the existing FastAPI app and compose its lifespan before the ASGI server starts: from contextlib import asynccontextmanager
mcp_app = proxy.http_app(path="/")
previous_lifespan = app.router.lifespan_context
@asynccontextmanager
async def combined_lifespan(app):
async with previous_lifespan(app) as state:
async with mcp_app.lifespan(app):
yield state
app.router.lifespan_context = combined_lifespan
app.mount("/mcp", mcp_app)Here If “already running” means the process has completed startup, apply this during app setup and restart/redeploy; changing the lifespan function afterward does not run it retroactively. I verified this with FastMCP 2.13.1 and FastAPI 0.141.1: a stateful MCP initialization and proxied tool call succeeded, and the original startup, shutdown, and lifespan state were preserved. I have not tested your remote backend. AI-assisted answer; the ASGI test was executed. |
Uh oh!
There was an error while loading. Please reload this page.
I created a fastmcp proxy and want to mount it with an already running fastapi service. How can I do this? I found that in the case of the document, the proxy is created first, and then the fastapi service is created with the proxy context as a parameter. If my fastapi service already exists, how can I do this?
All reactions