Build an MCP server in Python
Build a minimal MCP server in Python with one tool using the official MCP Python SDK, test it with MCP Inspector, and connect it to Claude Code. 9 min
Watch it done: build an MCP server in Python
Step 1 of 7
Create a new project with uv and move into it. If uv is not installed yet, install it from the MCP quickstart first and open a new terminal.
- Create a new project with uv and move into it. If uv is not installed yet, install it from the MCP quickstart first and open a new terminal. Type: uv init opening-hours && cd opening-hours.
- Create a virtual environment and switch it on, so the packages stay inside this project. Type: uv venv && source .venv/bin/activate.
- Install the official MCP Python SDK with its command line extras. Type: uv add "mcp[cli]".
- Replace the contents of server.py with this. The SDK builds the tool definition from the type hints and docstring, so the docstring is what Claude reads. Never use print() in a stdio server: it corrupts the protocol messages. Create server.py containing: from mcp.server import MCPServer from mcp.server.mcpserver.exceptions import ToolError mcp = MCPServer("opening-hours") HOURS = { "leeds": "Monday to Friday 9am to 5.30pm, Saturday 10am to 4pm", "york": "Monday to Saturday 9am to 5pm, closed Sunday", } @mcp.tool() async def get_opening_hours(branch: str) -> str: """Get the regular weekly opening hours for one of our branches. Args: branch: Branch name, for example Leeds or York """ key = branch.strip().lower() if key not in HOURS: raise ToolError(f"Unknown branch '{branch}'. Known branches: Leeds, York.") return HOURS[key] if __name__ == "__main__": mcp.run(transport="stdio")
- Test it with MCP Inspector, the official testing tool, before you involve Claude. It needs Node.js 22.23.4, 24.22.1 or 26.12.1 or later. Type: npx @modelcontextprotocol/inspector --cli uv run server.py --method tools/list. prints the tools your server offers: get_opening_hours, with branch as its one input.
- Add the server to Claude Code. Everything after -- is the command that starts your server, and the path must be absolute. Type: claude mcp add --transport stdio opening-hours -- uv --directory /absolute/path/to/opening-hours run server.py. Claude Code prints an Added line to confirm. Use the full path to your own project folder.
- Start claude, then ask a question your tool can answer. Run /mcp first if you want to check the server shows as connected. Type: When is the York branch open?. Claude calls get_opening_hours with York and answers from the hours your server returned.
Commands and code from the official MCP server quickstart, MCP Inspector docs and Claude Code MCP docs, checked 18 November 2026. Needs Python 3.10 or later, uv, and MCP Python SDK 2.0.0 or later. Lines marked Note describe what happens and are not real output.
Read
What you are building
An MCP server is a small program that tells Claude which tools it offers and runs them when asked. You will build one with a single tool, get_opening_hours, that answers "When is the York branch open?" from a list your business controls. Later you can swap the list for a database or an API.
The protocol changed in its 2026-07-28 version: MCP is now stateless, with no sessions at protocol level, and the official SDKs changed with it. The current quickstart needs Python 3.10 or later, uv, and version 2.0.0 or later of the MCP Python SDK, whose import names differ from older tutorials. Follow the code below rather than older blog posts (checked 18 November 2026).
uv init opening-hours cd opening-hours uv venv source .venv/bin/activate uv add "mcp[cli]"
uv init opening-hours cd opening-hours uv venv .venv\Scripts\activate uv add mcp[cli]
Lesson notes: Build an MCP server in Python
Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You have a working Python MCP server with one tool that Claude can call.
Step 1 of 5 · Read
What you are building
An MCP server is a small program that tells Claude which tools it offers and runs them when asked. You will build one with a single tool,
get_opening_hours, that answers "When is the York branch open?" from a list your business controls. Later you can swap the list for a database or an API.The protocol changed in its 2026-07-28 version: MCP is now stateless, with no sessions at protocol level, and the official SDKs changed with it. The current quickstart needs Python 3.10 or later,
uv, and version 2.0.0 or later of the MCP Python SDK, whose import names differ from older tutorials. Follow the code below rather than older blog posts (checked 18 November 2026).Set up (macOS or Linux) uv init opening-hours cd opening-hours uv venv source .venv/bin/activate uv add "mcp[cli]"
Set up (Windows PowerShell) uv init opening-hours cd opening-hours uv venv .venv\Scripts\activate uv add mcp[cli]
Step 2 of 5 · Read
The server, in one file
Save this as
server.py. The SDK reads the function's type hints and docstring to build the tool definition Claude sees, so the docstring is your tool description. Write it with care.Two details matter. Raising
ToolErrorsends Claude a clear error it can act on, here a list of valid branches. And because this server talks over stdio, it must never useprint(): anything written to standard output corrupts the protocol messages. Use theloggingmodule, which writes to standard error.server.py import logging from mcp.server import MCPServer from mcp.server.mcpserver.exceptions import ToolError logger = logging.getLogger(__name__) mcp = MCPServer("opening-hours") HOURS = { "leeds": "Monday to Friday 9am to 5.30pm, Saturday 10am to 4pm", "york": "Monday to Saturday 9am to 5pm, closed Sunday", } @mcp.tool() def get_opening_hours(branch: str) -> str: """Get the opening hours for one of our branches. Use this when someone asks when a branch is open. It returns regular weekly hours only, not bank holiday changes. Args: branch: Branch name, for example Leeds or York """ key = branch.strip().lower() logger.info("Looking up hours for %s", key) if key not in HOURS: known = ", ".join(sorted(HOURS)) raise ToolError(f"Unknown branch '{branch}'. Known branches: {known}.") return HOURS[key] if __name__ == "__main__": mcp.run(transport="stdio")Step 3 of 5 · Quiz
Check your understanding
Your stdio server works, then breaks as soon as you add print("got request") inside the tool. Why?
- Standard output carries the protocol messages, so printing to it corrupts them
- print() is too slow for MCP
- MCP tools must be async functions
Show the answer
Standard output carries the protocol messages, so printing to it corrupts them Over stdio, the client reads standard output as protocol messages. Log with the logging module, which writes to standard error.
Step 4 of 5 · Try it in the terminal
Test it with MCP Inspector
MCP Inspector is the official testing tool. Use its CLI mode to list your server's tools. Your server starts with: uv run server.py
Hint: npx @modelcontextprotocol/inspector --cli, then the command that starts your server, then --method tools/list.
Show the command
$ npx @modelcontextprotocol/inspector --cli uv run server.py --method tools/list { "tools": [ { "name": "get_opening_hours", "description": "Get the opening hours for one of our branches. ...", "inputSchema": { "type": "object", "properties": { "branch": { "type": "string" } }, "required": ["branch"] } } ] }Step 5 of 5 · Read
Try it by hand, then connect it to Claude
Without
--cli, the same Inspector command opens a web page where you can call the tool yourself and watch the raw messages. The Inspector needs a recent Node.js: 22.23.4, 24.22.1 or 26.12.1 or later (checked 18 November 2026).For Claude Code, add the server with stdio and an absolute path, run
/mcpto see it connected, then ask "When is the York branch open?". For the Claude desktop app, add the same command toclaude_desktop_config.jsonundermcpServersand restart the app. You may need the full path touv, whichwhich uv(macOS, Linux) orwhere uv(Windows) prints.Claude Code claude mcp add --transport stdio opening-hours \ -- uv --directory /absolute/path/to/opening-hours run server.py
claude_desktop_config.json { "mcpServers": { "opening-hours": { "command": "uv", "args": ["--directory", "/absolute/path/to/opening-hours", "run", "server.py"] } } }