Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

ToolDefinition.md

423 lines9.7 KB

1---
2id: ToolDefinition
3title: ToolDefinition
4---
5​
6Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:241
7​
8Tool definition builder that allows creating server or client tools from a shared definition
9​
10## Extends
11​
12- [`ToolDefinitionInstance`](ToolDefinitionInstance.md)\<`TInput`, `TOutput`, `TName`, `unknown`, `TNeedsApproval`, `TApprovalSchema`\>
13​
14## Type Parameters
15​
16### TInput
17​
18`TInput` *extends* [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined` = `undefined`
19​
20### TOutput
21​
22`TOutput` *extends* [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined` = `undefined`
23​
24### TName
25​
26`TName` *extends* `string` = `string`
27​
28### TNeedsApproval
29​
30`TNeedsApproval` *extends* `boolean` = `false`
31​
32### TApprovalSchema
33​
34`TApprovalSchema` *extends*
35 \| [`ApprovalSchemaConfig`](../type-aliases/ApprovalSchemaConfig.md)
36 \| `undefined` = `undefined`
37​
38## Properties
39​
40### \_\_toolSide
41​
42```ts
43__toolSide: "definition";
44```
45​
46Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:157
47​
48#### Inherited from
49​
50[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`__toolSide`](ToolDefinitionInstance.md#__toolside)
51​
52***
53​
54### \[toolApprovalCapability\]?
55​
56```ts
57readonly optional [toolApprovalCapability]?: object;
58```
59​
60Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:165
61​
62#### approvalSchema
63​
64```ts
65approvalSchema: TApprovalSchema;
66```
67​
68#### needsApproval
69​
70```ts
71needsApproval: TNeedsApproval;
72```
73​
74#### Inherited from
75​
76[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`[toolApprovalCapability]`](ToolDefinitionInstance.md#toolapprovalcapability)
77​
78***
79​
80### approvalSchema
81​
82```ts
83approvalSchema: TApprovalSchema;
84```
85​
86Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:163
87​
88#### Inherited from
89​
90[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`approvalSchema`](ToolDefinitionInstance.md#approvalschema)
91​
92***
93​
94### client
95​
96```ts
97client: <TContext>(execute?) => ClientTool<TInput, TOutput, TName, TContext, TNeedsApproval, TApprovalSchema> & BuiltToolSchemaFields<TInput, TOutput, TApprovalSchema>;
98```
99​
100Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:275
101​
102Create a client-side tool with optional execute function.
103Carries the definition's `needsApproval` literal through to the client
104tool so the tool-call part's `approval` field stays gated on it.
105​
106#### Type Parameters
107​
108##### TContext
109​
110`TContext` = `unknown`
111​
112#### Parameters
113​
114##### execute?
115​
116[`ToolExecuteFunction`](../type-aliases/ToolExecuteFunction.md)\<`TInput`, `TOutput`, `TContext`\>
117​
118#### Returns
119​
120[`ClientTool`](ClientTool.md)\<`TInput`, `TOutput`, `TName`, `TContext`, `TNeedsApproval`, `TApprovalSchema`\> & `BuiltToolSchemaFields`\<`TInput`, `TOutput`, `TApprovalSchema`\>
121​
122***
123​
124### description
125​
126```ts
127description: string;
128```
129​
130Defined in: packages/ai/src/types.ts:989
131​
132Clear description of what the tool does.
133​
134This is crucial - the model uses this to decide when to call the tool.
135Be specific about what the tool does, what parameters it needs, and what it returns.
136​
137#### Example
138​
139```ts
140"Get the current weather in a given location. Returns temperature, conditions, and forecast."
141```
142​
143#### Inherited from
144​
145[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`description`](ToolDefinitionInstance.md#description)
146​
147***
148​
149### execute?
150​
151```ts
152optional execute?: (args, context?) =>
153 | InferSchemaType<TOutput>
154| Promise<InferSchemaType<TOutput>>;
155```
156​
157Defined in: packages/ai/src/types.ts:1069
158​
159Optional function to execute when the model calls this tool.
160​
161If provided, the SDK will automatically execute the function with the model's arguments
162and feed the result back to the model. This enables autonomous tool use loops.
163​
164Can return any value - will be automatically stringified if needed.
165​
166#### Parameters
167​
168##### args
169​
170[`InferSchemaType`](../type-aliases/InferSchemaType.md)\<`TInput`\>
171​
172The arguments parsed from the model's tool call (validated against inputSchema)
173​
174##### context?
175​
176[`ToolExecutionContext`](../type-aliases/ToolExecutionContext.md)\<`unknown`\>
177​
178#### Returns
179​
180 \| [`InferSchemaType`](../type-aliases/InferSchemaType.md)\<`TOutput`\>
181 \| `Promise`\<[`InferSchemaType`](../type-aliases/InferSchemaType.md)\<`TOutput`\>\>
182​
183Result to send back to the model (validated against outputSchema if provided)
184​
185#### Example
186​
187```ts
188execute: async (args) => {
189 const weather = await fetchWeather(args.location);
190 return weather; // Can return object or string
192```
193​
194#### Inherited from
195​
196[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`execute`](ToolDefinitionInstance.md#execute)
197​
198***
199​
200### execution?
201​
202```ts
203optional execution?: "task";
204```
205​
206Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:164
207​
208#### Inherited from
209​
210[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`execution`](ToolDefinitionInstance.md#execution)
211​
212***
213​
214### inputSchema
215​
216```ts
217inputSchema: TInput;
218```
219​
220Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:160
221​
222Schema describing the tool's input parameters.
223​
224Can be any Standard JSON Schema compliant schema (Zod, ArkType, Valibot, etc.) or a plain JSON Schema object.
225Defines the structure and types of arguments the tool accepts.
226The model will generate arguments matching this schema.
227Standard JSON Schema compliant schemas are converted to JSON Schema for LLM providers.
228​
229#### See
230​
231 - https://standardschema.dev/json-schema
232 - https://json-schema.org/
233​
234#### Examples
235​
236```ts
237// Using Zod v4+ schema (natively supports Standard JSON Schema)
238import { z } from 'zod';
239z.object({
240 location: z.string().describe("City name or coordinates"),
241 unit: z.enum(["celsius", "fahrenheit"]).optional()
242})
243```
244​
245```ts
246// Using ArkType (natively supports Standard JSON Schema)
247import { type } from 'arktype';
248type({
249 location: 'string',
250 unit: "'celsius' | 'fahrenheit'"
251})
252```
253​
254```ts
255// Using plain JSON Schema
257 type: 'object',
258 properties: {
259 location: { type: 'string', description: 'City name or coordinates' },
260 unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
261 },
262 required: ['location']
264```
265​
266#### Inherited from
267​
268[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`inputSchema`](ToolDefinitionInstance.md#inputschema)
269​
270***
271​
272### lazy?
273​
274```ts
275optional lazy?: boolean;
276```
277​
278Defined in: packages/ai/src/types.ts:1075
279​
280If true, this tool is lazy and will only be sent to the LLM after being discovered via the lazy tool discovery mechanism. Works with both chat() (the synthetic discovery tool) and Code Mode (kept out of the system prompt and revealed via discover_tools).
281​
282#### Inherited from
283​
284[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`lazy`](ToolDefinitionInstance.md#lazy)
285​
286***
287​
288### metadata?
289​
290```ts
291optional metadata?: Record<string, any>;
292```
293​
294Defined in: packages/ai/src/types.ts:1086
295​
296Additional metadata for adapters or custom extensions
297​
298#### Inherited from
299​
300[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`metadata`](ToolDefinitionInstance.md#metadata)
301​
302***
303​
304### name
305​
306```ts
307name: TName;
308```
309​
310Defined in: packages/ai/src/types.ts:979
311​
312Unique name of the tool (used by the model to call it).
313​
314Should be descriptive and follow naming conventions (e.g., snake_case or camelCase).
315Must be unique within the tools array.
316​
317#### Example
318​
319```ts
320"get_weather", "search_database", "sendEmail"
321```
322​
323#### Inherited from
324​
325[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`name`](ToolDefinitionInstance.md#name)
326​
327***
328​
329### needsApproval?
330​
331```ts
332optional needsApproval?: TNeedsApproval;
333```
334​
335Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:162
336​
337If true, tool execution requires user approval before running. Works with both server and client tools.
338​
339#### Inherited from
340​
341[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`needsApproval`](ToolDefinitionInstance.md#needsapproval)
342​
343***
344​
345### outputSchema
346​
347```ts
348outputSchema: TOutput;
349```
350​
351Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:161
352​
353Optional schema for validating tool output.
354​
355Can be any Standard JSON Schema compliant schema or a plain JSON Schema object.
356If provided with a Standard Schema compliant schema, tool results will be validated
357against this schema before being sent back to the model. This catches bugs in tool
358implementations and ensures consistent output formatting.
359​
360Note: This is client-side validation only - not sent to LLM providers.
361Note: Plain JSON Schema output validation is not performed at runtime.
362​
363#### Example
364​
365```ts
366// Using Zod
367z.object({
368 temperature: z.number(),
369 conditions: z.string(),
370 forecast: z.array(z.string()).optional()
371})
372```
373​
374#### Inherited from
375​
376[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`outputSchema`](ToolDefinitionInstance.md#outputschema)
377​
378***
379​
380### replay?
381​
382```ts
383optional replay?: "safe" | "never";
384```
385​
386Defined in: packages/ai/src/types.ts:1083
387​
388Whether a host may run this tool again after a crash cut the run
389between the call and its result. `'safe'` runs it again. Omit the
390field, or set `'never'`, when a second run is not safe. The host
391writes the model note. `chat()` does not read this field.
392​
393#### Inherited from
394​
395[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`replay`](ToolDefinitionInstance.md#replay)
396​
397***
398​
399### server
400​
401```ts
402server: <TContext>(execute) => ServerTool<TInput, TOutput, TName, TContext, TNeedsApproval, TApprovalSchema> & BuiltToolSchemaFields<TInput, TOutput, TApprovalSchema>;
403```
404​
405Defined in: packages/ai/src/activities/chat/tools/tool-definition.ts:258
406​
407Create a server-side tool with execute function
408​
409#### Type Parameters
410​
411##### TContext
412​
413`TContext` = `unknown`
414​
415#### Parameters
416​
417##### execute
418​
419[`ToolExecuteFunction`](../type-aliases/ToolExecuteFunction.md)\<`TInput`, `TOutput`, `TContext`\>
420​
421#### Returns
422​
423[`ServerTool`](ServerTool.md)\<`TInput`, `TOutput`, `TName`, `TContext`, `TNeedsApproval`, `TApprovalSchema`\> & `BuiltToolSchemaFields`\<`TInput`, `TOutput`, `TApprovalSchema`\>