mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-24 06:24:18 +02:00
Fix FastAPI mounting examples in docs (#2962)
This commit is contained in:
parent
287ae44ff3
commit
27d318810f
2 changed files with 20 additions and 13 deletions
|
|
@ -344,13 +344,6 @@ Here's a quick example showing how to add MCP to an existing FastAPI application
|
|||
from fastapi import FastAPI
|
||||
from fastmcp import FastMCP
|
||||
|
||||
# Your existing API
|
||||
api = FastAPI()
|
||||
|
||||
@api.get("/api/status")
|
||||
def status():
|
||||
return {"status": "ok"}
|
||||
|
||||
# Create your MCP server
|
||||
mcp = FastMCP("API Tools")
|
||||
|
||||
|
|
@ -359,14 +352,28 @@ def query_database(query: str) -> dict:
|
|||
"""Run a database query"""
|
||||
return {"result": "data"}
|
||||
|
||||
# Create the MCP ASGI app with path="/" since we'll mount at /mcp
|
||||
mcp_app = mcp.http_app(path="/")
|
||||
|
||||
# Create FastAPI app with MCP lifespan (required for session management)
|
||||
api = FastAPI(lifespan=mcp_app.lifespan)
|
||||
|
||||
@api.get("/api/status")
|
||||
def status():
|
||||
return {"status": "ok"}
|
||||
|
||||
# Mount MCP at /mcp
|
||||
api.mount("/mcp", mcp.http_app())
|
||||
api.mount("/mcp", mcp_app)
|
||||
|
||||
# Run with: uvicorn app:api --host 0.0.0.0 --port 8000
|
||||
```
|
||||
|
||||
Your existing API remains at `http://localhost:8000/api` while MCP is available at `http://localhost:8000/mcp`.
|
||||
|
||||
<Warning>
|
||||
Just like with Starlette, you **must** pass the lifespan from the MCP app to FastAPI. Without this, the session manager won't initialize properly and requests will fail.
|
||||
</Warning>
|
||||
|
||||
## Mounting Authenticated Servers
|
||||
|
||||
<VersionBadge version="2.13.0" />
|
||||
|
|
|
|||
|
|
@ -390,14 +390,14 @@ def get_user(user_id: int):
|
|||
When mounting MCP servers, always pass the lifespan context:
|
||||
|
||||
```python
|
||||
# Correct - lifespan passed
|
||||
mcp_app = mcp.http_app(path='/mcp')
|
||||
# Correct - lifespan passed, path="/" since we mount at /mcp
|
||||
mcp_app = mcp.http_app(path="/")
|
||||
app = FastAPI(lifespan=mcp_app.lifespan)
|
||||
app.mount("/mcp", mcp_app)
|
||||
app.mount("/mcp", mcp_app) # MCP endpoint at /mcp
|
||||
|
||||
# Incorrect - missing lifespan
|
||||
app = FastAPI()
|
||||
app.mount("/mcp", mcp.http_app()) # Session manager won't initialize
|
||||
app.mount("/mcp", mcp.http_app(path="/")) # Session manager won't initialize
|
||||
```
|
||||
|
||||
If you're mounting an authenticated MCP server under a path prefix, see [Mounting Authenticated Servers](/deployment/http#mounting-authenticated-servers) for important OAuth routing considerations.
|
||||
|
|
@ -427,7 +427,7 @@ async def app_lifespan(app: FastAPI):
|
|||
|
||||
# Create MCP server
|
||||
mcp = FastMCP("Tools")
|
||||
mcp_app = mcp.http_app()
|
||||
mcp_app = mcp.http_app(path="/")
|
||||
|
||||
# Combine both lifespans
|
||||
app = FastAPI(lifespan=combine_lifespans(app_lifespan, mcp_app.lifespan))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue