Spaces:
Running
Running
| --- | |
| title: FastAPI Integration | |
| sidebarTitle: FastAPI | |
| description: Generate MCP servers from FastAPI apps | |
| icon: square-bolt | |
| --- | |
| import { VersionBadge } from '/snippets/version-badge.mdx' | |
| <VersionBadge version="2.0.0" /> | |
| <Note> | |
| **Documentation Moved**: The comprehensive FastAPI integration documentation has been moved to the [OpenAPI Integration](/patterns/openapi#fastapi-integration) page, where it's covered alongside all other OpenAPI features including route mapping and tags support. | |
| </Note> | |
| ## Quick Start | |
| FastMCP can automatically convert FastAPI applications into MCP servers: | |
| ```python | |
| from fastapi import FastAPI | |
| from fastmcp import FastMCP | |
| # A FastAPI app | |
| app = FastAPI() | |
| @app.get("/items") | |
| def list_items(): | |
| return [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}] | |
| @app.get("/items/{item_id}") | |
| def get_item(item_id: int): | |
| return {"id": item_id, "name": f"Item {item_id}"} | |
| @app.post("/items") | |
| def create_item(name: str): | |
| return {"id": 3, "name": name} | |
| # Create an MCP server from your FastAPI app | |
| mcp = FastMCP.from_fastapi(app=app) | |
| if __name__ == "__main__": | |
| mcp.run() # Start the MCP server | |
| ``` | |
| <Tip> | |
| For complete documentation including tag-based routing, route mapping configuration, timeout settings, authentication examples, and advanced configuration options, see the comprehensive [OpenAPI Integration documentation](/patterns/openapi#fastapi-integration). | |
| </Tip> |