How to Connect FastAPI to Claude Desktop in 5 Minutes
A step-by-step guide to generate MCP tools from FastAPI and register them in Claude Desktop.
Key Takeaways
- FastAPI already exposes OpenAPI, so MCP generation is quick.
- LegacyAI can generate tools directly from your FastAPI spec.
- Claude Desktop reads the MCP tool catalog and calls your API safely.
- Keep auth and scopes aligned to avoid accidental write access.
What you will build
In this guide you will connect a FastAPI service to Claude Desktop using MCP. You will export your OpenAPI spec, generate an MCP server, and register the tool catalog so Claude can call your endpoints.
You do not need to change your FastAPI routes. The workflow builds a thin MCP layer on top of your existing API.
Step 1: Confirm OpenAPI is enabled
FastAPI exposes OpenAPI at /openapi.json by default. If you disabled docs, re-enable them for build time or export the spec through a script.
from fastapi import FastAPI
app = FastAPI(title="LegacyAI Demo API")
@app.get("/customers")
def list_customers():
return [{"id": "c_1", "name": "Acme"}]
Step 2: Export the OpenAPI spec
You can fetch the spec from a running service or export it directly during CI. Save it as openapi.json for the MCP generator.
curl http://localhost:8000/openapi.json -o openapi.jsonStep 3: Generate the MCP server with LegacyAI
Upload openapi.json to LegacyAI or use the CLI to generate an MCP server. The generator maps each path to a tool with a structured schema and safe defaults.
LegacyAI also detects auth schemes and lets you enforce read-only mode or scoped access before you go live.
Step 4: Register the MCP server in Claude Desktop
Claude Desktop reads an MCP configuration file that points to your MCP server. The exact config file location depends on your OS, but the structure is the same.
{
"mcpServers": [
{
"name": "LegacyAI FastAPI",
"command": "node",
"args": ["./legacyai-mcp-server.js"],
"env": {
"API_BASE_URL": "https://api.yourcompany.com",
"API_TOKEN": "${API_TOKEN}"
}
}
]
}Step 5: Test a tool call
Open Claude Desktop, refresh tools, and ask the agent to call your endpoint. Start with a read-only route, confirm the request, and validate the response payload.
If the response fails, check the MCP server logs for validation errors. Most issues come from missing auth headers or mismatched parameter names.
Production tips
- Use read-only mode by default and enable writes only after testing.
- Rotate API keys and scope tokens per tool or environment.
- Enable logging so you can audit every tool call.
- Sync the MCP server when your OpenAPI spec changes.
FAQ
Do I need to expose /openapi.json in production?
No. You can export the spec locally or in CI and upload it to LegacyAI. Your production service does not need to expose the spec publicly.
How long does MCP generation take?
For most FastAPI projects, generation takes a few minutes. Large APIs may take longer if they include extensive schemas.
Can I limit Claude to GET endpoints?
Yes. LegacyAI supports read-only mode so only safe GET tools are exposed to Claude.
What if my API uses OAuth?
LegacyAI detects OAuth in your spec and lets you map scopes to individual tools or groups of tools.
How do I update tools when my API changes?
Enable dynamic sync or run the MCP generator in CI so the tool catalog updates when your OpenAPI spec changes.
Related articles
Tutorials
ASP.NET Core MCP Server - Complete Setup Guide
Configure Swashbuckle, export OpenAPI, and generate MCP servers for Claude Desktop.
Tutorials
How to Generate MCP Server from Swagger JSON
Use Swagger JSON to generate MCP tools, then verify and ship them safely.
Tutorials
MCP with Next.js - Integration Guide
Generate OpenAPI from Next.js, connect to LegacyAI, and configure Claude Desktop.