You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**Automatic discovery** of all FastAPI endpoints and conversion to MCP tools
23
23
-**Preserving schemas** of your request models and response models
24
24
-**Preserve documentation** of all your endpoints, just as it is in Swagger
25
-
-**Extend** - Add custom MCP tools alongside the auto-generated ones
25
+
-**Flexible deployment** - Mount your MCP server to the same app, or deploy separately
26
26
27
27
## Installation
28
28
@@ -44,45 +44,125 @@ The simplest way to use FastAPI-MCP is to add an MCP server directly to your Fas
44
44
45
45
```python
46
46
from fastapi import FastAPI
47
-
from fastapi_mcp importadd_mcp_server
47
+
from fastapi_mcp importFastApiMCP
48
48
49
-
# Your FastAPI app
50
49
app = FastAPI()
51
50
52
-
# Mount the MCP server to your app
53
-
add_mcp_server(
54
-
app, # Your FastAPI app
55
-
mount_path="/mcp", # Where to mount the MCP server
56
-
name="My API MCP", # Name for the MCP server
51
+
mcp = FastApiMCP(
52
+
app,
53
+
54
+
# Optional parameters
55
+
name="My API MCP",
56
+
description="My API description",
57
+
base_url="http://localhost:8000",
57
58
)
59
+
60
+
# Mount the MCP server directly to your FastAPI app
61
+
mcp.mount()
58
62
```
59
63
60
64
That's it! Your auto-generated MCP server is now available at `https://app.base.url/mcp`.
61
65
66
+
> **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.
67
+
68
+
## Tool Naming
69
+
70
+
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.
71
+
72
+
Compare these two endpoint definitions:
73
+
74
+
```python
75
+
# Auto-generated operation_id (something like "read_user_users__user_id__get")
76
+
@app.get("/users/{user_id}")
77
+
asyncdefread_user(user_id: int):
78
+
return {"user_id": user_id}
79
+
80
+
# Explicit operation_id (tool will be named "get_user_info")
For clearer, more intuitive tool names, we recommend adding explicit `operation_id` parameters to your FastAPI route definitions.
87
+
88
+
To find out more, read FastAPI's official docs about [advanced config of path operations.](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/)
89
+
62
90
## Advanced Usage
63
91
64
92
FastAPI-MCP provides several ways to customize and control how your MCP server is created and configured. Here are some advanced usage patterns:
65
93
94
+
### Customizing Schema Description
95
+
66
96
```python
67
97
from fastapi import FastAPI
68
-
from fastapi_mcp importadd_mcp_server
98
+
from fastapi_mcp importFastApiMCP
69
99
70
100
app = FastAPI()
71
101
72
-
mcp_server=add_mcp_server(
73
-
app,# Your FastAPI app
74
-
mount_path="/mcp", # Where to mount the MCP server
75
-
name="My API MCP", # Name for the MCP server
76
-
describe_all_responses=True, # False by default. Include all possible response schemas in tool descriptions, instead of just the successful response.
77
-
describe_full_response_schema=True# False by default. Include full JSON schema in tool descriptions, instead of just an LLM-friendly response example.
102
+
mcp=FastApiMCP(
103
+
app,
104
+
name="My API MCP",
105
+
base_url="http://localhost:8000",
106
+
describe_all_responses=True, #Include all possible response schemas in tool descriptions
107
+
describe_full_response_schema=True#Include full JSON schema in tool descriptions
78
108
)
79
109
80
-
# Optionally add custom tools in addition to existing APIs.
81
-
@mcp_server.tool()
82
-
asyncdefget_server_time() -> str:
83
-
"""Get the current server time."""
84
-
from datetime import datetime
85
-
return datetime.now().isoformat()
110
+
mcp.mount()
111
+
```
112
+
113
+
### Deploying Separately from Original FastAPI App
114
+
115
+
You are not limited to serving the MCP on the same FastAPI app from which it was created.
116
+
117
+
You can create an MCP server from one FastAPI app, and mount it to a different app:
118
+
119
+
```python
120
+
from fastapi import FastAPI
121
+
from fastapi_mcp import FastApiMCP
122
+
123
+
# Your API app
124
+
api_app = FastAPI()
125
+
# ... define your API endpoints on api_app ...
126
+
127
+
# A separate app for the MCP server
128
+
mcp_app = FastAPI()
129
+
130
+
# Create MCP server from the API app
131
+
mcp = FastApiMCP(
132
+
api_app,
133
+
base_url="http://api-host:8001", # The URL where the API app will be running
# Refresh the MCP server to include the new endpoint
165
+
mcp.setup_server()
86
166
```
87
167
88
168
## Examples
@@ -137,25 +217,19 @@ Find the path to mcp-proxy by running in Terminal: `which mcp-proxy`.
137
217
138
218
## Development and Contributing
139
219
140
-
**Notice:** We are currently refactoring our MCP auto-generation system. To avoid potential conflicts, we kindly request that you delay submitting contributions until this notice is removed from the README. Thank you for your understanding and patience.
141
-
142
-
Thank you for considering contributing to FastAPI-MCP open source projects! It's people like you that make it a reality for users in our community.
220
+
Thank you for considering contributing to FastAPI-MCP! We encourage the community to post Issues and Pull Requests.
143
221
144
-
Before you get started, please see [CONTRIBUTING.md](CONTRIBUTING.md).
222
+
Before you get started, please see our [Contribution Guide](CONTRIBUTING.md).
145
223
146
224
## Community
147
225
148
226
Join [MCParty Slack community](https://join.slack.com/t/themcparty/shared_invite/zt-30yxr1zdi-2FG~XjBA0xIgYSYuKe7~Xg) to connect with other MCP enthusiasts, ask questions, and share your experiences with FastAPI-MCP.
149
227
150
228
## Requirements
151
229
152
-
- Python 3.10+
230
+
- Python 3.10+ (Recommended 3.12)
153
231
- uv
154
232
155
233
## License
156
234
157
235
MIT License. Copyright (c) 2024 Tadata Inc.
158
-
159
-
## About
160
-
161
-
Developed and maintained by [Tadata Inc.](https://github.com/tadata-org)
0 commit comments