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

ToolDefinitionInstance.md

346 lines6.8 KB

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