Generate MCP servers from any OpenAPI specification
New in version:Β 2.0.0
FASTMCP_EXPERIMENTAL_ENABLE_NEW_OPENAPI_PARSER=true
.The new parser is largely API-compatible with the existing implementation and will become the default in a future version. We encourage all users to test it and report any issues before it becomes the default.FastMCP.from_openapi()
class method:
RouteMap
objects to determine how to map OpenAPI routes to various MCP component types.
Each RouteMap
specifies a combination of methods, patterns, and tags, as well as a corresponding MCP component type. Each OpenAPI route is checked against each RouteMap
in order, and the first one that matches every criteria is used to determine its converted MCP type. A special type, EXCLUDE
, can be used to exclude routes from the MCP server entirely.
["GET", "POST"]
or "*"
for all)r"^/users/.*"
or r".*"
for all){}
) means no tag filtering, so the route matches regardless of its tags.TOOL
, RESOURCE
, RESOURCE_TEMPLATE
, or EXCLUDE
)FASTMCP_EXPERIMENTAL_ENABLE_NEW_OPENAPI_PARSER=true
), import from the experimental module instead:RouteMap
objects. Your custom maps are processed before the default route maps, and routes will be assigned to the first matching custom map.
For example, prior to FastMCP 2.8.0, GET requests were automatically mapped to Resource
and ResourceTemplate
components based on whether they had path parameters. (This was changed solely for client compatibility reasons.) You can restore this behavior by providing custom route maps:
GET
requests are handled semantically, and all other methods (POST
, PUT
, etc.) will fall through to the default rule and become Tool
s.
Here is a more complete example that uses custom route maps to convert all GET
endpoints under /analytics/
to tools while excluding all admin endpoints and all routes tagged βinternalβ. All other routes will be handled by the default rules:
MCPType.EXCLUDE
.
You can use this to remove sensitive or internal routes by targeting them specifically:
New in version:Β 2.5.0
For advanced use cases that require more complex logic, you can provide a route_map_fn
callable. After the route map logic is applied, this function is called on each matched route and its assigned MCP component type. It can optionally return a different component type to override the mapped assignment. If it returns None
, the assigned type is used.
In addition to more precise targeting of methods, patterns, and tags, this function can access any additional OpenAPI metadata about the route.
route_map_fn
is called on routes that matched MCPType.EXCLUDE
in your custom maps, giving you an opportunity to override the exclusion.New in version:Β 2.5.0
FastMCP automatically generates names for MCP components based on the OpenAPI specification. By default, it uses the operationId
from your OpenAPI spec, up to the first double underscore (__
).
All component names are automatically:
mcp_names
dictionary that maps operationId
values to your desired names. The operationId
must be exactly as it appears in the OpenAPI spec. The provided name will always be slugified and truncated.
operationId
not found in mcp_names
will use the default strategy (operationId up to the first __
).
New in version:Β 2.8.0
FastMCP provides several ways to add tags to your MCP components, allowing you to categorize and organize them for better discoverability and filtering. Tags are combined from multiple sources to create the final set of tags on each component.
mcp_tags
parameter in RouteMap
. These tags will be applied to all components created from routes that match that particular route map.
tags
parameter when creating your MCP server. These global tags will be applied to every component created from your OpenAPI specification.
_meta._fastmcp.tags
field, allowing clients to filter and organize components based on the original OpenAPI tagging:
New in version:Β 2.5.0
By default, FastMCP creates MCP components using a variety of metadata from the OpenAPI spec, such as incorporating the OpenAPI description into the MCP component description.
At times you may want to modify those MCP components in a variety of ways, such as adding LLM-specific instructions or tags. For fine-grained customization, you can provide a mcp_component_fn
when creating the MCP server. After each MCP component has been created, this function is called on it and has the opportunity to modify it in-place.
mcp_component_fn
is expected to modify the component in-place, not to return a new component. The result of the function is ignored.None
values or empty strings are automatically filtered out.
None
valuesexplode
parameter (default: True
)