OpenAPI Integration
Generate MCP servers from OpenAPI specs
New in version: 2.0.0
FastMCP can automatically generate an MCP server from an OpenAPI specification. Users only need to provide an OpenAPI specification (3.0 or 3.1) and an API client.
Configuration Options
Timeout
You can set a timeout for all API requests:
This timeout is applied to all requests made by tools, resources, and resource templates.
Route Mapping
By default, OpenAPI routes are mapped to MCP components based on these rules:
OpenAPI Route | Example | MCP Component | Notes |
---|---|---|---|
GET without path params | GET /stats | Resource | Simple resources for fetching data |
GET with path params | GET /users/{id} | Resource Template | Path parameters become template parameters |
POST , PUT , PATCH , DELETE , etc. | POST /users | Tool | Operations that modify data |
Internally, FastMCP uses a priority-ordered set of RouteMap
objects to determine the component type. Route maps indicate that a specific HTTP method (or methods) and path pattern should be treated as a specific component type. This is the default set of route maps:
Custom Route Maps
Users can add custom route maps to override the default mapping behavior. User-supplied route maps are always applied first, before the default route maps.
How It Works
- FastMCP parses your OpenAPI spec to extract routes and schemas
- It applies mapping rules to categorize each route
- When an MCP client calls a tool or accesses a resource:
- FastMCP constructs an HTTP request based on the OpenAPI definition
- It sends the request through the provided httpx client
- It translates the HTTP response to the appropriate MCP format
Request Parameter Handling
FastMCP carefully handles different types of parameters in OpenAPI requests:
Query Parameters
By default, FastMCP will only include query parameters that have non-empty values. Parameters with None
values or empty strings (""
) are automatically filtered out of requests. This ensures that API servers don’t receive unnecessary empty parameters that might cause issues.
For example, if you call a tool with these parameters:
The resulting HTTP request will only include category=electronics&min_price=100
.
Path Parameters
For path parameters, which are typically required by REST APIs, FastMCP filters out None
values and checks that all required path parameters are provided. If a required path parameter is missing or None
, an error will be raised.