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

ServerTool.md

319 lines7.4 KB

1---
2id: ServerTool
3title: ServerTool
4---
5​
6Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:86](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L86)
7​
8Marker type for server-side tools
9​
10## Extends
11​
12- [`Tool`](Tool.md)\<`TInput`, `TOutput`, `TName`, `TContext`\>.`ToolApprovalCapabilityMarker`\<`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### TContext
29​
30`TContext` = `unknown`
31​
32### TNeedsApproval
33​
34`TNeedsApproval` *extends* `boolean` = `false`
35​
36### TApprovalSchema
37​
38`TApprovalSchema` *extends*
39 \| [`ApprovalSchemaConfig`](../type-aliases/ApprovalSchemaConfig.md)
40 \| `undefined` = `undefined`
41​
42## Properties
43​
44### \_\_toolSide
45​
46```ts
47__toolSide: "server";
48```
49​
50Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:97](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L97)
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:26](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L26)
61​
62#### approvalSchema
63​
64```ts
65approvalSchema: TApprovalSchema;
66```
67​
68#### needsApproval
69​
70```ts
71needsApproval: TNeedsApproval;
72```
73​
74#### Inherited from
75​
76```ts
77ToolApprovalCapabilityMarker.[toolApprovalCapability]
78```
79​
80***
81​
82### approvalSchema?
83​
84```ts
85optional approvalSchema?: TApprovalSchema;
86```
87​
88Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:101](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L101)
89​
90***
91​
92### description
93​
94```ts
95description: string;
96```
97​
98Defined in: [packages/ai/src/types.ts:774](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L774)
99​
100Clear description of what the tool does.
101​
102This is crucial - the model uses this to decide when to call the tool.
103Be specific about what the tool does, what parameters it needs, and what it returns.
104​
105#### Example
106​
107```ts
108"Get the current weather in a given location. Returns temperature, conditions, and forecast."
109```
110​
111#### Inherited from
112​
113[`Tool`](Tool.md).[`description`](Tool.md#description)
114​
115***
116​
117### execute?
118​
119```ts
120optional execute?: ToolExecuteFunction<TInput, TOutput, TContext>;
121```
122​
123Defined in: [packages/ai/src/types.ts:854](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L854)
124​
125Optional function to execute when the model calls this tool.
126​
127If provided, the SDK will automatically execute the function with the model's arguments
128and feed the result back to the model. This enables autonomous tool use loops.
129​
130Can return any value - will be automatically stringified if needed.
131​
132#### Param
133​
134**args**
135​
136The arguments parsed from the model's tool call (validated against inputSchema)
137​
138#### Returns
139​
140Result to send back to the model (validated against outputSchema if provided)
141​
142#### Example
143​
144```ts
145execute: async (args) => {
146 const weather = await fetchWeather(args.location);
147 return weather; // Can return object or string
149```
150​
151#### Inherited from
152​
153[`Tool`](Tool.md).[`execute`](Tool.md#execute)
154​
155***
156​
157### inputSchema?
158​
159```ts
160optional inputSchema?: TInput;
161```
162​
163Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:98](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L98)
164​
165Schema describing the tool's input parameters.
166​
167Can be any Standard JSON Schema compliant schema (Zod, ArkType, Valibot, etc.) or a plain JSON Schema object.
168Defines the structure and types of arguments the tool accepts.
169The model will generate arguments matching this schema.
170Standard JSON Schema compliant schemas are converted to JSON Schema for LLM providers.
171​
172#### See
173​
174 - https://standardschema.dev/json-schema
175 - https://json-schema.org/
176​
177#### Examples
178​
179```ts
180// Using Zod v4+ schema (natively supports Standard JSON Schema)
181import { z } from 'zod';
182z.object({
183 location: z.string().describe("City name or coordinates"),
184 unit: z.enum(["celsius", "fahrenheit"]).optional()
185})
186```
187​
188```ts
189// Using ArkType (natively supports Standard JSON Schema)
190import { type } from 'arktype';
191type({
192 location: 'string',
193 unit: "'celsius' | 'fahrenheit'"
194})
195```
196​
197```ts
198// Using plain JSON Schema
200 type: 'object',
201 properties: {
202 location: { type: 'string', description: 'City name or coordinates' },
203 unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
204 },
205 required: ['location']
207```
208​
209#### Overrides
210​
211[`Tool`](Tool.md).[`inputSchema`](Tool.md#inputschema)
212​
213***
214​
215### lazy?
216​
217```ts
218optional lazy?: boolean;
219```
220​
221Defined in: [packages/ai/src/types.ts:860](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L860)
222​
223If 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).
224​
225#### Inherited from
226​
227[`Tool`](Tool.md).[`lazy`](Tool.md#lazy)
228​
229***
230​
231### metadata?
232​
233```ts
234optional metadata?: Record<string, any>;
235```
236​
237Defined in: [packages/ai/src/types.ts:863](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L863)
238​
239Additional metadata for adapters or custom extensions
240​
241#### Inherited from
242​
243[`Tool`](Tool.md).[`metadata`](Tool.md#metadata)
244​
245***
246​
247### name
248​
249```ts
250name: TName;
251```
252​
253Defined in: [packages/ai/src/types.ts:764](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L764)
254​
255Unique name of the tool (used by the model to call it).
256​
257Should be descriptive and follow naming conventions (e.g., snake_case or camelCase).
258Must be unique within the tools array.
259​
260#### Example
261​
262```ts
263"get_weather", "search_database", "sendEmail"
264```
265​
266#### Inherited from
267​
268[`Tool`](Tool.md).[`name`](Tool.md#name)
269​
270***
271​
272### needsApproval?
273​
274```ts
275optional needsApproval?: TNeedsApproval;
276```
277​
278Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:100](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L100)
279​
280If true, tool execution requires user approval before running. Works with both server and client tools.
281​
282#### Overrides
283​
284[`Tool`](Tool.md).[`needsApproval`](Tool.md#needsapproval)
285​
286***
287​
288### outputSchema?
289​
290```ts
291optional outputSchema?: TOutput;
292```
293​
294Defined in: [packages/ai/src/activities/chat/tools/tool-definition.ts:99](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/tools/tool-definition.ts#L99)
295​
296Optional schema for validating tool output.
297​
298Can be any Standard JSON Schema compliant schema or a plain JSON Schema object.
299If provided with a Standard Schema compliant schema, tool results will be validated
300against this schema before being sent back to the model. This catches bugs in tool
301implementations and ensures consistent output formatting.
302​
303Note: This is client-side validation only - not sent to LLM providers.
304Note: Plain JSON Schema output validation is not performed at runtime.
305​
306#### Example
307​
308```ts
309// Using Zod
310z.object({
311 temperature: z.number(),
312 conditions: z.string(),
313 forecast: z.array(z.string()).optional()
314})
315```
316​
317#### Overrides
318​
319[`Tool`](Tool.md).[`outputSchema`](Tool.md#outputschema)