Was this page helpful?
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 使用伺服器端工具結果來制定回應
如果您正在建構使用 Model Context Protocol (MCP) 的應用程式,您可以直接在 Claude 的 Messages API 中使用來自 MCP 伺服器的工具。MCP 工具定義使用與 Claude 工具格式類似的結構描述格式。您只需要將 inputSchema 重新命名為 input_schema。
不想自己建構 MCP 客戶端? 使用 MCP 連接器從 Messages API 直接連接到遠端 MCP 伺服器,無需實作客戶端。
當您建構 MCP 客戶端並在 MCP 伺服器上呼叫 list_tools() 時,您會收到包含 inputSchema 欄位的工具定義。要將這些工具與 Claude 搭配使用,請將它們轉換為 Claude 的格式:
然後將這些轉換後的工具傳遞給 Claude:
當 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 |
These token counts are added to your normal input and output tokens to calculate the total cost of a request.
請參閱我們的模型概覽表以了解目前各模型的價格。
當您發送工具使用提示時,就像任何其他 API 請求一樣,回應會在報告的 usage 指標中輸出輸入和輸出的 token 計數。
在我們的 cookbook 中探索可直接實作的工具使用程式碼範例:
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_toolsimport 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?"}]
)| 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 |
建立一個響應式客戶服務機器人,利用客戶端工具來增強支援服務。