Back to MCP Catalog

FastAPI MCP Server

Developer ToolsPython
Convert FastAPI endpoints into Model Context Protocol tools
Available Tools

mcp_endpoint

Decorator that converts a FastAPI endpoint into an MCP tool

namedescriptionparameters

MCPRouter

Router that handles MCP protocol requests and exposes FastAPI endpoints as MCP tools

titledescriptionversionauthprefix

APIKeyAuth

Authentication handler for API key-based authentication

api_keysheader_name

OAuth2Auth

Authentication handler for OAuth2-based authentication

token_urlclient_idclient_secretscopes

FastAPI MCP is a powerful library that transforms your existing FastAPI endpoints into Model Context Protocol (MCP) tools with minimal code changes. This integration allows AI assistants to directly interact with your API endpoints, making your services accessible to AI models through a standardized protocol. The library supports authentication mechanisms, allowing you to secure your endpoints while still making them available as MCP tools. With FastAPI MCP, developers can quickly expose their existing APIs to AI assistants without having to rewrite their codebase or build separate interfaces.

Getting Started with FastAPI MCP

Installation

Install the package using pip:

pip install fastapi-mcp

Basic Usage

To convert your FastAPI endpoints into MCP tools, follow these steps:

  1. Import the necessary components:
from fastapi import FastAPI
from fastapi_mcp import MCPRouter, mcp_endpoint
  1. Create your FastAPI app and an MCP router:
app = FastAPI()
mcp_router = MCPRouter()
  1. Define your endpoints with the mcp_endpoint decorator:
@app.get("/hello/{name}")
@mcp_endpoint(
    name="say_hello",
    description="Say hello to someone",
    parameters=["name"]
)
async def hello(name: str):
    return {"message": f"Hello, {name}!"}
  1. Include the MCP router in your FastAPI app:
app.include_router(mcp_router)
  1. Run your FastAPI application:
uvicorn main:app --reload

Authentication

FastAPI MCP supports various authentication methods:

API Key Authentication

from fastapi_mcp.auth import APIKeyAuth

# Create an API key auth handler
api_key_auth = APIKeyAuth(
    api_keys=["your-secret-key"],
    header_name="X-API-Key"
)

# Use it with your MCP router
mcp_router = MCPRouter(auth=api_key_auth)

OAuth2 Authentication

from fastapi_mcp.auth import OAuth2Auth

# Create an OAuth2 auth handler
oauth2_auth = OAuth2Auth(
    token_url="https://your-auth-server.com/token",
    client_id="your-client-id",
    client_secret="your-client-secret",
    scopes=["read", "write"]
)

# Use it with your MCP router
mcp_router = MCPRouter(auth=oauth2_auth)

Advanced Configuration

You can customize the MCP router with additional options:

mcp_router = MCPRouter(
    title="My API",
    description="My API description",
    version="1.0.0",
    auth=api_key_auth,
    prefix="/mcp"
)

Testing Your MCP Server

You can test your MCP server using the Model Context Protocol Inspector:

  1. Install the inspector:
npm install -g @modelcontextprotocol/inspector
  1. Run the inspector against your server:
mcp-inspector http://localhost:8000/mcp

This will show you the available tools and allow you to test them interactively.

Integrating with AI Assistants

To use your MCP server with AI assistants, you'll need to provide the server URL to the assistant. The exact method depends on the assistant you're using, but typically involves adding your server to the assistant's configuration.

Related MCPs

Apple Shortcuts
Developer ToolsJavaScript

Control Apple Shortcuts automations from AI assistants

Clojars Dependency Lookup
Developer ToolsJavaScript

Fetch dependency information from Clojars, the Clojure community's artifact repository

Simple Timeserver
Developer ToolsPython

Provides Claude with current time and timezone information

About Model Context Protocol

Model Context Protocol (MCP) allows AI models to access external tools and services, extending their capabilities beyond their training data.

Generate Cursor Documentation

Save time on coding by generating custom documentation and prompts for Cursor IDE.