|
| 1 | +# FastAPI-MCP |
| 2 | + |
| 3 | +A magical, zero-configuration tool for generating Model Context Protocol (MCP) servers from FastAPI applications. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Automatic discovery** of all FastAPI endpoints in your application |
| 8 | +- **Zero configuration** required - just point it at your FastAPI app and it works |
| 9 | +- **CLI tool** for easy generation and execution of MCP servers |
| 10 | +- **Stdio support** for MCP protocol communication |
| 11 | +- **Type-safe conversion** from FastAPI endpoints to MCP tools |
| 12 | +- **Documentation preservation** from FastAPI to MCP |
| 13 | +- **Claude integration** for easy installation and use with Claude desktop application |
| 14 | +- **API Integration** - automatically makes HTTP requests to your FastAPI endpoints |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install fastapi-mcp |
| 20 | +``` |
| 21 | + |
| 22 | +For detailed installation instructions, see [INSTALL.md](INSTALL.md). |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Generate an MCP server from a FastAPI app |
| 27 | + |
| 28 | +```bash |
| 29 | +# Point to a FastAPI application file or module |
| 30 | +fastapi-mcp generate app.py |
| 31 | + |
| 32 | +# Or specify the app variable if it's not named 'app' |
| 33 | +fastapi-mcp generate module.py:my_app |
| 34 | + |
| 35 | +# Use a custom base URL for the API endpoints (default: http://localhost:8000) |
| 36 | +fastapi-mcp generate app.py --base-url https://api.example.com |
| 37 | +``` |
| 38 | + |
| 39 | +### Preview the generated MCP server |
| 40 | + |
| 41 | +```bash |
| 42 | +fastapi-mcp preview |
| 43 | +``` |
| 44 | + |
| 45 | +### Run the generated MCP server |
| 46 | + |
| 47 | +```bash |
| 48 | +fastapi-mcp run |
| 49 | +``` |
| 50 | + |
| 51 | +### Install the server for Claude |
| 52 | + |
| 53 | +```bash |
| 54 | +fastapi-mcp install |
| 55 | +``` |
| 56 | + |
| 57 | +## How It Works |
| 58 | + |
| 59 | +FastAPI-MCP automatically: |
| 60 | + |
| 61 | +1. Discovers all FastAPI endpoints in your application |
| 62 | +2. Converts route handlers to MCP tools |
| 63 | +3. Maps request/response models to MCP schemas |
| 64 | +4. Preserves documentation and type information |
| 65 | +5. Generates a standalone MCP server that uses the official MCP Python SDK |
| 66 | +6. **Makes actual HTTP requests** to the underlying FastAPI endpoints |
| 67 | + |
| 68 | +## Example |
| 69 | + |
| 70 | +Original FastAPI code: |
| 71 | + |
| 72 | +```python |
| 73 | +from fastapi import FastAPI |
| 74 | +from pydantic import BaseModel |
| 75 | + |
| 76 | +app = FastAPI() |
| 77 | + |
| 78 | +class Item(BaseModel): |
| 79 | + name: str |
| 80 | + price: float |
| 81 | + is_offer: bool = None |
| 82 | + |
| 83 | +@app.get("/items/{item_id}") |
| 84 | +def read_item(item_id: int, q: str = None): |
| 85 | + """Get details for a specific item""" |
| 86 | + return {"item_id": item_id, "q": q} |
| 87 | + |
| 88 | +@app.put("/items/{item_id}") |
| 89 | +def update_item(item_id: int, item: Item): |
| 90 | + """Update an item with new information""" |
| 91 | + return {"item_id": item_id, "item": item} |
| 92 | +``` |
| 93 | + |
| 94 | +Generated MCP server: |
| 95 | + |
| 96 | +```python |
| 97 | +# Generated MCP server |
| 98 | +# Original FastAPI app: FastAPI |
| 99 | + |
| 100 | +from mcp.server import FastMCP |
| 101 | +import json |
| 102 | +import requests |
| 103 | +from typing import Dict, List, Optional, Union, Any |
| 104 | +try: |
| 105 | + from pydantic import BaseModel |
| 106 | +except ImportError: |
| 107 | + from pydantic.main import BaseModel |
| 108 | + |
| 109 | +class Item(BaseModel): |
| 110 | + name: str |
| 111 | + price: float |
| 112 | + is_offer: Optional[bool] = None |
| 113 | + |
| 114 | +# Original handler: __main__.read_item |
| 115 | +# Original handler: __main__.update_item |
| 116 | + |
| 117 | +mcp = FastMCP("FastAPI") |
| 118 | + |
| 119 | +@mcp.tool() |
| 120 | +async def read_item(item_id: int, q: str = None) -> Any: |
| 121 | + """Get details for a specific item |
| 122 | +
|
| 123 | + Original route: GET /items/{item_id} |
| 124 | + """ |
| 125 | + # Original handler: __main__.read_item |
| 126 | + url = f'http://localhost:8000/items/{item_id}' |
| 127 | + params = {} |
| 128 | + if q is not None: |
| 129 | + params['q'] = q |
| 130 | + response = requests.get(url, params=params) |
| 131 | + response.raise_for_status() |
| 132 | + return response.json() |
| 133 | + |
| 134 | +@mcp.tool() |
| 135 | +async def update_item(item_id: int, name: str, price: float, is_offer: bool = None) -> Any: |
| 136 | + """Update an item with new information |
| 137 | +
|
| 138 | + Original route: PUT /items/{item_id} |
| 139 | + """ |
| 140 | + # Original handler: __main__.update_item |
| 141 | + url = f'http://localhost:8000/items/{item_id}' |
| 142 | + item = Item(**{"name": name, "price": price, "is_offer": is_offer}) |
| 143 | + json_data = item.dict() |
| 144 | + response = requests.put(url, json=json_data) |
| 145 | + response.raise_for_status() |
| 146 | + return response.json() |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + # Run the MCP server |
| 150 | + mcp.run() |
| 151 | +``` |
| 152 | + |
| 153 | +## Integration with FastAPI |
| 154 | + |
| 155 | +The generated MCP server makes HTTP requests to your actual FastAPI server, which must be running separately. You can: |
| 156 | + |
| 157 | +1. Run your FastAPI application normally (e.g., with `uvicorn app:app`) |
| 158 | +2. Generate the MCP server with a base URL pointing to your running FastAPI server |
| 159 | +3. Run the MCP server separately to provide MCP tools for LLMs |
| 160 | + |
| 161 | +If your API requires authentication, you can customize the generated server code to include the necessary headers or tokens. |
| 162 | + |
| 163 | +## Using with Claude |
| 164 | + |
| 165 | +FastAPI-MCP makes it easy to use your API endpoints with Claude: |
| 166 | + |
| 167 | +1. Generate an MCP server from your FastAPI app |
| 168 | +2. Install the server: `fastapi-mcp install` |
| 169 | +3. Open Claude desktop app |
| 170 | +4. Your API endpoints will be available as tools to Claude |
| 171 | + |
| 172 | +## Examples |
| 173 | + |
| 174 | +For more examples, see the [examples](examples) directory. |
| 175 | + |
| 176 | +## Requirements |
| 177 | + |
| 178 | +- Python 3.10+ |
| 179 | +- FastAPI 0.100.0+ |
| 180 | +- Pydantic 2.0.0+ |
| 181 | +- mcp 1.3.0+ |
| 182 | +- requests 2.25.0+ |
| 183 | + |
| 184 | +## Contributing |
| 185 | + |
| 186 | +Contributions are welcome! Please feel free to submit a pull request. |
| 187 | + |
| 188 | +## License |
| 189 | + |
| 190 | +MIT |
0 commit comments