
Build Your First MCP Server in 30 Lines: FastMCP Quickstart
Chris Harper
4 min read
Jun 26, 2026 · 20:05 UTC
TL;DR: FastMCP turns decorated Python functions into tools any MCP client — including Claude Code — can call; three decorators (@mcp.tool, @mcp.resource, @mcp.prompt) and 30 lines get you a working, testable server.
What you'll be able to do after this:
- Write a working MCP server in Python in under 30 lines using FastMCP and test it live in Claude Desktop
- Expose functions as tools (model-controlled actions), data as resources (context injection), and templates as prompts (user-triggered)
- Understand the three MCP primitives so you can design servers that fit the protocol rather than fight it
The three MCP primitives
MCP servers expose exactly three kinds of objects to a client. Getting this mental model right saves you from building the wrong thing:
| Primitive | Controlled by | Use for |
|---|---|---|
| Tool | The model | Actions — computations, external API calls, DB writes |
| Resource | The application | Context — read-only data injected into the conversation |
| Prompt | The user | Templates — structured interaction patterns triggerable from a UI |
Most tutorials only show tools. Tools are what Claude calls when it needs to do something. Resources are what the host application injects automatically as context. Prompts are reusable interaction templates your users can invoke directly.
Install
pip install fastmcp # or: uv add fastmcp
Requires Python 3.10+. FastMCP is the standard-recommended Python MCP library — it was originally a third-party framework that the official SDK incorporated, and around 70% of Python MCP servers in the wild use it.
A complete MCP server in 30 lines
from fastmcp import FastMCP
mcp = FastMCP("demo")
# Tool: model-controlled — Claude decides when to call this
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers and return their sum."""
return a + b
# Resource: application-controlled — injected as context
@mcp.resource("data://config/{key}")
def get_config(key: str) -> str:
"""Return a configuration value by key."""
config = {"env": "production", "region": "us-east-1"}
return config.get(key, "not found")
# Prompt: user-controlled — a reusable interaction template
@mcp.prompt()
def code_review(code: str, language: str = "Python") -> str:
"""A structured code review prompt."""
return f"Review this {language} code for correctness and style:\n\n```{language.lower()}\n{code}\n```"
if __name__ == "__main__":
mcp.run()
FastMCP reads your type annotations and docstrings to generate the JSON schema the protocol requires — no manual schema writing, no protocol boilerplate.
Connect to Claude Desktop
Add the server to ~/Library/Application Support/Claude/claude_desktop_config.json (macOS) or %APPDATA%\Claude\claude_desktop_config.json (Windows):
{
"mcpServers": {
"demo": {
"command": "python",
"args": ["/absolute/path/to/server.py"]
}
}
}
Restart Claude Desktop. The tool picker now lists add. Ask Claude to add two numbers and you'll see it call your function — you've closed the loop from natural language to your Python code.
Test without Claude using the MCP Inspector
The MCP Inspector gives you an interactive UI to test any server without a full client:
npx @modelcontextprotocol/inspector python server.py
Opens a local browser UI listing every tool, resource, and prompt. Call each, inspect the raw protocol messages, and iterate without restarting Claude Desktop.
What a real server looks like
Real servers follow the same three-decorator pattern — the only difference is the backing logic:
@mcp.tool()for anything that does work: database writes, external API calls, file operations, computations@mcp.resource()for anything that injects context: user profiles, config, docs, search results@mcp.prompt()for standardized workflows you want users to trigger consistently
See the full FastMCP docs for authentication, streaming responses, OAuth 2.1, and deploying as a remote HTTP server. The official MCP Python SDK covers the lower-level protocol API if you need to go beyond what FastMCP provides.
Sources: Build an MCP server — modelcontextprotocol.io | FastMCP docs — gofastmcp.com | MCP Python SDK — GitHub | How to Build Your First MCP Server — freeCodeCamp