python-sdk README

The project's README from github.com/modelcontextprotocol/python-sdk. As one markdown file: README.md.

MCP Python SDK

Python implementation of the Model Context Protocol (MCP)

PyPI MIT licensed Python Version Documentation Protocol Specification

[!NOTE] This is v2 of the MCP Python SDK, the current stable release line. It is a major rework of the SDK, both to support the 2026-07-28 MCP specification (and every earlier revision) and to fix long-standing architectural issues. Coming from v1? See What's new in v2 for the tour of what changed and the migration guide for every breaking change.

Not ready to migrate? v1.x lives on the v1.x branch, continues to receive critical bug fixes and security patches, and is documented at https://py.sdk.modelcontextprotocol.io/v1/. Since pip install mcp now installs 2.x, keep a <2 upper bound on your requirement (for example mcp>=1.28,<2) until you've migrated.

Something rough, confusing, or broken? Open an issue or find us in #python-sdk-dev on the MCP Contributors Discord.

Documentation

The documentation lives at https://py.sdk.modelcontextprotocol.io/.

It has a Get started guide, What's new in v2, the API reference, and the migration guide.

What is MCP?

The Model Context Protocol 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 designed for LLM interactions. With this SDK you can:

Requirements

Python 3.10+.

Installation

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

The cli extra adds the mcp command-line tool (mcp dev, mcp run, mcp install) on top of the SDK; install plain mcp if you don't need it. For one-off commands, uv run --with "mcp[cli]" mcp ... works without a project.

A server in 15 lines

Create a 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}!"

Full example: docs_src/index/tutorial001.py

That's a complete MCP server: one tool, one templated resource. Open it in the MCP Inspector:

uv run mcp dev server.py

Call add with a=1, b=2 and you get 3 back.

Notice what you did not write: no JSON Schema (a: int, b: int is the schema), no request parsing, no validation code, no protocol handling. Two type-hinted Python functions and a docstring.

Get started takes it from here.

A client in 10 lines

The same package is a full MCP client. Serve server.py over HTTP:

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

then point a Client at it:

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())

A URL means Streamable HTTP, the transport you deploy. Client can also launch a local server as a stdio subprocess or take any custom transport; Clients has the rest.

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 to get started.

License

This project is licensed under the MIT License. See the LICENSE file for details.

python-sdk (modelcontextprotocol/python-sdk)

Searching Database…

这是什么东西?Model Context Protocol(MCP)的官方 Python SDK——可以把它想象成一种 Web API,但专门为与大语言模型的交互而设计。它让你既能搭建把数据和功能暴露给大语言模型应用的服务器,也能编写连接任意 MCP 服务器的客户端。
那它是加到我代码里的库,还是一个独立的工具?它是一个可以通过 pip 安装的 Python 库(包名叫 mcp),不是独立应用——你需要在服务器或客户端代码里导入它。另外还有一个可选的 cli 扩展,会额外装一个 mcp 命令行工具,方便开发调试。
我怎样才能让第一个服务器跑起来?先执行 pip install 'mcp[cli]',再创建一个 server.py,写上带装饰器和类型注解的 Python 函数,然后运行即可——教程里的完整示例(一个工具加一个模板化资源)总共只有 15 行代码。
我需要哪个 Python 版本?只需要 Python 3.10 或更高版本。这个包发布在 PyPI 上,所以用普通的 pip 或 uv 安装就能搞定其余依赖。
我已经基于 v1 做了开发,升级到 v2 是不是得全部重写?v2 是一次大改版,包含破坏性变更,不过文档里为每一项变更都提供了迁移指南。如果你暂时不想升级,v1.x 在独立分支上维护,仍然会获得关键的 bug 修复和安全补丁。
服务器可以向大语言模型应用暴露哪些东西?工具(tools)、资源(resources,包括模板化资源)和提示(prompts)——任何 MCP 宿主都可以使用。教程示例就定义了一个工具和一个模板化资源。
开发过程中我怎么测试自己的服务器?运行 mcp dev 就会在 MCP Inspector 里打开它——教程里调用 add 并传入 a=1、b=2 会返回 3,全程不用写任何客户端代码。
不想建项目的话,能不能直接跑一次性命令?可以——用 uv run --with 'mcp[cli]' mcp ... 就能在没有项目的情况下运行。
v2 SDK 支持哪些 MCP 规范版本?支持 2026-07-28 规范以及之前所有版本。v2 就是围绕这个规范重构的,同时还修复了长期存在的架构问题。
Size
16.9 MB
Stars
24.6K
Age
2y
Commits · 90 Days