How to Build an MCP Server in Python and Connect It to Claude Code
Claude Code is powerful out of the box, but it's blind to your world. It can read files and run commands, but it can't query your database, hit your internal API, or look up an order in your ERP — until you give it tools
Claude Code is powerful out of the box, but it's blind to your world. It can read files and run commands, but it can't query your database, hit your internal API, or look up an order in your ERP — until you give it tools.
MCP (Model Context Protocol) is how you do that. Think of it as a USB-C port for AI agents: a standard way to expose any function in your codebase as a tool an agent can call.
Here's the path from zero to "Claude Code can now talk to my backend", in Python.
What MCP actually is
An MCP server is just a small program that exposes a list of tools (functions with descriptions). Claude Code, Cursor, and other MCP clients connect to it and can call those tools when they decide it helps.
Three concepts, that's it:
Server — your program, exposing tools
Client — Claude Code / Cursor, calling tools
Tools — named functions with docstrings
The minimal server (Python)
Install the official SDK:
pip install mcp
Then a server in about 20 lines:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("my-backend-tools")
@mcp.tool()
def get_order_status(order_id: str) -> str:
"""Look up an order's status by its ID."""
# Replace with a real DB query or API call
orders = {"1001": "shipped", "1002": "packing"}
return orders.get(order_id, "not found")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run()
The docstring is the important part. Claude Code reads it to decide when to use the tool. "Look up an order's status by its ID" is what makes it actually get called — write it like you're explaining the tool to a coworker.
Wire it into Claude Code
Claude Code reads .mcp.json from your project root:
{
"mcpServers": {
"my-backend-tools": {
"command": "python",
"args": ["server.py"]
}
}
}
Restart Claude Code. Now when you ask "what's the status of order 1002", it can call your tool instead of telling you it can't.
That's the whole local setup — under 15 minutes once you've done it once.
From "works on my machine" to "your team uses it"
The local stdio setup above is fine for you alone. But the moment another person (or another machine) needs the same tools, a process running on your laptop doesn't scale.
That's when you expose the MCP server over HTTP and deploy it — same FastMCP server, but run it as a service on Fly.io (or any host) and point clients at the URL. Now your whole team's Claude Code / Cursor instances share one source of truth, and you can add tools, auth, and monitoring in one place.
This is the version people actually pay for: a deployed, multi-tool MCP server with auth and logging, not a script on a dev's laptop.
Build it yourself, or hand it off
If you need one tool for yourself, the 20-line script above is all you need — go build it.
If you need a multi-tool MCP server deployed for a team (FastAPI backend, auth, monitoring, wired into Claude Code and Cursor), that's a bigger build. I do exactly this as a service: ai agents, mcp servers and claude code automation
I run a 28-tool MCP server in production myself, so this is the thing I do daily, not a tutorial I'm paraphrasing.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.