tool / FunctionTool

View as MarkdownOpen in Claude

tool

1function tool<P = any>(options: {
2 description: string;
3 parameters?: any;
4 execute: (params: P, context: { ctx: RunContext }) => any;
5}): FunctionTool

Creates a FunctionTool definition that can be passed into an Agent’s tools map. The tool() function mirrors llm.tool() from @livekit/agents-js.

When the underlying SignalWire agent is built, each tool is registered as a SWAIG function via defineTool().

Parameters

description
stringRequired

A description of what the tool does. This is sent to the LLM so it knows when to invoke the tool.

parameters
any

JSON Schema describing the tool’s parameters, or a Zod schema object. If omitted, the tool accepts no parameters.

execute
(params: P, context: { ctx: RunContext }) => anyRequired

The function to run when the tool is invoked. Receives the parsed parameters and a context object containing the RunContext. Can return a string, an object (serialized to JSON), or a FunctionResult.

Returns

FunctionTool — A tool definition object. The name field is empty and gets filled in when the tool is assigned to a key in Agent.tools.

Examples

1import { tool } from '@signalwire/sdk/livewire';
2
3const getWeather = tool({
4 description: 'Get the current weather for a city.',
5 parameters: {
6 city: { type: 'string', description: 'The city name' },
7 },
8 execute: (params) => {
9 return `It is sunny in ${params.city}.`;
10 },
11});

FunctionTool Interface

The FunctionTool interface represents a tool definition that can be registered on an Agent.

1interface FunctionTool {
2 name: string;
3 description: string;
4 parameters?: Record<string, unknown>;
5 execute: (params: any, context: { ctx: RunContext }) => any;
6}
name
string

The tool name. When created via tool(), this is initially empty and is set automatically when the tool is added to an Agent’s tools map (the map key becomes the name).

description
string

A description of what the tool does.

parameters
Record<string, unknown> | undefined

JSON Schema or Zod schema describing the tool’s accepted parameters.

execute
(params: any, context: { ctx: RunContext }) => any

The handler function invoked when the LLM calls this tool.