Generate MCP servers from OpenAPI specs and FastAPI apps
New in version: 2.0.0
FastMCP can automatically generate an MCP server from an OpenAPI specification or FastAPI app. Instead of manually creating tools and resources, you provide an OpenAPI spec and FastMCP intelligently converts your API endpoints into the appropriate MCP components.
To convert an OpenAPI specification to an MCP server, you can use the FastMCP.from_openapi
class method. This method takes an OpenAPI specification and an async HTTPX client that can be used to make requests to the API, and returns an MCP server.
Here’s an example:
That’s it! Your entire API is now available as an MCP server. Clients can discover and interact with your API endpoints through the MCP protocol, with full schema validation and type safety.
By default, FastMCP converts every endpoint in your OpenAPI specification into an MCP Tool. This provides a simple, predictable starting point that ensures all your API’s functionality is immediately available to the vast majority of LLM clients which only support MCP tools.
While this is a pragmatic default for maximum compatibility, you can easily customize this behavior. Internally, FastMCP uses an ordered list of 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
)Here is FastMCP’s default rule:
When creating your FastMCP server, you can customize routing behavior by providing your own list of 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:
With these 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:
The default route maps are always applied after your custom maps, so you do not have to create route maps for every possible route.
To exclude routes from the MCP server, use a route map to assign them to MCPType.EXCLUDE
.
You can use this to remove sensitive or internal routes by targeting them specifically:
Or you can use a catch-all rule to exclude everything that your maps don’t handle explicitly:
Using a catch-all exclusion rule will prevent the default route mappings from being applied, since it will match every remaining route. This is useful if you want to explicitly allow-list certain routes.
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.
The 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.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.
You can add custom tags to components created from specific routes using the mcp_tags
parameter in RouteMap
. These tags will be applied to all components created from routes that match that particular route map.
You can add tags to all components by providing a tags
parameter when creating your FastMCP server with from_openapi
or from_fastapi
. These global tags will be applied to every component created from your OpenAPI specification.
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:
For more control over component names, you can provide an 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.
Any operationId
not found in mcp_names
will use the default strategy (operationId up to the first __
).
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.
Your mcp_component_fn
is expected to modify the component in-place, not to return a new component. The result of the function is ignored.
FastMCP intelligently handles different types of parameters in OpenAPI requests:
By default, FastMCP only includes query parameters that have non-empty values. Parameters with None
values or empty strings are automatically filtered out.
Path parameters are typically required by REST APIs. FastMCP:
None
valuesFastMCP handles array parameters according to OpenAPI specifications:
explode
parameter (default: True
)Header parameters are automatically converted to strings and included in the HTTP request.
If your API requires authentication, configure it on the HTTP client before creating the MCP server:
Set a timeout for all API requests:
New in version: 2.0.0
FastMCP can directly convert FastAPI applications into MCP servers by extracting their OpenAPI specifications:
FastMCP does not include FastAPI as a dependency; you must install it separately to use this integration.
Note that operation ids are optional, but are used to create component names. You can also provide custom names, just like with OpenAPI specs.
FastMCP servers are not FastAPI apps, even when created from one. To learn how to deploy them as an ASGI app, see the ASGI Integration documentation.
All OpenAPI integration features work with FastAPI apps:
Generate MCP servers from OpenAPI specs and FastAPI apps
New in version: 2.0.0
FastMCP can automatically generate an MCP server from an OpenAPI specification or FastAPI app. Instead of manually creating tools and resources, you provide an OpenAPI spec and FastMCP intelligently converts your API endpoints into the appropriate MCP components.
To convert an OpenAPI specification to an MCP server, you can use the FastMCP.from_openapi
class method. This method takes an OpenAPI specification and an async HTTPX client that can be used to make requests to the API, and returns an MCP server.
Here’s an example:
That’s it! Your entire API is now available as an MCP server. Clients can discover and interact with your API endpoints through the MCP protocol, with full schema validation and type safety.
By default, FastMCP converts every endpoint in your OpenAPI specification into an MCP Tool. This provides a simple, predictable starting point that ensures all your API’s functionality is immediately available to the vast majority of LLM clients which only support MCP tools.
While this is a pragmatic default for maximum compatibility, you can easily customize this behavior. Internally, FastMCP uses an ordered list of 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
)Here is FastMCP’s default rule:
When creating your FastMCP server, you can customize routing behavior by providing your own list of 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:
With these 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:
The default route maps are always applied after your custom maps, so you do not have to create route maps for every possible route.
To exclude routes from the MCP server, use a route map to assign them to MCPType.EXCLUDE
.
You can use this to remove sensitive or internal routes by targeting them specifically:
Or you can use a catch-all rule to exclude everything that your maps don’t handle explicitly:
Using a catch-all exclusion rule will prevent the default route mappings from being applied, since it will match every remaining route. This is useful if you want to explicitly allow-list certain routes.
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.
The 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.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.
You can add custom tags to components created from specific routes using the mcp_tags
parameter in RouteMap
. These tags will be applied to all components created from routes that match that particular route map.
You can add tags to all components by providing a tags
parameter when creating your FastMCP server with from_openapi
or from_fastapi
. These global tags will be applied to every component created from your OpenAPI specification.
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:
For more control over component names, you can provide an 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.
Any operationId
not found in mcp_names
will use the default strategy (operationId up to the first __
).
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.
Your mcp_component_fn
is expected to modify the component in-place, not to return a new component. The result of the function is ignored.
FastMCP intelligently handles different types of parameters in OpenAPI requests:
By default, FastMCP only includes query parameters that have non-empty values. Parameters with None
values or empty strings are automatically filtered out.
Path parameters are typically required by REST APIs. FastMCP:
None
valuesFastMCP handles array parameters according to OpenAPI specifications:
explode
parameter (default: True
)Header parameters are automatically converted to strings and included in the HTTP request.
If your API requires authentication, configure it on the HTTP client before creating the MCP server:
Set a timeout for all API requests:
New in version: 2.0.0
FastMCP can directly convert FastAPI applications into MCP servers by extracting their OpenAPI specifications:
FastMCP does not include FastAPI as a dependency; you must install it separately to use this integration.
Note that operation ids are optional, but are used to create component names. You can also provide custom names, just like with OpenAPI specs.
FastMCP servers are not FastAPI apps, even when created from one. To learn how to deploy them as an ASGI app, see the ASGI Integration documentation.
All OpenAPI integration features work with FastAPI apps: