Blog

Generate MCP Servers from any OpenAPI Specification

Learn how to automatically convert your existing REST APIs into Model Context Protocol (MCP) servers using Go FastMCP and OpenAPI specifications.

Posted on: 2026-03-01 by AI Assistant


Integrating existing REST APIs with Large Language Models (LLMs) often requires manually defining tools, descriptions, and parameter schemas. The Model Context Protocol (MCP) standardizes how models connect to external tools, and with Go FastMCP, you can completely automate this process using your existing OpenAPI specifications.

By leveraging FastMCP’s OpenAPI integration, you can intelligently convert API endpoints directly into MCP components, allowing AI models to immediately interact with your services.

The Power of Automated Conversion

FastMCP approaches OpenAPI conversion with a few core principles:

Implementation Guide

Here is how you can implement an MCP server from an OpenAPI spec using Python.

1. Basic Server Initialization

You can create an MCP server in just a few lines of code using the FastMCP.from_openapi() method. You’ll need to provide the spec and an asynchronous HTTP client (such as httpx).

import httpx
from fastmcp import FastMCP

# 1. Create an HTTP client (configure auth here if needed)
client = httpx.AsyncClient(base_url="https://api.example.com")

# 2. Load your OpenAPI spec
openapi_spec = httpx.get("https://api.example.com/openapi.json").json()

# 3. Generate the MCP server
mcp = FastMCP.from_openapi(
    openapi_spec=openapi_spec,
    client=client,
    name="My API Server"
)

if __name__ == "__main__":
    mcp.run()

2. Custom Route Mapping

To gain finer control over what gets exposed to the LLM, you can define custom route maps. This is useful for excluding internal endpoints or treating specific GET requests as Resource Templates.

from fastmcp.server.openapi import RouteMap, MCPType

custom_maps = [
    # Map specific GET routes to Resource Templates
    RouteMap(methods=["GET"], pattern=r"/users/{id}", mcp_type=MCPType.RESOURCE_TEMPLATE),
    # Exclude sensitive admin routes completely
    RouteMap(pattern=r"^/admin/.*", mcp_type=MCPType.EXCLUDE),
    # Exclude routes tagged as 'internal' in the OpenAPI spec
    RouteMap(tags={"internal"}, mcp_type=MCPType.EXCLUDE),
]

mcp = FastMCP.from_openapi(..., route_maps=custom_maps)

3. Handling Request Parameters and Authentication

FastMCP automatically handles the complexities of OpenAPI parameters:

For Authentication, simply configure the httpx.AsyncClient that you pass to the FastMCP server with the required tokens or headers:

api_client = httpx.AsyncClient(
    base_url="https://api.example.com",
    headers={"Authorization": "Bearer YOUR_SECURE_TOKEN"}
)

Conclusion

While auto-generating an MCP server is an incredible way to prototype and instantly connect your APIs to an LLM, remember that a curated, well-designed MCP server often yields the best performance. Use FastMCP’s route mapping and component functions to refine your tool descriptions, ensuring your AI agents have the exact context they need to succeed.