
Build Your First MCP Server in 20 Lines of Python with FastMCP
Chris Harper
3 min read
Aug 7, 2026 · 12:03 UTC
Turn any Python function into an MCP tool Claude can call — FastMCP's decorator API eliminates all the protocol boilerplate so you ship a working server in under 20 lines.
What you'll be able to do after this:
- Write a Python function and expose it as an MCP tool that Claude Code discovers and calls automatically
- Add read-only resources (data the model can browse) and reusable prompt templates alongside your tools
- Connect the server to Claude Code locally (stdio) or run it remotely over HTTP for team-shared tooling
Why build your own MCP server?
Every capability Claude Code reaches for — file reads, shell commands, web search — is served by an MCP server. Writing your own gives Claude direct, type-safe access to your internal APIs, databases, or proprietary tooling, with inputs validated and documentation auto-generated from Python type hints.
A complete server in 20 lines
pip install fastmcp
from fastmcp import FastMCP
mcp = FastMCP("my-tools")
@mcp.tool
def get_issue(repo: str, number: int) -> dict:
"""Fetch a GitHub issue by repo and number."""
import httpx
r = httpx.get(f"https://api.github.com/repos/{repo}/issues/{number}")
return r.json()
@mcp.resource("config://defaults")
def defaults() -> str:
"""Return project-wide defaults."""
return "timeout=30\nmax_retries=3"
if __name__ == "__main__":
mcp.run() # stdio by default; pass transport="http" for remote
The @mcp.tool decorator reads the function's type hints to build the JSON Schema and uses the docstring as the tool description. No separate schema file, no serialization code.
Wire it into Claude Code
Add the server to .claude/settings.json:
{
"mcpServers": {
"my-tools": {
"command": "python",
"args": ["/path/to/server.py"]
}
}
}
Claude Code discovers get_issue on the next session start. For a remote (HTTP) server shared across a team:
mcp.run(transport="http", host="0.0.0.0", port=8000)
then update the settings to "type": "http", "url": "http://your-server:8000/mcp".
The three MCP primitives
FastMCP covers all three things an MCP server can expose:
| Primitive | Decorator | What it does |
|---|---|---|
| Tool | @mcp.tool | Executable function the model can call |
| Resource | @mcp.resource("uri://…") | Read-only data the model can browse |
| Prompt | @mcp.prompt | Reusable instruction template in Claude Code's prompt picker |
The FastMCP quickstart builds all three in a single file — a good first project to clone and extend.
Sources: FastMCP quickstart — gofastmcp.com · Official MCP build-server guide — modelcontextprotocol.io · How to Build Your First MCP Server — freeCodeCamp · FastMCP on GitHub — PrefectHQ/fastmcp