Skip to content

Commit fe0d3d7

Browse files
committed
docs(toolbox-adk): improve README with comprehensive usage examples
1 parent f5c316b commit fe0d3d7

File tree

1 file changed

+162
-7
lines changed

1 file changed

+162
-7
lines changed

packages/toolbox-adk/README.md

Lines changed: 162 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
![MCP Toolbox Logo](https://raw.githubusercontent.com/googleapis/genai-toolbox/main/logo.png)
2+
13
# Toolbox ADK Integration
24

3-
This package allows Google ADK agents to natively use tools from the MCP Toolbox.
5+
This package allows Google ADK (Agent Development Kit) agents to natively use tools from the [MCP Toolbox](https://github.com/googleapis/genai-toolbox).
6+
7+
It provides a seamless bridge between the `toolbox-core` SDK and the ADK's `BaseTool` / `BaseToolset` interfaces, handling authentication propagation, header management, and tool wrapping automatically.
48

59
## Installation
610

@@ -10,18 +14,169 @@ pip install toolbox-adk
1014

1115
## Usage
1216

17+
The primary entry point is the `ToolboxToolset`, which loads tools from a remote Toolbox server and adapts them for use with ADK agents.
18+
19+
> [!NOTE]
20+
> The `ToolboxToolset` in this package mirrors the `ToolboxToolset` in the [`adk-python`](https://github.com/anubhav756/adk-python) package. The `adk-python` version is a shim that delegates all functionality to this implementation.
21+
1322
```python
1423
from toolbox_adk import ToolboxToolset, CredentialStrategy
24+
from google.adk.agents import Agent
1525

16-
# Configure auth (e.g., Use the agent's identity)
17-
creds = CredentialStrategy.toolbox_identity()
26+
# 1. Configure Authentication Strategy
27+
# Use the agent's own identity (Workload Identity)
28+
creds = CredentialStrategy.workload_identity(target_audience="https://my-toolbox-service-url")
29+
30+
# 2. Create the Toolset
31+
toolset = ToolboxToolset(
32+
server_url="https://my-toolbox-service-url",
33+
toolset_name="my-toolset", # Optional: Load specific toolset
34+
credentials=creds
35+
)
36+
37+
# 3. Use in your ADK Agent
38+
agent = Agent(tools=[toolset])
39+
```
40+
41+
## Authentication Strategies
42+
43+
The `toolbox-adk` package provides flexible authentication strategies to handle `Client-to-Server` authentication (securing the connection to the Toolbox server) and `User Identity` propagation (authenticating the user for specific tools).
44+
45+
Use the `CredentialStrategy` factory methods to create your configuration.
46+
47+
### Workload Identity (Recommended for Cloud Run / GKE)
48+
49+
Uses the agent's environment credentials (ADC) to generate an OIDC ID token. This is the standard way for one service to authenticate to another on Google Cloud.
50+
51+
```python
52+
# target_audience should match the URL of your Toolbox server
53+
creds = CredentialStrategy.workload_identity(target_audience="https://my-toolbox-service.run.app")
1854

19-
# Create the toolset
2055
toolset = ToolboxToolset(
21-
server_url="http://localhost:5000",
56+
server_url="https://my-toolbox-service.run.app",
2257
credentials=creds
2358
)
59+
```
60+
61+
### User Identity (3-Legged OAuth)
62+
63+
Propagates the end-user's identity to the Toolbox. This is used when the tools themselves need to act on behalf of the user (e.g., accessing the user's Drive or Calendar).
64+
65+
```python
66+
creds = CredentialStrategy.user_identity(
67+
client_id="YOUR_CLIENT_ID",
68+
client_secret="YOUR_CLIENT_SECRET",
69+
scopes=["https://www.googleapis.com/auth/drive"]
70+
)
71+
```
72+
73+
### Manual Token (Development / Testing)
74+
75+
Manually supply a token (e.g., a static API key or a temporary token).
76+
77+
```python
78+
creds = CredentialStrategy.manual_token("my-secret-token")
79+
```
2480

25-
# Use in your agent
26-
# agent = Agent(tools=toolset.get_tools())
81+
### Manual Credentials Object
82+
83+
Uses a provided `google.auth` Credentials object directly.
84+
85+
```python
86+
from google.oauth2 import service_account
87+
88+
my_creds = service_account.Credentials.from_service_account_file('key.json')
89+
creds = CredentialStrategy.manual_creds(my_creds)
90+
```
91+
92+
### Toolbox Identity (No Auth)
93+
94+
Use this if your Toolbox server does not require authentication (e.g., local development).
95+
96+
```python
97+
creds = CredentialStrategy.toolbox_identity()
2798
```
99+
100+
### Native ADK Integration
101+
102+
If you are using ADK's configuration system (`AuthConfig` objects), you can create the strategy directly from it.
103+
104+
```python
105+
# auth_config is an instance of google.adk.auth.auth_tool.AuthConfig
106+
creds = CredentialStrategy.from_adk_auth_config(auth_config)
107+
```
108+
109+
Or if you have the `AuthScheme` and `AuthCredential` objects separately:
110+
111+
```python
112+
# scheme is google.adk.auth.auth_tool.AuthScheme
113+
# credential is google.adk.auth.auth_credential.AuthCredential
114+
creds = CredentialStrategy.from_adk_credentials(scheme, credential)
115+
```
116+
117+
## Advanced Configuration
118+
119+
### Additional Headers
120+
121+
You can inject custom headers into every request made to the Toolbox server. This is useful for passing tracing IDs, API keys, or other metadata.
122+
123+
```python
124+
toolset = ToolboxToolset(
125+
server_url="...",
126+
additional_headers={
127+
"X-Trace-ID": "12345",
128+
"X-My-Header": lambda: get_dynamic_header_value() # Can be a callable
129+
}
130+
)
131+
```
132+
133+
### Global Parameter Binding
134+
135+
Bind values to tool parameters globally across all loaded tools. These values will be fixed and hidden from the LLM.
136+
137+
```python
138+
toolset = ToolboxToolset(
139+
server_url="...",
140+
bound_params={
141+
"region": "us-central1",
142+
"api_key": lambda: get_api_key() # Can be a callable
143+
}
144+
)
145+
```
146+
147+
### Usage with Hooks
148+
149+
You can attach `pre_hook` and `post_hook` functions to execute logic before and after every tool invocation.
150+
151+
> [!NOTE]
152+
> The `pre_hook` can modify `context.arguments` to dynamically alter the inputs passed to the tool.
153+
154+
```python
155+
from toolbox_adk import ToolboxContext
156+
157+
async def log_start(context: ToolboxContext):
158+
print(f"Starting tool with args: {context.arguments}")
159+
# context.tool_context is the underlying ADK ToolContext
160+
# Example: Inject or modify arguments
161+
# context.arguments["user_id"] = "123"
162+
163+
async def log_end(context: ToolboxContext):
164+
print("Finished tool execution")
165+
# Inspect result or error
166+
if context.error:
167+
print(f"Tool failed: {context.error}")
168+
169+
toolset = ToolboxToolset(
170+
server_url="...",
171+
pre_hook=log_start,
172+
post_hook=log_end
173+
)
174+
```
175+
176+
## Contributing
177+
178+
Contributions are welcome! Please refer to the `toolbox-core` [DEVELOPER.md](../toolbox-core/DEVELOPER.md) for general guidelines.
179+
180+
## License
181+
182+
This project is licensed under the Apache License 2.0.

0 commit comments

Comments
 (0)