@@ -16,85 +16,72 @@ A zero-configuration tool for integrating Model Context Protocol (MCP) servers w
16
16
17
17
## Installation
18
18
19
- You can install FastAPI-MCP directly from [ PyPI] ( https://pypi.org/project/fastapi-mcp/ ) :
19
+ We recommend using [ uv] ( https://docs.astral.sh/uv/ ) , a fast Python package installer:
20
+
21
+ ``` bash
22
+ uv add fastapi-mcp
23
+ ```
24
+
25
+ Alternatively, you can install with pip:
20
26
21
27
``` bash
22
28
pip install fastapi-mcp
23
29
```
24
30
25
31
For detailed installation instructions and alternative methods, see [ INSTALL.md] ( INSTALL.md ) .
26
32
27
- ## Usage
28
-
29
- ### Direct integration (Recommended)
33
+ ## Basic Usage
30
34
31
35
The simplest way to use FastAPI-MCP is to add an MCP server directly to your FastAPI application:
32
36
33
37
``` python
34
38
from fastapi import FastAPI
35
39
from fastapi_mcp import add_mcp_server
36
40
37
- # Create your FastAPI app
41
+ # Your FastAPI app
38
42
app = FastAPI()
39
43
40
- # Define your endpoints...
41
- @app.get (" /items/{item_id} " )
42
- def read_item (item_id : int , q : str = None ):
43
- """ Get details for a specific item"""
44
- return {" item_id" : item_id, " q" : q}
45
-
46
- # Add an MCP server to your app
47
- mcp_server = add_mcp_server(
44
+ # Mount the MCP server to your app
45
+ add_mcp_server(
48
46
app, # Your FastAPI app
49
47
mount_path = " /mcp" , # Where to mount the MCP server
50
48
name = " My API MCP" , # Name for the MCP server
51
- base_url = " http://localhost:8000" # Base URL for API requests
52
49
)
53
-
54
- # Optionally add custom MCP tools
55
- @mcp_server.tool ()
56
- async def get_item_count () -> int :
57
- """ Get the total number of items in the database."""
58
- return 42 # Your custom implementation
59
50
```
60
51
61
- Your FastAPI app will now have an MCP server mounted at the specified path, with all your API endpoints available as MCP tools .
52
+ That's it! Your auto-generated MCP server is now available at ` https://app.base.url/mcp ` .
62
53
63
- ### Legacy CLI Usage
54
+ ## Advanced Usage
64
55
65
- The CLI is still available for backward compatibility :
56
+ FastAPI-MCP provides several ways to customize and control how your MCP server is created and configured. Here are some advanced usage patterns :
66
57
67
- ``` bash
68
- # Generate an MCP server from a FastAPI app
69
- fastapi-mcp generate app.py
58
+ ``` python
59
+ from fastapi import FastAPI
60
+ from fastapi_mcp import add_mcp_server
70
61
71
- # Preview the generated server
72
- fastapi-mcp preview
62
+ app = FastAPI()
73
63
74
- # Run the generated server
75
- fastapi-mcp run
64
+ mcp_server = add_mcp_server(
65
+ app, # Your FastAPI app
66
+ mount_path = " /mcp" , # Where to mount the MCP server
67
+ name = " My API MCP" , # Name for the MCP server
68
+ describe_all_responses = True , # False by default. Include all possible response schemas in tool descriptions, instead of just the successful response.
69
+ describe_full_response_schema = True # False by default. Include full JSON schema in tool descriptions, instead of just an LLM-friendly response example.
70
+ )
76
71
77
- # Install the server for Claude
78
- fastapi-mcp install
72
+ # Add custom tools in addition to existing APIs.
73
+ @mcp_server.tool ()
74
+ async def get_server_time () -> str :
75
+ """ Get the current server time."""
76
+ from datetime import datetime
77
+ return datetime.now().isoformat()
79
78
```
80
79
81
- ## How It Works
82
-
83
- FastAPI-MCP:
84
-
85
- 1 . Takes your FastAPI application
86
- 2 . Creates an MCP server instance
87
- 3 . Mounts the MCP server to your FastAPI app
88
- 4 . Extracts endpoint information from your OpenAPI schema
89
- 5 . Creates MCP tools that make HTTP requests to your API endpoints
90
- 6 . Preserves documentation and type information
91
- 7 . Registers the tools with the MCP server
92
-
93
80
## Examples
94
81
95
82
See the [ examples] ( examples ) directory for complete examples.
96
83
97
- ### Simple direct integration example:
84
+ ### Simple integration example:
98
85
99
86
``` python
100
87
from fastapi import FastAPI
@@ -130,49 +117,30 @@ Once your FastAPI app with MCP integration is running, you can connect to it wit
130
117
2 . In Claude, use the URL of your MCP server endpoint (e.g., ` http://localhost:8000/mcp ` )
131
118
3 . Claude will discover all available tools and resources automatically
132
119
133
- ## Advanced Configuration
134
-
135
- FastAPI-MCP provides several options for advanced configuration:
120
+ ## Development and Contributing
136
121
137
- ``` python
138
- mcp_server = add_mcp_server(
139
- app,
140
- mount_path = " /mcp" ,
141
- name = " My Custom MCP Server" ,
142
- description = " Custom description for the MCP server" ,
143
- capabilities = {" streaming" : True }, # Set MCP capabilities
144
- serve_tools = True , # Whether to serve API endpoints as MCP tools
145
- base_url = " https://api.example.com" # Base URL for API requests
146
- )
147
- ```
122
+ If you're interested in contributing to FastAPI-MCP:
148
123
149
- You can also create and mount an MCP server separately:
150
-
151
- ``` python
152
- from fastapi_mcp import create_mcp_server, mount_mcp_server
153
-
154
- # Create an MCP server
155
- mcp_server = create_mcp_server(app, name = " My MCP Server" )
124
+ ``` bash
125
+ # Clone the repository
126
+ git clone https://github.com/tadata-org/fastapi_mcp.git
127
+ cd fastapi_mcp
156
128
157
- # Add custom tools
158
- @mcp_server.tool ()
159
- async def custom_tool ():
160
- return " Custom tool result "
129
+ # Create a virtual environment and install dependencies with uv
130
+ uv venv
131
+ source .venv/bin/activate # On Windows: .venv\Scripts\activate
132
+ uv add -e " .[dev] "
161
133
162
- # Mount the MCP server to the FastAPI app
163
- mount_mcp_server(app, mcp_server, mount_path = " /mcp " )
134
+ # Run tests
135
+ uv run pytest
164
136
```
165
137
138
+ For more details about contributing, see [ CONTRIBUTING.md] ( CONTRIBUTING.md ) .
139
+
166
140
## Requirements
167
141
168
142
- Python 3.10+
169
- - FastAPI 0.100.0+
170
- - Pydantic 2.0.0+
171
- - MCP 1.3.0+
172
-
173
- ## Contributing
174
-
175
- Contributions are welcome! Please feel free to submit a pull request. See [ CONTRIBUTING.md] ( CONTRIBUTING.md ) for more information.
143
+ - uv
176
144
177
145
## License
178
146
0 commit comments