defineTools

View as MarkdownOpen in Claude

Protected lifecycle method for registering tools on the agent. Subclasses override this method to call defineTool() or registerSwaigFunction() after all instance fields are initialized.

This method is not called automatically. Subclasses must invoke this.defineTools() explicitly at the end of their constructor.

Parameters

None.

Returns

void

Example

1import { AgentBase } from '@signalwire/sdk';
2
3class WeatherAgent extends AgentBase {
4 constructor() {
5 super({ name: 'weather', route: '/weather' });
6 this.setPromptText('You are a weather assistant.');
7 this.defineTools();
8 }
9
10 protected override defineTools(): void {
11 this.defineTool({
12 name: 'check_weather',
13 description: 'Check the current weather',
14 parameters: {
15 type: 'object',
16 properties: {
17 city: { type: 'string', description: 'City name' },
18 },
19 required: ['city'],
20 },
21 handler: async (args) => {
22 return { response: `The weather in ${args.city} is sunny and 72F.` };
23 },
24 });
25 }
26}
27
28const agent = new WeatherAgent();
29await agent.serve();