modelcontextprotocol/python-sdk
The official Python SDK for Model Context Protocol servers and clients
What it is
MCP Python SDK – a Python library that implements the Model Context Protocol (MCP). MCP is a standardized way for large‑language‑model (LLM) applications to talk to external services (tools, resources, prompts) over a web‑API‑like protocol.
What you can do with it
- Write MCP servers in just a few lines of Python. By decorating type‑annotated functions you expose them as tools (callable actions) or resources (templated URLs) that LLMs can invoke.
- Write MCP clients that call those tools/resources over any supported transport (stdio, Streamable HTTP, Server‑Sent Events). The client handles schema generation, request/response encoding, and returns structured results.
- Use the bundled CLI (
mcp dev,mcp run,mcp install) for quick development, local testing, and installation of MCP packages.
How it works (high‑level)
- Server side – You create an
MCPServerinstance, decorate functions with@mcp.tool()or@mcp.resource(). The SDK extracts the Python type hints and docstrings to build JSON‑Schema descriptions automatically, then serves them via the chosen transport. - Transport layer – MCP supports three standard transports:
- stdio – useful for embedding a server in a subprocess.
- Streamable HTTP – a lightweight HTTP endpoint that streams messages.
- SSE – Server‑Sent Events for push‑style communication.
- Client side – A
Clientobject connects to a server URL (or stdio subprocess) and provides methods likecall_tool(name, args)that return a response object withstructured_content.
Who might use it
- LLM application developers who need a clean, typed interface for exposing custom tools (e.g., calculators, database look‑ups) to LLMs.
- Platform builders creating marketplaces of reusable LLM‑compatible services.
- Researchers prototyping new interaction patterns between LLMs and external code.
Getting started (quick example)
# 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:
return f"Hello, {name}!"
Run it locally:
uv run mcp dev server.py # starts a dev server (streamable‑http by default)
Client side:
import asyncio
from mcp import Client
async def main():
async with Client("http://localhost:8000/mcp") as client:
res = await client.call_tool("add", {"a": 1, "b": 2})
print(res.structured_content) # → {'result': 3}
asyncio.run(main())
Documentation & resources
- Full docs: https://py.sdk.modelcontextprotocol.io/
- Get‑started guide, API reference, migration guide for v1 → v2.
- Protocol spec: https://modelcontextprotocol.io/specification/latest
- CLI reference:
mcp dev,mcp run,mcp install.
License
MIT – free to use, modify, and distribute.
All details are taken directly from the repository’s README.
Related
- Project
- Project
- Project
- Project
- Project