# Building an MCP Server with FastAPI and FastMCP
Source: https://speakeasy.com/mcp/framework-guides/building-fastapi-server

If you want to make your FastAPI application work with Claude, Cursor, and other AI agents, you need an MCP server. But building one from scratch is tedious when you already have routes and schemas in place.

FastMCP and Speakeasy can both generate an MCP server directly from an existing API without rewriting logic or duplicating schemas, making your backend instantly agent-ready.

This article shows you how to build an MCP server for a FastAPI application using either [FastMCP](https://gofastmcp.com/getting-started/welcome) or [Speakeasy](https://www.speakeasy.com/), and compares the tools in terms of ease of setup, visibility into generated code, and the long-term maintainability of your MCP server.

## Setting up the example project

We'll build an MCP server with each tool using the [APItizing Burgers example project](https://github.com/speakeasy-api/examples/tree/main/framework-fastapi).

Start by cloning the project with the following command:

```bash
git clone https://github.com/speakeasy-api/examples
cd examples/framework-fastapi
```

To make the setup easier, we'll use the `uv` environment manager. You can use any Python environment creation tool you're familiar with.

```bash
uv venv
source .venv/bin/activate
```

Next, install the required dependencies for the project. The `apitizing-burger` project is already set up with FastAPI, so install the FastAPI package and its dependencies:

```bash
uv pip install "fastapi[all]"
```

Now run the API:

```bash
uvicorn app.main:server --reload
```

It's a good idea to have [Claude Desktop](https://claude.ai/download) installed on your machine to test the MCP servers.

## Building an MCP server with FastAPI and FastMCP

FastMCP is a Python package that provides a high-level implementation of the MCP Python SDK. You can use FastMCP to quickly build an MCP server without worrying about low-level implementation details, like managing component lifecycles, defining tools, handling resources, or configuring prompts. Through its [FastAPI integration](https://gofastmcp.com/servers/openapi#fastapi-integration), FastMCP automatically transforms your routes into MCP tools.

### 1. Installing FastMCP

First, install the [FastMCP package](https://github.com/jlowin/fastmcp?tab=readme-ov-file#installation):

```bash
uv pip install fastmcp
```

### 2. Creating the MCP server file

Create a file in the `app` directory called `mcp_server.py`:

```bash
cd app/ && touch mcp_server.py
```

### 3. Creating the MCP server

Add the code below to the `mcp_server.py` file:

```python
from fastmcp import FastMCP
from app.main import server