Patterns
FastAPI Integration
Generate MCP servers from FastAPI apps
New in version 2.0.0
FastMCP can automatically convert FastAPI applications into MCP servers.
FastMCP does not include FastAPI as a dependency; you must install it separately to run these examples.
Route Mapping
By default, FastMCP will map FastAPI routes to MCP components according to the following rules:
FastAPI Route Type | FastAPI Example | MCP Component | Notes |
---|---|---|---|
GET without path params | @app.get("/stats") | Resource | Simple resources for fetching data |
GET with path params | @app.get("/users/{id}") | Resource Template | Path parameters become template parameters |
POST, PUT, DELETE, etc. | @app.post("/users") | Tool | Operations that modify data |
For more details on route mapping or custom mapping rules, see the OpenAPI integration documentation; FastMCP uses the same mapping rules for both FastAPI and OpenAPI integrations.
Complete Example
Here’s a more detailed example with a data model:
Benefits
- Leverage existing FastAPI apps - No need to rewrite your API logic
- Schema reuse - FastAPI’s Pydantic models and validation are inherited
- Full feature support - Works with FastAPI’s authentication, dependencies, etc.
- ASGI transport - Direct communication without additional HTTP overhead