Was this page helpful?
Claudeはツールや関数と連携する機能を備えており、Claudeの能力を拡張してより幅広いタスクを実行できます。
厳密なツール使用によるスキーマ準拠の保証
Structured Outputsは、ツール入力に対する保証されたスキーマバリデーションを提供します。ツール定義に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は2種類のツールをサポートしています:
クライアントツール:あなたのシステム上で実行されるツールで、以下を含みます:
サーバーツール:ウェブ検索やウェブフェッチツールなど、Anthropicのサーバー上で実行されるツール。これらのツールはAPIリクエストで指定する必要がありますが、あなた側での実装は不要です。
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)を使用するアプリケーションを構築している場合、MCPサーバーのツールをClaudeのMessages APIで直接使用できます。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メトリクスの一部として入力トークン数と出力トークン数の両方を出力します。
クックブックで、すぐに実装できるツール使用のコード例のリポジトリをご覧ください:
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 |
クライアントツールを活用してサポートを強化する、レスポンシブなカスタマーサービスボットを構築しましょう。