Tools are how AI agents interact with the world. A well-designed tool is the difference between an agent that works and one that hallucinates, fails silently, or costs 10x more tokens than necessary. This skill covers tool design from schema to error handling. JSON Schema best practices, description writing that actually helps the LLM, validation, and the emerging MCP standard that's becoming the lingua franca for AI tools. Key insight: Tool descriptions are more important than tool implementa
Tools are how AI agents interact with the world. A well-designed tool is the
difference between an agent that works and one that hallucinates, fails
silently, or costs 10x more tokens than necessary.
This skill covers tool design from schema to error handling. JSON Schema
best practices, description writing that actually helps the LLM, validation,
and the emerging MCP standard that's becoming the lingua franca for AI tools.
Key insight: Tool descriptions are more important than tool implementations.
The LLM never sees your code - it only sees the schema and description.
Principles
Description quality > implementation quality for LLM accuracy
Aim for fewer than 20 tools - more causes confusion
GOOD - Comprehensive:
{
"name": "get_stock_price",
"description": "Retrieves the current stock price for a given ticker
symbol. The ticker symbol must be a valid symbol for a publicly
traded company on a major US stock exchange like NYSE or NASDAQ.
Returns the latest trade price in USD. Use when the user asks
about current or recent stock prices. Does NOT provide historical
data, company info, or predictions.",
"input_schema": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
}
},
"required": ["ticker"]
}
}
"""
2. Parameter Descriptions
"""
Every parameter needs:
What it is
Format expected
Example value
Edge cases/limitations
{
"location": {
"type": "string",
"description": "City and state/country. Format: 'City, State' for US
(e.g., 'San Francisco, CA') or 'City, Country' for international
(e.g., 'Tokyo, Japan'). Do not use ZIP codes or coordinates."
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit. Defaults to user's locale if not
specified. Use 'fahrenheit' for US users, 'celsius' for others."
}
}
"""
- Show minimal, partial, and full specification patterns
- Keep concise: 1-5 examples per tool
- Focus on ambiguous cases
Tool Error Handling
Returning errors that help the LLM recover
When to use: Any tool that can fail
ERROR HANDLING BEST PRACTICES:
Return Informative Errors
"""
BAD:
{"error": "Failed"}
{"error": true}
GOOD:
{
"error": true,
"error_type": "not_found",
"message": "Location 'Atlantis' not found in weather database.
Please provide a real city name like 'San Francisco, CA'.",
"suggestions": ["San Francisco, CA", "Los Angeles, CA"]
}
"""
Anthropic Tool Result with Error
"""
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "Error: Location 'Atlantis' not found in weather database.
Please provide a real city name like 'San Francisco, CA'.",
"is_error": true
}
"""
Error Categories to Handle
"""
Input Validation Errors
Missing required parameters
Invalid format
Out of range values
External Service Errors
API unavailable
Rate limited
Timeout
Business Logic Errors
Resource not found
Permission denied
Conflict/duplicate
Internal Errors
Unexpected exceptions
Data corruption
"""
Implementation Pattern
"""
from dataclasses import dataclass
from typing import Union
def get_weather(location: str) -> ToolResult:
# Validate input
if not location or len(location) < 2:
return ToolResult(
success=False,
content="Location must be at least 2 characters",
error_type="validation_error"
)
try:
data = weather_api.fetch(location)
return ToolResult(
success=True,
content=f"Temperature: {data.temp}°F, Conditions: {data.conditions}"
)
except LocationNotFound:
return ToolResult(
success=False,
content=f"Location '{location}' not found",
error_type="not_found",
suggestions=weather_api.suggest_locations(location)
)
except RateLimitError:
return ToolResult(
success=False,
content="Weather service rate limit exceeded. Try again in 60 seconds.",
error_type="rate_limit"
)
except Exception as e:
return ToolResult(
success=False,
content=f"Unexpected error: {str(e)}",
error_type="internal_error"
)
"""
MCP Tool Pattern
Building tools using Model Context Protocol
When to use: Creating reusable, cross-platform tools
MCP TOOL IMPLEMENTATION:
"""
MCP (Model Context Protocol) is Anthropic's open standard for
connecting AI agents to external systems. Build once, use everywhere.
"""
Basic MCP Server (TypeScript)
"""
import { Server } from "@modelcontextprotocol/sdk/server";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio";
const server = new Server({
name: "weather-server",
version: "1.0.0"
});
// Define tools
server.setRequestHandler("tools/list", async () => ({
tools: [
{
name: "get_weather",
description: "Get current weather for a location. Returns
temperature, conditions, and humidity. Use for weather
queries about specific cities.",
inputSchema: {
type: "object",
properties: {
location: {
type: "string",
description: "City and state, e.g. 'San Francisco, CA'"
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
default: "fahrenheit"
}
},
required: ["location"]
}
}
]
}));
// Start server
const transport = new StdioServerTransport();
await server.connect(transport);
"""
MCP Benefits
"""
Universal compatibility across LLM providers
Reusable tool libraries
Streaming and SSE transport support
Built-in observability
Tool access controls
"""
Tool Runner Pattern
Using SDK tool runners for automatic handling
When to use: Building tool loops without manual management
TOOL RUNNER (Anthropic SDK Beta):
"""
The tool runner handles the tool call loop automatically:
Executes tools when Claude calls them
Manages conversation state
Handles error retries
Provides streaming support
"""
Python Example
"""
import anthropic
from anthropic import beta_tool
client = anthropic.Anthropic()
@beta_tool
def get_weather(location: str, unit: str = "fahrenheit") -> str:
'''Get the current weather in a given location.
Args:
location: The city and state, e.g. San Francisco, CA
unit: Temperature unit, either 'celsius' or 'fahrenheit'
'''
# Implementation
return json.dumps({"temperature": "72°F", "conditions": "Sunny"})
@beta_tool
def search_web(query: str) -> str:
'''Search the web for information.
"""
Add to system prompt:
"For maximum efficiency, whenever you need to perform multiple
independent operations, invoke all relevant tools simultaneously
rather than sequentially."
"""