Creativity · MCP — pattern
Building an MCP Server in Python (Tutorial)
This tutorial walks through building a Model Context Protocol server from scratch in Python. We use the official `mcp` Python SDK — specifically its FastMCP helper — to stand up a server that exposes one tool and one resource over stdio. By the end you can plug it into Claude Desktop, see it show up in the tools drawer, and call it from a chat. Total code length: under 30 lines. Total reading time: about 10 minutes.
MCP facts
- Kind
- pattern
- Ecosystem
- anthropic-mcp
- Language
- Python
- Transports
- stdio
Capabilities
- Uses FastMCP for decorator-based tool and resource definitions
- stdio transport for Claude Desktop compatibility
- Supports async handlers, type hints translate to tool schemas
Install
pip install "mcp[cli]" Configuration
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("weather-demo")
@mcp.tool()
def get_weather(city: str) -> str:
"""Return a mock weather report for a city."""
return f"Weather in {city}: 24 C, partly cloudy"
@mcp.resource("weather://last-city")
def last_city() -> str:
return "Delhi"
if __name__ == "__main__":
mcp.run() # stdio by default Frequently asked questions
Why FastMCP over the low-level server?
FastMCP handles schema generation from type hints, lifecycle plumbing, and the async event loop. You can drop to the lower-level Server class if you need custom transport or advanced handshake behavior.
How do I test the server without Claude Desktop?
Use the MCP Inspector (`npx -y @modelcontextprotocol/inspector`). Point it at your `python server.py` command and you get a browser UI for calling tools interactively.
How do I add it to Claude Desktop?
Add an entry to claude_desktop_config.json under mcpServers with command set to `python` and args to the absolute path of your script. Restart Claude Desktop and the tool appears.
Sources
- MCP Python SDK — accessed 2026-04-20
- MCP Quickstart — accessed 2026-04-20