MCP · Lesson 69 of 120
Mounting in FastAPI
One app.
Many teams already run a FastAPI app. Instead of a separate server, we can mount the MCP server inside it. One app then serves both normal web routes and MCP.

- Existing FastAPI app
- Mount MCP inside
- One app
MCP inside FastAPI
We build the MCP app, at the root of its own path. FastAPI needs to run the MCP app's lifespan, so we pass it in. A normal health route sits next to it, and the MCP app is mounted at slash MCP. Run it. The health route and the MCP tool both answer.
fastapi_app.pypython
import asyncio, httpx
from fastapi import FastAPI
from mcp import Client
from northwind import mcp
from serve import start
mcp_app = mcp.streamable_http_app(streamable_http_path="/")
app = FastAPI(lifespan=lambda a: mcp_app.router.lifespan_context(a))
app.get("/health")(lambda: {"ok": True})
app.mount("/mcp", mcp_app)
start(app)
async def main():
print("health:", httpx.get("http://127.0.0.1:8765/health").json())
async with Client("http://127.0.0.1:8765/mcp/") as client:
print("1044:", (await client.call_tool("order_status", {"order_id": 1044})).content[0].text)
asyncio.run(main())Output
health: {'ok': True}
1044: deliveredAlso uses northwind.py
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.exceptions import ToolError
mcp = MCPServer("northwind")
ORDERS = {1042: "shipped", 1043: "packing", 1044: "delivered"}
@mcp.tool()
def order_status(order_id: int) -> str:
"""Look up the status of a Northwind order by its number."""
if order_id not in ORDERS:
raise ToolError(f"Order {order_id} not found")
return ORDERS[order_id]
@mcp.tool()
def delivery_cost(order_total: float) -> str:
"""UK delivery cost for an order total in pounds."""
return "free" if order_total >= 50 else "£4.99"
@mcp.resource("policy://returns")
def returns_policy() -> str:
"""Northwind's returns policy."""
return "You can return any unused item within 30 days of delivery."
@mcp.prompt()
def support_reply(question: str) -> str:
"""Draft a polite reply to a customer."""
return f"Write a short, polite reply to this Northwind customer: {question}"
if __name__ == "__main__":
mcp.run()Also uses serve.py
import threading, time, uvicorn
def start(app, port=8765):
"""Run a web app in the background, just for demos."""
threading.Thread(target=uvicorn.run, args=(app,), daemon=True,
kwargs={"port": port, "log_level": "critical"}).start()
time.sleep(1)Why mount
One deployment, one domain, and one set of logins. Your existing routes keep working. And MCP becomes just another part of your web app.
| key | value |
|---|---|
| Deploy | once |
| Domain | shared |
| Auth | shared |
| Routes | unchanged |
Summary
Mount the MCP app inside FastAPI, and pass its lifespan along. Next, stateless servers.
Key takeaways- mount('/mcp')
- lifespan
- One app
Next up: Stateless Servers — scaling out