Skip to content

Commit 0155538

Browse files
author
shiraayal-tadata
committed
edit readme and docs
1 parent ee613ec commit 0155538

File tree

7 files changed

+218
-229
lines changed

7 files changed

+218
-229
lines changed

README.md

Lines changed: 14 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
## Features
1919

20-
- **Direct integration** - Mount an MCP server directly to your FastAPI app
21-
- **Zero configuration** required - just point it at your FastAPI app and it works
22-
- **Automatic discovery** of all FastAPI endpoints and conversion to MCP tools
23-
- **Preserving schemas** of your request models and response models
24-
- **Preserve documentation** of all your endpoints, just as it is in Swagger
25-
- **Flexible deployment** - Mount your MCP server to the same app, or deploy separately
20+
- **Zero configuration** - Just point it at your FastAPI app and it works, with automatic discovery of endpoints and conversion to MCP tools
21+
- **Schema & docs preservation** - Keep the same request/response models and preserve documentation of all your endpoints
22+
- **Flexible deployment** - Mount your MCP server to the same FastAPI application, or deploy separately
23+
- **Custom endpoint exposure** - Control which endpoints become MCP tools using operation IDs and tags
2624
- **ASGI transport** - Uses FastAPI's ASGI interface directly by default for efficient communication
2725

2826
## Installation
@@ -55,236 +53,23 @@ mcp = FastApiMCP(app)
5553
mcp.mount()
5654
```
5755

58-
That's it! Your auto-generated MCP server is now available at `https://app.base.url/mcp`.
56+
That's it! Your auto-generated MCP server is now available at `https://app.base.url/mcp`.
5957

60-
## Tool Naming
58+
> **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.
6159
62-
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.
60+
## Documentation, Examples and Advanced Usage
6361

64-
Compare these two endpoint definitions:
62+
FastAPI-MCP provides comprehensive documentation in the `docs` folder:
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
6567

66-
```python
67-
# Auto-generated operation_id (something like "read_user_users__user_id__get")
68-
@app.get("/users/{user_id}")
69-
async def read_user(user_id: int):
70-
return {"user_id": user_id}
71-
72-
# Explicit operation_id (tool will be named "get_user_info")
73-
@app.get("/users/{user_id}", operation_id="get_user_info")
74-
async def read_user(user_id: int):
75-
return {"user_id": user_id}
76-
```
77-
78-
For clearer, more intuitive tool names, we recommend adding explicit `operation_id` parameters to your FastAPI route definitions.
79-
80-
To find out more, read FastAPI's official docs about [advanced config of path operations.](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/)
81-
82-
## Advanced Usage
83-
84-
FastAPI-MCP provides several ways to customize and control how your MCP server is created and configured. Here are some advanced usage patterns:
85-
86-
### Customizing Schema Description
87-
88-
```python
89-
from fastapi import FastAPI
90-
from fastapi_mcp import FastApiMCP
91-
92-
app = FastAPI()
93-
94-
mcp = FastApiMCP(
95-
app,
96-
name="My API MCP",
97-
describe_all_responses=True, # Include all possible response schemas in tool descriptions
98-
describe_full_response_schema=True # Include full JSON schema in tool descriptions
99-
)
100-
101-
mcp.mount()
102-
```
103-
104-
### Customizing Exposed Endpoints
105-
106-
You can control which FastAPI endpoints are exposed as MCP tools using Open API operation IDs or tags:
107-
108-
```python
109-
from fastapi import FastAPI
110-
from fastapi_mcp import FastApiMCP
111-
112-
app = FastAPI()
113-
114-
# Only include specific operations
115-
mcp = FastApiMCP(
116-
app,
117-
include_operations=["get_user", "create_user"]
118-
)
119-
120-
# Exclude specific operations
121-
mcp = FastApiMCP(
122-
app,
123-
exclude_operations=["delete_user"]
124-
)
125-
126-
# Only include operations with specific tags
127-
mcp = FastApiMCP(
128-
app,
129-
include_tags=["users", "public"]
130-
)
131-
132-
# Exclude operations with specific tags
133-
mcp = FastApiMCP(
134-
app,
135-
exclude_tags=["admin", "internal"]
136-
)
137-
138-
# Combine operation IDs and tags (include mode)
139-
mcp = FastApiMCP(
140-
app,
141-
include_operations=["user_login"],
142-
include_tags=["public"]
143-
)
144-
145-
mcp.mount()
146-
```
147-
148-
Notes on filtering:
149-
- You cannot use both `include_operations` and `exclude_operations` at the same time
150-
- You cannot use both `include_tags` and `exclude_tags` at the same time
151-
- You can combine operation filtering with tag filtering (e.g., use `include_operations` with `include_tags`)
152-
- When combining filters, a greedy approach will be taken. Endpoints matching either criteria will be included
153-
154-
### Deploying Separately from Original FastAPI App
155-
156-
You are not limited to serving the MCP on the same FastAPI app from which it was created.
157-
158-
You can create an MCP server from one FastAPI app, and mount it to a different app:
159-
160-
```python
161-
from fastapi import FastAPI
162-
from fastapi_mcp import FastApiMCP
163-
164-
# Your API app
165-
api_app = FastAPI()
166-
# ... define your API endpoints on api_app ...
167-
168-
# A separate app for the MCP server
169-
mcp_app = FastAPI()
170-
171-
# Create MCP server from the API app
172-
mcp = FastApiMCP(api_app)
173-
174-
# Mount the MCP server to the separate app
175-
mcp.mount(mcp_app)
176-
177-
# Now you can run both apps separately:
178-
# uvicorn main:api_app --host api-host --port 8001
179-
# uvicorn main:mcp_app --host mcp-host --port 8000
180-
```
181-
182-
### Adding Endpoints After MCP Server Creation
183-
184-
If you add endpoints to your FastAPI app after creating the MCP server, you'll need to refresh the server to include them:
185-
186-
```python
187-
from fastapi import FastAPI
188-
from fastapi_mcp import FastApiMCP
189-
190-
app = FastAPI()
191-
# ... define initial endpoints ...
192-
193-
# Create MCP server
194-
mcp = FastApiMCP(app)
195-
mcp.mount()
196-
197-
# Add new endpoints after MCP server creation
198-
@app.get("/new/endpoint/", operation_id="new_endpoint")
199-
async def new_endpoint():
200-
return {"message": "Hello, world!"}
201-
202-
# Refresh the MCP server to include the new endpoint
203-
mcp.setup_server()
204-
```
205-
206-
### Communication with the FastAPI App
207-
208-
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.
209-
210-
It's not even necessary that the FastAPI server will run. See the examples folder for more.
211-
212-
If you need to specify a custom base URL or use a different transport method, you can provide your own `httpx.AsyncClient`:
213-
214-
```python
215-
import httpx
216-
from fastapi import FastAPI
217-
from fastapi_mcp import FastApiMCP
218-
219-
app = FastAPI()
220-
221-
# Use a custom HTTP client with a specific base URL
222-
custom_client = httpx.AsyncClient(
223-
base_url="https://api.example.com",
224-
timeout=30.0
225-
)
226-
227-
mcp = FastApiMCP(
228-
app,
229-
http_client=custom_client
230-
)
231-
232-
mcp.mount()
233-
```
234-
235-
## Examples
236-
237-
See the [examples](examples) directory for complete examples.
238-
239-
## Connecting to the MCP Server using SSE
240-
241-
Once your FastAPI app with MCP integration is running, you can connect to it with any MCP client supporting SSE, such as Cursor:
242-
243-
1. Run your application.
244-
245-
2. In Cursor -> Settings -> MCP, use the URL of your MCP server endpoint (e.g., `http://localhost:8000/mcp`) as sse.
246-
247-
3. Cursor will discover all available tools and resources automatically.
248-
249-
## Connecting to the MCP Server using [mcp-proxy stdio](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#1-stdio-to-sse)
250-
251-
If your MCP client does not support SSE, for example Claude Desktop:
252-
253-
1. Run your application.
254-
255-
2. Install [mcp-proxy](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#installing-via-pypi), for example: `uv tool install mcp-proxy`.
256-
257-
3. Add in Claude Desktop MCP config file (`claude_desktop_config.json`):
258-
259-
On Windows:
260-
```json
261-
{
262-
"mcpServers": {
263-
"my-api-mcp-proxy": {
264-
"command": "mcp-proxy",
265-
"args": ["http://127.0.0.1:8000/mcp"]
266-
}
267-
}
268-
}
269-
```
270-
On MacOS:
271-
```json
272-
{
273-
"mcpServers": {
274-
"my-api-mcp-proxy": {
275-
"command": "/Full/Path/To/Your/Executable/mcp-proxy",
276-
"args": ["http://127.0.0.1:8000/mcp"]
277-
}
278-
}
279-
}
280-
```
281-
Find the path to mcp-proxy by running in Terminal: `which mcp-proxy`.
282-
283-
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.
28469

28570
## Development and Contributing
28671

287-
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.
28873

28974
Before you get started, please see our [Contribution Guide](CONTRIBUTING.md).
29075

README_zh-CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
<p align="center"><a href="https://github.com/tadata-org/fastapi_mcp"><img src="https://github.com/user-attachments/assets/1cba1bf2-2fa4-46c7-93ac-1e9bb1a95257" alt="fastapi-mcp-usage" height="400"/></a></p>
1616

17+
> 注意:最新版本请参阅 [README.md](README.md).
18+
1719
## 特点
1820

1921
- **直接集成** - 直接将 MCP 服务器挂载到您的 FastAPI 应用

FAQ.md renamed to docs/00_FAQ.md

File renamed without changes.

docs/01_tool_naming.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Tool Naming
2+
3+
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.
4+
5+
Compare these two endpoint definitions:
6+
7+
```python
8+
# Auto-generated operation_id (something like "read_user_users__user_id__get")
9+
@app.get("/users/{user_id}")
10+
async def read_user(user_id: int):
11+
return {"user_id": user_id}
12+
13+
# Explicit operation_id (tool will be named "get_user_info")
14+
@app.get("/users/{user_id}", operation_id="get_user_info")
15+
async def read_user(user_id: int):
16+
return {"user_id": user_id}
17+
```
18+
19+
For clearer, more intuitive tool names, we recommend adding explicit `operation_id` parameters to your FastAPI route definitions.
20+
21+
To find out more, read FastAPI's official docs about [advanced config of path operations.](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Connecting a client to the MCP server
2+
3+
## Connecting to the MCP Server using SSE
4+
5+
Once your FastAPI app with MCP integration is running, you can connect to it with any MCP client supporting SSE, such as Cursor:
6+
7+
1. Run your application.
8+
2. In Cursor -> Settings -> MCP, use the URL of your MCP server endpoint (e.g., `http://localhost:8000/mcp`) as sse.
9+
3. Cursor will discover all available tools and resources automatically.
10+
11+
## Connecting to the MCP Server using [mcp-proxy stdio](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#1-stdio-to-sse)
12+
13+
If your MCP client does not support SSE, for example Claude Desktop:
14+
15+
1. Run your application.
16+
2. Install [mcp-proxy](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#installing-via-pypi), for example: `uv tool install mcp-proxy`.
17+
3. Add in Claude Desktop MCP config file (`claude_desktop_config.json`):
18+
19+
On Windows:
20+
```json
21+
{
22+
"mcpServers": {
23+
"my-api-mcp-proxy": {
24+
"command": "mcp-proxy",
25+
"args": ["http://127.0.0.1:8000/mcp"]
26+
}
27+
}
28+
}
29+
```
30+
On MacOS:
31+
```json
32+
{
33+
"mcpServers": {
34+
"my-api-mcp-proxy": {
35+
"command": "/Full/Path/To/Your/Executable/mcp-proxy",
36+
"args": ["http://127.0.0.1:8000/mcp"]
37+
}
38+
}
39+
}
40+
```
41+
Find the path to mcp-proxy by running in Terminal: `which mcp-proxy`.
42+
43+
4. Claude Desktop will discover all available tools and resources automatically.

0 commit comments

Comments
 (0)