Deployment Strategies
While mounting the MCP server directly onto your main FastAPI application is the simplest approach, fastapi-mcp
also supports deploying your MCP server as a separate, independent service.
Deploying the MCP Server Separately
This strategy is useful for:
- Isolation: Keep your core API and your AI-facing MCP interface separate.
- Scalability: Scale your API and MCP server independently based on their specific loads.
- Security: Expose only the MCP server to the public internet while keeping your main API internal.
Here's how to set it up:
1. Create Two FastAPI Applications
In your code, you'll have two FastAPI
instances: one for your main API and one for the MCP server.
# main.py
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
# 1. Your main API application
# In a real project, this would be imported from another module.
api_app = FastAPI()
@api_app.get("/items/{item_id}", operation_id="get_item")
def get_item(item_id: int):
return {"id": item_id, "name": f"Item {item_id}"}
# 2. A separate FastAPI application for the MCP server
mcp_app = FastAPI()
# 3. Create the MCP server from the main API app's logic
mcp = FastApiMCP(api_app)
# 4. Mount the MCP server onto the separate MCP application
mcp.mount_http(mcp_app)
# Now `api_app` and `mcp_app` are two distinct applications.
2. Run Both Applications
You can now run both applications as separate processes using a server like uvicorn
. The MCP server communicates with the API app's logic in-memory via ASGI, so the API app does not need to be running as a network service for the MCP server to function.
However, if you have provided a custom httpx.AsyncClient
with a base_url
, the API application must be running and accessible at that URL.
Running the MCP Server:
# This command exposes your MCP server on port 8000
uvicorn main:mcp_app --host 0.0.0.0 --port 8000
Running the API Server (if needed for other purposes):
# This command runs your main API on port 8001
uvicorn main:api_app --host 0.0.0.0 --port 8001
Your MCP client would connect to http://<mcp-host>:8000/mcp
, and this service would handle all interactions with your API logic internally.