Skip to main content

Function: defineZodTool()

defineZodTool<TSchema, TFn>(definition): ZodToolDefinition<TSchema, Awaited<ReturnType<TFn>>>

Defined in: packages/core/src/utils/tools.ts:421

Define a tool using Zod schema for automatic type inference

Recommended API - provides full type safety!

Type Parameters

TSchema

TSchema extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> = ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>

TFn

TFn extends (params, options?) => unknown = (params, options?) => unknown

Parameters

definition

description

string

execute

TFn

inputSchema

TSchema

name

string

requiresApproval?

boolean | ((params) => boolean | Promise<boolean>)

strict?

boolean

toModelOutput?

(context) => ToolModelOutput

Returns

ZodToolDefinition<TSchema, Awaited<ReturnType<TFn>>>

Examples

const weatherTool = defineZodTool({
name: 'get_weather',
description: 'Get weather for a location',
inputSchema: z.object({
location: z.string().describe('City name'),
units: z.enum(['celsius', 'fahrenheit']).optional()
}),
execute: async (params) => {
// params is { location: string, units?: 'celsius' | 'fahrenheit' }
return { temp: 22, condition: 'sunny' };
}
});
// Return type: { temp: number, condition: string }
const dangerousTool = defineZodTool({
name: 'execute_command',
description: 'Execute a shell command',
inputSchema: z.object({ command: z.string() }),
requiresApproval: true, // Require approval
strict: true, // Strict schema mode
toModelOutput: ({ output }) => ({
type: 'text',
value: `Exit code: ${output.exitCode}`
}),
execute: async ({ command }) => {
return { exitCode: 0, stdout: '...' };
}
});