Class AgentTool<TInput, TOutput>Abstract

Base class for tools that AI agents can use.

Tools are validated functions that agents can call to perform specific actions like querying databases, searching knowledge bases, or creating resources.

Note: When using with schema validation libraries like Zod, implement the validation in the concrete tool class.

class SearchKnowledgeBaseTool extends AgentTool<
{ query: string },
{ articles: Article[] }
> {
readonly name = 'search_knowledge_base';
readonly description = 'Search company knowledge base';
readonly requiresApproval = false;

async execute(input: { query: string }) {
const results = await this.knowledgeBase.search(input.query);
return { articles: results };
}

async validate(input: unknown): Promise<{ query: string }> {
// Validate input shape
if (typeof input !== 'object' || !input || !('query' in input)) {
throw new Error('Invalid input');
}
return input as { query: string };
}
}

Type Parameters

  • TInput

    Input type for the tool

  • TOutput

    Output type from the tool

Constructors

Methods

  • Validates input before execution

    Parameters

    • input: unknown

      The raw input to validate

    Returns Promise<TInput>

    The validated input

    If validation fails

  • Executes the tool with automatic validation

    Parameters

    • input: unknown

      The raw input

    Returns Promise<TOutput>

    The tool output

    If validation fails or execution errors

Properties

name: string

Unique name of the tool

description: string

Human-readable description of what the tool does

requiresApproval: boolean = false

Whether this tool requires human approval before execution