Skip to content

Agent Tool

import { agentTool } from "@swenyai/providers/agent-tool";
interface AgentTool<T extends ZodRawShape = ZodRawShape> {
name: string;
description: string;
schema: T;
execute(args: Record<string, unknown>): Promise<ToolResult>;
}
interface ToolResult {
content: { type: "text"; text: string }[];
isError?: boolean;
}

Creates a typed tool with a Zod schema for argument validation:

import { z } from "zod";
const greetTool = agentTool(
"greet",
"Greet a user by name",
{ name: z.string() },
async (args) => ({
content: [{ type: "text", text: `Hello, ${args.name}!` }],
}),
);

The agentTool factory accepts four arguments:

ArgumentTypeDescription
namestringUnique tool name
descriptionstringHuman-readable description
schemaZodRawShapeZod schema defining accepted args
execute(args: Record<string, unknown>) => Promise<ToolResult>Function that runs the tool

Return a ToolResult with isError: true to signal a failure without throwing.