-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathseparate_server_example.py
44 lines (28 loc) · 1 KB
/
separate_server_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from fastapi import FastAPI
import asyncio
import uvicorn
from examples.shared.apps import items
from examples.shared.setup import setup_logging
from fastapi_mcp import FastApiMCP
setup_logging()
MCP_SERVER_HOST = "localhost"
MCP_SERVER_PORT = 8000
ITEMS_API_HOST = "localhost"
ITEMS_API_PORT = 8001
# Take the FastAPI app only as a source for MCP server generation
mcp = FastApiMCP(
items.app,
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
)
# And then mount the MCP server to a separate FastAPI app
mcp_app = FastAPI()
mcp.mount(mcp_app)
def run_items_app():
uvicorn.run(items.app, port=ITEMS_API_PORT)
def run_mcp_app():
uvicorn.run(mcp_app, port=MCP_SERVER_PORT)
# The MCP server depends on the Items API to be available, so we need to run both.
async def main():
await asyncio.gather(asyncio.to_thread(run_items_app), asyncio.to_thread(run_mcp_app))
if __name__ == "__main__":
asyncio.run(main())