Claude 能够与工具和函数进行交互,使您可以扩展 Claude 的能力来执行更广泛的任务。
通过严格工具使用保证模式一致性
结构化输出为工具输入提供有保证的模式验证。在您的工具定义中添加 strict: true,以确保 Claude 的工具调用始终与您的模式完全匹配——不再有类型不匹配或缺失字段的问题。
非常适合无效工具参数会导致故障的生产环境代理。了解何时使用严格工具使用 →
以下是如何使用 Messages API 向 Claude 提供工具的示例:
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-4-6",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
],
"messages": [
{
"role": "user",
"content": "What is the weather like in San Francisco?"
}
]
}'Claude 支持两种类型的工具:
Anthropic 定义的工具使用版本化类型(例如 web_search_20250305、text_editor_20250124)以确保跨模型版本的兼容性。
通过以下步骤将客户端工具与 Claude 集成:
向 Claude 提供工具和用户提示
Claude 决定使用工具
stop_reason 为 tool_use,表示 Claude 的意图。执行工具并返回结果
tool_result 内容块的新 user 消息中返回结果Claude 使用工具结果生成响应
注意:步骤 3 和 4 是可选的。对于某些工作流程,Claude 的工具使用请求(步骤 2)可能就是您所需要的全部,无需将结果发送回 Claude。
服务端工具遵循不同的工作流程:
向 Claude 提供工具和用户提示
Claude 执行服务端工具
Claude 使用服务端工具结果生成响应
如果您正在构建使用模型上下文协议 (MCP) 的应用程序,您可以直接在 Claude 的 Messages API 中使用来自 MCP 服务器的工具。MCP 工具定义使用与 Claude 工具格式类似的模式格式。您只需要将 inputSchema 重命名为 input_schema。
不想自己构建 MCP 客户端? 使用 MCP 连接器直接从 Messages API 连接到远程 MCP 服务器,无需实现客户端。
当您构建 MCP 客户端并在 MCP 服务器上调用 list_tools() 时,您将收到带有 inputSchema 字段的工具定义。要在 Claude 中使用这些工具,请将它们转换为 Claude 的格式:
from mcp import ClientSession
async def get_claude_tools(mcp_session: ClientSession):
"""Convert MCP tools to Claude's tool format."""
mcp_tools = await mcp_session.list_tools()
claude_tools = []
for tool in mcp_tools.tools:
claude_tools.append({
"name": tool.name,
"description": tool.description or "",
"input_schema": tool.inputSchema # Rename inputSchema to input_schema
})
return claude_tools然后将这些转换后的工具传递给 Claude:
import anthropic
client = anthropic.Anthropic()
claude_tools = await get_claude_tools(mcp_session)
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
tools=claude_tools,
messages=[{"role": "user", "content": "What tools do you have available?"}]
)当 Claude 响应 tool_use 块时,使用 call_tool() 在您的 MCP 服务器上执行该工具,并在 tool_result 块中将结果返回给 Claude。
有关构建 MCP 客户端的完整指南,请参阅构建 MCP 客户端。
以下是一些演示各种工具使用模式和技术的代码示例。为简洁起见,这些工具都是简单工具,工具描述也比确保最佳性能所需的理想长度要短。
Tool use requests are priced based on:
tools parameter)Client-side tools are priced the same as any other Claude API request, while server-side tools may incur additional charges based on their specific usage.
The additional tokens from tool use come from:
tools parameter in API requests (tool names, descriptions, and schemas)tool_use content blocks in API requests and responsestool_result content blocks in API requestsWhen you use tools, we also automatically include a special system prompt for the model which enables tool use. The number of tool use tokens required for each model are listed below (excluding the additional tokens listed above). Note that the table assumes at least 1 tool is provided. If no tools are provided, then a tool choice of none uses 0 additional system prompt tokens.
| Model | Tool choice | Tool use system prompt token count |
|---|---|---|
| Claude Opus 4.6 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4.1 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 4 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 3.7 (deprecated) | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Haiku 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Haiku 3.5 | auto, noneany, tool | 264 tokens 340 tokens |
| Claude Opus 3 (deprecated) | auto, noneany, tool | 530 tokens 281 tokens |
| Claude Sonnet 3 | auto, noneany, tool | 159 tokens 235 tokens |
| Claude Haiku 3 | auto, noneany, tool | 264 tokens 340 tokens |
These token counts are added to your normal input and output tokens to calculate the total cost of a request.
请参阅我们的模型概览表了解当前各模型的价格。
当您发送工具使用提示时,与任何其他 API 请求一样,响应将在报告的 usage 指标中输出输入和输出的 token 计数。
在我们的 cookbook 中探索可直接实现的工具使用代码示例:
了解如何将简单的计算器工具与 Claude 集成,以实现精确的数值计算。
构建一个响应式客户服务机器人,利用客户端工具来增强支持服务。
了解 Claude 和工具使用如何从非结构化文本中提取结构化数据。
Was this page helpful?