Skip to content

Commit 3a354ce

Browse files
committed
add docs
1 parent fe93385 commit 3a354ce

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

packages/toolbox-core/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,86 @@ workflow.add_edge("tools", "agent")
221221
app = workflow.compile()
222222
```
223223

224+
## Authenticating to the Toolbox Server
225+
226+
This section describes how to authenticate the ToolboxClient itself when
227+
connecting to a Toolbox server instance that requires authentication. This is
228+
crucial for securing your Toolbox server endpoint, especially when deployed on
229+
platforms like Cloud Run, GKE, or any environment where unauthenticated access is restricted.
230+
231+
This client-to-server authentication ensures that the Toolbox server can verify the identity of the client making the request before any tool is loaded or called. It is different from [Authenticating Tools](#authenticating-tools), which deals with providing credentials for specific tools within an already connected Toolbox session.
232+
233+
### When is Client-to-Server Authentication Needed?
234+
You'll need this type of authentication if your Toolbox server is configured to deny unauthenticated requests. For example:
235+
236+
- Your Toolbox server is deployed on Cloud Run and configured to "Require authentication."
237+
- Your server is behind an Identity-Aware Proxy (IAP) or a similar authentication layer.
238+
- You have custom authentication middleware on your self-hosted Toolbox server.
239+
240+
Without proper client authentication in these scenarios, attempts to connect or
241+
make calls (like `load_tool`) will likely fail with `Unauthorized` errors.
242+
243+
### How it works
244+
245+
The `ToolboxClient` (and `ToolboxSyncClient`) allows you to specify functions (or coroutines for the async client) that dynamically generate HTTP headers for every request sent to the Toolbox server. The most common use case is to add an Authorization header with a bearer token (e.g., a Google ID token).
246+
247+
These header-generating functions are called just before each request, ensuring
248+
that fresh credentials or header values can be used.
249+
250+
### Configuration
251+
252+
You can configure these dynamic headers in two ways:
253+
254+
1. **During Client Initialization**
255+
256+
```python
257+
from toolbox_core import ToolboxClient
258+
259+
client = ToolboxClient("toolbox-url", headers={"header1": header1_getter, "header2": header2_getter, ...})
260+
```
261+
262+
1. **After Client Initialization**
263+
264+
```python
265+
from toolbox_core import ToolboxClient
266+
267+
client = ToolboxClient("toolbox-url")
268+
client.add_headers({"header1": header1_getter, "header2": header2_getter, ...})
269+
```
270+
271+
### Authenticating with Google Cloud Servers
272+
273+
For Toolbox servers hosted on Google Cloud (e.g., Cloud Run) and requiring
274+
`Google ID token` authentication, the helper module
275+
[auth_methods](src/toolbox_core/auth_methods.py) provides utility functions.
276+
277+
### Step by Step Guide for Cloud Run
278+
279+
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
280+
Run service to the principal. This could be your `user account email` or a
281+
`service account`.
282+
2. **Configure Credentials**
283+
- Local Development: Set up
284+
[ADC](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).
285+
- Google Cloud Environments: When running within Google Cloud (e.g., Compute
286+
Engine, GKE, another Cloud Run service, Cloud Functions), ADC is typically
287+
configured automatically, using the environment's default service account.
288+
3. **Connect to the Toolbox Server**
289+
290+
```python
291+
from toolbox_core import auth_methods
292+
293+
auth_token_provider = auth_methods.aget_google_id_token # can also use sync method
294+
client = ToolboxClient(
295+
URL,
296+
client_headers={"Authorization": auth_token_provider},
297+
)
298+
tools = await client.load_toolset()
299+
300+
# Now, you can use the client as usual.
301+
```
302+
303+
224304
## Authenticating Tools
225305

226306
> [!WARNING]

0 commit comments

Comments
 (0)