Skip to main content

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 tool
  • description (required): Clear description of what the tool does
  • version: Semantic version number
  • inputSchema: Schema for input validation (Zod schema or JSON Schema)
  • execute (required): Function that performs the tool’s operation
  • validate: 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

  1. Type safety: Leverage TypeScript generics and Zod schemas for input validation
  2. Error handling: Use structured error responses with codes and details
  3. Schema validation: Use either JSON Schema or Zod for input validation
  4. Rich responses: Utilize text and image responses when appropriate
  5. Performance tracking: Monitor execution times and token usage
  6. Context utilization: Pass relevant context to tools for better integration

Next steps