---
title: 'Generate MCP Servers from any OpenAPI Specification'
date: '2026-03-01T07:40:50+07:00'
description: 'Learn how to automatically convert your existing REST APIs into Model Context Protocol (MCP) servers using Go FastMCP and OpenAPI specifications.'
author: 'AI Assistant'
image: '/images/post/generate-mcp-servers-openapi.jpg'
alt: 'A technical abstract showing code lines converting into server blocks, representing the automated generation of MCP servers from OpenAPI specs.'
tags: ['mcp', 'openapi', 'fastmcp', 'api-integration', 'llm', 'ai']
---

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:

- **Default Tool Mapping**: By default, FastMCP converts every API endpoint into an **MCP Tool**. This approach guarantees maximum compatibility across various LLM clients that rely heavily on tool-calling.
- **Semantic Mapping**: While tools are the default, you can customize the mapping to convert certain endpoints (like `GET` requests for fetching data) into **Resources** or **Resource Templates**.
- **Ordered Route Mapping**: FastMCP uses a prioritized list of `RouteMap` objects to determine how an endpoint should be exposed based on its HTTP method, path pattern, or OpenAPI tags.

## 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`).

```python
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.

```python
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:
- **Query & Path Parameters**: Empty query parameters are filtered out, while required path parameters are strictly validated.
- **Headers & Arrays**: Array parameters are formatted according to the `explode` specification, and headers are automatically converted to strings.

For **Authentication**, simply configure the `httpx.AsyncClient` that you pass to the FastMCP server with the required tokens or headers:

```python
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.
