toolbox-core 0.2.1__tar.gz
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.
- toolbox_core-0.2.1/PKG-INFO +588 -0
- toolbox_core-0.2.1/README.md +553 -0
- toolbox_core-0.2.1/pyproject.toml +69 -0
- toolbox_core-0.2.1/setup.cfg +4 -0
- toolbox_core-0.2.1/src/toolbox_core/__init__.py +18 -0
- toolbox_core-0.2.1/src/toolbox_core/auth_methods.py +171 -0
- toolbox_core-0.2.1/src/toolbox_core/client.py +367 -0
- toolbox_core-0.2.1/src/toolbox_core/protocol.py +72 -0
- toolbox_core-0.2.1/src/toolbox_core/py.typed +13 -0
- toolbox_core-0.2.1/src/toolbox_core/sync_client.py +184 -0
- toolbox_core-0.2.1/src/toolbox_core/sync_tool.py +238 -0
- toolbox_core-0.2.1/src/toolbox_core/tool.py +461 -0
- toolbox_core-0.2.1/src/toolbox_core/utils.py +146 -0
- toolbox_core-0.2.1/src/toolbox_core/version.py +15 -0
- toolbox_core-0.2.1/src/toolbox_core.egg-info/PKG-INFO +588 -0
- toolbox_core-0.2.1/src/toolbox_core.egg-info/SOURCES.txt +26 -0
- toolbox_core-0.2.1/src/toolbox_core.egg-info/dependency_links.txt +1 -0
- toolbox_core-0.2.1/src/toolbox_core.egg-info/requires.txt +15 -0
- toolbox_core-0.2.1/src/toolbox_core.egg-info/top_level.txt +1 -0
- toolbox_core-0.2.1/tests/test_auth_methods.py +405 -0
- toolbox_core-0.2.1/tests/test_client.py +1512 -0
- toolbox_core-0.2.1/tests/test_e2e.py +219 -0
- toolbox_core-0.2.1/tests/test_protocol.py +108 -0
- toolbox_core-0.2.1/tests/test_sync_client.py +591 -0
- toolbox_core-0.2.1/tests/test_sync_e2e.py +187 -0
- toolbox_core-0.2.1/tests/test_sync_tool.py +326 -0
- toolbox_core-0.2.1/tests/test_tool.py +738 -0
- toolbox_core-0.2.1/tests/test_utils.py +459 -0
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: toolbox-core
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Python Base SDK for interacting with the Toolbox service
|
|
5
|
+
Author-email: Google LLC <googleapis-packages@google.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/googleapis/mcp-toolbox-sdk-python/blob/main/packages/toolbox-core
|
|
7
|
+
Project-URL: Repository, https://github.com/googleapis/mcp-toolbox-sdk-python.git
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/googleapis/mcp-toolbox-sdk-python/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/googleapis/mcp-toolbox-sdk-python/blob/main/packages/toolbox-core/CHANGELOG.md
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
Requires-Dist: pydantic<3.0.0,>=2.7.0
|
|
22
|
+
Requires-Dist: aiohttp<4.0.0,>=3.8.6
|
|
23
|
+
Requires-Dist: deprecated<2.0.0,>=1.2.15
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: black[jupyter]==25.1.0; extra == "test"
|
|
26
|
+
Requires-Dist: isort==6.0.1; extra == "test"
|
|
27
|
+
Requires-Dist: mypy==1.16.0; extra == "test"
|
|
28
|
+
Requires-Dist: pytest==8.4.0; extra == "test"
|
|
29
|
+
Requires-Dist: pytest-aioresponses==0.3.0; extra == "test"
|
|
30
|
+
Requires-Dist: pytest-asyncio==1.0.0; extra == "test"
|
|
31
|
+
Requires-Dist: pytest-cov==6.2.1; extra == "test"
|
|
32
|
+
Requires-Dist: pytest-mock==3.14.1; extra == "test"
|
|
33
|
+
Requires-Dist: google-cloud-secret-manager==2.24.0; extra == "test"
|
|
34
|
+
Requires-Dist: google-cloud-storage==3.1.0; extra == "test"
|
|
35
|
+
|
|
36
|
+

|
|
37
|
+
|
|
38
|
+
# MCP Toolbox Core SDK
|
|
39
|
+
|
|
40
|
+
[](https://badge.fury.io/py/toolbox-core) [](https://pypi.org/project/toolbox-core/) [](https://coveralls.io/github/googleapis/genai-toolbox?branch=main)
|
|
41
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
42
|
+
|
|
43
|
+
This SDK allows you to seamlessly integrate the functionalities of
|
|
44
|
+
[Toolbox](https://github.com/googleapis/genai-toolbox) allowing you to load and
|
|
45
|
+
use tools defined in the service as standard Python functions within your GenAI
|
|
46
|
+
applications.
|
|
47
|
+
|
|
48
|
+
This simplifies integrating external functionalities (like APIs, databases, or
|
|
49
|
+
custom logic) managed by the Toolbox into your workflows, especially those
|
|
50
|
+
involving Large Language Models (LLMs).
|
|
51
|
+
|
|
52
|
+
<!-- TOC ignore:true -->
|
|
53
|
+
<!-- TOC -->
|
|
54
|
+
|
|
55
|
+
- [Installation](#installation)
|
|
56
|
+
- [Quickstart](#quickstart)
|
|
57
|
+
- [Usage](#usage)
|
|
58
|
+
- [Loading Tools](#loading-tools)
|
|
59
|
+
- [Load a toolset](#load-a-toolset)
|
|
60
|
+
- [Load a single tool](#load-a-single-tool)
|
|
61
|
+
- [Invoking Tools](#invoking-tools)
|
|
62
|
+
- [Synchronous Usage](#synchronous-usage)
|
|
63
|
+
- [Use with LangGraph](#use-with-langgraph)
|
|
64
|
+
- [Client to Server Authentication](#client-to-server-authentication)
|
|
65
|
+
- [When is Client-to-Server Authentication Needed?](#when-is-client-to-server-authentication-needed)
|
|
66
|
+
- [How it works](#how-it-works)
|
|
67
|
+
- [Configuration](#configuration)
|
|
68
|
+
- [Authenticating with Google Cloud Servers](#authenticating-with-google-cloud-servers)
|
|
69
|
+
- [Step by Step Guide for Cloud Run](#step-by-step-guide-for-cloud-run)
|
|
70
|
+
- [Authenticating Tools](#authenticating-tools)
|
|
71
|
+
- [When is Authentication Needed?](#when-is-authentication-needed)
|
|
72
|
+
- [Supported Authentication Mechanisms](#supported-authentication-mechanisms)
|
|
73
|
+
- [Step 1: Configure Tools in Toolbox Service](#step-1-configure-tools-in-toolbox-service)
|
|
74
|
+
- [Step 2: Configure SDK Client](#step-2-configure-sdk-client)
|
|
75
|
+
- [Provide an ID Token Retriever Function](#provide-an-id-token-retriever-function)
|
|
76
|
+
- [Option A: Add Authentication to a Loaded Tool](#option-a-add-authentication-to-a-loaded-tool)
|
|
77
|
+
- [Option B: Add Authentication While Loading Tools](#option-b-add-authentication-while-loading-tools)
|
|
78
|
+
- [Complete Authentication Example](#complete-authentication-example)
|
|
79
|
+
- [Binding Parameter Values](#binding-parameter-values)
|
|
80
|
+
- [Why Bind Parameters?](#why-bind-parameters)
|
|
81
|
+
- [Option A: Binding Parameters to a Loaded Tool](#option-a-binding-parameters-to-a-loaded-tool)
|
|
82
|
+
- [Option B: Binding Parameters While Loading Tools](#option-b-binding-parameters-while-loading-tools)
|
|
83
|
+
- [Binding Dynamic Values](#binding-dynamic-values)
|
|
84
|
+
- [Contributing](#contributing)
|
|
85
|
+
- [License](#license)
|
|
86
|
+
- [Support](#support)
|
|
87
|
+
|
|
88
|
+
<!-- /TOC -->
|
|
89
|
+
|
|
90
|
+
## Installation
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pip install toolbox-core
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
> [!NOTE]
|
|
97
|
+
>
|
|
98
|
+
> - The primary `ToolboxClient` is asynchronous and requires using `await` for
|
|
99
|
+
> loading and invoking tools, as shown in most examples.
|
|
100
|
+
> - Asynchronous code needs to run within an event loop (e.g., using
|
|
101
|
+
> `asyncio.run()` or in an async framework). See the [Python `asyncio`
|
|
102
|
+
> documentation](https://docs.python.org/3/library/asyncio-task.html) for more
|
|
103
|
+
> details.
|
|
104
|
+
> - If you prefer synchronous execution, refer to the [Synchronous
|
|
105
|
+
> Usage](#synchronous-usage) section below.
|
|
106
|
+
|
|
107
|
+
> [!IMPORTANT]
|
|
108
|
+
>
|
|
109
|
+
> The `ToolboxClient` (and its synchronous counterpart `ToolboxSyncClient`)
|
|
110
|
+
> interacts with network resources using an underlying HTTP client session. You
|
|
111
|
+
> should remember to use a context manager or explicitly call `close()` to clean
|
|
112
|
+
> up these resources. If you provide your own session, you'll need to close it
|
|
113
|
+
> in addition to calling `ToolboxClient.close()`.
|
|
114
|
+
|
|
115
|
+
## Quickstart
|
|
116
|
+
|
|
117
|
+
Here's a minimal example to get you started. Ensure your Toolbox service is
|
|
118
|
+
running and accessible.
|
|
119
|
+
|
|
120
|
+
```py
|
|
121
|
+
import asyncio
|
|
122
|
+
from toolbox_core import ToolboxClient
|
|
123
|
+
|
|
124
|
+
async def main():
|
|
125
|
+
# Replace with the actual URL where your Toolbox service is running
|
|
126
|
+
async with ToolboxClient("http://127.0.0.1:5000") as toolbox:
|
|
127
|
+
weather_tool = await toolbox.load_tool("get_weather")
|
|
128
|
+
result = await weather_tool(location="London")
|
|
129
|
+
print(result)
|
|
130
|
+
|
|
131
|
+
if __name__ == "__main__":
|
|
132
|
+
asyncio.run(main())
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
> [!IMPORTANT]
|
|
136
|
+
> If you initialize `ToolboxClient` without providing an external session and
|
|
137
|
+
> cannot use `async with`, you must explicitly close the client using `await
|
|
138
|
+
> toolbox.close()` in a `finally` block. This ensures the internally created
|
|
139
|
+
> session is closed.
|
|
140
|
+
>
|
|
141
|
+
> ```py
|
|
142
|
+
> toolbox = ToolboxClient("http://127.0.0.1:5000")
|
|
143
|
+
> try:
|
|
144
|
+
> # ... use toolbox ...
|
|
145
|
+
> finally:
|
|
146
|
+
> await toolbox.close()
|
|
147
|
+
> ```
|
|
148
|
+
|
|
149
|
+
## Usage
|
|
150
|
+
|
|
151
|
+
Import and initialize a Toolbox client, pointing it to the URL of your running
|
|
152
|
+
Toolbox service.
|
|
153
|
+
|
|
154
|
+
```py
|
|
155
|
+
from toolbox_core import ToolboxClient
|
|
156
|
+
|
|
157
|
+
# Replace with your Toolbox service's URL
|
|
158
|
+
async with ToolboxClient("http://127.0.0.1:5000") as toolbox:
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
All interactions for loading and invoking tools happen through this client.
|
|
162
|
+
|
|
163
|
+
> [!NOTE]
|
|
164
|
+
> For advanced use cases, you can provide an external `aiohttp.ClientSession`
|
|
165
|
+
> during initialization (e.g., `ToolboxClient(url, session=my_session)`). If you
|
|
166
|
+
> provide your own session, you are responsible for managing its lifecycle;
|
|
167
|
+
> `ToolboxClient` *will not* close it.
|
|
168
|
+
|
|
169
|
+
> [!IMPORTANT]
|
|
170
|
+
> Closing the `ToolboxClient` also closes the underlying network session shared by
|
|
171
|
+
> all tools loaded from that client. As a result, any tool instances you have
|
|
172
|
+
> loaded will cease to function and will raise an error if you attempt to invoke
|
|
173
|
+
> them after the client is closed.
|
|
174
|
+
|
|
175
|
+
## Loading Tools
|
|
176
|
+
|
|
177
|
+
You can load tools individually or in groups (toolsets) as defined in your
|
|
178
|
+
Toolbox service configuration. Loading a toolset is convenient when working with
|
|
179
|
+
multiple related functions, while loading a single tool offers more granular
|
|
180
|
+
control.
|
|
181
|
+
|
|
182
|
+
### Load a toolset
|
|
183
|
+
|
|
184
|
+
A toolset is a collection of related tools. You can load all tools in a toolset
|
|
185
|
+
or a specific one:
|
|
186
|
+
|
|
187
|
+
```py
|
|
188
|
+
# Load all tools
|
|
189
|
+
tools = await toolbox.load_toolset()
|
|
190
|
+
|
|
191
|
+
# Load a specific toolset
|
|
192
|
+
tools = await toolbox.load_toolset("my-toolset")
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Load a single tool
|
|
196
|
+
|
|
197
|
+
Loads a specific tool by its unique name. This provides fine-grained control.
|
|
198
|
+
|
|
199
|
+
```py
|
|
200
|
+
tool = await toolbox.load_tool("my-tool")
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Invoking Tools
|
|
204
|
+
|
|
205
|
+
Once loaded, tools behave like awaitable Python functions. You invoke them using
|
|
206
|
+
`await` and pass arguments corresponding to the parameters defined in the tool's
|
|
207
|
+
configuration within the Toolbox service.
|
|
208
|
+
|
|
209
|
+
```py
|
|
210
|
+
tool = await toolbox.load_tool("my-tool")
|
|
211
|
+
result = await tool("foo", bar="baz")
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
> [!TIP]
|
|
215
|
+
> For a more comprehensive guide on setting up the Toolbox service itself, which
|
|
216
|
+
> you'll need running to use this SDK, please refer to the [Toolbox Quickstart
|
|
217
|
+
> Guide](https://googleapis.github.io/genai-toolbox/getting-started/local_quickstart).
|
|
218
|
+
|
|
219
|
+
## Synchronous Usage
|
|
220
|
+
|
|
221
|
+
By default, the `ToolboxClient` and the `ToolboxTool` objects it produces behave like asynchronous Python functions, requiring the use of `await`.
|
|
222
|
+
|
|
223
|
+
If your application primarily uses synchronous code, or you prefer not to manage an asyncio event loop, you can use the synchronous alternatives provided:
|
|
224
|
+
|
|
225
|
+
* `ToolboxSyncClient`: The synchronous counterpart to `ToolboxClient`.
|
|
226
|
+
* `ToolboxSyncTool`: The synchronous counterpart to `ToolboxTool`.
|
|
227
|
+
|
|
228
|
+
The `ToolboxSyncClient` handles communication with the Toolbox service synchronously and produces `ToolboxSyncTool` instances when you load tools. You do not use the `await` keyword when interacting with these synchronous versions.
|
|
229
|
+
|
|
230
|
+
```py
|
|
231
|
+
from toolbox_core import ToolboxSyncClient
|
|
232
|
+
|
|
233
|
+
with ToolboxSyncClient("http://127.0.0.1:5000") as toolbox:
|
|
234
|
+
weather_tool = toolbox.load_tool("get_weather")
|
|
235
|
+
result = weather_tool(location="Paris")
|
|
236
|
+
print(result)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
> [!TIP]
|
|
240
|
+
> While synchronous invocation is available for convenience, it's generally
|
|
241
|
+
> considered best practice to use asynchronous operations (like those provided
|
|
242
|
+
> by the default `ToolboxClient` and `ToolboxTool`) for an I/O-bound task like
|
|
243
|
+
> tool invocation. Asynchronous programming allows for cooperative multitasking,
|
|
244
|
+
> often leading to better performance and resource utilization, especially in
|
|
245
|
+
> applications handling concurrent requests.
|
|
246
|
+
|
|
247
|
+
## Use with LangGraph
|
|
248
|
+
|
|
249
|
+
The Toolbox Core SDK integrates smoothly with frameworks like LangGraph,
|
|
250
|
+
allowing you to incorporate tools managed by the Toolbox service into your
|
|
251
|
+
agentic workflows.
|
|
252
|
+
|
|
253
|
+
> [!TIP]
|
|
254
|
+
> The loaded tools (both async `ToolboxTool` and sync `ToolboxSyncTool`) are
|
|
255
|
+
> callable and can often be used directly. However, to ensure parameter
|
|
256
|
+
> descriptions from Google-style docstrings are accurately parsed and made
|
|
257
|
+
> available to the LLM (via `bind_tools()`) and LangGraph internals, it's
|
|
258
|
+
> recommended to wrap the loaded tools using LangChain's
|
|
259
|
+
> [`StructuredTool`](https://python.langchain.com/api_reference/core/tools/langchain_core.tools.structured.StructuredTool.html).
|
|
260
|
+
|
|
261
|
+
Here's a conceptual example adapting the [official LangGraph tool calling
|
|
262
|
+
guide](https://langchain-ai.github.io/langgraph/how-tos/tool-calling):
|
|
263
|
+
|
|
264
|
+
```py
|
|
265
|
+
from toolbox_core import ToolboxClient
|
|
266
|
+
from langchain_google_vertexai import ChatVertexAI
|
|
267
|
+
from langgraph.graph import StateGraph, MessagesState, START, END
|
|
268
|
+
from langgraph.prebuilt import ToolNode
|
|
269
|
+
from langchain.tools import StructuredTool
|
|
270
|
+
|
|
271
|
+
async with ToolboxClient("http://127.0.0.1:5000") as toolbox:
|
|
272
|
+
tools = await toolbox.load_toolset()
|
|
273
|
+
wrapped_tools = [StructuredTool.from_function(tool, parse_docstring=True) for tool in tools]
|
|
274
|
+
model_with_tools = ChatVertexAI(model="gemini-2.0-flash-001").bind_tools(wrapped_tools)
|
|
275
|
+
|
|
276
|
+
def call_model(state: MessagesState):
|
|
277
|
+
messages = state["messages"]
|
|
278
|
+
response = model_with_tools.invoke(messages)
|
|
279
|
+
return {"messages": [response]}
|
|
280
|
+
|
|
281
|
+
def should_continue(state: MessagesState):
|
|
282
|
+
messages = state["messages"]
|
|
283
|
+
last_message = messages[-1]
|
|
284
|
+
if last_message.tool_calls:
|
|
285
|
+
return "tools"
|
|
286
|
+
return END
|
|
287
|
+
|
|
288
|
+
workflow = StateGraph(MessagesState)
|
|
289
|
+
|
|
290
|
+
workflow.add_node("agent", call_model)
|
|
291
|
+
workflow.add_node("tools", ToolNode(wrapped_tools))
|
|
292
|
+
|
|
293
|
+
workflow.add_edge(START, "agent")
|
|
294
|
+
workflow.add_conditional_edges("agent", should_continue, ["tools", END])
|
|
295
|
+
workflow.add_edge("tools", "agent")
|
|
296
|
+
|
|
297
|
+
app = workflow.compile()
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## Client to Server Authentication
|
|
301
|
+
|
|
302
|
+
This section describes how to authenticate the ToolboxClient itself when
|
|
303
|
+
connecting to a Toolbox server instance that requires authentication. This is
|
|
304
|
+
crucial for securing your Toolbox server endpoint, especially when deployed on
|
|
305
|
+
platforms like Cloud Run, GKE, or any environment where unauthenticated access is restricted.
|
|
306
|
+
|
|
307
|
+
This client-to-server authentication ensures that the Toolbox server can verify
|
|
308
|
+
the identity of the client making the request before any tool is loaded or
|
|
309
|
+
called. It is different from [Authenticating Tools](#authenticating-tools),
|
|
310
|
+
which deals with providing credentials for specific tools within an already
|
|
311
|
+
connected Toolbox session.
|
|
312
|
+
|
|
313
|
+
### When is Client-to-Server Authentication Needed?
|
|
314
|
+
|
|
315
|
+
You'll need this type of authentication if your Toolbox server is configured to
|
|
316
|
+
deny unauthenticated requests. For example:
|
|
317
|
+
|
|
318
|
+
- Your Toolbox server is deployed on Cloud Run and configured to "Require authentication."
|
|
319
|
+
- Your server is behind an Identity-Aware Proxy (IAP) or a similar
|
|
320
|
+
authentication layer.
|
|
321
|
+
- You have custom authentication middleware on your self-hosted Toolbox server.
|
|
322
|
+
|
|
323
|
+
Without proper client authentication in these scenarios, attempts to connect or
|
|
324
|
+
make calls (like `load_tool`) will likely fail with `Unauthorized` errors.
|
|
325
|
+
|
|
326
|
+
### How it works
|
|
327
|
+
|
|
328
|
+
The `ToolboxClient` (and `ToolboxSyncClient`) allows you to specify functions
|
|
329
|
+
(or coroutines for the async client) that dynamically generate HTTP headers for
|
|
330
|
+
every request sent to the Toolbox server. The most common use case is to add an
|
|
331
|
+
Authorization header with a bearer token (e.g., a Google ID token).
|
|
332
|
+
|
|
333
|
+
These header-generating functions are called just before each request, ensuring
|
|
334
|
+
that fresh credentials or header values can be used.
|
|
335
|
+
|
|
336
|
+
### Configuration
|
|
337
|
+
|
|
338
|
+
You can configure these dynamic headers as seen below:
|
|
339
|
+
|
|
340
|
+
```python
|
|
341
|
+
from toolbox_core import ToolboxClient
|
|
342
|
+
|
|
343
|
+
async with ToolboxClient("toolbox-url", client_headers={"header1": header1_getter, "header2": header2_getter, ...}) as client:
|
|
344
|
+
# Use client
|
|
345
|
+
pass
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### Authenticating with Google Cloud Servers
|
|
349
|
+
|
|
350
|
+
For Toolbox servers hosted on Google Cloud (e.g., Cloud Run) and requiring
|
|
351
|
+
`Google ID token` authentication, the helper module
|
|
352
|
+
[auth_methods](src/toolbox_core/auth_methods.py) provides utility functions.
|
|
353
|
+
|
|
354
|
+
### Step by Step Guide for Cloud Run
|
|
355
|
+
|
|
356
|
+
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
|
|
357
|
+
Run service to the principal. This could be your `user account email` or a
|
|
358
|
+
`service account`.
|
|
359
|
+
2. **Configure Credentials**
|
|
360
|
+
- Local Development: Set up
|
|
361
|
+
[ADC](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).
|
|
362
|
+
- Google Cloud Environments: When running within Google Cloud (e.g., Compute
|
|
363
|
+
Engine, GKE, another Cloud Run service, Cloud Functions), ADC is typically
|
|
364
|
+
configured automatically, using the environment's default service account.
|
|
365
|
+
3. **Connect to the Toolbox Server**
|
|
366
|
+
|
|
367
|
+
```python
|
|
368
|
+
from toolbox_core import auth_methods
|
|
369
|
+
|
|
370
|
+
auth_token_provider = auth_methods.aget_google_id_token # can also use sync method
|
|
371
|
+
async with ToolboxClient(
|
|
372
|
+
URL,
|
|
373
|
+
client_headers={"Authorization": auth_token_provider},
|
|
374
|
+
) as client:
|
|
375
|
+
tools = await client.load_toolset()
|
|
376
|
+
|
|
377
|
+
# Now, you can use the client as usual.
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Authenticating Tools
|
|
381
|
+
|
|
382
|
+
> [!WARNING]
|
|
383
|
+
> **Always use HTTPS** to connect your application with the Toolbox service,
|
|
384
|
+
> especially in **production environments** or whenever the communication
|
|
385
|
+
> involves **sensitive data** (including scenarios where tools require
|
|
386
|
+
> authentication tokens). Using plain HTTP lacks encryption and exposes your
|
|
387
|
+
> application and data to significant security risks, such as eavesdropping and
|
|
388
|
+
> tampering.
|
|
389
|
+
|
|
390
|
+
Tools can be configured within the Toolbox service to require authentication,
|
|
391
|
+
ensuring only authorized users or applications can invoke them, especially when
|
|
392
|
+
accessing sensitive data.
|
|
393
|
+
|
|
394
|
+
### When is Authentication Needed?
|
|
395
|
+
|
|
396
|
+
Authentication is configured per-tool within the Toolbox service itself. If a
|
|
397
|
+
tool you intend to use is marked as requiring authentication in the service, you
|
|
398
|
+
must configure the SDK client to provide the necessary credentials (currently
|
|
399
|
+
Oauth2 tokens) when invoking that specific tool.
|
|
400
|
+
|
|
401
|
+
### Supported Authentication Mechanisms
|
|
402
|
+
|
|
403
|
+
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)
|
|
404
|
+
|
|
405
|
+
### Step 1: Configure Tools in Toolbox Service
|
|
406
|
+
|
|
407
|
+
First, ensure the target tool(s) are configured correctly in the Toolbox service
|
|
408
|
+
to require authentication. Refer to the [Toolbox Service Documentation -
|
|
409
|
+
Authenticated
|
|
410
|
+
Parameters](https://googleapis.github.io/genai-toolbox/resources/tools/#authenticated-parameters)
|
|
411
|
+
for instructions.
|
|
412
|
+
|
|
413
|
+
### Step 2: Configure SDK Client
|
|
414
|
+
|
|
415
|
+
Your application needs a way to obtain the required Oauth2 token for the
|
|
416
|
+
authenticated user. The SDK requires you to provide a function capable of
|
|
417
|
+
retrieving this token *when the tool is invoked*.
|
|
418
|
+
|
|
419
|
+
#### Provide an ID Token Retriever Function
|
|
420
|
+
|
|
421
|
+
You must provide the SDK with a function (sync or async) that returns the
|
|
422
|
+
necessary token when called. The implementation depends on your application's
|
|
423
|
+
authentication flow (e.g., retrieving a stored token, initiating an OAuth flow).
|
|
424
|
+
|
|
425
|
+
> [!IMPORTANT]
|
|
426
|
+
> The name used when registering the getter function with the SDK (e.g.,
|
|
427
|
+
> `"my_api_token"`) must exactly match the `name` of the corresponding
|
|
428
|
+
> `authServices` defined in the tool's configuration within the Toolbox service.
|
|
429
|
+
|
|
430
|
+
```py
|
|
431
|
+
async def get_auth_token():
|
|
432
|
+
# ... Logic to retrieve ID token (e.g., from local storage, OAuth flow)
|
|
433
|
+
# This example just returns a placeholder. Replace with your actual token retrieval.
|
|
434
|
+
return "YOUR_ID_TOKEN" # Placeholder
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
> [!TIP]
|
|
438
|
+
> Your token retriever function is invoked every time an authenticated parameter
|
|
439
|
+
> requires a token for a tool call. Consider implementing caching logic within
|
|
440
|
+
> this function to avoid redundant token fetching or generation, especially for
|
|
441
|
+
> tokens with longer validity periods or if the retrieval process is
|
|
442
|
+
> resource-intensive.
|
|
443
|
+
|
|
444
|
+
#### Option A: Add Authentication to a Loaded Tool
|
|
445
|
+
|
|
446
|
+
You can add the token retriever function to a tool object *after* it has been
|
|
447
|
+
loaded. This modifies the specific tool instance.
|
|
448
|
+
|
|
449
|
+
```py
|
|
450
|
+
async with ToolboxClient("http://127.0.0.1:5000") as toolbox:
|
|
451
|
+
tool = await toolbox.load_tool("my-tool")
|
|
452
|
+
|
|
453
|
+
auth_tool = tool.add_auth_token_getter("my_auth", get_auth_token) # Single token
|
|
454
|
+
|
|
455
|
+
# OR
|
|
456
|
+
|
|
457
|
+
multi_auth_tool = tool.add_auth_token_getters({
|
|
458
|
+
"my_auth_1": get_auth_token_1,
|
|
459
|
+
"my_auth_2": get_auth_token_2,
|
|
460
|
+
}) # Multiple tokens
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
#### Option B: Add Authentication While Loading Tools
|
|
464
|
+
|
|
465
|
+
You can provide the token retriever(s) directly during the `load_tool` or
|
|
466
|
+
`load_toolset` calls. This applies the authentication configuration only to the
|
|
467
|
+
tools loaded in that specific call, without modifying the original tool objects
|
|
468
|
+
if they were loaded previously.
|
|
469
|
+
|
|
470
|
+
```py
|
|
471
|
+
auth_tool = await toolbox.load_tool(auth_token_getters={"my_auth": get_auth_token})
|
|
472
|
+
|
|
473
|
+
# OR
|
|
474
|
+
|
|
475
|
+
auth_tools = await toolbox.load_toolset(auth_token_getters={"my_auth": get_auth_token})
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
> [!NOTE]
|
|
479
|
+
> Adding auth tokens during loading only affect the tools loaded within that
|
|
480
|
+
> call.
|
|
481
|
+
|
|
482
|
+
### Complete Authentication Example
|
|
483
|
+
|
|
484
|
+
```py
|
|
485
|
+
import asyncio
|
|
486
|
+
from toolbox_core import ToolboxClient
|
|
487
|
+
|
|
488
|
+
async def get_auth_token():
|
|
489
|
+
# ... Logic to retrieve ID token (e.g., from local storage, OAuth flow)
|
|
490
|
+
# This example just returns a placeholder. Replace with your actual token retrieval.
|
|
491
|
+
return "YOUR_ID_TOKEN" # Placeholder
|
|
492
|
+
|
|
493
|
+
async with ToolboxClient("http://127.0.0.1:5000") as toolbox:
|
|
494
|
+
tool = await toolbox.load_tool("my-tool")
|
|
495
|
+
|
|
496
|
+
auth_tool = tool.add_auth_token_getters({"my_auth": get_auth_token})
|
|
497
|
+
result = auth_tool(input="some input")
|
|
498
|
+
print(result)
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
> [!NOTE]
|
|
502
|
+
> An auth token getter for a specific name (e.g., "GOOGLE_ID") will replace any
|
|
503
|
+
> client header with the same name followed by "_token" (e.g.,
|
|
504
|
+
> "GOOGLE_ID_token").
|
|
505
|
+
|
|
506
|
+
## Binding Parameter Values
|
|
507
|
+
|
|
508
|
+
The SDK allows you to pre-set, or "bind", values for specific tool parameters
|
|
509
|
+
before the tool is invoked or even passed to an LLM. These bound values are
|
|
510
|
+
fixed and will not be requested or modified by the LLM during tool use.
|
|
511
|
+
|
|
512
|
+
### Why Bind Parameters?
|
|
513
|
+
|
|
514
|
+
- **Protecting sensitive information:** API keys, secrets, etc.
|
|
515
|
+
- **Enforcing consistency:** Ensuring specific values for certain parameters.
|
|
516
|
+
- **Pre-filling known data:** Providing defaults or context.
|
|
517
|
+
|
|
518
|
+
> [!IMPORTANT]
|
|
519
|
+
> The parameter names used for binding (e.g., `"api_key"`) must exactly match the
|
|
520
|
+
> parameter names defined in the tool's configuration within the Toolbox
|
|
521
|
+
> service.
|
|
522
|
+
|
|
523
|
+
> [!NOTE]
|
|
524
|
+
> You do not need to modify the tool's configuration in the Toolbox service to
|
|
525
|
+
> bind parameter values using the SDK.
|
|
526
|
+
|
|
527
|
+
### Option A: Binding Parameters to a Loaded Tool
|
|
528
|
+
|
|
529
|
+
Bind values to a tool object *after* it has been loaded. This modifies the
|
|
530
|
+
specific tool instance.
|
|
531
|
+
|
|
532
|
+
```py
|
|
533
|
+
async with ToolboxClient("http://127.0.0.1:5000") as toolbox:
|
|
534
|
+
tool = await toolbox.load_tool("my-tool")
|
|
535
|
+
|
|
536
|
+
bound_tool = tool.bind_param("param", "value")
|
|
537
|
+
|
|
538
|
+
# OR
|
|
539
|
+
|
|
540
|
+
bound_tool = tool.bind_params({"param": "value"})
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
### Option B: Binding Parameters While Loading Tools
|
|
544
|
+
|
|
545
|
+
Specify bound parameters directly when loading tools. This applies the binding
|
|
546
|
+
only to the tools loaded in that specific call.
|
|
547
|
+
|
|
548
|
+
```py
|
|
549
|
+
bound_tool = await toolbox.load_tool("my-tool", bound_params={"param": "value"})
|
|
550
|
+
|
|
551
|
+
# OR
|
|
552
|
+
|
|
553
|
+
bound_tools = await toolbox.load_toolset(bound_params={"param": "value"})
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
> [!NOTE]
|
|
557
|
+
> Bound values during loading only affect the tools loaded in that call.
|
|
558
|
+
|
|
559
|
+
### Binding Dynamic Values
|
|
560
|
+
|
|
561
|
+
Instead of a static value, you can bind a parameter to a synchronous or
|
|
562
|
+
asynchronous function. This function will be called *each time* the tool is
|
|
563
|
+
invoked to dynamically determine the parameter's value at runtime.
|
|
564
|
+
|
|
565
|
+
```py
|
|
566
|
+
async def get_dynamic_value():
|
|
567
|
+
# Logic to determine the value
|
|
568
|
+
return "dynamic_value"
|
|
569
|
+
|
|
570
|
+
dynamic_bound_tool = tool.bind_param("param", get_dynamic_value)
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
> [!IMPORTANT]
|
|
574
|
+
> You don't need to modify tool configurations to bind parameter values.
|
|
575
|
+
|
|
576
|
+
# Contributing
|
|
577
|
+
|
|
578
|
+
Contributions are welcome! Please refer to the [DEVELOPER.md](./DEVELOPER.md)
|
|
579
|
+
file for guidelines on how to set up a development environment and run tests.
|
|
580
|
+
|
|
581
|
+
# License
|
|
582
|
+
|
|
583
|
+
This project is licensed under the Apache License 2.0. See the
|
|
584
|
+
[LICENSE](https://github.com/googleapis/genai-toolbox/blob/main/LICENSE) file for details.
|
|
585
|
+
|
|
586
|
+
# Support
|
|
587
|
+
|
|
588
|
+
If you encounter issues or have questions, check the existing [GitHub Issues](https://github.com/googleapis/genai-toolbox/issues) for the main Toolbox project.
|