From an existing API to MCP (FastAPI-MCP)
From an existing API to MCP: convert what you already have
Level: from scratch — no prior experience needed. In the previous lesson we built an MCP server from scratch with FastMCP. Now we look at the other path: you already have an API running and you want an agent to use it. Watch the video above and follow this guide at your own pace.
Tons of companies already have a REST API in production: a backend that answers requests over the internet (for example, one built with FastAPI, a very popular Python framework). The natural question is: "do I have to rewrite everything as an MCP server, or can I reuse what already works?" The good news: you can reuse it. In this lesson we convert an existing API into an MCP server with very few lines, using a library called FastAPI-MCP.
What you'll learn
- What a REST API is and what an endpoint is (we define them from scratch).
- How to take an existing FastAPI API and expose it as MCP tools.
- Why we serve it over Streamable HTTP (and not over stdio) so it can be remote.
- When it's worth converting an existing API and when to build from scratch.
First, two key words
- REST API: a standard way for two programs to talk over the internet. You send a request to a URL and it returns a response (usually in JSON format).
- Endpoint: each concrete "door" of that API. For example, an endpoint
POST /multiplythat receives two numbers and returns their product. An API has many endpoints, one for each thing it knows how to do.
Today's core idea: each endpoint of your API can become an MCP tool that the model can invoke. You don't rewrite the logic; you wrap it.
The starting point: an existing FastAPI API
Suppose you already have this small calculator as a REST API. Notice it has nothing to do with MCP yet — it's a normal API:
from fastapi import FastAPI
app = FastAPI()
@app.post("/multiply")
def multiply(a: float, b: float) -> float:
"""Multiply two numbers and return the result."""
return a * b
@app.post("/multiply") is a decorator: it tells FastAPI "when a POST request
arrives at /multiply, run this function." That is an endpoint.
Step 1 — Install the dependencies
pip install fastapi "fastapi-mcp>=0.3" uvicorn
- fastapi: the API framework (you may already have had it).
- fastapi-mcp: the library that converts the API into MCP. It's the star.
- uvicorn: the server that sets your app listening for requests over the network.
Pin the version. fastapi-mcp still evolves quickly: that's why we pin
>=0.3 (ideally pin an exact version in your project too). This keeps a
library change from breaking this lesson's code.
Step 2 — Mount MCP on the app (13 in the video)
Here's all the magic. With three lines we expose the existing endpoints as MCP tools:
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
app = FastAPI()
@app.post("/multiply")
def multiply(a: float, b: float) -> float:
"""Multiply two numbers and return the result."""
return a * b
# We convert the existing API into an MCP server:
mcp = FastApiMCP(app, name="calculator MCP")
mcp.mount() # exposes the API's endpoints as MCP tools
FastApiMCP(app, name="calculator MCP")takes your app as it is and builds an MCP layer on top of it. Thenameis how the server will present itself to the agent.mcp.mount()hooks that MCP server inside the same app. From here on, yourmultiplyis not just an HTTP endpoint: it's also a tool that a model can discover and call. The tools' names and descriptions come from your functions and their docstrings, so writing good descriptions is worth it.
Step 3 — Serve over Streamable HTTP (20 in the video)
Since this API lives on the network (others connect over the internet), we serve it with Streamable HTTP, the remote transport we saw in the transports lesson — not with stdio, which is for local tools. We bring it up with uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000
Your MCP server becomes available over HTTP for any compatible client to connect.
Watch out — deprecated transport: you'll see old tutorials that use the HTTP+SSE transport (from 2024). It's now deprecated. For anything new, use Streamable HTTP. It's exactly the rule from the transports lesson.
Verify the transport in your version. The mount/transport API of fastapi-mcp
changes between versions. Before taking anything for granted, confirm that the version you
installed serves over Streamable HTTP (and not over the deprecated SSE): check the README of
the version and test the connection in the Inspector.
Convert an API or build from scratch?
There's no single answer; it depends on where you stand:
- Convert with FastAPI-MCP when… you already have a REST API in FastAPI that's running and tested. You reuse the logic, gain speed, and avoid duplicating code. Ideal for bringing to MCP something that's already in production.
- Build from scratch with FastMCP (previous lesson) when… you start from zero, or you want fine control over each tool, resource, and prompt, or your tool wasn't a web API to begin with (for example, something local that runs over stdio).
Simple rule for beginners: does the API already exist? Convert it. Starting from scratch? Build it.
Key moments in the video
- 13 — FastAPI-MCP: how to take an existing API and mount MCP on top with
FastApiMCP(app)+mount(). It's the heart of this lesson; as you watch, keep in mind that you don't rewrite anything, you wrap what you already have. - 20 — Streamable HTTP: why we serve the MCP over HTTP (remote) and not over stdio. Connect this to the rule "is the server on my machine or on the network?".
Quick glossary
- REST API: a standard way for two programs to communicate over the internet via URLs.
- Endpoint: each "door" of an API (e.g.
POST /multiply); it becomes an MCP tool. - FastAPI: a Python framework for creating REST APIs quickly.
- FastAPI-MCP: a library that converts a FastAPI app into an MCP server.
mount(): the method that hooks the MCP server inside your existing app.- uvicorn: a server that sets your app listening for requests over the network.
- Streamable HTTP: MCP's network transport (spec 2025-03-26), for remote servers.
- stdio: a local transport (subprocess); not what we use here.
To download
- 📄 Commented code — From API to MCP with FastAPI-MCP (PDF: the complete app, the mount, and how to run it over HTTP, explained line by line to copy and paste).
Before you move on
If you take away one idea, let it be this: you don't always need to rewrite. If you already
have an API in FastAPI, with FastApiMCP(app) + mount() you convert it into an MCP server
and serve it over Streamable HTTP. In the next lesson we go deeper into how
agents discover and use those tools.
