Core APIs
Tool API
Complete reference for the Tool class
Tool class
The Tool
class provides a type-safe way to create tools that agents can use.
Constructor
Parameters
name
(required): Unique identifier for the tooldescription
(required): Clear description of what the tool doesversion
: Semantic version numberinputSchema
: Schema for input validation (Zod schema or JSON Schema)execute
(required): Function that performs the tool’s operationvalidate
: Custom validation function
Types
ToolResponse
ToolContext
JSONSchema
ValidationResult
Examples
Basic tool with JSON Schema
Tool with Zod schema
Tool with rich response format
Tool with custom validation
Best practices
- Type safety: Leverage TypeScript generics and Zod schemas for input validation
- Error handling: Use structured error responses with codes and details
- Schema validation: Use either JSON Schema or Zod for input validation
- Rich responses: Utilize text and image responses when appropriate
- Performance tracking: Monitor execution times and token usage
- Context utilization: Pass relevant context to tools for better integration
Next steps
- Learn about Agent API
- Explore provider APIs
- Read about middleware