服务器端执行的工具共享以下机制:server_tool_use 块、pause_turn 续接、混合服务器和客户端工具的轮次、"Zero Data Retention"(零数据保留),即 ZDR 的资格,以及域名过滤。有关各个工具的信息,请参阅工具参考。
当服务器端执行的工具运行时,server_tool_use 块会出现在 Claude 的响应中。其 id 字段使用 srvtoolu_ 前缀,以区别于客户端工具调用:
{
"type": "server_tool_use",
"id": "srvtoolu_01A2B3C4D5E6F7G8H9",
"name": "web_search",
"input": { "query": "latest quantum computing breakthroughs" }
}API 在内部执行该工具。您可以在响应中看到调用及其结果,但您不需要处理执行过程。与客户端 tool_use 块不同,您无需以 tool_result 进行响应。该工具的结果块(例如,网络搜索的 web_search_tool_result)在同一个助手轮次中紧跟在 server_tool_use 块之后,通过 tool_use_id 配对。如果 Claude 同时调用了您的某个客户端工具,则 server_tool_use 块会在没有其结果的情况下出现,并且响应以 stop_reason: "tool_use" 结束。当您在下一个请求中返回客户端 tool_result 块时,API 会运行该工具。
当使用诸如网络搜索之类的服务器工具时,API 会在服务器端的代理循环中执行工具调用。在长时间运行的轮次中,API 可能会暂停该循环并返回 pause_turn 停止原因。
以下是处理 pause_turn 停止原因的方法:
client = anthropic.Anthropic()
# 带有网络搜索的初始请求
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Search for comprehensive information about quantum computing breakthroughs in 2025",
}
],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 10}],
)
# 检查响应的停止原因是否为 pause_turn
if response.stop_reason == "pause_turn":
# 使用暂停的内容继续对话
messages = [
{
"role": "user",
"content": "Search for comprehensive information about quantum computing breakthroughs in 2025",
},
{"role": "assistant", "content": response.content},
]
# 发送继续请求
continuation = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=messages,
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 10}],
)
print(continuation)
else:
print(response)处理 pause_turn 时:
server_tool_use 块结束,如果续接请求中缺少该工具,API 会返回验证错误。stop_reason 并继续,直到获得不同的停止原因,并像处理任何重试循环一样限制续接次数的上限。有关其他 stop_reason 值和一般处理模式,请参阅停止原因和回退。
Claude 可以在同一组并行工具调用中同时调用服务器工具和客户端工具,例如,web_fetch 与用户定义的工具一起调用。客户端工具是指由您的代码执行并产生 tool_use 块的任何工具,无论它是用户定义的工具还是 Anthropic 模式的客户端工具(例如 Bash 工具)。发生这种情况时,API 不会运行服务器工具。它会立即返回,以便您可以先运行客户端工具:
stop_reason 是 "tool_use",而不是 "pause_turn"。content 包含 server_tool_use 块和客户端 tool_use 块,但没有服务器工具的结果块:该调用尚未完成。id 没有匹配结果块的 server_tool_use 块来检测该状态。来自 MCP 连接器的 mcp_tool_use 块的行为方式相同。在同一响应中已经有其结果块的服务器工具调用已完成,不需要您做任何事情。{
"stop_reason": "tool_use",
"content": [
{
"type": "text",
"text": "I'll fetch the article and check your system at the same time."
},
{
"type": "server_tool_use",
"id": "srvtoolu_01HxbWnMRmbWyMfUtJKC45rA",
"name": "web_fetch",
"input": { "url": "https://example.com/article" }
},
{
"type": "tool_use",
"id": "toolu_01PjgRJLbXrXEMZwDNYLnBqk",
"name": "run_command",
"input": { "command": "uname -a" }
}
]
}要继续该轮次,请运行客户端工具并发送一条用户消息,其内容仅包含 tool_result 块,该响应中的每个 tool_use 块对应一个。保持相同的 tools 数组:如果恢复请求不再定义正在等待的服务器工具,则会失败并返回 400 错误,其消息以 but no `web_fetch` tool was provided 结尾。
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01PjgRJLbXrXEMZwDNYLnBqk",
"content": "Linux demo-host 6.8.0-52-generic x86_64 GNU/Linux"
}
]
}API 会将您的结果附加到仍处于打开状态的助手轮次,运行被推迟的服务器工具(对于暂停的代码执行,则恢复它),然后让 Claude 继续。对于 Claude 直接调用的服务器工具,下一个响应以回应上一个响应的 server_tool_use id 的结果块开始,随后是新生成的内容和一个新的 stop_reason:
{
"stop_reason": "end_turn",
"content": [
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_01HxbWnMRmbWyMfUtJKC45rA",
"content": {
"type": "web_fetch_result",
"url": "https://example.com/article",
"content": {
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "Full text content of the article..."
}
}
}
},
{
"type": "text",
"text": "The article argues that... and your machine is running Linux..."
}
]
}server_tool_use 块及其结果块通过 tool_use_id 配对,而不是通过位置:在此流程中,它们出现在两个不同的响应中,并且 server_tool_use 块不会在第二个响应中重复。在后续请求中,请按顺序将整个交互保留在您的 messages 数组中:第一个响应作为 assistant 消息,tool_result 用户消息,然后下一个响应作为另一个 assistant 消息,就像您累积任何其他工具使用交互一样。
这与 pause_turn 的区别: pause_turn 响应也可能以一个尚未运行的 server_tool_use 块结束,但它绝不会留下一个等待您处理的客户端 tool_use 块,因此您通过原样重新发送助手内容来继续它。留下等待您处理的客户端 tool_use 块的响应,其 stop_reason 绝不会是 pause_turn:当 Claude 停下来调用您的工具时,stop_reason 是 tool_use,您通过发送客户端 tool_result 块来继续它,而不是重新发送响应。在这两种情况下,API 都会在下一个请求开始时运行待处理的服务器工具。
以下示例同时启用了网络获取和用户定义的 run_command 工具,并处理混合响应:
client = anthropic.Anthropic()
tools = [
{"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 5},
{
"name": "run_command",
"description": "Run a shell command on this computer and return its output.",
"input_schema": {
"type": "object",
"properties": {
"command": {"type": "string", "description": "The command to run"}
},
"required": ["command"],
},
},
]
messages = [
{
"role": "user",
"content": "Summarize https://example.com/article and run uname -a to tell me what system this is on.",
}
]
response = client.messages.create(
model="claude-opus-4-8", max_tokens=1024, tools=tools, messages=messages
)
tool_results = [
{
"type": "tool_result",
"tool_use_id": block.id,
# 在此处运行您的工具。此示例返回一个固定字符串。
"content": "Linux demo-host 6.8.0-52-generic x86_64 GNU/Linux",
}
for block in response.content
if block.type == "tool_use"
]
if response.stop_reason == "tool_use" and tool_results:
# 此响应中没有对应结果块的 server_tool_use 块尚未完成;其结果将在后续响应中返回。
# 仅发送回客户端的 tool_result 块,并使用相同的工具。
continuation = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=tools,
messages=[
*messages,
{"role": "assistant", "content": response.content},
{"role": "user", "content": tool_results},
],
)
# 如果某个 web_fetch 被推迟,它将在此请求中运行,并且其
# web_fetch_tool_result 是 continuation.content 的第一个块。
print(continuation)
else:
print(response)当 Claude 没有混合这两种调用时,这段代码同样是正确的。只有客户端 tool_use 块的轮次走相同的续接路径,而只有服务器工具调用的轮次不需要您提供客户端 tool_result 块:其结果块通常已经存在,而以挂起状态返回的轮次(例如 pause_turn 响应)则应原样重新发送。
网络搜索(web_search_20250305)和网络获取(web_fetch_20250910)的基础版本符合零数据保留(ZDR)的资格。
带有动态过滤的 _20260209 及更高版本默认不符合 ZDR 资格,因为动态过滤在内部依赖于代码执行。
要在 ZDR 下使用 _20260209 或更高版本的服务器工具,请通过在工具上设置 "allowed_callers": ["direct"] 来禁用动态过滤:
{
"type": "web_search_20260209",
"name": "web_search",
"allowed_callers": ["direct"]
}这会将工具限制为仅直接调用,从而绕过内部的代码执行步骤。
allowed_callers 控制工具的调用方式:由 Claude 直接调用("direct")、从代码执行容器内部调用(例如 "code_execution_20260120"),或两者兼有。网络工具的 _20260209 版本默认仅使用代码执行调用方;较早的版本默认为 ["direct"]。在不支持程序化工具调用的模型上,这些版本需要 allowed_callers: ["direct"];如果没有它,API 会返回一个验证错误,提示需要设置它。
访问网络的服务器工具接受 allowed_domains 和 blocked_domains 参数,以控制 Claude 可以访问哪些域名。两者都是工具对象上的字段:
{
"type": "web_search_20250305",
"name": "web_search",
"allowed_domains": ["example.com", "docs.python.org"]
}使用域名过滤器时:
example.com 而不是 https://example.com)。example.com 涵盖 docs.example.com)。docs.example.com 仅返回该子域名的结果,而不返回 example.com 或 api.example.com 的结果)。example.com/blog 匹配 example.com/blog/post-1)。allowed_domains 或 blocked_domains,但不能在同一请求中同时使用两者。通配符支持:
*),只能在其后的路径中使用。example.com/*、example.com/*/articles*.example.com、ex*.com无效的域名格式会在请求时被拒绝,并返回 400 invalid_request_error。
网络搜索和网络获取的 _20260209 及更高版本在内部使用代码执行来对搜索结果应用动态过滤器。
服务器工具事件作为正常的"server-sent events"(服务器发送事件),即 SSE 流程的一部分进行流式传输。Claude 直接调用的 server_tool_use 块的流式传输方式与客户端 tool_use 块相同:一个 content_block_start 事件,后跟 input_json_delta 事件。结果块在单个 content_block_start 事件中完整到达,没有增量。
有关完整的事件参考,请参阅流式传输。各个工具页面会记录工具特定的事件名称(如有不同)。
所有服务器工具都支持批量处理。在批量处理中,代理循环的运行方式与同步请求相同,但每轮迭代限制更高。如果循环达到该限制,响应将以 stop_reason: "pause_turn" 结束;您可以通过提交包含返回内容的后续请求来继续它。有关详细信息,请参阅服务器工具和代理循环。
常见的批量工作负载包括使用来自网络的信息丰富数据集、根据当前来源检查大量文档,以及对许多文件运行分析代码。
使用从症状到修复的诊断表修复最常见的工具使用错误。
搜索网络并引用结果。
从特定 URL 获取并读取内容,以使用实时网络内容增强 Claude 的上下文。
在沙盒容器中运行 Python 和 bash 代码,以分析数据、生成文件并迭代解决方案。
按需发现和加载工具。
Was this page helpful?