49b9bb6724
Deploy Docs / deploy-docs (push) Failing after 1s
Conformance Tests / client-conformance (push) Failing after 3s
Conformance Tests / server-conformance (push) Failing after 1s
GitHub Actions Security Analysis / zizmor (push) Failing after 1s
CI / checks (push) Failing after 59m20s
CI / all-green (push) Waiting to run
starlette-mount
Embed an MCP server inside an existing Starlette (or FastAPI) app at a
sub-path, next to your own routes. mcp.streamable_http_app() returns a
mountable ASGI app; the two things to get right are the path (the default
streamable_http_path="/mcp" stacks under your mount prefix) and the
lifespan (Starlette does not run a mounted sub-app's lifespan, so the
parent must enter mcp.session_manager.run()).
Run it
# HTTP — the client self-hosts the mounted app on a free port at /api/, runs,
# then tears it down
uv run python -m stories.starlette_mount.client --http
# against a server you run yourself (real uvicorn on :8000)
uv run python -m stories.starlette_mount.server --port 8000 &
SERVER_PID=$!
curl http://127.0.0.1:8000/health # → {"status":"ok"}
uv run python -m stories.starlette_mount.client --http http://127.0.0.1:8000/api/
kill "$SERVER_PID"
What to look at
client.pymain— opens withasync with Client(target, mode=mode) as client:. Nothing on the client side knows about the mount: the/api/URL handed in astargetis just another streamable-HTTP endpoint.server.pystreamable_http_path="/"— without this the endpoint would be/api/mcp; with it,Mount("/api", ...)serves MCP at/api/(trailing slash required — Starlette'sMountforwards/apias an empty path that the inner/route won't match).server.pylifespan—mcp.session_manager.run()must be entered by the parent app. Forget it and every MCP request fails immediately with a 500 (RuntimeError: Task group is not initialized. Make sure to use run().) — the sub-app's own lifespan never fires underMount.server.pyRoute("/health", ...)— non-MCP routes live alongside the mount; FastAPI users do the same withapp.mount("/api", mcp_app).
Caveats
- DNS-rebinding protection is on by default; the example passes
transport_security=NO_DNS_REBINDbecause the in-process test client sends noOriginheader. Remove it (or configure allowed hosts) for a real deployment. - The parent-lifespan dance is a known SDK ergonomics gap (other SDKs mount with no extra ceremony); tracked for the beta reshape. The recipe shown here is what works today.
Spec
See also
stateless_legacy/ (the one-liner mcp.streamable_http_app() without a parent
app), json_response/, legacy_routing/. TS-SDK equivalent: examples/hono/.