# MCP Python SDK

> The official Python SDK for Model Context Protocol servers and clients

- Category: Agents & Automation / Agent Frameworks & Builders
- Language: Python
- Licence: MIT
- Repository: https://github.com/modelcontextprotocol/python-sdk
- Website: https://py.sdk.modelcontextprotocol.io/
- Canonical page: https://ai-tldr.dev/tools/mcp-python-sdk/
- Updated: 2026-09-07

## In short

MCP Python SDK is the official Python library for the Model Context Protocol, the open standard that lets an LLM application discover and call capabilities hosted outside itself. It sits in the Agent Frameworks & Builders category of the Agents & Automation tools and is open-source, written in Python and licensed MIT.

## Overview

MCP Python SDK is the official Python library for the Model Context Protocol, the open standard that lets an LLM application discover and call capabilities hosted outside itself. One package covers both sides of the wire: you can write a server that publishes tools, resources and prompts, and you can write a client that connects to any MCP server and calls them.

The server side is decorator-driven. You annotate ordinary type-hinted Python functions with `@mcp.tool()` or `@mcp.resource()`, and the SDK derives the JSON Schema, the wire format and the protocol handshake from the signature and docstring. There is no schema file to maintain by hand and no JSON-RPC plumbing to write, which is what makes a working server fit in about fifteen lines.

Transport is a deployment choice rather than a rewrite. The same server runs over stdio for a local subprocess, or over Streamable HTTP when you want to host it as a network service; SSE is also supported. The SDK ships an OAuth layer for the HTTP transports, with issuer validation and per-server token-resource checks, so an exposed server can require real authorization instead of trusting whoever connects.

## What it does

- Define tools, resources and prompts from type-hinted Python functions using decorators — schemas are derived, not hand-written
- One package for both servers (`MCPServer`) and clients (`Client`)
- Transports for stdio, Streamable HTTP and SSE, chosen at run time rather than in code
- OAuth support for HTTP transports, including authorization-server issuer validation and token-resource restriction
- Streamable HTTP session controls: configurable idle timeout and a ceiling on concurrent sessions
- A `mcp` CLI (via the `cli` extra) for developing and running servers locally

## Getting started

Install the package with the CLI extra, write a server as decorated Python functions, then run it locally or serve it over Streamable HTTP and connect a client.

1. Install the SDK

   The `cli` extra adds the `mcp` command-line tools; plain `mcp` works without them. Python 3.10 or newer is required.

```bash
uv add "mcp[cli]"      # or: pip install "mcp[cli]"
```

2. Write a minimal server

   Save this as server.py. The decorators turn each function into an MCP tool or resource; the type hints and docstring become its schema and description.

```python
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}!"
```

3. Run it in the development inspector

   This starts the server and opens the MCP development tooling against it, so you can call the tool by hand before wiring up a client.

```bash
uv run mcp dev server.py
```

4. Serve it over HTTP and call it from a client

   Start the same server on the Streamable HTTP transport, then connect with the SDK's client. A URL means Streamable HTTP — the transport you deploy.

```bash
uv run mcp run server.py --transport streamable-http
```

5. Connect a client

   The client is in the same package. Opening it as an async context manager handles the handshake and tears the session down on exit.

```python
import asyncio
from mcp import Client

async def main() -> None:
    async with Client("http://localhost:8000/mcp") as client:
        result = await client.call_tool("add", {"a": 1, "b": 2})
        print(result.structured_content)  # {'result': 3}

asyncio.run(main())
```

## When to use it

- Expose an internal API or database as MCP tools that Claude, ChatGPT or a coding agent can call
- Host an MCP server as a network service over Streamable HTTP with OAuth in front of it
- Write a Python client that connects to third-party MCP servers and invokes their tools programmatically
- Ship a local stdio server that an agent launches as a subprocess, with no network exposure

## Version history

- 2026-09-07 v2.2.0: Streamable HTTP sessions now expire after 30 minutes idle and a server holds at most 10,000 at once, both configurable through new `session_idle_timeout=` and `max_sessions=` settings. HTTP client redirects are constrained to the endpoint's own origin, and OAuth issuer validation was extended to the legacy discovery path. The same changes shipped as 1.30.0 on the maintenance 1.x line.
