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

ToolDefinition.md

390 lines10.2 KB

1---
2id: ToolDefinition
3title: ToolDefinition
4---
5​
6Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:229](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L229)
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:154](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L154)
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:161](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L161)
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:160](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L160)
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:263](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L263)
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:774](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L774)
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:854](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L854)
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### inputSchema
201​
202```ts
203inputSchema: TInput;
204```
205​
206Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:157](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L157)
207​
208Schema describing the tool's input parameters.
209​
210Can be any Standard JSON Schema compliant schema (Zod, ArkType, Valibot, etc.) or a plain JSON Schema object.
211Defines the structure and types of arguments the tool accepts.
212The model will generate arguments matching this schema.
213Standard JSON Schema compliant schemas are converted to JSON Schema for LLM providers.
214​
215#### See
216​
217 - https://standardschema.dev/json-schema
218 - https://json-schema.org/
219​
220#### Examples
221​
222```ts
223// Using Zod v4+ schema (natively supports Standard JSON Schema)
224import { z } from 'zod';
225z.object({
226 location: z.string().describe("City name or coordinates"),
227 unit: z.enum(["celsius", "fahrenheit"]).optional()
228})
229```
230​
231```ts
232// Using ArkType (natively supports Standard JSON Schema)
233import { type } from 'arktype';
234type({
235 location: 'string',
236 unit: "'celsius' | 'fahrenheit'"
237})
238```
239​
240```ts
241// Using plain JSON Schema
243 type: 'object',
244 properties: {
245 location: { type: 'string', description: 'City name or coordinates' },
246 unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
247 },
248 required: ['location']
250```
251​
252#### Inherited from
253​
254[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`inputSchema`](ToolDefinitionInstance.md#inputschema)
255​
256***
257​
258### lazy?
259​
260```ts
261optional lazy?: boolean;
262```
263​
264Defined in: [packages/ai/src/types.ts:860](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L860)
265​
266If 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).
267​
268#### Inherited from
269​
270[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`lazy`](ToolDefinitionInstance.md#lazy)
271​
272***
273​
274### metadata?
275​
276```ts
277optional metadata?: Record<string, any>;
278```
279​
280Defined in: [packages/ai/src/types.ts:863](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L863)
281​
282Additional metadata for adapters or custom extensions
283​
284#### Inherited from
285​
286[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`metadata`](ToolDefinitionInstance.md#metadata)
287​
288***
289​
290### name
291​
292```ts
293name: TName;
294```
295​
296Defined in: [packages/ai/src/types.ts:764](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L764)
297​
298Unique name of the tool (used by the model to call it).
299​
300Should be descriptive and follow naming conventions (e.g., snake_case or camelCase).
301Must be unique within the tools array.
302​
303#### Example
304​
305```ts
306"get_weather", "search_database", "sendEmail"
307```
308​
309#### Inherited from
310​
311[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`name`](ToolDefinitionInstance.md#name)
312​
313***
314​
315### needsApproval?
316​
317```ts
318optional needsApproval?: TNeedsApproval;
319```
320​
321Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:159](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L159)
322​
323If true, tool execution requires user approval before running. Works with both server and client tools.
324​
325#### Inherited from
326​
327[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`needsApproval`](ToolDefinitionInstance.md#needsapproval)
328​
329***
330​
331### outputSchema
332​
333```ts
334outputSchema: TOutput;
335```
336​
337Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:158](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L158)
338​
339Optional schema for validating tool output.
340​
341Can be any Standard JSON Schema compliant schema or a plain JSON Schema object.
342If provided with a Standard Schema compliant schema, tool results will be validated
343against this schema before being sent back to the model. This catches bugs in tool
344implementations and ensures consistent output formatting.
345​
346Note: This is client-side validation only - not sent to LLM providers.
347Note: Plain JSON Schema output validation is not performed at runtime.
348​
349#### Example
350​
351```ts
352// Using Zod
353z.object({
354 temperature: z.number(),
355 conditions: z.string(),
356 forecast: z.array(z.string()).optional()
357})
358```
359​
360#### Inherited from
361​
362[`ToolDefinitionInstance`](ToolDefinitionInstance.md).[`outputSchema`](ToolDefinitionInstance.md#outputschema)
363​
364***
365​
366### server
367​
368```ts
369server: <TContext>(execute) => ServerTool<TInput, TOutput, TName, TContext, TNeedsApproval, TApprovalSchema> & BuiltToolSchemaFields<TInput, TOutput, TApprovalSchema>;
370```
371​
372Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:246](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L246)
373​
374Create a server-side tool with execute function
375​
376#### Type Parameters
377​
378##### TContext
379​
380`TContext` = `unknown`
381​
382#### Parameters
383​
384##### execute
385​
386[`ToolExecuteFunction`](../type-aliases/ToolExecuteFunction.md)\<`TInput`, `TOutput`, `TContext`\>
387​
388#### Returns
389​
390[`ServerTool`](ServerTool.md)\<`TInput`, `TOutput`, `TName`, `TContext`, `TNeedsApproval`, `TApprovalSchema`\> & `BuiltToolSchemaFields`\<`TInput`, `TOutput`, `TApprovalSchema`\>