Skip to content

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.

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

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 ParameterDefault typeDescription
TInput extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>-Zod schema type for input validation
TReturn extends JSONValueJSONValueReturn type of the callback function
ParameterTypeDescription
configZodToolConfig<TInput, TReturn>Tool configuration with Zod schema

InvokableTool<output<TInput>, TReturn>

An InvokableTool with typed input and output

function tool(config): InvokableTool<unknown, JSONValue>;

Defined in: src/tools/tool-factory.ts:36

Creates an InvokableTool from a JSON schema and callback function.

ParameterTypeDescription
configFunctionToolConfigTool configuration with optional JSON schema

InvokableTool<unknown, JSONValue>

An InvokableTool with unknown input