Skip to content

MCP Python SDK

You are viewing the in-development v2 documentation

For the current stable release, see the v1.x documentation.

The Model Context Protocol (MCP) lets applications provide context to LLMs in a standardized way, separating the concern of providing context from the LLM interaction itself.

This is the official Python SDK for it. With it you can:

  • Build MCP servers that expose tools, resources, and prompts to any MCP host.
  • Build MCP clients that connect to any MCP server.
  • Speak every standard transport: stdio, Streamable HTTP, and SSE.

Requirements

Python 3.10+.

Installation

uv add "mcp[cli]==2.0.0a3"
pip install "mcp[cli]==2.0.0a3"

The [cli] extra gives you the mcp command; you'll want it for development.

Pin the version while v2 is in alpha

Installers never select a pre-release unless you name one, so an unpinned uv add "mcp[cli]" gives you the latest v1.x release, which this documentation does not describe. Check PyPI for the newest alpha before you copy the line above. See Installation for the details.

Example

Create it

Create a file server.py:

server.py
from mcp.server import MCPServer

mcp = MCPServer("Demo")


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


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

That's a complete MCP server.

It exposes one tool, add, and one templated resource, greeting://{name}.

Run it

uv run mcp dev server.py

This starts your server and opens the MCP Inspector, an interactive UI for poking at it. Open the URL it prints.

Note

The Inspector is a Node.js app, so mcp dev needs npx on your PATH.

Try it

In the Inspector, go to Tools and call add with a=1, b=2.

You get 3 back. ✨

The Inspector built that form (a required integer field for a, another for b) from your type hints. So will Claude, and every other MCP host.

Now go to Resources and read greeting://World:

Hello, World!

Recap

Look again at what you did not write:

  • No JSON Schema. a: int, b: int is the schema.
  • No request parsing, no serialization, no validation code.
  • No protocol handling at all.

You wrote two Python functions with type hints and a docstring. The SDK does the rest.

Where to go next

  • The Tutorial walks through everything a server can do, one small step at a time.
  • Migrating from v1? Start with the Migration Guide.
  • Hunting for an exact signature? The API Reference is generated from the source.
  • Reading with an LLM? This documentation is also published in the llms.txt format: llms.txt is an index of the pages, and llms-full.txt contains every page in a single file.