0
# Tools Framework
1
2
Comprehensive tool ecosystem including built-in tools, custom function wrappers, and specialized toolsets for Google Cloud services, databases, and APIs.
3
4
## Capabilities
5
6
### Core Tool Classes
7
8
Base classes and implementations for creating and managing tools within the ADK framework.
9
10
```python { .api }
11
class BaseTool:
12
"""Base class for all tools."""
13
14
def __init__(self, name: str, description: str, **kwargs):
15
"""
16
Initialize a base tool.
17
18
Args:
19
name (str): Tool name
20
description (str): Tool description
21
**kwargs: Additional tool parameters
22
"""
23
pass
24
25
def execute(self, context: ToolContext, **kwargs):
26
"""
27
Execute the tool.
28
29
Args:
30
context (ToolContext): Tool execution context
31
**kwargs: Tool-specific parameters
32
33
Returns:
34
Tool execution result
35
"""
36
pass
37
38
class FunctionTool(BaseTool):
39
"""Tool wrapper for Python functions."""
40
41
def __init__(self, func: callable, name: str = None, description: str = None, **kwargs):
42
"""
43
Initialize a function tool.
44
45
Args:
46
func (callable): Python function to wrap
47
name (str, optional): Tool name (defaults to function name)
48
description (str, optional): Tool description (defaults to function docstring)
49
**kwargs: Additional tool parameters
50
"""
51
pass
52
53
class LongRunningFunctionTool(BaseTool):
54
"""Tool for long-running operations."""
55
56
def __init__(self, func: callable, timeout: float = None, **kwargs):
57
"""
58
Initialize a long-running function tool.
59
60
Args:
61
func (callable): Long-running function to wrap
62
timeout (float, optional): Execution timeout in seconds
63
**kwargs: Additional tool parameters
64
"""
65
pass
66
67
class AgentTool(BaseTool):
68
"""Tool for agent interactions."""
69
70
def __init__(self, agent, **kwargs):
71
"""
72
Initialize an agent tool.
73
74
Args:
75
agent: Agent instance to wrap as a tool
76
**kwargs: Additional tool parameters
77
"""
78
pass
79
80
class ExampleTool(BaseTool):
81
"""Example tool implementation for reference."""
82
83
def __init__(self, **kwargs):
84
"""Initialize example tool."""
85
pass
86
87
class ToolContext:
88
"""Context passed to tools during execution."""
89
90
def __init__(self, session_id: str = None, user_id: str = None, **kwargs):
91
"""
92
Initialize tool context.
93
94
Args:
95
session_id (str, optional): Session identifier
96
user_id (str, optional): User identifier
97
**kwargs: Additional context parameters
98
"""
99
pass
100
```
101
102
### Built-in Tool Functions
103
104
Pre-built tools for common operations and integrations.
105
106
```python { .api }
107
def google_search(query: str, num_results: int = 10, **kwargs) -> dict:
108
"""
109
Perform Google search and return results.
110
111
Args:
112
query (str): Search query
113
num_results (int): Number of results to return
114
**kwargs: Additional search parameters
115
116
Returns:
117
dict: Search results with titles, URLs, and snippets
118
"""
119
pass
120
121
def enterprise_web_search(query: str, **kwargs) -> dict:
122
"""
123
Enterprise web search functionality.
124
125
Args:
126
query (str): Search query
127
**kwargs: Additional search parameters
128
129
Returns:
130
dict: Enterprise search results
131
"""
132
pass
133
134
def url_context(url: str, **kwargs) -> dict:
135
"""
136
Extract context and content from a URL.
137
138
Args:
139
url (str): URL to extract context from
140
**kwargs: Additional extraction parameters
141
142
Returns:
143
dict: URL content and metadata
144
"""
145
pass
146
147
def exit_loop(**kwargs):
148
"""
149
Exit loop functionality for LoopAgent.
150
151
Args:
152
**kwargs: Exit parameters
153
"""
154
pass
155
156
def get_user_choice(choices: list, prompt: str = None, **kwargs):
157
"""
158
Prompt user to make a choice from given options.
159
160
Args:
161
choices (list): List of available choices
162
prompt (str, optional): Prompt message for the user
163
**kwargs: Additional choice parameters
164
165
Returns:
166
Selected choice
167
"""
168
pass
169
170
def load_artifacts(artifact_ids: list = None, **kwargs) -> dict:
171
"""
172
Load artifacts from storage.
173
174
Args:
175
artifact_ids (list, optional): Specific artifact IDs to load
176
**kwargs: Additional loading parameters
177
178
Returns:
179
dict: Loaded artifacts
180
"""
181
pass
182
183
def load_memory(memory_keys: list = None, **kwargs) -> dict:
184
"""
185
Load memory data.
186
187
Args:
188
memory_keys (list, optional): Specific memory keys to load
189
**kwargs: Additional loading parameters
190
191
Returns:
192
dict: Loaded memory data
193
"""
194
pass
195
196
def preload_memory(memory_data: dict, **kwargs):
197
"""
198
Preload memory with data.
199
200
Args:
201
memory_data (dict): Memory data to preload
202
**kwargs: Additional preloading parameters
203
"""
204
pass
205
206
def transfer_to_agent(agent_name: str, message: str, **kwargs):
207
"""
208
Transfer control to another agent.
209
210
Args:
211
agent_name (str): Name of the target agent
212
message (str): Message to pass to the target agent
213
**kwargs: Additional transfer parameters
214
"""
215
pass
216
```
217
218
### Specialized Toolsets
219
220
Advanced toolsets for specific integrations and services.
221
222
```python { .api }
223
class APIHubToolset:
224
"""API Hub integration toolset."""
225
226
def __init__(self, api_hub_config: dict, **kwargs):
227
"""
228
Initialize API Hub toolset.
229
230
Args:
231
api_hub_config (dict): API Hub configuration
232
**kwargs: Additional configuration parameters
233
"""
234
pass
235
236
class VertexAiSearchTool(BaseTool):
237
"""Vertex AI search functionality."""
238
239
def __init__(self, project_id: str, location: str, **kwargs):
240
"""
241
Initialize Vertex AI search tool.
242
243
Args:
244
project_id (str): Google Cloud project ID
245
location (str): Vertex AI location
246
**kwargs: Additional configuration parameters
247
"""
248
pass
249
250
class MCPToolset:
251
"""Model Context Protocol toolset (Python 3.10+)."""
252
253
def __init__(self, mcp_config: dict, **kwargs):
254
"""
255
Initialize MCP toolset.
256
257
Note: Requires Python 3.10 or higher.
258
259
Args:
260
mcp_config (dict): MCP configuration
261
**kwargs: Additional configuration parameters
262
"""
263
pass
264
```
265
266
### REST API Tools
267
268
Tools for working with REST APIs and OpenAPI specifications.
269
270
```python { .api }
271
class OpenAPIToolset:
272
"""OpenAPI specification-based toolset."""
273
274
def __init__(self, openapi_spec: dict, base_url: str = None, **kwargs):
275
"""
276
Initialize OpenAPI toolset.
277
278
Args:
279
openapi_spec (dict): OpenAPI specification
280
base_url (str, optional): Base URL for API calls
281
**kwargs: Additional configuration parameters
282
"""
283
pass
284
285
class RestApiTool(BaseTool):
286
"""REST API tool for making HTTP requests."""
287
288
def __init__(self, base_url: str, auth_config: dict = None, **kwargs):
289
"""
290
Initialize REST API tool.
291
292
Args:
293
base_url (str): Base URL for the API
294
auth_config (dict, optional): Authentication configuration
295
**kwargs: Additional configuration parameters
296
"""
297
pass
298
```
299
300
## Usage Examples
301
302
### Creating Custom Function Tools
303
304
```python
305
from google.adk.tools import FunctionTool
306
307
def calculate_area(length: float, width: float) -> float:
308
"""Calculate the area of a rectangle."""
309
return length * width
310
311
# Wrap function as a tool
312
area_tool = FunctionTool(
313
func=calculate_area,
314
name="area_calculator",
315
description="Calculate the area of a rectangle given length and width"
316
)
317
318
# Use with an agent
319
from google.adk.agents import Agent
320
agent = Agent(
321
name="math_assistant",
322
model="gemini-2.0-flash",
323
tools=[area_tool]
324
)
325
```
326
327
### Using Built-in Tools
328
329
```python
330
from google.adk.tools import google_search, url_context
331
from google.adk.agents import Agent
332
333
# Create agent with built-in tools
334
agent = Agent(
335
name="research_agent",
336
model="gemini-2.0-flash",
337
instruction="Help users research topics using web search and URL analysis.",
338
tools=[google_search, url_context]
339
)
340
```
341
342
### Custom Tool Class
343
344
```python
345
from google.adk.tools import BaseTool, ToolContext
346
347
class WeatherTool(BaseTool):
348
def __init__(self, api_key: str):
349
super().__init__(
350
name="weather_tool",
351
description="Get current weather information for a location"
352
)
353
self.api_key = api_key
354
355
def execute(self, context: ToolContext, location: str, **kwargs):
356
# Implementation to fetch weather data
357
return f"Weather in {location}: Sunny, 72°F"
358
359
# Use custom tool
360
weather_tool = WeatherTool(api_key="your-api-key")
361
agent = Agent(
362
name="weather_assistant",
363
model="gemini-2.0-flash",
364
tools=[weather_tool]
365
)
366
```
367
368
### Long-Running Tool
369
370
```python
371
from google.adk.tools import LongRunningFunctionTool
372
import time
373
374
def process_large_dataset(data_path: str) -> dict:
375
"""Process a large dataset - this might take a while."""
376
time.sleep(10) # Simulate long processing
377
return {"status": "completed", "records_processed": 10000}
378
379
# Wrap as long-running tool with timeout
380
processing_tool = LongRunningFunctionTool(
381
func=process_large_dataset,
382
timeout=30, # 30 second timeout
383
name="dataset_processor"
384
)
385
```
386
387
### Agent as Tool
388
389
```python
390
from google.adk.tools import AgentTool
391
from google.adk.agents import Agent
392
393
# Create specialized agent
394
translator = Agent(
395
name="translator",
396
model="gemini-2.0-flash",
397
instruction="Translate text between languages accurately."
398
)
399
400
# Wrap agent as a tool
401
translation_tool = AgentTool(agent=translator)
402
403
# Use in another agent
404
main_agent = Agent(
405
name="multilingual_assistant",
406
model="gemini-2.0-flash",
407
tools=[translation_tool]
408
)
409
```
410
411
### OpenAPI Integration
412
413
```python
414
from google.adk.tools import OpenAPIToolset
415
416
# Load OpenAPI spec (could be from file or URL)
417
openapi_spec = {
418
"openapi": "3.0.0",
419
"info": {"title": "My API", "version": "1.0.0"},
420
"paths": { ... } # API endpoints
421
}
422
423
# Create toolset from OpenAPI spec
424
api_toolset = OpenAPIToolset(
425
openapi_spec=openapi_spec,
426
base_url="https://api.example.com"
427
)
428
429
# Use with agent
430
agent = Agent(
431
name="api_agent",
432
model="gemini-2.0-flash",
433
tools=api_toolset.get_tools() # Get all tools from the toolset
434
)
435
```