diff --git a/README.md b/README.md index 3822976..e1e8d1f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,14 @@ + +> [!NOTE] +> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。 +> [English](./README.en.md) · [原始项目](https://github.com/modelcontextprotocol/python-sdk) · [上游 README](https://github.com/modelcontextprotocol/python-sdk/blob/HEAD/README.md) +> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。 + # MCP Python SDK
-Python implementation of the Model Context Protocol (MCP) +Model Context Protocol(MCP)的 Python 实现 [![PyPI][pypi-badge]][pypi-url] [![MIT licensed][mit-badge]][mit-url] @@ -14,41 +20,41 @@
> [!CAUTION] -> **This README documents v2 of the MCP Python SDK — a pre-release (alpha/beta) line under active development. Do not use v2 in production.** Pre-releases are published to PyPI as `2.0.0aN` / `2.0.0bN`, and **each pre-release may contain breaking changes from the previous one**. Pin an exact version and expect to update your code when you bump the pin. +> **本 README 记录的是 MCP Python SDK v2 —— 一条正在积极开发的预发布(alpha/beta)分支。请勿在生产环境使用 v2。** 预发布版本会发布到 PyPI,包名为 `2.0.0aN` / `2.0.0bN`,且**每个预发布版本相对前一版都可能包含破坏性变更**。请固定精确版本,并在提升版本 pin 时预期需要更新代码。 > -> **v1.x is the only stable release line and remains recommended for production.** It lives on the [`v1.x` branch](https://github.com/modelcontextprotocol/python-sdk/tree/v1.x) and continues to receive critical bug fixes and security patches; see [the v1.x README](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/README.md) for its documentation. `pip` and `uv` don't select a pre-release unless you explicitly request one, so existing installs are unaffected. **If your package depends on `mcp`, add a `<2` upper bound to your version constraint (for example `mcp>=1.27,<2`) before the stable release lands.** +> **v1.x 是唯一稳定的发布分支,仍建议用于生产环境。** 它位于 [`v1.x` 分支](https://github.com/modelcontextprotocol/python-sdk/tree/v1.x),并继续接收关键 bug 修复和安全补丁;文档见 [v1.x README](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/README.md)。`pip` 和 `uv` 不会选择预发布版本,除非你显式请求,因此现有安装不受影响。**若你的包依赖 `mcp`,请在稳定版发布前,为版本约束添加上界 `<2`(例如 `mcp>=1.27,<2`)。** > -> v2 is a major rework of the SDK, both to support the [2026-07-28 MCP specification release](https://blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/) and to fix long-standing architectural issues. See [What's new in v2](https://py.sdk.modelcontextprotocol.io/v2/whats-new/) for the tour of what changed, and the [migration guide](https://py.sdk.modelcontextprotocol.io/v2/migration/) for every breaking change. Stable v2 is targeted for 2026-07-27, alongside the spec release. Try the pre-releases and [tell us what breaks](https://github.com/modelcontextprotocol/python-sdk/issues/new?template=v2-feedback.yaml), or discuss in [#python-sdk-dev on the MCP Contributors Discord](https://discord.gg/6CSzBmMkjX). +> v2 是对 SDK 的一次重大重构,既是为了支持 [2026-07-28 MCP 规范发布](https://blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/),也是为了修复长期存在的架构问题。变更概览见 [v2 新特性](https://py.sdk.modelcontextprotocol.io/v2/whats-new/),各项破坏性变更见 [迁移指南](https://py.sdk.modelcontextprotocol.io/v2/migration/)。稳定版 v2 计划于 2026-07-27 与规范同步发布。欢迎试用预发布版本,并[告诉我们哪里会出问题](https://github.com/modelcontextprotocol/python-sdk/issues/new?template=v2-feedback.yaml),,或在 [MCP Contributors Discord 的 #python-sdk-dev](https://discord.gg/6CSzBmMkjX). 频道讨论。 -## Documentation +## 文档 -**The documentation lives at .** +**文档地址:。** -It has a [Get started guide](https://py.sdk.modelcontextprotocol.io/v2/get-started/), [What's new in v2](https://py.sdk.modelcontextprotocol.io/v2/whats-new/), the [API reference](https://py.sdk.modelcontextprotocol.io/v2/api/mcp/), and the [migration guide](https://py.sdk.modelcontextprotocol.io/v2/migration/). +其中包含 [入门指南](https://py.sdk.modelcontextprotocol.io/v2/get-started/),、[v2 新特性](https://py.sdk.modelcontextprotocol.io/v2/whats-new/),、[API 参考](https://py.sdk.modelcontextprotocol.io/v2/api/mcp/),,以及 [迁移指南](https://py.sdk.modelcontextprotocol.io/v2/migration/).。 -## What is MCP? +## 什么是 MCP? -The [Model Context Protocol](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 designed for LLM interactions. With this SDK you can: +[Model Context Protocol](https://modelcontextprotocol.io)(MCP)让你能够以安全、标准化的方式构建向 LLM 应用暴露数据与功能的服务器。可以把它理解为面向 LLM 交互设计的 Web API。借助本 SDK,你可以: -- **Build MCP servers** that expose tools, resources, and prompts to any MCP host -- **Build MCP clients** that connect to any MCP server -- Speak every standard transport: stdio, Streamable HTTP, and SSE +- **构建 MCP 服务器**,向任意 MCP 主机暴露工具、资源和提示词 +- **构建 MCP 客户端**,连接任意 MCP 服务器 +- 支持所有标准传输方式:stdio、Streamable HTTP 和 SSE -## Requirements +## 环境要求 -Python 3.10+. +Python 3.10+。 -## Installation +## 安装 ```bash uv add "mcp[cli]==2.0.0b1" # or: pip install "mcp[cli]==2.0.0b1" ``` -The pin matters while v2 is in pre-release: an unpinned install resolves to the latest stable v1.x, which this README does not describe. Check [PyPI](https://pypi.org/project/mcp/#history) for the newest pre-release, and use `uv run --with "mcp==2.0.0b1"` for one-off commands. +在 v2 处于预发布阶段时,版本 pin 很重要:未固定版本的安装会解析到最新的稳定版 v1.x,而本 README 并不描述该版本。可在 [PyPI](https://pypi.org/project/mcp/#history) 查看最新预发布版本,一次性命令可使用 `uv run --with "mcp==2.0.0b1"`。 -## A server in 15 lines +## 15 行代码搭建一个服务器 -Create a `server.py`: +创建一个 `server.py`: ```python @@ -69,24 +75,24 @@ def greeting(name: str) -> str: return f"Hello, {name}!" ``` -_Full example: [docs_src/index/tutorial001.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/docs_src/index/tutorial001.py)_ +_完整示例:[docs_src/index/tutorial001.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/docs_src/index/tutorial001.py)_ -That's a complete MCP server: one tool, one templated resource. Open it in the [MCP Inspector](https://github.com/modelcontextprotocol/inspector): +这就是一个完整的 MCP 服务器:一个工具、一个模板化资源。在 [MCP Inspector](https://github.com/modelcontextprotocol/inspector): 中打开它。 ```bash uv run mcp dev server.py ``` -Call `add` with `a=1`, `b=2` and you get `3` back. +使用 `a=1` 和 `b=2` 调用 `add`,即可得到 `3`。 -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. +注意你**没有**编写的内容:没有 JSON Schema(`a: int, b: int` _就是_ schema)、没有请求解析、没有校验代码、没有协议处理。只有两个带类型注解的 Python 函数和一个 docstring。 -[Get started](https://py.sdk.modelcontextprotocol.io/v2/get-started/) takes it from here. +从这里继续阅读 [入门指南](https://py.sdk.modelcontextprotocol.io/v2/get-started/)。 -## A client in 10 lines +## 10 行代码搭建一个客户端 -The same package is a full MCP **client**. `Client` connects to a URL, a stdio subprocess, a custom transport, or (for tests) straight to a server object in memory with no transport at all: +同一个包也是完整的 MCP **客户端**。`Client` 可连接到 URL、stdio 子进程、自定义传输层,或(用于测试)直接连接内存中的服务器对象,完全无需传输层: ```python import asyncio @@ -105,15 +111,15 @@ async def main() -> None: asyncio.run(main()) ``` -Swap `mcp` for `"http://localhost:8000/mcp"` and the exact same code talks to a remote server. +将 `mcp` 替换为 `"http://localhost:8000/mcp"`,同样的代码即可与远程服务器通信。 -## 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](https://github.com/modelcontextprotocol/python-sdk/blob/main/CONTRIBUTING.md) to get started. +我们热忱欢迎各经验水平的贡献者参与,并期待你的加入。入门请阅读 [贡献指南](https://github.com/modelcontextprotocol/python-sdk/blob/main/CONTRIBUTING.md)。 -## License +## 许可证 -This project is licensed under the MIT License. See the [LICENSE](https://github.com/modelcontextprotocol/python-sdk/blob/main/LICENSE) file for details. +本项目采用 MIT License 授权。详见 [LICENSE](https://github.com/modelcontextprotocol/python-sdk/blob/main/LICENSE) 文件。 [pypi-badge]: https://img.shields.io/pypi/v/mcp.svg [pypi-url]: https://pypi.org/project/mcp/