Integrate FastMCP with FastAPI applications
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.Copy
button to copy it for use with other code blocks):
app
.New in version: 2.0.0
One of the most common ways to bootstrap an MCP server is to generate it from an existing FastAPI application. FastMCP will expose your FastAPI endpoints as MCP components (tools, by default) in order to expose your API to LLM clients.
RouteMap
to map all GET requests to MCP resources, and all POST/PUT/DELETE requests to MCP tools:
httpx_client_kwargs
parameter. For example, to add authentication to your FastAPI app, you can pass a headers
dictionary to the httpx_client_kwargs
parameter:
New in version: 2.3.1
In addition to generating servers, FastMCP can facilitate adding MCP servers to your existing FastAPI application. You can do this by mounting the MCP ASGI application.
http_app
method on your FastMCP instance. This will return an ASGI application that can be mounted to your FastAPI application.
async with
statements - the inner context (MCP) will be initialized after the outer context (your app), and cleaned up before it. This maintains the correct initialization and cleanup order for all your resources.