Skip to content

Commit ca0ab79

Browse files
committed
add docs
1 parent b58d649 commit ca0ab79

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

packages/toolbox-langchain/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ applications, enabling advanced orchestration and interaction with GenAI models.
2020
- [Represent Tools as Nodes](#represent-tools-as-nodes)
2121
- [Connect Tools with LLM](#connect-tools-with-llm)
2222
- [Manual usage](#manual-usage)
23+
- [Client to Server Authentication](#client-to-server-authentication)
24+
- [When is Client-to-Server Authentication Needed?](#when-is-client-to-server-authentication-needed)
25+
- [How it works](#how-it-works)
26+
- [Configuration](#configuration)
27+
- [Authenticating with Google Cloud Servers](#authenticating-with-google-cloud-servers)
28+
- [Step by Step Guide for Cloud Run](#step-by-step-guide-for-cloud-run)
2329
- [Authenticating Tools](#authenticating-tools)
2430
- [Supported Authentication Mechanisms](#supported-authentication-mechanisms)
2531
- [Configure Tools](#configure-tools)
@@ -186,6 +192,87 @@ result = tools[0].invoke({"name": "Alice", "age": 30})
186192
This is useful for testing tools or when you need precise control over tool
187193
execution outside of an agent framework.
188194

195+
## Client to Server Authentication
196+
197+
This section describes how to authenticate the ToolboxClient itself when
198+
connecting to a Toolbox server instance that requires authentication. This is
199+
crucial for securing your Toolbox server endpoint, especially when deployed on
200+
platforms like Cloud Run, GKE, or any environment where unauthenticated access is restricted.
201+
202+
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.
203+
204+
### When is Client-to-Server Authentication Needed?
205+
206+
You'll need this type of authentication if your Toolbox server is configured to deny unauthenticated requests. For example:
207+
208+
- Your Toolbox server is deployed on Cloud Run and configured to "Require authentication."
209+
- Your server is behind an Identity-Aware Proxy (IAP) or a similar authentication layer.
210+
- You have custom authentication middleware on your self-hosted Toolbox server.
211+
212+
Without proper client authentication in these scenarios, attempts to connect or
213+
make calls (like `load_tool`) will likely fail with `Unauthorized` errors.
214+
215+
### How it works
216+
217+
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).
218+
219+
These header-generating functions are called just before each request, ensuring
220+
that fresh credentials or header values can be used.
221+
222+
### Configuration
223+
224+
You can configure these dynamic headers in two ways:
225+
226+
1. **During Client Initialization**
227+
228+
```python
229+
from toolbox_langchain import ToolboxClient
230+
231+
client = ToolboxClient("toolbox-url", headers={"header1": header1_getter, "header2": header2_getter, ...})
232+
```
233+
234+
1. **After Client Initialization**
235+
236+
```python
237+
from toolbox_langchain import ToolboxClient
238+
239+
client = ToolboxClient("toolbox-url")
240+
client.add_headers({"header1": header1_getter, "header2": header2_getter, ...})
241+
```
242+
243+
### Authenticating with Google Cloud Servers
244+
245+
For Toolbox servers hosted on Google Cloud (e.g., Cloud Run) and requiring
246+
`Google ID token` authentication, the helper module
247+
[auth_methods](src/toolbox_core/auth_methods.py) provides utility functions.
248+
249+
### Step by Step Guide for Cloud Run
250+
251+
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
252+
Run service to the principal. This could be your `user account email` or a
253+
`service account`.
254+
2. **Configure Credentials**
255+
- Local Development: Set up
256+
[ADC](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).
257+
- Google Cloud Environments: When running within Google Cloud (e.g., Compute
258+
Engine, GKE, another Cloud Run service, Cloud Functions), ADC is typically
259+
configured automatically, using the environment's default service account.
260+
3. **Connect to the Toolbox Server**
261+
262+
```python
263+
from toolbox_core import auth_methods
264+
265+
auth_token_provider = auth_methods.aget_google_id_token # can also use sync method
266+
client = ToolboxClient(
267+
URL,
268+
client_headers={"Authorization": auth_token_provider},
269+
)
270+
tools = await client.load_toolset()
271+
272+
# Now, you can use the client as usual.
273+
```
274+
275+
189276
## Authenticating Tools
190277

191278
> [!WARNING]

0 commit comments

Comments
 (0)