How to Build an MCP Server with Python

The official MCP Python SDK lets you create a useful server with ordinary typed Python functions. The SDK derives tool schemas from type hints and docstrings, handles protocol messages, and gives you both local and HTTP deployment paths.

Install the SDK

uv add "mcp[cli]"
# or
pip install "mcp[cli]"

The current v2 docs require Python 3.10+. The CLI extra gives you mcp dev, which launches the server with MCP Inspector for development.

Create a minimal server

from mcp.server import MCPServer

mcp = MCPServer("Demo")

@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two integers."""
    return a + b

@mcp.resource("greeting://{name}")
def greeting(name: str) -> str:
    """Return a greeting resource."""
    return f"Hello, {name}!"

if __name__ == "__main__":
    mcp.run()

By default, run() uses stdio, which is ideal when a host launches your server as a subprocess.

Run it with Inspector

uv run mcp dev server.py

MCP Inspector can list the tool, build an input form from the generated schema, invoke it, and inspect resource output.

What the SDK did for you

Core idea: build business capabilities first. Transport and protocol plumbing should be delegated to the SDK unless you genuinely need a low-level server.

References