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
Copy file name to clipboardExpand all lines: README.md
+80-30Lines changed: 80 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@
22
22
-**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,101 @@ 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
+
62
68
## Advanced Usage
63
69
64
70
FastAPI-MCP provides several ways to customize and control how your MCP server is created and configured. Here are some advanced usage patterns:
65
71
72
+
### Customizing Schema Description
73
+
66
74
```python
67
75
from fastapi import FastAPI
68
-
from fastapi_mcp importadd_mcp_server
76
+
from fastapi_mcp importFastApiMCP
69
77
70
78
app = FastAPI()
71
79
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.
80
+
mcp = FastApiMCP(
81
+
app,
82
+
name="My API MCP",
83
+
base_url="http://localhost:8000",
84
+
describe_all_responses=True, # Include all possible response schemas in tool descriptions
85
+
describe_full_response_schema=True# Include full JSON schema in tool descriptions
86
+
)
87
+
88
+
mcp.mount()
89
+
```
90
+
91
+
### Mounting to a Separate FastAPI App
92
+
93
+
You can create an MCP server from one FastAPI app and mount it to a different app:
94
+
95
+
```python
96
+
from fastapi import FastAPI
97
+
from fastapi_mcp import FastApiMCP
98
+
99
+
# Your API app
100
+
api_app = FastAPI()
101
+
# ... define your API endpoints on api_app ...
102
+
103
+
# A separate app for the MCP server
104
+
mcp_app = FastAPI()
105
+
106
+
# Create MCP server from the API app
107
+
mcp = FastApiMCP(
108
+
api_app,
109
+
base_url="http://api-host:8001", # The URL where the API app will be running
78
110
)
79
111
80
-
# Optionally add custom tools in addition to existing APIs.
# Refresh the MCP server to include the new endpoint
141
+
mcp.setup_server()
86
142
```
87
143
88
144
## Examples
@@ -137,25 +193,19 @@ Find the path to mcp-proxy by running in Terminal: `which mcp-proxy`.
137
193
138
194
## Development and Contributing
139
195
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.
196
+
Thank you for considering contributing to FastAPI-MCP! We encourage the community to post Issues and Pull Requests.
141
197
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.
143
-
144
-
Before you get started, please see [CONTRIBUTING.md](CONTRIBUTING.md).
198
+
Before you get started, please see our [Contribution Guide](CONTRIBUTING.md).
145
199
146
200
## Community
147
201
148
202
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
203
150
204
## Requirements
151
205
152
-
- Python 3.10+
206
+
- Python 3.10+ (Recommended 3.12)
153
207
- uv
154
208
155
209
## License
156
210
157
211
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