tool
function tool<I, O>(definition: ToolDefinition<I, O>): ToolDefinition<I, O>;Identity helper that keeps a tool’s schema type, so its handler’s arguments are typed for real.
tools is an array, and TypeScript does not infer a generic per array
element — every entry falls back to ToolDefinition’s default and the
handler receives any. That has always been true here; Zod 3 hid it because
its ZodTypeAny default WAS any, so nothing complained.
Wrapping one definition restores the inference, because the generic is then captured at the call:
tools: [ tool({ name: 'search', description: 'Search.', input: z.object({ query: z.string() }), async handler(args) { return args.query.toUpperCase(); // typed, not `any` }, }),]Purely a type-level device: it returns its argument untouched, costs nothing at runtime, and is opt-in per tool.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
I extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> |
O extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
definition | ToolDefinition<I, O> | The tool definition. |
Returns
Section titled “Returns”ToolDefinition<I, O>
The same definition, with its schema types preserved.