Code Examples
This page contains a collection of practical examples demonstrating various features of FastAPI-MCP.
Basic Usage
This is the simplest way to get started. It takes an existing FastAPI app and mounts an MCP server to it.
# examples/01_basic_usage_example.py
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
app = FastAPI()
# ... your FastAPI routes defined here ...
# Add MCP server to the FastAPI app
mcp = FastApiMCP(app)
# Mount the MCP server to the FastAPI app using HTTP transport
mcp.mount_http()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Customizing Tool Descriptions
You can enrich the information available to the LLM by including full JSON schemas for responses.
# examples/02_full_schema_description_example.py
from fastapi_mcp import FastApiMCP
from your_app import app
mcp = FastApiMCP(
app,
name="Item API MCP",
description="MCP server for the Item API",
# Describe the full response JSON-schema instead of just an example
describe_full_response_schema=True,
# Describe all possible responses (e.g., 404, 422) not just 2xx
describe_all_responses=True,
)
mcp.mount_http()
Filtering Endpoints
Selectively expose endpoints using tags.
# examples/03_custom_exposed_endpoints_example.py
from fastapi_mcp import FastApiMCP
from your_app import app
# Only include endpoints tagged with "items"
mcp_include = FastApiMCP(
app,
name="Item API MCP - Included Tags",
include_tags=["items"],
)
mcp_include.mount_http(mount_path="/mcp-items-only")
# Exclude endpoints tagged with "search"
mcp_exclude = FastApiMCP(
app,
name="Item API MCP - Excluded Tags",
exclude_tags=["search"],
)
mcp_exclude.mount_http(mount_path="/mcp-no-search")
Separate MCP Server Deployment
Run your MCP server as a separate application from your main API.
# examples/04_separate_server_example.py
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
from your_api_app import app as api_app # Your main API
# Create an MCP server instance from your main API app
mcp = FastApiMCP(api_app)
# Create a new, separate FastAPI app just for the MCP server
mcp_app = FastAPI()
# Mount the MCP server to the new, separate app
mcp.mount_http(mcp_app)
# Now you can run `mcp_app` and `api_app` as two separate services.
if __name__ == "__main__":
import uvicorn
# Assume api_app is running elsewhere
uvicorn.run(mcp_app, host="0.0.0.0", port=8000)
Authentication with Token Passthrough
Protect your MCP server by requiring an Authorization
header.
# examples/08_auth_example_token_passthrough.py
from fastapi import Depends, FastAPI
from fastapi.security import HTTPBearer
from fastapi_mcp import AuthConfig, FastApiMCP
app = FastAPI()
token_auth_scheme = HTTPBearer()
# This dependency will be applied to the MCP endpoint
async def verify_token(token=Depends(token_auth_scheme)):
# Add your token validation logic here
if token.credentials != "your-secret-token":
raise HTTPException(status_code=401, detail="Invalid token")
return token.credentials
@app.get("/private", dependencies=[Depends(verify_token)])
async def private_data():
return {"message": "This is protected data."}
# Create the MCP server with the authentication dependency
mcp = FastApiMCP(
app,
name="Protected MCP",
auth_config=AuthConfig(
dependencies=[Depends(verify_token)],
),
)
mcp.mount_http()