/ Annuaire / Playground / FastAPI MCP
● Communauté tadata-org ⚡ Instantané

FastAPI MCP

par tadata-org · tadata-org/fastapi_mcp

Mount one line, turn every FastAPI route into an MCP tool — schemas from your Pydantic models, auth from your middleware.

fastapi-mcp wraps an existing FastAPI app and exposes its routes as MCP tools. Tool schemas are generated from your Pydantic request/response models. Your existing auth middleware runs unchanged. The fastest path from 'I have a Python API' to 'agents can call it'.

Pourquoi l'utiliser

Fonctionnalités clés

Démo en direct

Aperçu en pratique

fastapi-mcp.replay ▶ prêt
0/0

Installer

Choisissez votre client

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "fastapi-mcp": {
      "command": "uvx",
      "args": [
        "fastapi-mcp"
      ]
    }
  }
}

Ouvrez Claude Desktop → Settings → Developer → Edit Config. Redémarrez après avoir enregistré.

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "fastapi-mcp": {
      "command": "uvx",
      "args": [
        "fastapi-mcp"
      ]
    }
  }
}

Cursor utilise le même schéma mcpServers que Claude Desktop. La config projet l'emporte sur la globale.

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "fastapi-mcp": {
      "command": "uvx",
      "args": [
        "fastapi-mcp"
      ]
    }
  }
}

Cliquez sur l'icône MCP Servers dans la barre latérale Cline, puis "Edit Configuration".

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "fastapi-mcp": {
      "command": "uvx",
      "args": [
        "fastapi-mcp"
      ]
    }
  }
}

Même format que Claude Desktop. Redémarrez Windsurf pour appliquer.

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "fastapi-mcp",
      "command": "uvx",
      "args": [
        "fastapi-mcp"
      ]
    }
  ]
}

Continue utilise un tableau d'objets serveur plutôt qu'une map.

~/.config/zed/settings.json
{
  "context_servers": {
    "fastapi-mcp": {
      "command": {
        "path": "uvx",
        "args": [
          "fastapi-mcp"
        ]
      }
    }
  }
}

Ajoutez dans context_servers. Zed recharge à chaud à la sauvegarde.

claude mcp add fastapi-mcp -- uvx fastapi-mcp

Une seule ligne. Vérifiez avec claude mcp list. Supprimez avec claude mcp remove.

Cas d'usage

Usages concrets : FastAPI MCP

Expose an existing FastAPI app as MCP without refactoring

👤 Backend devs with a running FastAPI service ⏱ ~30 min intermediate

Quand l'utiliser : You already ship a FastAPI API. You want agents to use it. You don't want a parallel codebase.

Prérequis
  • Working FastAPI app with Pydantic models on requests/responses — If your routes return dict, refactor to Pydantic BaseModel first — otherwise tool schemas will be object
Déroulement
  1. Install and mount
    Add fastapi-mcp to my project. In my main.py, after app = FastAPI(), add FastApiMCP(app).mount(). Show the diff.✓ Copié
    → Minimal diff; server still runs
  2. Verify tools appear
    Run the server. Connect with npx -y mcp-remote http://localhost:8000/mcp. List tools — do all my GET/POST routes show up?✓ Copié
    → Tools listed, names match route operation_ids
  3. Filter what's exposed
    I don't want /admin/* routes exposed. Reconfigure with include_operations or exclude by tag.✓ Copié
    → Admin routes no longer in tool list

Résultat : Your existing API is now MCP-addressable with zero duplication of schemas or logic.

Pièges
  • Every route becomes a tool — including internal ones you didn't mean to expose — Explicitly set include_tags=['public'] or exclude_operations=[...] before going live
  • Routes without operation_id get generated names like read_item_items__item_id__get — ugly — Always set explicit operation_id on routes: @app.get('/items/{id}', operation_id='get_item')
Combiner avec : fastmcp

Expose an authenticated FastAPI app as MCP without breaking auth

👤 Backend devs with bearer/OAuth2 on FastAPI ⏱ ~30 min intermediate

Quand l'utiliser : Your FastAPI routes use Depends(get_current_user) — you need that to still fire when called via MCP.

Prérequis
  • Existing auth dependencyDepends(oauth2_scheme) or equivalent
Déroulement
  1. Pass through the Authorization header
    Configure fastapi-mcp to forward the Authorization header from MCP context to the underlying route. Point me at the settings.✓ Copié
    → Config change; routes now receive the header
  2. Test an authed call
    Call the protected endpoint via MCP. First with no token (expect 401), then with a valid one (expect 200).✓ Copié
    → 401 unauth, 200 auth — same as direct HTTP
  3. Document the client-side setup
    Write the mcp-remote config users need so their Claude Desktop passes an Authorization header.✓ Copié
    → Copy-pasteable client config snippet

Résultat : Auth enforced identically whether called from curl or from an agent.

Pièges
  • Agent accidentally uses a long-lived root token — Mint short-lived scoped tokens per user; rotate aggressively — treat MCP like any other API client

Deploy the MCP endpoint alongside your existing API

👤 Platform engineers ⏱ ~45 min advanced

Quand l'utiliser : You run FastAPI on Cloud Run / Fly / Render. You want MCP to share that deploy.

Déroulement
  1. Mount at a stable path
    Mount fastapi-mcp at /mcp. Confirm /docs (Swagger) and /mcp both work from the same process.✓ Copié
    → Both paths respond
  2. Expose via your existing ingress
    Update my Cloud Run / nginx / API gateway config to route /mcp to this service with SSE-compatible timeouts (no buffering, long idle).✓ Copié
    → Remote mcp-remote connects cleanly
  3. Document the client setup
    Write the team-facing README: how to add this MCP to Claude Desktop + Cursor.✓ Copié
    → Copy-paste config per client

Résultat : One deploy, two protocols (REST + MCP), zero extra infra.

Pièges
  • Proxy buffers SSE responses, connection appears hung — Disable proxy buffering — nginx: proxy_buffering off; Cloud Run: already fine; Cloudflare: enable streaming
Combiner avec : cloud-run · vercel

Combinaisons

Associez-le à d'autres MCPs pour un effet X10

fastapi-mcp + fastmcp

Use fastapi-mcp for legacy routes, FastMCP for greenfield agent-specific tools in the same process

Mount fastapi-mcp for my existing /api/* routes and register 3 new FastMCP tools for agent-only operations like bulk_sync.✓ Copié
fastapi-mcp + cloud-run

Deploy REST + MCP together on Cloud Run

Take my FastAPI service, add fastapi-mcp, containerize, and deploy to Cloud Run with IAM auth on /mcp.✓ Copié

Outils

Ce que ce MCP expose

OutilEntréesQuand appelerCoût
FastApiMCP(app, **options) app: FastAPI, include_operations?, exclude_operations?, include_tags?, exclude_tags?, name?, description? Build-time wiring — create once at startup free
.mount(path='/mcp') path? Right after instantiation free
Auto-generated tools derived from each FastAPI route's request/response models No action — they appear once you mount same as your route

Coût et limites

Coût d'exécution

Quota d'API
Inherits from your API
Tokens par appel
Same as direct HTTP calls — no overhead
Monétaire
Free, open source
Astuce
Exclude verbose admin/debug routes from MCP — they pollute the agent's tool list and cost tokens on every list_tools

Sécurité

Permissions, secrets, portée

Stockage des identifiants : Whatever your FastAPI uses — fastapi-mcp doesn't add a new auth layer
Sortie de données : Wherever your routes already go

Dépannage

Erreurs courantes et correctifs

Tools appear but calls return 422 validation errors

Your route uses dict instead of Pydantic BaseModel — MCP passes typed JSON and your route can't parse it. Refactor to BaseModel.

Route exists at /items but no matching tool

fastapi-mcp skips routes without a response_model or operation_id in some cases. Add response_model=ItemOut and operation_id='get_item'.

Vérifier : curl http://localhost:8000/openapi.json | jq '.paths'
Auth dependency doesn't fire when called via MCP

Configure header forwarding in FastApiMCP options. If your dependency reads cookies instead of headers, switch to header-based auth — MCP has no cookie jar.

SSE hangs behind nginx

Add proxy_buffering off; proxy_cache off; proxy_read_timeout 3600s; to the /mcp location block.

Alternatives

FastAPI MCP vs autres

AlternativeQuand l'utiliserCompromis
FastMCPGreenfield — no existing FastAPI app, just writing an MCP serverCleaner DX for new projects; fastapi-mcp wins for retrofitting
Hand-written SDK serverYou need fine-grained control over tool names, descriptions, and schemasMore code, more maintenance — usually not worth it

Plus

Ressources

📖 Lire le README officiel sur GitHub

🐙 Voir les issues ouvertes

🔍 Parcourir les 400+ serveurs MCP et Skills