Skip to content

Commit 0c58451

Browse files
author
shiraayal-tadata
committed
edit examples
1 parent 91b629b commit 0c58451

13 files changed

+38
-145
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ That's it! Your auto-generated MCP server is now available at `https://app.base.
6060
## Documentation, Examples and Advanced Usage
6161

6262
FastAPI-MCP provides comprehensive documentation in the `docs` folder:
63+
- [Best Practices](docs/00_BEST_PRACTICES.md) - Essential guidelines for converting APIs to MCP tools safely and effectively
6364
- [FAQ](docs/00_FAQ.md) - Frequently asked questions about usage, development and support
6465
- [Tool Naming](docs/01_tool_naming.md) - Best practices for naming your MCP tools using operation IDs
6566
- [Connecting to MCP Server](docs/02_connecting_to_the_mcp_server.md) - How to connect various MCP clients like Cursor and Claude Desktop

examples/01_simple_example.py renamed to examples/01_basic_usage_example.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
"""
33
This example shows a simple MCP server created from a FastAPI app.
44
"""
5-
from examples.shared.apps.items import app # The FastAPI app
6-
from examples.shared.setup import setup_logging
7-
5+
from examples.shared.items_app import app # The FastAPI app
86
from fastapi_mcp import FastApiMCP
97

10-
setup_logging()
11-
128
# Add MCP server to the FastAPI app
139
mcp = FastApiMCP(
1410
app,
1511
name="Item API MCP",
1612
description="MCP server for the Item API",
17-
base_url="http://localhost:8000",
1813
)
1914

15+
# Mount the MCP server to the FastAPI app
2016
mcp.mount()
2117

2218
if __name__ == "__main__":
2319
import uvicorn
24-
2520
uvicorn.run(app, host="0.0.0.0", port=8000)

examples/02_full_schema_description_example.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
"""
33
This example shows how to describe the full response schema instead of just a response example.
44
"""
5-
from examples.shared.apps.items import app # The FastAPI app
6-
from examples.shared.setup import setup_logging
7-
5+
from examples.shared.items_app import app # The FastAPI app
86
from fastapi_mcp import FastApiMCP
97

10-
setup_logging()
11-
12-
# Add MCP server to the FastAPI app
138
mcp = FastApiMCP(
149
app,
1510
describe_full_response_schema=True, # Include all possible response schemas in tool descriptions

examples/03_custom_exposed_endpoints_example.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,42 @@
66
- You can combine operation filtering with tag filtering (e.g., use `include_operations` with `include_tags`)
77
- When combining filters, a greedy approach will be taken. Endpoints matching either criteria will be included
88
"""
9-
from examples.shared.apps.items import app
10-
from examples.shared.setup import setup_logging
11-
9+
from examples.shared.items_app import app # The FastAPI app
1210
from fastapi_mcp import FastApiMCP
1311

14-
setup_logging()
1512

1613
# Filter by including specific operation IDs
1714
include_operations_mcp = FastApiMCP(
1815
app,
16+
name="Item API MCP - Included Operations",
1917
include_operations=["get_item", "list_items"],
2018
)
2119

2220
# Filter by excluding specific operation IDs
2321
exclude_operations_mcp = FastApiMCP(
2422
app,
23+
name="Item API MCP - Excluded Operations",
2524
exclude_operations=["create_item", "update_item", "delete_item"],
2625
)
2726

2827
# Filter by including specific tags
2928
include_tags_mcp = FastApiMCP(
3029
app,
30+
name="Item API MCP - Included Tags",
3131
include_tags=["items"],
3232
)
3333

3434
# Filter by excluding specific tags
3535
exclude_tags_mcp = FastApiMCP(
3636
app,
37+
name="Item API MCP - Excluded Tags",
3738
exclude_tags=["search"],
3839
)
3940

4041
# Combine operation IDs and tags (include mode)
4142
combined_include_mcp = FastApiMCP(
4243
app,
44+
name="Item API MCP - Combined Include",
4345
include_operations=["delete_item"],
4446
include_tags=["search"],
4547
)

examples/04_separate_server_example.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,28 @@
33
You can create an MCP server from one FastAPI app, and mount it to a different app.
44
"""
55
from fastapi import FastAPI
6-
import asyncio
7-
import uvicorn
86

9-
from examples.shared.apps.items import app
10-
from examples.shared.setup import setup_logging
7+
from examples.shared.items_app import app
118

129
from fastapi_mcp import FastApiMCP
1310

14-
setup_logging()
15-
16-
1711
MCP_SERVER_HOST = "localhost"
1812
MCP_SERVER_PORT = 8000
1913
ITEMS_API_HOST = "localhost"
2014
ITEMS_API_PORT = 8001
2115

2216

2317
# Take the FastAPI app only as a source for MCP server generation
24-
mcp = FastApiMCP(
25-
app,
26-
base_url=f"http://{ITEMS_API_HOST}:{ITEMS_API_PORT}", # Note how the base URL is the **Items API** URL, not the MCP server URL
27-
)
18+
mcp = FastApiMCP(app)
2819

29-
# And then mount the MCP server to a separate FastAPI app
20+
# Mount the MCP server to a separate FastAPI app
3021
mcp_app = FastAPI()
3122
mcp.mount(mcp_app)
3223

33-
34-
def run_items_app():
35-
uvicorn.run(app, port=ITEMS_API_PORT)
36-
37-
38-
def run_mcp_app():
39-
uvicorn.run(mcp_app, port=MCP_SERVER_PORT)
40-
41-
42-
# The MCP server depends on the Items API to be available, so we need to run both.
43-
async def main():
44-
await asyncio.gather(asyncio.to_thread(run_items_app), asyncio.to_thread(run_mcp_app))
45-
46-
24+
# Run the MCP server separately from the original FastAPI app.
25+
# It still works 🚀
26+
# Your original API is **not exposed**, only via the MCP server.
4727
if __name__ == "__main__":
48-
asyncio.run(main())
28+
import uvicorn
29+
30+
uvicorn.run(mcp_app, host="0.0.0.0", port=8000)
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
"""
22
This example shows how to re-register tools if you add endpoints after the MCP server was created.
33
"""
4-
from examples.shared.apps.items import app
5-
from examples.shared.setup import setup_logging
6-
4+
from examples.shared.items_app import app # The FastAPI app
75
from fastapi_mcp import FastApiMCP
86

9-
setup_logging()
10-
117

12-
# Add MCP server to the FastAPI app
13-
mcp = FastApiMCP(app)
14-
mcp.mount()
8+
mcp = FastApiMCP(app) # Add MCP server to the FastAPI app
9+
mcp.mount() # MCP server
1510

1611

1712
# This endpoint will not be registered as a tool, since it was added after the MCP instance was created
@@ -26,5 +21,4 @@ async def new_endpoint():
2621

2722
if __name__ == "__main__":
2823
import uvicorn
29-
3024
uvicorn.run(app, host="0.0.0.0", port=8000)
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
"""
22
This example shows how to mount the MCP server to a specific APIRouter, giving a custom mount path.
33
"""
4-
from examples.shared.apps.items import app
5-
from examples.shared.setup import setup_logging
6-
4+
from examples.shared.items_app import app # The FastAPI app
75
from fastapi import APIRouter
86
from fastapi_mcp import FastApiMCP
97

10-
setup_logging()
11-
128

13-
router = APIRouter(prefix="/other/route")
14-
app.include_router(router)
9+
other_router = APIRouter(prefix="/other/route")
10+
app.include_router(other_router)
1511

1612
mcp = FastApiMCP(app)
1713

1814
# Mount the MCP server to a specific router.
1915
# It will now only be available at `/other/route/mcp`
20-
mcp.mount(router)
16+
mcp.mount(other_router)
2117

2218

2319
if __name__ == "__main__":
2420
import uvicorn
25-
2621
uvicorn.run(app, host="0.0.0.0", port=8000)

examples/07_configure_http_timeout_example.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44
"""
55
import httpx
66

7-
from examples.shared.apps.items import app
8-
from examples.shared.setup import setup_logging
9-
7+
from examples.shared.items_app import app # The FastAPI app
108
from fastapi_mcp import FastApiMCP
119

12-
setup_logging()
1310

1411
mcp = FastApiMCP(
1512
app,
16-
http_client=httpx.AsyncClient(timeout=10)
13+
http_client=httpx.AsyncClient(timeout=20)
1714
)
18-
mcp.mount(mcp)
15+
mcp.mount()
1916

2017

2118
if __name__ == "__main__":
2219
import uvicorn
23-
2420
uvicorn.run(app, host="0.0.0.0", port=8000)

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The following examples demonstrate various features and usage patterns of FastAPI-MCP:
44

5-
1. [Simple Example](examples/01_simple_example.py) - Basic FastAPI-MCP integration
5+
1. [Basic Usage Example](examples/01_basic_usage_example.py) - Basic FastAPI-MCP integration
66
2. [Full Schema Description](examples/02_full_schema_description_example.py) - Customizing schema descriptions
77
3. [Custom Exposed Endpoints](examples/03_custom_exposed_endpoints_example.py) - Controlling which endpoints are exposed
88
4. [Separate Server](examples/04_separate_server_example.py) - Deploying MCP server separately

examples/full_schema_description_example.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)