The registerTool method supports automatic argument validation if the schema argument contains ZodType instances.
When a schema is provided using Zod, the ToolFactory performs the following:
- Connection Check: Ensures the
BotConnection is active. - Parsing: Uses
z.object(schema).passthrough().parse(args) to validate the input. - Error Handling: If validation fails, it catches the
ZodError and returns a formatted error response in the format: Invalid tool arguments: path.to.field: error message; ....
If the schema is an empty object or does not contain Zod types, validation is skipped and the raw args are passed to the executor.
import { z } from "zod";
toolFactory.registerTool(
"move_player",
"Moves the player to specific coordinates",
{
x: z.number().int().describe("X coordinate"),
y: z.number().int().describe("Y coordinate"),
z: z.number().int().describe("Z coordinate"),
direction: z.enum(["north", "south", "east", "west"]).optional()
},
async (args) => {
// args is typed via the schema during execution
return toolFactory.createResponse(`Moving to ${args.x}, ${args.y}, ${args.z}`);
}
);