Defining Tools
TypeScript tool patterns: The SDK offers two ways to define tools:
createFunctionTool- Simple function-based approach (shown above). Best for getting started.@tooldecorator with classes - Groups related tools in a class, extracted withextractTools(). Better for organizing many tools. See Class Pattern below.
Using Tools with Agents
Pass tools to the agent constructor:Tool Parameters
Simple Types
Tools can accept basic types directly:Complex Types with Schemas
For complex inputs, use structured schemas:Async Tools
Tools can be async for I/O operations:Tool Results
Tools return values directly. Errors are caught automatically and passed to the LLM:Tool Execution Context
Tools receive execution context with useful information:agent_name/agentName: Name of the executing agentiteration: Current loop iterationspan_id/spanId: Current trace span ID
Class Pattern (TypeScript)
For organizing many related tools, use the@tool decorator with classes:
Best Practices
- Write clear descriptions: The LLM uses descriptions to decide when to call tools
- Use specific names:
get_user_emailis better thanget_data - Validate inputs: Use schemas to catch errors early
- Handle errors gracefully: Return helpful error messages
- Keep tools focused: One tool should do one thing well