Build an MCP server with Python
FastMCP is the easiest way to build an MCP server in Python. You decorate functions and the framework handles schemas, transport, and routing.
Prerequisites
- +Python 3.10+
- +uv or pip
- +An MCP client (Claude Code, Claude Desktop)
Step-by-Step
- 1
Install FastMCP
FastMCP is included in the official mcp Python SDK. Use uv for the snappiest install.
uv add 'mcp[cli]' - 2
Create the server
Decorate functions with @mcp.tool. The framework introspects type hints and docstrings to build the schema.
from mcp.server.fastmcp import FastMCP mcp = FastMCP('weather') @mcp.tool() def get_weather(city: str) -> str: """Return current weather for the given city.""" return f'It is 72F in {city}' - 3
Add a resource
Resources are read-only context the agent can pull. Great for config, recent logs, or canned data.
@mcp.resource('config://app') def config() -> str: return open('config.yaml').read() - 4
Run it
The CLI provides stdio and HTTP transports. Use stdio for local clients.
uv run mcp run server.py - 5
Inspect with the dev tool
mcp dev launches the inspector against your server.
uv run mcp dev server.py - 6
Connect from a client
Add the server to your client config and reload.
{ "mcpServers": { "weather": { "command": "uv", "args": ["run", "mcp", "run", "/abs/path/server.py"] } } }
Common Pitfalls
- !Print statements break stdio transport. Use the logging module to stderr.
- !Missing docstrings produce empty tool descriptions and confuse the agent.
- !Heavy imports at module top slow startup.
Agent Hub
Run, monitor, and orchestrate your agent fleet from one dashboard. Built for multi-agent teams.
What's Next
- ->Add @mcp.prompt for reusable templates.
- ->Deploy via SSE or HTTP transport for remote use.
