tool
Creates an InvokableTool from either a Zod schema or JSON schema configuration.
When a Zod schema is provided as inputSchema, input is validated at runtime and
the callback receives typed input. When a JSON schema (or no schema) is provided,
the callback receives unknown input with no runtime validation.
Example
Section titled “Example”import { tool } from '@strands-agents/sdk'import { z } from 'zod'
// With Zod schema (typed + validated)const calculator = tool({ name: 'calculator', description: 'Adds two numbers', inputSchema: z.object({ a: z.number(), b: z.number() }), callback: (input) => input.a + input.b,})
// With JSON schema (untyped, no validation)const greeter = tool({ name: 'greeter', description: 'Greets a person', inputSchema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], }, callback: (input) => `Hello, ${(input as { name: string }).name}!`,})Tool configuration
Call Signature
Section titled “Call Signature”function tool<TInput, TReturn>(config): InvokableTool<output<TInput>, TReturn>;Defined in: src/tools/tool-factory.ts:26
Creates an InvokableTool from a Zod schema and callback function.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
TInput extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> | - | Zod schema type for input validation |
TReturn extends JSONValue | JSONValue | Return type of the callback function |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config | ZodToolConfig<TInput, TReturn> | Tool configuration with Zod schema |
Returns
Section titled “Returns”InvokableTool<output<TInput>, TReturn>
An InvokableTool with typed input and output
Call Signature
Section titled “Call Signature”function tool(config): InvokableTool<unknown, JSONValue>;Defined in: src/tools/tool-factory.ts:36
Creates an InvokableTool from a JSON schema and callback function.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config | FunctionToolConfig | Tool configuration with optional JSON schema |
Returns
Section titled “Returns”InvokableTool<unknown, JSONValue>
An InvokableTool with unknown input