Tool use with Claude
Claude 工具使用
Claude is capable of interacting with tools and functions, allowing you to extend Claude's capabilities to perform a wider variety of tasks.
Claude 能夠與工具和函式互動,讓你可以擴展 Claude 的能力來執行更多種類的任務。
How tool use works
工具使用如何運作
Claude supports two types of tools:
Claude 支援兩種類型的工具:
- Client tools: Tools that execute on your systems, including user-defined custom tools and Anthropic-defined tools like computer use and text editor.
- Server tools: Tools that execute on Anthropic's servers, like web search and web fetch. These don't require implementation on your part.
- 客戶端工具:在你的系統上執行的工具,包括使用者自定義的工具和 Anthropic 定義的工具如電腦使用和文字編輯器。
- 伺服器端工具:在 Anthropic 伺服器上執行的工具,如網頁搜尋和網頁抓取。這些不需要你實作。
Client tools workflow
客戶端工具流程
- Step 1: Provide Claude with tools and a user prompt - Define tools with names, descriptions, and input schemas.
- Step 2: Claude decides to use a tool - Claude assesses if any tools can help and constructs a tool use request.
- Step 3: Execute the tool and return results - Extract the tool name and input, execute the tool, and return results.
- Step 4: Claude formulates a response - Claude analyzes the tool results to craft its final response.
- 步驟 1:提供工具和使用者提示給 Claude - 定義工具的名稱、描述和輸入結構。
- 步驟 2:Claude 決定使用工具 - Claude 評估是否有工具可以幫忙,並建構工具使用請求。
- 步驟 3:執行工具並回傳結果 - 提取工具名稱和輸入,執行工具,然後回傳結果。
- 步驟 4:Claude 生成回應 - Claude 分析工具結果來生成最終回應。
Single tool example
單一工具範例
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
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?"}]
)
print(response)
Pricing
定價
Tool use requests are priced based on: (1) The total number of input tokens sent, (2) The number of output tokens generated, (3) For server-side tools, additional usage-based pricing.
工具使用請求的計費基於:(1) 發送的總輸入 token 數,(2) 生成的輸出 token 數,(3) 對於伺服器端工具,額外的使用量計費。
Using MCP tools with Claude
使用 MCP 工具與 Claude
If you're building an application that uses the Model Context Protocol (MCP), you can use tools from MCP servers directly with Claude's Messages API. Just rename inputSchema to input_schema.
如果你正在建構使用 Model Context Protocol (MCP) 的應用程式,你可以直接在 Claude 的 Messages API 中使用 MCP 伺服器的工具。只需將 inputSchema 重新命名為 input_schema。