Skip to content

Commit 9d746c2

Browse files
author
shiraayal-tadata
committed
changed examples
1 parent 6a04d2b commit 9d746c2

9 files changed

+132
-113
lines changed

examples/01_simple_example.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
"""
3+
This example shows a simple MCP server created from a FastAPI app.
4+
"""
5+
from examples.shared.apps.items import app # The FastAPI app
6+
from examples.shared.setup import setup_logging
7+
8+
from fastapi_mcp import FastApiMCP
9+
10+
setup_logging()
11+
12+
# Add MCP server to the FastAPI app
13+
mcp = FastApiMCP(
14+
app,
15+
name="Item API MCP",
16+
description="MCP server for the Item API",
17+
base_url="http://localhost:8000",
18+
)
19+
20+
mcp.mount()
21+
22+
if __name__ == "__main__":
23+
import uvicorn
24+
25+
uvicorn.run(app, host="0.0.0.0", port=8000)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
"""
3+
This example shows how to describe the full response schema instead of just a response example.
4+
"""
5+
from examples.shared.apps.items import app # The FastAPI app
6+
from examples.shared.setup import setup_logging
7+
8+
from fastapi_mcp import FastApiMCP
9+
10+
setup_logging()
11+
12+
# Add MCP server to the FastAPI app
13+
mcp = FastApiMCP(
14+
app,
15+
describe_full_response_schema=True, # Include all possible response schemas in tool descriptions
16+
describe_all_responses=True, # Include full JSON schema in tool descriptions
17+
)
18+
mcp.mount()
19+
20+
if __name__ == "__main__":
21+
import uvicorn
22+
uvicorn.run(app, host="0.0.0.0", port=8000)
Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,40 @@
1-
from examples.shared.apps import items
1+
"""
2+
This example shows how to customize exposing endpoints by filtering operation IDs and tags.
3+
"""
4+
from examples.shared.apps.items import app
25
from examples.shared.setup import setup_logging
36

4-
from fastapi import FastAPI
57
from fastapi_mcp import FastApiMCP
68

79
setup_logging()
810

9-
app = FastAPI()
10-
app.include_router(items.router)
11-
12-
# Example demonstrating how to filter MCP tools by operation IDs and tags
13-
1411
# Filter by including specific operation IDs
1512
include_operations_mcp = FastApiMCP(
1613
app,
17-
name="Item API MCP - Included Operations",
18-
description="MCP server showing only specific operations",
1914
include_operations=["get_item", "list_items"],
2015
)
2116

2217
# Filter by excluding specific operation IDs
2318
exclude_operations_mcp = FastApiMCP(
24-
app,
25-
name="Item API MCP - Excluded Operations",
26-
description="MCP server showing all operations except the excluded ones",
19+
app,
2720
exclude_operations=["create_item", "update_item", "delete_item"],
2821
)
2922

3023
# Filter by including specific tags
3124
include_tags_mcp = FastApiMCP(
3225
app,
33-
name="Item API MCP - Included Tags",
34-
description="MCP server showing operations with specific tags",
3526
include_tags=["items"],
3627
)
3728

3829
# Filter by excluding specific tags
3930
exclude_tags_mcp = FastApiMCP(
4031
app,
41-
name="Item API MCP - Excluded Tags",
42-
description="MCP server showing operations except those with specific tags",
4332
exclude_tags=["search"],
4433
)
4534

4635
# Combine operation IDs and tags (include mode)
4736
combined_include_mcp = FastApiMCP(
4837
app,
49-
name="Item API MCP - Combined Include",
50-
description="MCP server showing operations by combining include filters",
5138
include_operations=["delete_item"],
5239
include_tags=["search"],
5340
)
@@ -68,4 +55,4 @@
6855
print(" - /include-tags-mcp: Only operations with the 'items' tag")
6956
print(" - /exclude-tags-mcp: All operations except those with the 'search' tag")
7057
print(" - /combined-include-mcp: Operations with 'search' tag or delete_item operation")
71-
uvicorn.run(items.router, host="0.0.0.0", port=8000)
58+
uvicorn.run(app, host="0.0.0.0", port=8000)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
This example shows how to run the MCP server and the FastAPI app separately.
3+
"""
4+
from fastapi import FastAPI
5+
import asyncio
6+
import uvicorn
7+
8+
from examples.shared.apps.items import app
9+
from examples.shared.setup import setup_logging
10+
11+
from fastapi_mcp import FastApiMCP
12+
13+
setup_logging()
14+
15+
16+
MCP_SERVER_HOST = "localhost"
17+
MCP_SERVER_PORT = 8000
18+
ITEMS_API_HOST = "localhost"
19+
ITEMS_API_PORT = 8001
20+
21+
22+
# Take the FastAPI app only as a source for MCP server generation
23+
mcp = FastApiMCP(
24+
app,
25+
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
26+
)
27+
28+
# And then mount the MCP server to a separate FastAPI app
29+
mcp_app = FastAPI()
30+
mcp.mount(mcp_app)
31+
32+
33+
def run_items_app():
34+
uvicorn.run(app, port=ITEMS_API_PORT)
35+
36+
37+
def run_mcp_app():
38+
uvicorn.run(mcp_app, port=MCP_SERVER_PORT)
39+
40+
41+
# The MCP server depends on the Items API to be available, so we need to run both.
42+
async def main():
43+
await asyncio.gather(asyncio.to_thread(run_items_app), asyncio.to_thread(run_mcp_app))
44+
45+
46+
if __name__ == "__main__":
47+
asyncio.run(main())
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
from examples.shared.apps import items
1+
"""
2+
This example shows how to re-register tools if you add endpoints after the MCP server was created.
3+
"""
4+
from examples.shared.apps.items import app
25
from examples.shared.setup import setup_logging
36

4-
from fastapi import FastAPI
57
from fastapi_mcp import FastApiMCP
68

79
setup_logging()
810

911

10-
app = FastAPI()
11-
app.include_router(items.router)
12-
13-
1412
# Add MCP server to the FastAPI app
1513
mcp = FastApiMCP(app)
16-
17-
18-
# MCP server
1914
mcp.mount()
2015

2116

2217
# This endpoint will not be registered as a tool, since it was added after the MCP instance was created
23-
@items.router.get("/new/endpoint/", operation_id="new_endpoint", response_model=dict[str, str])
18+
@app.get("/new/endpoint/", operation_id="new_endpoint", response_model=dict[str, str])
2419
async def new_endpoint():
2520
return {"message": "Hello, world!"}
2621

@@ -32,4 +27,4 @@ async def new_endpoint():
3227
if __name__ == "__main__":
3328
import uvicorn
3429

35-
uvicorn.run(items.router, host="0.0.0.0", port=8000)
30+
uvicorn.run(app, host="0.0.0.0", port=8000)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
This example shows how to mount the MCP server to a specific APIRouter, giving a custom mount path.
3+
"""
4+
from examples.shared.apps.items import app
5+
from examples.shared.setup import setup_logging
6+
7+
from fastapi import APIRouter
8+
from fastapi_mcp import FastApiMCP
9+
10+
setup_logging()
11+
12+
13+
router = APIRouter(prefix="/other/route")
14+
app.include_router(router)
15+
16+
mcp = FastApiMCP(app)
17+
18+
# Mount the MCP server to a specific router.
19+
# It will now only be available at `/other/route/mcp`
20+
mcp.mount(router)
21+
22+
23+
if __name__ == "__main__":
24+
import uvicorn
25+
26+
uvicorn.run(app, host="0.0.0.0", port=8000)

examples/mount_specific_router_example.py

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

examples/separate_server_example.py

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

examples/simple_example.py

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

0 commit comments

Comments
 (0)