Expose an existing FastAPI app as MCP without refactoring
When to use: You already ship a FastAPI API. You want agents to use it. You don't want a parallel codebase.
Prerequisites
- Working FastAPI app with Pydantic models on requests/responses — If your routes return
dict, refactor to Pydantic BaseModel first — otherwise tool schemas will beobject
Flow
-
Install and mountAdd
fastapi-mcpto my project. In my main.py, afterapp = FastAPI(), addFastApiMCP(app).mount(). Show the diff.✓ Copied→ Minimal diff; server still runs -
Verify tools appearRun the server. Connect with
npx -y mcp-remote http://localhost:8000/mcp. List tools — do all my GET/POST routes show up?✓ Copied→ Tools listed, names match route operation_ids -
Filter what's exposedI don't want /admin/* routes exposed. Reconfigure with
include_operationsor exclude by tag.✓ Copied→ Admin routes no longer in tool list
Outcome: Your existing API is now MCP-addressable with zero duplication of schemas or logic.
Pitfalls
- Every route becomes a tool — including internal ones you didn't mean to expose — Explicitly set
include_tags=['public']orexclude_operations=[...]before going live - Routes without operation_id get generated names like
read_item_items__item_id__get— ugly — Always set explicitoperation_idon routes:@app.get('/items/{id}', operation_id='get_item')