# MCP Python SDK
Python implementation of the Model Context Protocol (MCP) [![PyPI][pypi-badge]][pypi-url] [![MIT licensed][mit-badge]][mit-url] [![Python Version][python-badge]][python-url] [![Documentation][docs-badge]][docs-url] [![Protocol][protocol-badge]][protocol-url] [![Specification][spec-badge]][spec-url]
## Table of Contents - [MCP Python SDK](#mcp-python-sdk) - [Overview](#overview) - [Installation](#installation) - [Quickstart](#quickstart) - [What is MCP?](#what-is-mcp) - [Documentation](#documentation) - [Contributing](#contributing) - [License](#license) [pypi-badge]: https://img.shields.io/pypi/v/mcp.svg [pypi-url]: https://pypi.org/project/mcp/ [mit-badge]: https://img.shields.io/pypi/l/mcp.svg [mit-url]: https://github.com/modelcontextprotocol/python-sdk/blob/main/LICENSE [python-badge]: https://img.shields.io/pypi/pyversions/mcp.svg [python-url]: https://www.python.org/downloads/ [docs-badge]: https://img.shields.io/badge/docs-python--sdk-blue.svg [docs-url]: https://modelcontextprotocol.github.io/python-sdk/ [protocol-badge]: https://img.shields.io/badge/protocol-modelcontextprotocol.io-blue.svg [protocol-url]: https://modelcontextprotocol.io [spec-badge]: https://img.shields.io/badge/spec-spec.modelcontextprotocol.io-blue.svg [spec-url]: https://modelcontextprotocol.io/specification/latest ## Overview The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This Python SDK implements the full MCP specification, making it easy to: - Build MCP clients that can connect to any MCP server - Create MCP servers that expose resources, prompts and tools - Use standard transports like stdio, SSE, and Streamable HTTP - Handle all MCP protocol messages and lifecycle events ## Installation ### Adding MCP to your python project We recommend using [uv](https://docs.astral.sh/uv/) to manage your Python projects. If you haven't created a uv-managed project yet, create one: ```bash uv init mcp-server-demo cd mcp-server-demo ``` Then add MCP to your project dependencies: ```bash uv add "mcp[cli]" ``` Alternatively, for projects using pip for dependencies: ```bash pip install "mcp[cli]" ``` ### Running the standalone MCP development tools To run the mcp command with uv: ```bash uv run mcp ``` ## Quickstart Let's create a simple MCP server that exposes a calculator tool and some data: ```python """ FastMCP quickstart example. Run from the repository root: uv run examples/snippets/servers/fastmcp_quickstart.py """ from mcp.server.fastmcp import FastMCP # Create an MCP server mcp = FastMCP("Demo", json_response=True) # Add an addition tool @mcp.tool() def add(a: int, b: int) -> int: """Add two numbers""" return a + b # Add a dynamic greeting resource @mcp.resource("greeting://{name}") def get_greeting(name: str) -> str: """Get a personalized greeting""" return f"Hello, {name}!" # Add a prompt @mcp.prompt() def greet_user(name: str, style: str = "friendly") -> str: """Generate a greeting prompt""" styles = { "friendly": "Please write a warm, friendly greeting", "formal": "Please write a formal, professional greeting", "casual": "Please write a casual, relaxed greeting", } return f"{styles.get(style, styles['friendly'])} for someone named {name}." # Run with streamable HTTP transport if __name__ == "__main__": mcp.run(transport="streamable-http") ``` _Full example: [examples/snippets/servers/fastmcp_quickstart.py](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/fastmcp_quickstart.py)_ You can install this server in [Claude Code](https://docs.claude.com/en/docs/claude-code/mcp) and interact with it right away. First, run the server: ```bash uv run --with mcp examples/snippets/servers/fastmcp_quickstart.py ``` Then add it to Claude Code: ```bash claude mcp add --transport http my-server http://localhost:8000/mcp ``` Alternatively, you can test it with the MCP Inspector. Start the server as above, then in a separate terminal: ```bash npx -y @modelcontextprotocol/inspector ``` In the inspector UI, connect to `http://localhost:8000/mcp`. ## What is MCP? The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) lets you build servers that expose data and functionality to LLM applications in a secure, standardized way. Think of it like a web API, but specifically designed for LLM interactions. MCP servers can: - Expose data through **Resources** (think of these sort of like GET endpoints; they are used to load information into the LLM's context) - Provide functionality through **Tools** (sort of like POST endpoints; they are used to execute code or otherwise produce a side effect) - Define interaction patterns through **Prompts** (reusable templates for LLM interactions) - And more! ## Documentation - [Building Servers](docs/server.md) -- tools, resources, prompts, logging, completions, sampling, elicitation, transports, ASGI mounting - [Writing Clients](docs/client.md) -- connecting to servers, using tools/resources/prompts, display utilities - [Authorization](docs/authorization.md) -- OAuth 2.1, token verification, client authentication - [Low-Level Server](docs/low-level-server.md) -- direct handler registration for advanced use cases - [Protocol Features](docs/protocol.md) -- MCP primitives, server capabilities - [Testing](docs/testing.md) -- in-memory transport testing with pytest - [API Reference](https://modelcontextprotocol.github.io/python-sdk/api/) - [Experimental Features (Tasks)](https://modelcontextprotocol.github.io/python-sdk/experimental/tasks/) - [Model Context Protocol documentation](https://modelcontextprotocol.io) - [Model Context Protocol specification](https://modelcontextprotocol.io/specification/latest) - [Officially supported servers](https://github.com/modelcontextprotocol/servers) ## Contributing We are passionate about supporting contributors of all levels of experience and would love to see you get involved in the project. See the [contributing guide](CONTRIBUTING.md) to get started. ## License This project is licensed under the MIT License - see the LICENSE file for details.