# Create an MCP server from your FastAPI app
mcp = FastMCP.from_fastapi(app=server)

if __name__ == "__main__":
  mcp.run()
```

This code first registers your FastAPI app with FastMCP. Then, FastMCP inspects the app, finds the declared routes in `app/main.py`, and turns each route into an MCP tool without needing any additional configuration.

### 4. Adding the MCP server to Claude desktop

There is no need to write the configuration to add the MCP server to Claude Desktop. Navigate to the `app` directory and run the following command:

```bash
fastmcp install mcp_server.py
```

FastMCP will return output similar to below, confirming that your server has been installed in Claude.

![FastMCP Installing MCP server](/assets/mcp/building-fastapi-server/fastmcp-installing-mcp-server.png)

However, the MCP server won't work yet because the configuration is missing the `fastapi[all]` dependency and the Python path environment variable. Let's fix that now.

In Claude Desktop, go to **Settings -> Developer -> Edit config in Claude Desktop** and open the `claude-desktop-config.json` file. Update the generated configuration in `claude-desktop-config.json` to match the following:

```json
{
  "mcpServers": {
    "mcp_server": {
      "command": "uv",
      "args": [
        "run",
        "--with",
        "fastmcp",
        "--with",
        "fastapi[all]",
        "fastmcp",
        "run",
        "path/to/project/fastmcp-speakeasy-project/base/app/mcp_server.py"
      ],
      "env": {
        "PYTHONPATH": "path/to/project/fastmcp-speakeasy-project/base/"
      }
    }
  }
}
```

This updated configuration adds the `fastapi[all]` dependency and the `PYTHONPATH` environment variable.

The integration of FastAPI and FastMCP is now complete, and you can test the MCP server in Claude Desktop.

### Testing the integration

In Claude Desktop, click **Search and tools**.

![Clicking on the search and tools button](/assets/mcp/building-fastapi-server/mcp-server-search-and-tools-button.png)

Your configured MCP servers will appear in the menu that opens.

![Selecting the MCP server](/assets/mcp/building-fastapi-server/mcp-server-fastmcp-claude.png)

Your MCP server will appear as `mcp_server`. Click on it to see the tools that have been added.

![Listing the tools](/assets/mcp/building-fastapi-server/mcp-server-fastmcp-claude-listing-tools.png)

These tools correspond to your FastAPI routes and can now be used by Claude.

Now test the MCP server by asking Claude to create a burger. As shown in the screenshot below, Claude successfully calls your API to create a burger.

![Creating a burger](/assets/mcp/building-fastapi-server/claude-creating-a-burger.png)

### More configuration with FastMCP

FastMCP gives you some flexibility in how tools are exposed. You can customize the server name, set timeout values, rename tools, and control which endpoints are included or excluded.

For example, the code below renames the server, overrides a tool name, and excludes the route for deleting a burger:

```python
mcp = FastMCP.from_fastapi(
    app=server,
    name="Apitizer MCP Server",
    timeout=5.0,
    mcp_names={"createBurger": "Create a burger menu"},
    route_maps=[
        # Exclude delete burger route
        RouteMap(methods="DELETE", pattern=r".*", mcp_type=MCPType.EXCLUDE, tags={"burger"}),
    ],
  )
```

### Considerations for FastMCP in FastAPI

FastMCP avoids code duplication by reusing your existing FastAPI endpoints. Typing and schema definitions are preserved, including inheritance and Pydantic validations. This preservation also extends to dependencies, middleware, and authentication, all of which carry over into the MCP layer.

FastMCP behaves like a black box. You get speed and simplicity, but not much visibility into how tools, prompts, or resources are constructed. In contrast, when you use the MCP Python SDK directly, you build things manually but retain full control. You can interact with the component lifecycle, adjust behavior, and trace execution more easily.

Without visibility, debugging tool behavior can be difficult when something breaks or doesn't work as expected.

## Building an MCP server with FastAPI and Speakeasy

[Speakeasy](https://www.speakeasy.com/) generates SDKs in multiple languages, documentation, Terraform providers, and MCP servers from OpenAPI documents. Speakeasy's support for [generating MCP servers](https://www.speakeasy.com/docs/standalone-mcp/build-server) is currently limited to TypeScript SDKs, with additional language support coming soon. All you need is an OpenAPI document and a [Speakeasy account](https://www.speakeasy.com/docs/introduction#sign-up).

### 1. Installing the Speakeasy CLI

First, install the [Speakeasy CLI](https://www.speakeasy.com/docs/introduction#install-the-speakeasy-cli) on your machine.

On macOS or Linux:

```bash