附录 A.2 API 与命令速查表
本节摘要:本节是写代码时的速查入口,把高频 API 与命令压缩成可查的表。覆盖五个高频领域:服务端装饰器、客户端核心方法、传输参数、协议 mode 取值、CLI 命令。每张表都设计成「扫一眼就能找到用法」,供你编码时随手查阅。
一、服务端装饰器速查
| 装饰器 |
作用 |
关键参数 |
详见 |
@mcp.tool() |
注册工具 |
name(覆盖)、description、annotations(ToolAnnotations)、cache_hint、structured_output |
第 4 章 |
@mcp.resource(uri) |
注册资源/模板 |
URI(必填,带 {param} 是模板) |
第 5.1 节 |
@mcp.prompt() |
注册提示词 |
name、description |
第 5.3 节 |
@mcp.completion(ref, arg) |
注册补全 |
目标引用、参数名 |
第 5.4 节 |
@mcp.custom_route(path, method) |
注册自定义 HTTP 路由 |
path、method |
第 12.5 节 |
# 典型用法速查
@mcp.tool(annotations=ToolAnnotations(read_only=True))
def get_data(key: str) -> dict: ...
@mcp.resource("db://users/{user_id}")
def get_user(user_id: str) -> dict: ...
@mcp.prompt()
def summarize(text: str) -> str: ...
二、客户端核心方法速查
| 方法 |
作用 |
返回 |
详见 |
Client(server_or_url_or_transport) |
构造客户端 |
Client 对象 |
第 9.1 节 |
async with Client(...) as client |
打开连接 |
client 可用 |
第 9.2 节 |
await client.call_tool(name, args) |
调工具 |
CallToolResult(content, structured_content, is_error) |
第 9.3 节 |
await client.list_tools() |
列工具 |
list[Tool] |
第 9.3 节 |
await client.read_resource(uri) |
读资源 |
资源内容 |
第 9.3 节 |
await client.list_resources() |
列具体资源 |
list[Resource] |
第 9.3 节 |
await client.list_resource_templates() |
列资源模板 |
list[ResourceTemplate] |
第 9.3 节 |
await client.get_prompt(name, args) |
获取提示词 |
list[Message] |
第 9.3 节 |
await client.list_prompts() |
列提示词 |
list[Prompt] |
第 9.3 节 |
client.server_capabilities |
服务端能力声明 |
dict |
第 3.3 节 |
await client.subscribe(uri) |
订阅资源 |
(无) |
第 10.4 节 |
client.listen() |
监听事件 |
异步迭代器 |
第 10.4 节 |
# 典型用法速查
async with Client("http://localhost:8000/mcp") as client:
tools = await client.list_tools()
result = await client.call_tool("add", {"a": 1, "b": 2})
if result.is_error:
...
value = result.structured_content["result"]
三、传输参数速查
stdio 传输(StdioServerParameters)
| 参数 |
作用 |
默认/注意 |
command |
启动命令 |
要在 PATH |
args |
命令参数 |
第一个通常是文件 |
env |
环境变量 |
必须显式注入(白名单) |
流式 HTTP 传输
| 参数 |
作用 |
默认 |
| URL 或 host/port |
服务端地址 |
localhost:8000 |
streamable_http_path |
端点路径 |
/mcp |
json_response |
响应形态 |
False(SSE 流) |
stateless_http |
无状态模式 |
False |
headers(客户端) |
请求头 |
认证等 |
timeout(客户端) |
超时 |
(库默认) |
服务端启动
| 写法 |
作用 |
mcp.run() |
跑 stdio(默认) |
mcp.run("streamable-http") |
跑流式 HTTP |
mcp.run("sse") |
跑 SSE(不推荐) |
mcp.streamable_http_app() |
拿 ASGI app 嵌入 |
四、协议 mode 取值速查
| mode |
行为 |
适用 |
"auto"(默认) |
自动协商最高版本 |
绝大多数场景 |
"legacy" |
强制 initialize 握手 |
连老服务端 |
| 特定版本字符串 |
强制版本 |
测试 |
async with Client(url, mode="auto") as client: ... # 默认
async with Client(url, mode="legacy") as client: ... # 强制 legacy
async with Client(url, mode="2026-07-28") as client: ... # 强制现代
五、CLI 命令速查
需要 mcp[cli] 安装。在服务端文件所在目录执行:
| 命令 |
作用 |
详见 |
mcp dev server.py |
在检查器里跑服务端 |
第 1.4 节 |
mcp run server.py |
直接跑服务端 |
第 8 章 |
mcp install server.py |
注册到 Claude Desktop |
(官方文档) |
mcp version |
打印版本 |
第 1.1 节 |
# 典型用法
uv run mcp dev server.py # 用 uv 跑
mcp dev server.py # 直接跑
六、常用导入速查
# 服务端
from mcp.server import MCPServer
from mcp.server.mcpserver import Context, ToolAnnotations
from mcp.server import Server # 低层
from mcp.server.auth import AuthSettings # 认证
from mcp.server.extension import Extension, MethodBinding # 扩展
from mcp.shared.middleware import ServerMiddleware # 中间件
# 客户端
from mcp import Client
from mcp.client.stdio import stdio_client, StdioServerParameters
from mcp.client.streamable_http import streamable_http_client
from mcp.client.session_group import ClientSessionGroup
from mcp.client.auth import OAuthClientProvider
# 类型
from mcp import types
# Resolve 与依赖
from typing import Annotated
from mcp.shared import resolve # Resolve, Elicit
调工具返回的结构,常用字段:
| 字段 |
内容 |
给谁 |
content |
文本列表(给模型) |
模型继续对话 |
structured_content |
类型化数据(给应用) |
应用做后续逻辑 |
is_error |
是否出错 |
判断成功/失败 |
result = await client.call_tool("add", {"a": 1, "b": 2})
# result.content: [TextContent(type="text", text="3")]
# result.structured_content: {"result": 3} ← 标量被包一层
# result.is_error: False
本节要点回顾
- 五张速查表:服务端装饰器、客户端方法、传输参数、mode 取值、CLI 命令。
- 每张表「扫一眼就能找到用法」,设计成随手查阅。
- 常用导入速查列出高频导入路径,避免记不住。
- CallToolResult 字段速查:content(模型)、structured_content(应用)、is_error(判断)。
- 编码卡壳时查本节,比翻文档快。
速查表清楚了,最后一节是常见报错与排查,以及 v1→v2 迁移要点。