@toolbox-sdk/adk 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +516 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
# MCP Toolbox SDKs for Javascript
|
|
4
|
+
|
|
5
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
6
|
+
|
|
7
|
+
This SDK allows you to seamlessly integrate the functionalities of
|
|
8
|
+
[Toolbox](https://github.com/googleapis/genai-toolbox) allowing you to load and
|
|
9
|
+
use tools defined in the service as standard JS functions within your GenAI
|
|
10
|
+
applications.
|
|
11
|
+
|
|
12
|
+
This simplifies integrating external functionalities (like APIs, databases, or
|
|
13
|
+
custom logic) managed by the Toolbox into your workflows, especially those
|
|
14
|
+
involving Large Language Models (LLMs).
|
|
15
|
+
|
|
16
|
+
<!-- TOC -->
|
|
17
|
+
|
|
18
|
+
- [MCP Toolbox SDKs for Javascript](#mcp-toolbox-sdks-for-javascript)
|
|
19
|
+
- [Supported Environments](#supported-environments)
|
|
20
|
+
- [Installation](#installation)
|
|
21
|
+
- [Quickstart](#quickstart)
|
|
22
|
+
- [Usage](#usage)
|
|
23
|
+
- [Loading Tools](#loading-tools)
|
|
24
|
+
- [Load a toolset](#load-a-toolset)
|
|
25
|
+
- [Load a single tool](#load-a-single-tool)
|
|
26
|
+
- [Invoking Tools](#invoking-tools)
|
|
27
|
+
- [Client to Server Authentication](#client-to-server-authentication)
|
|
28
|
+
- [When is Client-to-Server Authentication Needed?](#when-is-client-to-server-authentication-needed)
|
|
29
|
+
- [How it works](#how-it-works)
|
|
30
|
+
- [Configuration](#configuration)
|
|
31
|
+
- [Authenticating with Google Cloud Servers](#authenticating-with-google-cloud-servers)
|
|
32
|
+
- [Step by Step Guide for Cloud Run](#step-by-step-guide-for-cloud-run)
|
|
33
|
+
- [Authenticating Tools](#authenticating-tools)
|
|
34
|
+
- [When is Authentication Needed?](#when-is-authentication-needed)
|
|
35
|
+
- [Supported Authentication Mechanisms](#supported-authentication-mechanisms)
|
|
36
|
+
- [Step 1: Configure Tools in Toolbox Service](#step-1-configure-tools-in-toolbox-service)
|
|
37
|
+
- [Step 2: Configure SDK Client](#step-2-configure-sdk-client)
|
|
38
|
+
- [Provide an ID Token Retriever Function](#provide-an-id-token-retriever-function)
|
|
39
|
+
- [Option A: Add Authentication to a Loaded Tool](#option-a-add-authentication-to-a-loaded-tool)
|
|
40
|
+
- [Option B: Add Authentication While Loading Tools](#option-b-add-authentication-while-loading-tools)
|
|
41
|
+
- [Complete Authentication Example](#complete-authentication-example)
|
|
42
|
+
- [Binding Parameter Values](#binding-parameter-values)
|
|
43
|
+
- [Why Bind Parameters?](#why-bind-parameters)
|
|
44
|
+
- [Option A: Binding Parameters to a Loaded Tool](#option-a-binding-parameters-to-a-loaded-tool)
|
|
45
|
+
- [Option B: Binding Parameters While Loading Tools](#option-b-binding-parameters-while-loading-tools)
|
|
46
|
+
- [Binding Dynamic Values](#binding-dynamic-values)
|
|
47
|
+
- [Using with ADK](#using-with-adk)
|
|
48
|
+
- [Contributing](#contributing)
|
|
49
|
+
- [License](#license)
|
|
50
|
+
- [Support](#support)
|
|
51
|
+
|
|
52
|
+
<!-- /TOC -->
|
|
53
|
+
|
|
54
|
+
# Supported Environments
|
|
55
|
+
|
|
56
|
+
This SDK is a standard Node.js package built with TypeScript, ensuring broad
|
|
57
|
+
compatibility with the modern JavaScript ecosystem.
|
|
58
|
+
|
|
59
|
+
- Node.js: Actively supported on Node.js v18.x and higher. The package is
|
|
60
|
+
compatible with both modern ES Modules (import) and legacy CommonJS
|
|
61
|
+
(require).
|
|
62
|
+
- TypeScript: The SDK is written in TypeScript and ships with its own type
|
|
63
|
+
declarations, providing a first-class development experience with
|
|
64
|
+
autocompletion and type-checking out of the box.
|
|
65
|
+
- JavaScript: Fully supports modern JavaScript in Node.js environments.
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm install @toolbox-sdk/adk
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Quickstart
|
|
74
|
+
|
|
75
|
+
Here's a minimal example to get you started. Ensure your Toolbox service is running and accessible.
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
|
|
79
|
+
import { ToolboxClient } from '@toolbox-sdk/adk';
|
|
80
|
+
const client = new ToolboxClient(URL);
|
|
81
|
+
|
|
82
|
+
async function quickstart() {
|
|
83
|
+
try {
|
|
84
|
+
const tools = await client.loadToolset();
|
|
85
|
+
// Use tools
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error("unable to load toolset:", error.message);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
quickstart();
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
> [!NOTE]
|
|
94
|
+
> This guide uses modern ES Module (`import`) syntax. If your project uses
|
|
95
|
+
> CommonJS, you can import the library using require: `const { ToolboxClient }
|
|
96
|
+
> = require('@toolbox-sdk/adk')`;.
|
|
97
|
+
|
|
98
|
+
## Usage
|
|
99
|
+
|
|
100
|
+
Import and initialize a Toolbox client, pointing it to the URL of your running
|
|
101
|
+
Toolbox service.
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
import { ToolboxClient } from '@toolbox-sdk/adk';
|
|
105
|
+
|
|
106
|
+
// Replace with the actual URL where your Toolbox service is running
|
|
107
|
+
const URL = 'http://127.0.0.1:5000';
|
|
108
|
+
|
|
109
|
+
let client = new ToolboxClient(URL);
|
|
110
|
+
const tools = await client.loadToolset();
|
|
111
|
+
|
|
112
|
+
// Use the client and tools as per requirement
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
All interactions for loading and invoking tools happen through this client.
|
|
116
|
+
> [!IMPORTANT]
|
|
117
|
+
> Closing the `ToolboxClient` also closes the underlying network session shared by
|
|
118
|
+
> all tools loaded from that client. As a result, any tool instances you have
|
|
119
|
+
> loaded will cease to function and will raise an error if you attempt to invoke
|
|
120
|
+
> them after the client is closed.
|
|
121
|
+
|
|
122
|
+
> [!NOTE]
|
|
123
|
+
> For advanced use cases, you can provide an external `AxiosInstance`
|
|
124
|
+
> during initialization (e.g., `ToolboxClient(url, my_session)`).
|
|
125
|
+
|
|
126
|
+
## Loading Tools
|
|
127
|
+
|
|
128
|
+
You can load tools individually or in groups (toolsets) as defined in your
|
|
129
|
+
Toolbox service configuration. Loading a toolset is convenient when working with
|
|
130
|
+
multiple related functions, while loading a single tool offers more granular
|
|
131
|
+
control.
|
|
132
|
+
|
|
133
|
+
### Load a toolset
|
|
134
|
+
|
|
135
|
+
A toolset is a collection of related tools. You can load all tools in a toolset
|
|
136
|
+
or a specific one:
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
// Load all tools
|
|
140
|
+
const tools = await toolbox.loadToolset()
|
|
141
|
+
|
|
142
|
+
// Load a specific toolset
|
|
143
|
+
const tools = await toolbox.loadToolset("my-toolset")
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Load a single tool
|
|
147
|
+
|
|
148
|
+
Loads a specific tool by its unique name. This provides fine-grained control.
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
const tool = await toolbox.loadTool("my-tool")
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Invoking Tools
|
|
155
|
+
|
|
156
|
+
Once loaded, tools behave like awaitable JS functions. You invoke them using
|
|
157
|
+
`await` and pass arguments corresponding to the parameters defined in the tool's
|
|
158
|
+
configuration within the Toolbox service.
|
|
159
|
+
|
|
160
|
+
```javascript
|
|
161
|
+
const tool = await toolbox.loadTool("my-tool")
|
|
162
|
+
const result = await tool.runAsync(args: {a: 5, b: 2})
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
> [!TIP]
|
|
166
|
+
> For a more comprehensive guide on setting up the Toolbox service itself, which
|
|
167
|
+
> you'll need running to use this SDK, please refer to the [Toolbox Quickstart
|
|
168
|
+
> Guide](https://googleapis.github.io/genai-toolbox/getting-started/local_quickstart).
|
|
169
|
+
|
|
170
|
+
## Client to Server Authentication
|
|
171
|
+
|
|
172
|
+
This section describes how to authenticate the ToolboxClient itself when
|
|
173
|
+
connecting to a Toolbox server instance that requires authentication. This is
|
|
174
|
+
crucial for securing your Toolbox server endpoint, especially when deployed on
|
|
175
|
+
platforms like Cloud Run, GKE, or any environment where unauthenticated access is restricted.
|
|
176
|
+
|
|
177
|
+
This client-to-server authentication ensures that the Toolbox server can verify
|
|
178
|
+
the identity of the client making the request before any tool is loaded or
|
|
179
|
+
called. It is different from [Authenticating Tools](#authenticating-tools),
|
|
180
|
+
which deals with providing credentials for specific tools within an already
|
|
181
|
+
connected Toolbox session.
|
|
182
|
+
|
|
183
|
+
### When is Client-to-Server Authentication Needed?
|
|
184
|
+
|
|
185
|
+
You'll need this type of authentication if your Toolbox server is configured to
|
|
186
|
+
deny unauthenticated requests. For example:
|
|
187
|
+
|
|
188
|
+
- Your Toolbox server is deployed on Cloud Run and configured to "Require authentication."
|
|
189
|
+
- Your server is behind an Identity-Aware Proxy (IAP) or a similar
|
|
190
|
+
authentication layer.
|
|
191
|
+
- You have custom authentication middleware on your self-hosted Toolbox server.
|
|
192
|
+
|
|
193
|
+
Without proper client authentication in these scenarios, attempts to connect or
|
|
194
|
+
make calls (like `load_tool`) will likely fail with `Unauthorized` errors.
|
|
195
|
+
|
|
196
|
+
### How it works
|
|
197
|
+
|
|
198
|
+
The `ToolboxClient` allows you to specify functions that dynamically generate
|
|
199
|
+
HTTP headers for every request sent to the Toolbox server. The most common use
|
|
200
|
+
case is to add an [Authorization
|
|
201
|
+
header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Authorization)
|
|
202
|
+
with a bearer token (e.g., a Google ID token).
|
|
203
|
+
|
|
204
|
+
These header-generating functions are called just before each request, ensuring
|
|
205
|
+
that fresh credentials or header values can be used.
|
|
206
|
+
|
|
207
|
+
### Configuration
|
|
208
|
+
|
|
209
|
+
You can configure these dynamic headers as seen below:
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
import { ToolboxClient } from '@toolbox-sdk/adk';
|
|
213
|
+
import {getGoogleIdToken} from '@toolbox-sdk/core/auth'
|
|
214
|
+
|
|
215
|
+
const URL = 'http://127.0.0.1:5000';
|
|
216
|
+
const getGoogleIdTokenGetter = () => getGoogleIdToken(URL);
|
|
217
|
+
const client = new ToolboxClient(URL, null, {"Authorization": getGoogleIdTokenGetter});
|
|
218
|
+
|
|
219
|
+
// Use the client as usual
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Authenticating with Google Cloud Servers
|
|
223
|
+
|
|
224
|
+
For Toolbox servers hosted on Google Cloud (e.g., Cloud Run) and requiring
|
|
225
|
+
`Google ID token` authentication, the helper module
|
|
226
|
+
[auth_methods](src/toolbox_core/authMethods.ts) provides utility functions.
|
|
227
|
+
|
|
228
|
+
### Step by Step Guide for Cloud Run
|
|
229
|
+
|
|
230
|
+
1. **Configure Permissions**: [Grant](https://cloud.google.com/run/docs/securing/managing-access#service-add-principals) the `roles/run.invoker` IAM role on the Cloud
|
|
231
|
+
Run service to the principal. This could be your `user account email` or a
|
|
232
|
+
`service account`.
|
|
233
|
+
2. **Configure Credentials**
|
|
234
|
+
- Local Development: Set up
|
|
235
|
+
[ADC](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).
|
|
236
|
+
- Google Cloud Environments: When running within Google Cloud (e.g., Compute
|
|
237
|
+
Engine, GKE, another Cloud Run service, Cloud Functions), ADC is typically
|
|
238
|
+
configured automatically, using the environment's default service account.
|
|
239
|
+
3. **Connect to the Toolbox Server**
|
|
240
|
+
|
|
241
|
+
```javascript
|
|
242
|
+
import { ToolboxClient } from '@toolbox-sdk/adk';
|
|
243
|
+
import {getGoogleIdToken} from '@toolbox-sdk/core/auth'
|
|
244
|
+
|
|
245
|
+
const URL = 'http://127.0.0.1:5000';
|
|
246
|
+
const getGoogleIdTokenGetter = () => getGoogleIdToken(URL);
|
|
247
|
+
const client = new ToolboxClient(URL, null, {"Authorization": getGoogleIdTokenGetter});
|
|
248
|
+
|
|
249
|
+
// Use the client as usual
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Authenticating Tools
|
|
253
|
+
|
|
254
|
+
> [!WARNING]
|
|
255
|
+
> **Always use HTTPS** to connect your application with the Toolbox service,
|
|
256
|
+
> especially in **production environments** or whenever the communication
|
|
257
|
+
> involves **sensitive data** (including scenarios where tools require
|
|
258
|
+
> authentication tokens). Using plain HTTP lacks encryption and exposes your
|
|
259
|
+
> application and data to significant security risks, such as eavesdropping and
|
|
260
|
+
> tampering.
|
|
261
|
+
|
|
262
|
+
Tools can be configured within the Toolbox service to require authentication,
|
|
263
|
+
ensuring only authorized users or applications can invoke them, especially when
|
|
264
|
+
accessing sensitive data.
|
|
265
|
+
|
|
266
|
+
### When is Authentication Needed?
|
|
267
|
+
|
|
268
|
+
Authentication is configured per-tool within the Toolbox service itself. If a
|
|
269
|
+
tool you intend to use is marked as requiring authentication in the service, you
|
|
270
|
+
must configure the SDK client to provide the necessary credentials (currently
|
|
271
|
+
Oauth2 tokens) when invoking that specific tool.
|
|
272
|
+
|
|
273
|
+
### Supported Authentication Mechanisms
|
|
274
|
+
|
|
275
|
+
The Toolbox service enables secure tool usage through **Authenticated Parameters**. For detailed information on how these mechanisms work within the Toolbox service and how to configure them, please refer to [Toolbox Service Documentation - Authenticated Parameters](https://googleapis.github.io/genai-toolbox/resources/tools/#authenticated-parameters)
|
|
276
|
+
|
|
277
|
+
### Step 1: Configure Tools in Toolbox Service
|
|
278
|
+
|
|
279
|
+
First, ensure the target tool(s) are configured correctly in the Toolbox service
|
|
280
|
+
to require authentication. Refer to the [Toolbox Service Documentation -
|
|
281
|
+
Authenticated
|
|
282
|
+
Parameters](https://googleapis.github.io/genai-toolbox/resources/tools/#authenticated-parameters)
|
|
283
|
+
for instructions.
|
|
284
|
+
|
|
285
|
+
### Step 2: Configure SDK Client
|
|
286
|
+
|
|
287
|
+
Your application needs a way to obtain the required Oauth2 token for the
|
|
288
|
+
authenticated user. The SDK requires you to provide a function capable of
|
|
289
|
+
retrieving this token *when the tool is invoked*.
|
|
290
|
+
|
|
291
|
+
#### Provide an ID Token Retriever Function
|
|
292
|
+
|
|
293
|
+
You must provide the SDK with a function (sync or async) that returns the
|
|
294
|
+
necessary token when called. The implementation depends on your application's
|
|
295
|
+
authentication flow (e.g., retrieving a stored token, initiating an OAuth flow).
|
|
296
|
+
|
|
297
|
+
> [!IMPORTANT]
|
|
298
|
+
> The name used when registering the getter function with the SDK (e.g.,
|
|
299
|
+
> `"my_api_token"`) must exactly match the `name` of the corresponding
|
|
300
|
+
> `authServices` defined in the tool's configuration within the Toolbox service.
|
|
301
|
+
|
|
302
|
+
```javascript
|
|
303
|
+
|
|
304
|
+
async function getAuthToken() {
|
|
305
|
+
// ... Logic to retrieve ID token (e.g., from local storage, OAuth flow)
|
|
306
|
+
// This example just returns a placeholder. Replace with your actual token retrieval.
|
|
307
|
+
return "YOUR_ID_TOKEN" // Placeholder
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
> [!TIP]
|
|
312
|
+
> Your token retriever function is invoked every time an authenticated parameter
|
|
313
|
+
> requires a token for a tool call. Consider implementing caching logic within
|
|
314
|
+
> this function to avoid redundant token fetching or generation, especially for
|
|
315
|
+
> tokens with longer validity periods or if the retrieval process is
|
|
316
|
+
> resource-intensive.
|
|
317
|
+
|
|
318
|
+
#### Option A: Add Authentication to a Loaded Tool
|
|
319
|
+
|
|
320
|
+
You can add the token retriever function to a tool object *after* it has been
|
|
321
|
+
loaded. This modifies the specific tool instance.
|
|
322
|
+
|
|
323
|
+
```javascript
|
|
324
|
+
const URL = 'http://127.0.0.1:5000';
|
|
325
|
+
let client = new ToolboxClient(URL);
|
|
326
|
+
let tool = await client.loadTool("my-tool")
|
|
327
|
+
|
|
328
|
+
const authTool = tool.addAuthTokenGetter("my_auth", get_auth_token) // Single token
|
|
329
|
+
|
|
330
|
+
// OR
|
|
331
|
+
|
|
332
|
+
const multiAuthTool = tool.addAuthTokenGetters({
|
|
333
|
+
"my_auth_1": getAuthToken1,
|
|
334
|
+
"my_auth_2": getAuthToken2,
|
|
335
|
+
}) // Multiple tokens
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
#### Option B: Add Authentication While Loading Tools
|
|
339
|
+
|
|
340
|
+
You can provide the token retriever(s) directly during the `loadTool` or
|
|
341
|
+
`loadToolset` calls. This applies the authentication configuration only to the
|
|
342
|
+
tools loaded in that specific call, without modifying the original tool objects
|
|
343
|
+
if they were loaded previously.
|
|
344
|
+
|
|
345
|
+
```javascript
|
|
346
|
+
const authTool = await toolbox.loadTool("toolName", {"myAuth": getAuthToken})
|
|
347
|
+
|
|
348
|
+
// OR
|
|
349
|
+
|
|
350
|
+
const authTools = await toolbox.loadToolset({"myAuth": getAuthToken})
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
> [!NOTE]
|
|
354
|
+
> Adding auth tokens during loading only affect the tools loaded within that
|
|
355
|
+
> call.
|
|
356
|
+
|
|
357
|
+
### Complete Authentication Example
|
|
358
|
+
|
|
359
|
+
```javascript
|
|
360
|
+
import { ToolboxClient } from '@toolbox-sdk/adk';
|
|
361
|
+
|
|
362
|
+
async function getAuthToken() {
|
|
363
|
+
// ... Logic to retrieve ID token (e.g., from local storage, OAuth flow)
|
|
364
|
+
// This example just returns a placeholder. Replace with your actual token retrieval.
|
|
365
|
+
return "YOUR_ID_TOKEN" // Placeholder
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const URL = 'http://127.0.0.1:5000';
|
|
369
|
+
let client = new ToolboxClient(URL);
|
|
370
|
+
const tool = await client.loadTool("my-tool");
|
|
371
|
+
const authTool = tool.addAuthTokenGetters({"my_auth": getAuthToken});
|
|
372
|
+
const result = await authTool.runAsync(args: {input:"some input"});
|
|
373
|
+
console.log(result);
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
## Binding Parameter Values
|
|
377
|
+
|
|
378
|
+
The SDK allows you to pre-set, or "bind", values for specific tool parameters
|
|
379
|
+
before the tool is invoked or even passed to an LLM. These bound values are
|
|
380
|
+
fixed and will not be requested or modified by the LLM during tool use.
|
|
381
|
+
|
|
382
|
+
### Why Bind Parameters?
|
|
383
|
+
|
|
384
|
+
- **Protecting sensitive information:** API keys, secrets, etc.
|
|
385
|
+
- **Enforcing consistency:** Ensuring specific values for certain parameters.
|
|
386
|
+
- **Pre-filling known data:** Providing defaults or context.
|
|
387
|
+
|
|
388
|
+
> [!IMPORTANT]
|
|
389
|
+
> The parameter names used for binding (e.g., `"api_key"`) must exactly match the
|
|
390
|
+
> parameter names defined in the tool's configuration within the Toolbox
|
|
391
|
+
> service.
|
|
392
|
+
|
|
393
|
+
> [!NOTE]
|
|
394
|
+
> You do not need to modify the tool's configuration in the Toolbox service to
|
|
395
|
+
> bind parameter values using the SDK.
|
|
396
|
+
|
|
397
|
+
### Option A: Binding Parameters to a Loaded Tool
|
|
398
|
+
|
|
399
|
+
Bind values to a tool object *after* it has been loaded. This modifies the
|
|
400
|
+
specific tool instance.
|
|
401
|
+
|
|
402
|
+
```javascript
|
|
403
|
+
|
|
404
|
+
import { ToolboxClient } from '@toolbox-sdk/adk';
|
|
405
|
+
|
|
406
|
+
const URL = 'http://127.0.0.1:5000';
|
|
407
|
+
let client = new ToolboxClient(URL);
|
|
408
|
+
const tool = await client.loadTool("my-tool");
|
|
409
|
+
|
|
410
|
+
const boundTool = tool.bindParam("param", "value");
|
|
411
|
+
|
|
412
|
+
// OR
|
|
413
|
+
|
|
414
|
+
const boundTool = tool.bindParams({"param": "value"});
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
### Option B: Binding Parameters While Loading Tools
|
|
418
|
+
|
|
419
|
+
Specify bound parameters directly when loading tools. This applies the binding
|
|
420
|
+
only to the tools loaded in that specific call.
|
|
421
|
+
|
|
422
|
+
```javascript
|
|
423
|
+
const boundTool = await client.loadTool("my-tool", null, {"param": "value"})
|
|
424
|
+
|
|
425
|
+
// OR
|
|
426
|
+
|
|
427
|
+
const boundTools = await client.loadToolset(null, {"param": "value"})
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
> [!NOTE]
|
|
431
|
+
> Bound values during loading only affect the tools loaded in that call.
|
|
432
|
+
|
|
433
|
+
### Binding Dynamic Values
|
|
434
|
+
|
|
435
|
+
Instead of a static value, you can bind a parameter to a synchronous or
|
|
436
|
+
asynchronous function. This function will be called *each time* the tool is
|
|
437
|
+
invoked to dynamically determine the parameter's value at runtime.
|
|
438
|
+
|
|
439
|
+
```javascript
|
|
440
|
+
|
|
441
|
+
async function getDynamicValue() {
|
|
442
|
+
// Logic to determine the value
|
|
443
|
+
return "dynamicValue";
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const dynamicBoundTool = tool.bindParam("param", getDynamicValue)
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
> [!IMPORTANT]
|
|
450
|
+
> You don't need to modify tool configurations to bind parameter values.
|
|
451
|
+
|
|
452
|
+
# Using with ADK
|
|
453
|
+
|
|
454
|
+
ADK JS:
|
|
455
|
+
|
|
456
|
+
```javascript
|
|
457
|
+
import {FunctionTool, InMemoryRunner, LlmAgent} from '@google/adk';
|
|
458
|
+
import {Content} from '@google/genai';
|
|
459
|
+
import {ToolboxClient} from '@toolbox-sdk/core'
|
|
460
|
+
|
|
461
|
+
const toolboxClient = new ToolboxClient("http://127.0.0.1:5000");
|
|
462
|
+
const loadedTools = await toolboxClient.loadToolset();
|
|
463
|
+
|
|
464
|
+
export const rootAgent = new LlmAgent({
|
|
465
|
+
name: 'weather_time_agent',
|
|
466
|
+
model: 'gemini-2.5-flash',
|
|
467
|
+
description:
|
|
468
|
+
'Agent to answer questions about the time and weather in a city.',
|
|
469
|
+
instruction:
|
|
470
|
+
'You are a helpful agent who can answer user questions about the time and weather in a city.',
|
|
471
|
+
tools: loadedTools,
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
async function main() {
|
|
475
|
+
const userId = 'test_user';
|
|
476
|
+
const appName = rootAgent.name;
|
|
477
|
+
const runner = new InMemoryRunner({agent: rootAgent, appName});
|
|
478
|
+
const session = await runner.sessionService.createSession({
|
|
479
|
+
appName,
|
|
480
|
+
userId,
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
const prompt = 'What is the weather in New York? And the time?';
|
|
484
|
+
const content: Content = {
|
|
485
|
+
role: 'user',
|
|
486
|
+
parts: [{text: prompt}],
|
|
487
|
+
};
|
|
488
|
+
console.log(content);
|
|
489
|
+
for await (const e of runner.runAsync({
|
|
490
|
+
userId,
|
|
491
|
+
sessionId: session.id,
|
|
492
|
+
newMessage: content,
|
|
493
|
+
})) {
|
|
494
|
+
if (e.content?.parts?.[0]?.text) {
|
|
495
|
+
console.log(`${e.author}: ${JSON.stringify(e.content, null, 2)}`);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
main().catch(console.error);
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
# Contributing
|
|
505
|
+
|
|
506
|
+
Contributions are welcome! Please refer to the [DEVELOPER.md](./DEVELOPER.md)
|
|
507
|
+
file for guidelines on how to set up a development environment and run tests.
|
|
508
|
+
|
|
509
|
+
# License
|
|
510
|
+
|
|
511
|
+
This project is licensed under the Apache License 2.0. See the
|
|
512
|
+
[LICENSE](https://github.com/googleapis/genai-toolbox/blob/main/LICENSE) file for details.
|
|
513
|
+
|
|
514
|
+
# Support
|
|
515
|
+
|
|
516
|
+
If you encounter issues or have questions, check the existing [GitHub Issues](https://github.com/googleapis/genai-toolbox/issues) for the main Toolbox project.
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@toolbox-sdk/adk",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "JavaScript ADK SDK for interacting with the Toolbox service",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "Google LLC",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"developers",
|
|
10
|
+
"google",
|
|
11
|
+
"toolbox",
|
|
12
|
+
"sdk",
|
|
13
|
+
"llm",
|
|
14
|
+
"genai",
|
|
15
|
+
"agents",
|
|
16
|
+
"mcp"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./build/index.js",
|
|
21
|
+
"require": "./build/cjs/index.js",
|
|
22
|
+
"types": "./build/index.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"build"
|
|
27
|
+
],
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/googleapis/mcp-toolbox-sdk-js.git"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/googleapis/mcp-toolbox-sdk-js/blob/main/packages/toolbox-core",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/googleapis/mcp-toolbox-sdk-js/issues"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=20.0.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"fix": "gts fix",
|
|
41
|
+
"lint": "gts check",
|
|
42
|
+
"compile": "npm run compile:esm && npm run compile:cjs",
|
|
43
|
+
"compile:esm": "tsc -p tsconfig.esm.json",
|
|
44
|
+
"compile:cjs": "tsc -p tsconfig.cjs.json",
|
|
45
|
+
"test:unit": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --config jest.config.json",
|
|
46
|
+
"test:e2e": "npx tsc -p tsconfig.test.json && cross-env NODE_OPTIONS=--experimental-vm-modules jest --config jest.e2e.config.json --runInBand",
|
|
47
|
+
"coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --config jest.config.json --coverage"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@google/adk": "^0.1.2",
|
|
51
|
+
"@google/genai": "^1.14.0",
|
|
52
|
+
"@modelcontextprotocol/sdk": "1.17.5",
|
|
53
|
+
"@toolbox-sdk/core": "^0.1.2",
|
|
54
|
+
"axios": "^1.12.2",
|
|
55
|
+
"openapi-types": "^12.1.3",
|
|
56
|
+
"zod": "^3.24.4"
|
|
57
|
+
}
|
|
58
|
+
}
|