Expert guidance for implementing Claude API tool use / function calling patterns. Covers client tools, server tools, MCP integration, structured outputs, and best practices for extending Claude's capabilities.
Expert guidance for implementing Claude API tool use (function calling) patterns based on official Anthropic documentation.
This skill helps you implement tool use with Claude, enabling Claude to interact with external functions and APIs. It covers:
1. **Client Tools**: Execute on your systems
- Custom tools you define and implement
- Anthropic-defined tools requiring client implementation (computer use, text editor)
- You handle execution and return results to Claude
2. **Server Tools**: Execute on Anthropic's servers
- Web search and web fetch tools
- Specified in API request but no implementation needed
- Results automatically incorporated into Claude's response
1. **Define tools** - Provide tool definitions with name, description, and input schema
2. **Claude decides** - Claude determines if a tool can help and constructs a tool_use request
3. **Execute tool** - Extract tool parameters, run your code, return results in tool_result block
4. **Claude responds** - Claude uses tool results to formulate final answer
1. **Define tools** - Include server tool definitions in your request
2. **Claude executes** - Claude automatically runs the server tool
3. **Claude responds** - Results are incorporated into Claude's response (no additional user interaction)
When a user needs to implement tool use:
**Read the user's requirements** to understand:
**Create a tool definition** with three required fields:
```json
{
"name": "tool_name",
"description": "Clear description of what the tool does and when to use it",
"input_schema": {
"type": "object",
"properties": {
"param_name": {
"type": "string",
"description": "Description of this parameter"
}
},
"required": ["param_name"]
}
}
```
**Best practices for tool definitions:**
**Construct the API request** with your tools:
```python
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's the weather in San Francisco?"}
]
)
```
**Check the response** for tool use:
**Parse the tool use request:**
```python
if response.stop_reason == "tool_use":
for content_block in response.content:
if content_block.type == "tool_use":
tool_name = content_block.name
tool_input = content_block.input
tool_use_id = content_block.id
# Execute your tool
result = execute_tool(tool_name, tool_input)
```
**Return results to Claude:**
```python
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=[...], # Same tools as before
messages=[
{"role": "user", "content": "What's the weather in San Francisco?"},
{"role": "assistant", "content": response.content}, # Claude's tool use request
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use_id,
"content": str(result)
}
]
}
]
)
```
**When to use strict mode:**
**Add strict: true to tool definition:**
```python
{
"name": "process_order",
"description": "Process a customer order",
"input_schema": {
"type": "object",
"properties": {
"order_id": {"type": "string"},
"amount": {"type": "number"},
"items": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["order_id", "amount", "items"]
},
"strict": true # Guarantees schema validation
}
```
**If user is working with MCP servers:**
```python
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
```
**When Claude requests a tool, execute it on the MCP server:**
```python
if content_block.type == "tool_use":
# Execute on MCP server
result = await mcp_session.call_tool(
content_block.name,
content_block.input
)
# Return result to Claude in tool_result block
```
**For web search tool:**
```python
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=[
{
"type": "web_search_20250305",
"name": "web_search",
"max_results": 5
}
],
messages=[
{"role": "user", "content": "Search for the latest AI news"}
]
)
```
**For web fetch tool:**
```python
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=[
{
"type": "web_fetch_20250305",
"name": "web_fetch"
}
],
messages=[
{"role": "user", "content": "Analyze the content at https://example.com"}
]
)
```
**Parallel tool use** - Claude may request multiple tools at once:
```python
for content_block in response.content:
if content_block.type == "tool_use":
# Execute each tool
results.append({
"type": "tool_result",
"tool_use_id": content_block.id,
"content": execute_tool(content_block.name, content_block.input)
})
```
**Sequential tool use** - Claude may chain tools:
**Calculate expected costs:**
**Optimize token usage:**
User asks a question → Claude uses one tool → Claude responds with answer
User asks complex question → Claude uses multiple tools simultaneously → Claude synthesizes results
User asks question → Claude uses tool A → Uses result to inform tool B → Uses both to answer
User asks vague question → Claude requests clarification → User provides details → Claude uses tools
Complex reasoning task → Claude thinks step-by-step → Uses tools at decision points → Explains reasoning
1. **Insufficient tool descriptions** - Claude needs clear guidance on when to use each tool
2. **Missing schema details** - Parameter descriptions help Claude provide correct inputs
3. **Not handling multi-turn** - Tool use often requires multiple API calls
4. **Forgetting tool_use_id** - Required to match results with requests
5. **Incomplete error handling** - Tools may fail; return error messages in tool_result
6. **Wrong tool choice setting** - Understand when to use auto/none/any/specific tool
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/claude-tool-use-expert/raw