OpenAPI Import

Generate MCP servers from existing OpenAPI specs or FastAPI apps. Huge time-saver for wrapping REST APIs as MCP tools.

From URL

from fastmcp import FastMCP
import httpx

mcp = FastMCP.from_openapi_url(
    "https://api.example.com/openapi.json",
    client=httpx.AsyncClient(
        base_url="https://api.example.com",
        headers={"Authorization": "Bearer ..."},
    ),
)

mcp.run()

Every endpoint becomes an MCP tool. GET endpoints can optionally become resources.

From File

import json

with open("openapi.json") as f:
    spec = json.load(f)

mcp = FastMCP.from_openapi(
    spec,
    client=httpx.AsyncClient(base_url="https://api.example.com"),
)

Route Maps (Selective Conversion)

Control which endpoints become tools vs resources, and apply tags:

from fastmcp.server.openapi import MCPType, RouteMap

mcp = FastMCP.from_openapi_url(
    "https://api.example.com/openapi.json",
    client=httpx.AsyncClient(base_url="https://api.example.com"),
    route_maps=[
        # Admin endpoints as tools with admin tag
        RouteMap(
            methods="*",
            pattern=r".*/admin/.*",
            mcp_type=MCPType.TOOL,
            tags={"admin"},
        ),
        # GET endpoints as resources
        RouteMap(
            methods="GET",
            pattern=r".*",
            mcp_type=MCPType.RESOURCE,
        ),
        # Everything else as tools
        RouteMap(
            methods="*",
            pattern=r".*",
            mcp_type=MCPType.TOOL,
        ),
    ],
)

From FastAPI

Convert an existing FastAPI app directly:

from fastapi import FastAPI
from fastmcp import FastMCP

app = FastAPI()

@app.get("/users/{user_id}")
async def get_user(user_id: int):
    return {"id": user_id, "name": "Alice"}

@app.post("/users")
async def create_user(name: str):
    return {"id": 1, "name": name}

# Convert to MCP server
mcp = FastMCP.from_fastapi(app)
mcp.run()

As a Provider

Add OpenAPI endpoints to an existing server:

from fastmcp.server.providers.openapi import OpenAPIProvider

main = FastMCP("Gateway")

# Add your own tools
@main.tool
def local_tool() -> str:
    return "local"

# Add API tools
main.add_provider(
    OpenAPIProvider.from_url(
        "https://api.example.com/openapi.json",
        client=httpx.AsyncClient(base_url="https://api.example.com"),
        namespace="api",
    )
)

MyLocalGPT Relevance

This is a fast path for integrating any REST API into the MCP ecosystem. Instead of hand-writing tool definitions for every API, point at the OpenAPI spec and get tools automatically. Combined with auth and the credential proxy, this makes wrapping third-party APIs trivial.

Pattern for MyLocalGPT:

  1. API has OpenAPI spec - use from_openapi_url
  2. API is a FastAPI app you control - use from_fastapi
  3. API has no spec - write tools by hand with @mcp.tool