• Creates a tool from a configuration object.

    This is a more user-friendly alternative to FunctionTool that provides:

    • Automatic argument validation using Zod schemas
    • Clear error messages for invalid inputs
    • Automatic JSON Schema generation for LLM function declarations
    • Support for both sync and async functions
    • Optional ToolContext parameter support

    Type Parameters

    • T extends Record<string, any>

    Parameters

    Returns BaseTool

    A BaseTool instance ready for use with agents

    import { createTool } from '@iqai/adk';
    import { z } from 'zod';

    // Tool with parameters
    const calculatorTool = createTool({
    name: 'calculator',
    description: 'Performs basic arithmetic operations',
    schema: z.object({
    operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
    a: z.number().describe('First number'),
    b: z.number().describe('Second number')
    }),
    fn: ({ operation, a, b }) => {
    switch (operation) {
    case 'add': return { result: a + b };
    case 'subtract': return { result: a - b };
    case 'multiply': return { result: a * b };
    case 'divide': return { result: b !== 0 ? a / b : 'Cannot divide by zero' };
    default: return { error: 'Unknown operation' };
    }
    }
    });

    // Tool without parameters (schema is optional)
    const timestampTool = createTool({
    name: 'timestamp',
    description: 'Gets the current timestamp',
    fn: () => ({ timestamp: Date.now() })
    });
  • Returns BaseTool