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)

  1. Server side – You create an MCPServer instance, 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.
  2. 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.
  3. Client side – A Client object connects to a server URL (or stdio subprocess) and provides methods like call_tool(name, args) that return a response object with structured_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

License

MIT – free to use, modify, and distribute.


All details are taken directly from the repository’s README.

Related

  • Project
  • Project
  • Project
  • Project
  • Project