|
16 | 16 |
|
17 | 17 | ## Features
|
18 | 18 |
|
19 |
| -- **Direct integration** - Mount an MCP server directly to your FastAPI app |
20 |
| -- **Zero configuration** required - just point it at your FastAPI app and it works |
21 |
| -- **Automatic discovery** of all FastAPI endpoints and conversion to MCP tools |
22 |
| -- **Preserving schemas** of your request models and response models |
23 |
| -- **Preserve documentation** of all your endpoints, just as it is in Swagger |
24 |
| -- **Flexible deployment** - Mount your MCP server to the same app, or deploy separately |
| 19 | +- **Zero configuration** - Just point it at your FastAPI app and it works, with automatic discovery of endpoints and conversion to MCP tools |
| 20 | +- **Schema & docs preservation** - Keep the same request/response models and preserve documentation of all your endpoints |
| 21 | +- **Flexible deployment** - Mount your MCP server to the same FastAPI application, or deploy separately |
| 22 | +- **Custom endpoint exposure** - Control which endpoints become MCP tools using operation IDs and tags |
25 | 23 | - **ASGI transport** - Uses FastAPI's ASGI interface directly by default for efficient communication
|
26 | 24 |
|
27 | 25 | ## Installation
|
@@ -54,236 +52,24 @@ mcp = FastApiMCP(app)
|
54 | 52 | mcp.mount()
|
55 | 53 | ```
|
56 | 54 |
|
57 |
| -That's it! Your auto-generated MCP server is now available at `https://app.base.url/mcp`. |
| 55 | +That's it! Your auto-generated MCP server is now available at `https://app.base.url/mcp`. |
58 | 56 |
|
59 |
| -## Tool Naming |
| 57 | +> **Note on `base_url`**: While `base_url` is optional, it is highly recommended to provide it explicitly. The `base_url` tells the MCP server where to send API requests when tools are called. Without it, the library will attempt to determine the URL automatically, which may not work correctly in deployed environments where the internal and external URLs differ. |
60 | 58 |
|
61 |
| -FastAPI-MCP uses the `operation_id` from your FastAPI routes as the MCP tool names. When you don't specify an `operation_id`, FastAPI auto-generates one, but these can be cryptic. |
| 59 | +## Documentation, Examples and Advanced Usage |
62 | 60 |
|
63 |
| -Compare these two endpoint definitions: |
| 61 | +FastAPI-MCP provides comprehensive documentation in the `docs` folder: |
| 62 | +- [Best Practices](docs/00_BEST_PRACTICES.md) - Essential guidelines for converting APIs to MCP tools safely and effectively |
| 63 | +- [FAQ](docs/00_FAQ.md) - Frequently asked questions about usage, development and support |
| 64 | +- [Tool Naming](docs/01_tool_naming.md) - Best practices for naming your MCP tools using operation IDs |
| 65 | +- [Connecting to MCP Server](docs/02_connecting_to_the_mcp_server.md) - How to connect various MCP clients like Cursor and Claude Desktop |
| 66 | +- [Advanced Usage](docs/03_advanced_usage.md) - Advanced features like custom schemas, endpoint filtering, and separate deployment |
64 | 67 |
|
65 |
| -```python |
66 |
| -# Auto-generated operation_id (something like "read_user_users__user_id__get") |
67 |
| -@app.get("/users/{user_id}") |
68 |
| -async def read_user(user_id: int): |
69 |
| - return {"user_id": user_id} |
70 |
| - |
71 |
| -# Explicit operation_id (tool will be named "get_user_info") |
72 |
| -@app.get("/users/{user_id}", operation_id="get_user_info") |
73 |
| -async def read_user(user_id: int): |
74 |
| - return {"user_id": user_id} |
75 |
| -``` |
76 |
| - |
77 |
| -For clearer, more intuitive tool names, we recommend adding explicit `operation_id` parameters to your FastAPI route definitions. |
78 |
| - |
79 |
| -To find out more, read FastAPI's official docs about [advanced config of path operations.](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/) |
80 |
| - |
81 |
| -## Advanced Usage |
82 |
| - |
83 |
| -FastAPI-MCP provides several ways to customize and control how your MCP server is created and configured. Here are some advanced usage patterns: |
84 |
| - |
85 |
| -### Customizing Schema Description |
86 |
| - |
87 |
| -```python |
88 |
| -from fastapi import FastAPI |
89 |
| -from fastapi_mcp import FastApiMCP |
90 |
| - |
91 |
| -app = FastAPI() |
92 |
| - |
93 |
| -mcp = FastApiMCP( |
94 |
| - app, |
95 |
| - name="My API MCP", |
96 |
| - describe_all_responses=True, # Include all possible response schemas in tool descriptions |
97 |
| - describe_full_response_schema=True # Include full JSON schema in tool descriptions |
98 |
| -) |
99 |
| - |
100 |
| -mcp.mount() |
101 |
| -``` |
102 |
| - |
103 |
| -### Customizing Exposed Endpoints |
104 |
| - |
105 |
| -You can control which FastAPI endpoints are exposed as MCP tools using Open API operation IDs or tags: |
106 |
| - |
107 |
| -```python |
108 |
| -from fastapi import FastAPI |
109 |
| -from fastapi_mcp import FastApiMCP |
110 |
| - |
111 |
| -app = FastAPI() |
112 |
| - |
113 |
| -# Only include specific operations |
114 |
| -mcp = FastApiMCP( |
115 |
| - app, |
116 |
| - include_operations=["get_user", "create_user"] |
117 |
| -) |
118 |
| - |
119 |
| -# Exclude specific operations |
120 |
| -mcp = FastApiMCP( |
121 |
| - app, |
122 |
| - exclude_operations=["delete_user"] |
123 |
| -) |
124 |
| - |
125 |
| -# Only include operations with specific tags |
126 |
| -mcp = FastApiMCP( |
127 |
| - app, |
128 |
| - include_tags=["users", "public"] |
129 |
| -) |
130 |
| - |
131 |
| -# Exclude operations with specific tags |
132 |
| -mcp = FastApiMCP( |
133 |
| - app, |
134 |
| - exclude_tags=["admin", "internal"] |
135 |
| -) |
136 |
| - |
137 |
| -# Combine operation IDs and tags (include mode) |
138 |
| -mcp = FastApiMCP( |
139 |
| - app, |
140 |
| - include_operations=["user_login"], |
141 |
| - include_tags=["public"] |
142 |
| -) |
143 |
| - |
144 |
| -mcp.mount() |
145 |
| -``` |
146 |
| - |
147 |
| -Notes on filtering: |
148 |
| -- You cannot use both `include_operations` and `exclude_operations` at the same time |
149 |
| -- You cannot use both `include_tags` and `exclude_tags` at the same time |
150 |
| -- You can combine operation filtering with tag filtering (e.g., use `include_operations` with `include_tags`) |
151 |
| -- When combining filters, a greedy approach will be taken. Endpoints matching either criteria will be included |
152 |
| - |
153 |
| -### Deploying Separately from Original FastAPI App |
154 |
| - |
155 |
| -You are not limited to serving the MCP on the same FastAPI app from which it was created. |
156 |
| - |
157 |
| -You can create an MCP server from one FastAPI app, and mount it to a different app: |
158 |
| - |
159 |
| -```python |
160 |
| -from fastapi import FastAPI |
161 |
| -from fastapi_mcp import FastApiMCP |
162 |
| - |
163 |
| -# Your API app |
164 |
| -api_app = FastAPI() |
165 |
| -# ... define your API endpoints on api_app ... |
166 |
| - |
167 |
| -# A separate app for the MCP server |
168 |
| -mcp_app = FastAPI() |
169 |
| - |
170 |
| -# Create MCP server from the API app |
171 |
| -mcp = FastApiMCP(api_app) |
172 |
| - |
173 |
| -# Mount the MCP server to the separate app |
174 |
| -mcp.mount(mcp_app) |
175 |
| - |
176 |
| -# Now you can run both apps separately: |
177 |
| -# uvicorn main:api_app --host api-host --port 8001 |
178 |
| -# uvicorn main:mcp_app --host mcp-host --port 8000 |
179 |
| -``` |
180 |
| - |
181 |
| -### Adding Endpoints After MCP Server Creation |
182 |
| - |
183 |
| -If you add endpoints to your FastAPI app after creating the MCP server, you'll need to refresh the server to include them: |
184 |
| - |
185 |
| -```python |
186 |
| -from fastapi import FastAPI |
187 |
| -from fastapi_mcp import FastApiMCP |
188 |
| - |
189 |
| -app = FastAPI() |
190 |
| -# ... define initial endpoints ... |
191 |
| - |
192 |
| -# Create MCP server |
193 |
| -mcp = FastApiMCP(app) |
194 |
| -mcp.mount() |
195 |
| - |
196 |
| -# Add new endpoints after MCP server creation |
197 |
| -@app.get("/new/endpoint/", operation_id="new_endpoint") |
198 |
| -async def new_endpoint(): |
199 |
| - return {"message": "Hello, world!"} |
200 |
| - |
201 |
| -# Refresh the MCP server to include the new endpoint |
202 |
| -mcp.setup_server() |
203 |
| -``` |
204 |
| - |
205 |
| -### Communication with the FastAPI App |
206 |
| - |
207 |
| -FastAPI-MCP uses ASGI transport by default, which means it communicates directly with your FastAPI app without making HTTP requests. This is more efficient and doesn't require a base URL. |
208 |
| - |
209 |
| -It's not even necessary that the FastAPI server will run. See the examples folder for more. |
210 |
| - |
211 |
| -If you need to specify a custom base URL or use a different transport method, you can provide your own `httpx.AsyncClient`: |
212 |
| - |
213 |
| -```python |
214 |
| -import httpx |
215 |
| -from fastapi import FastAPI |
216 |
| -from fastapi_mcp import FastApiMCP |
217 |
| - |
218 |
| -app = FastAPI() |
219 |
| - |
220 |
| -# Use a custom HTTP client with a specific base URL |
221 |
| -custom_client = httpx.AsyncClient( |
222 |
| - base_url="https://api.example.com", |
223 |
| - timeout=30.0 |
224 |
| -) |
225 |
| - |
226 |
| -mcp = FastApiMCP( |
227 |
| - app, |
228 |
| - http_client=custom_client |
229 |
| -) |
230 |
| - |
231 |
| -mcp.mount() |
232 |
| -``` |
233 |
| - |
234 |
| -## Examples |
235 |
| - |
236 |
| -See the [examples](examples) directory for complete examples. |
237 |
| - |
238 |
| -## Connecting to the MCP Server using SSE |
239 |
| - |
240 |
| -Once your FastAPI app with MCP integration is running, you can connect to it with any MCP client supporting SSE, such as Cursor: |
241 |
| - |
242 |
| -1. Run your application. |
243 |
| - |
244 |
| -2. In Cursor -> Settings -> MCP, use the URL of your MCP server endpoint (e.g., `http://localhost:8000/mcp`) as sse. |
245 |
| - |
246 |
| -3. Cursor will discover all available tools and resources automatically. |
247 |
| - |
248 |
| -## Connecting to the MCP Server using [mcp-proxy stdio](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#1-stdio-to-sse) |
249 |
| - |
250 |
| -If your MCP client does not support SSE, for example Claude Desktop: |
251 |
| - |
252 |
| -1. Run your application. |
253 |
| - |
254 |
| -2. Install [mcp-proxy](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#installing-via-pypi), for example: `uv tool install mcp-proxy`. |
255 |
| - |
256 |
| -3. Add in Claude Desktop MCP config file (`claude_desktop_config.json`): |
257 |
| - |
258 |
| -On Windows: |
259 |
| -```json |
260 |
| -{ |
261 |
| - "mcpServers": { |
262 |
| - "my-api-mcp-proxy": { |
263 |
| - "command": "mcp-proxy", |
264 |
| - "args": ["http://127.0.0.1:8000/mcp"] |
265 |
| - } |
266 |
| - } |
267 |
| -} |
268 |
| -``` |
269 |
| -On MacOS: |
270 |
| -```json |
271 |
| -{ |
272 |
| - "mcpServers": { |
273 |
| - "my-api-mcp-proxy": { |
274 |
| - "command": "/Full/Path/To/Your/Executable/mcp-proxy", |
275 |
| - "args": ["http://127.0.0.1:8000/mcp"] |
276 |
| - } |
277 |
| - } |
278 |
| -} |
279 |
| -``` |
280 |
| -Find the path to mcp-proxy by running in Terminal: `which mcp-proxy`. |
281 |
| - |
282 |
| -4. Claude Desktop will discover all available tools and resources automatically |
| 68 | +Check out the [examples directory](examples) for code samples demonstrating these features in action. |
283 | 69 |
|
284 | 70 | ## Development and Contributing
|
285 | 71 |
|
286 |
| -Thank you for considering contributing to FastAPI-MCP! We encourage the community to post Issues and Pull Requests. |
| 72 | +Thank you for considering contributing to FastAPI-MCP! We encourage the community to post Issues and create Pull Requests. |
287 | 73 |
|
288 | 74 | Before you get started, please see our [Contribution Guide](CONTRIBUTING.md).
|
289 | 75 |
|
|
0 commit comments