这是 Model Context Protocol 的官方 Python 实现,面向希望把数据和功能安全、标准化地暴露给大模型应用的开发者。借助它,你可以用带类型标注的普通 Python 函数定义工具、资源与提示词,框架会自动生成 JSON Schema、处理请求解析与参数校验,省去手写协议代码。同一个包也提供完整的客户端能力,能连接任意 MCP 服务器,并支持 stdio、Streamable HTTP 与 SSE 三种标准传输方式。附带的可选 CLI 提供 mcp dev、mcp run、mcp install 等命令,方便本地调试与安装服务器。要求 Python 3.11 以上,采用 MIT 许可。
pip install "mcp[cli]"Python implementation of the Model Context Protocol (MCP)
The documentation lives at .
It has a Get started guide, What's new in v2, the API reference, and the migration guide.
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:
Python 3.11+.
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.
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.
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.
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.
This project is licensed under the MIT License. See the LICENSE file for details.
内容由编辑整理,部分信息来自 GitHub 与项目官网,版权归原作者所有。发现错误?报告问题