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

mergeAgentTools.md

116 lines3.2 KB

1---
2id: mergeAgentTools
3title: mergeAgentTools
4---
5​
6## Call Signature
7​
8```ts
9function mergeAgentTools<TServerTools>(serverTools, clientTools): TServerTools;
10```
11​
12Defined in: packages/ai/src/utilities/chat-params.ts:349
13​
14Merge a server-side tool array with the AG-UI client-declared tools
15received in the request body.
16​
17Rules:
18- Server tools win on name collision. The client's declaration is
19 ignored if the server already has a tool with that name. The client's
20 UI-side handler still fires when the streamed tool-result event comes
21 through (see `chat-client.ts` `onToolCall`), giving the
22 "after server execution the client also handles" semantic for free.
23- Client-only tools (name not in `serverTools`) become no-execute
24 entries: the runtime's existing `ClientToolRequest` path handles
25 them — server emits a tool-call request, client executes via its
26 registered handler, client posts back the result.
27​
28Typing:
29- Empty `clientTools` preserves the server tuple (closed name union).
30- Non-empty `clientTools` returns a widened array that honestly includes
31 client stubs, so the merged array does not claim a closed server-only
32 name union.
33​
34### Type Parameters
35​
36#### TServerTools
37​
38`TServerTools` *extends* readonly [`AnyTool`](../type-aliases/AnyTool.md)[]
39​
40### Parameters
41​
42#### serverTools
43​
44`TServerTools`
45​
46The server's tool array (e.g. from
47 `[myToolDef.server(...)]`). Pass directly to `chat({ tools })`.
48​
49#### clientTools
50​
51readonly \[\]
52​
53The `tools` array received from
54 `chatParamsFromRequest(...)` / `chatParamsFromRequestBody(...)`.
55​
56### Returns
57​
58`TServerTools`
59​
60A merged array suitable for `chat({ tools })`.
61​
62## Call Signature
63​
64```ts
65function mergeAgentTools<TServerTools>(serverTools, clientTools): MergedAgentTools<TServerTools>;
66```
67​
68Defined in: packages/ai/src/utilities/chat-params.ts:352
69​
70Merge a server-side tool array with the AG-UI client-declared tools
71received in the request body.
72​
73Rules:
74- Server tools win on name collision. The client's declaration is
75 ignored if the server already has a tool with that name. The client's
76 UI-side handler still fires when the streamed tool-result event comes
77 through (see `chat-client.ts` `onToolCall`), giving the
78 "after server execution the client also handles" semantic for free.
79- Client-only tools (name not in `serverTools`) become no-execute
80 entries: the runtime's existing `ClientToolRequest` path handles
81 them — server emits a tool-call request, client executes via its
82 registered handler, client posts back the result.
83​
84Typing:
85- Empty `clientTools` preserves the server tuple (closed name union).
86- Non-empty `clientTools` returns a widened array that honestly includes
87 client stubs, so the merged array does not claim a closed server-only
88 name union.
89​
90### Type Parameters
91​
92#### TServerTools
93​
94`TServerTools` *extends* readonly [`AnyTool`](../type-aliases/AnyTool.md)[]
95​
96### Parameters
97​
98#### serverTools
99​
100`TServerTools`
101​
102The server's tool array (e.g. from
103 `[myToolDef.server(...)]`). Pass directly to `chat({ tools })`.
104​
105#### clientTools
106​
107readonly `object`[]
108​
109The `tools` array received from
110 `chatParamsFromRequest(...)` / `chatParamsFromRequestBody(...)`.
111​
112### Returns
113​
114[`MergedAgentTools`](../type-aliases/MergedAgentTools.md)\<`TServerTools`\>
115​
116A merged array suitable for `chat({ tools })`.