Menu

Tool Framework

Relevant source files

Purpose and Scope

The Tool Framework provides the foundational abstractions and interfaces for enabling AI agents to interact with external systems, APIs, and data sources. This page documents the core tool interfaces (BaseTool, BaseAuthenticatedTool, BaseToolset), the tool declaration mechanism, schema conversion utilities, and the execution contract that all tools must implement.

For information about specific tool types, see:


Tool Architecture Overview

Diagram: Tool Framework Core Components

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py68-450 src/google/adk/tools/_gemini_schema_util.py1-182 src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py35-270


Core Abstractions

BaseTool

BaseTool is the abstract base class that defines the interface contract for all tools in ADK. Every tool must extend this class and implement its required methods.

Key Characteristics:

  • Provides tool identity through name and description attributes
  • Declares input parameters via _get_declaration() method returning FunctionDeclaration
  • Defines execution contract through run_async() method
  • Supports both synchronous and asynchronous execution patterns

Tool Identity:

PropertyTypePurpose
namestrUnique identifier for the tool, used by LLMs to select tools
descriptionstrHuman-readable explanation of tool purpose and usage
is_long_runningboolIndicates if tool execution may take extended time

Core Methods:

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py68-94 src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py185-193 src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py358-436


BaseAuthenticatedTool

BaseAuthenticatedTool extends BaseTool to add authentication capabilities. Tools that require API keys, OAuth tokens, or other credentials should extend this class.

Authentication Properties:

PropertyTypePurpose
auth_schemeAuthSchemeDefines authentication method (API key, OAuth2, etc.)
auth_credentialAuthCredentialContains actual credentials or credential retrieval info
credential_exchangerAutoAuthCredentialExchangerHandles credential exchange and refresh

Authentication Configuration:

Authentication Flow:

Diagram: Authenticated Tool Execution Flow

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py82-155 src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py195-220 src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py364-393


BaseToolset

BaseToolset is an abstract base class for grouping multiple related tools together. Toolsets simplify tool management when dealing with families of related operations (e.g., all BigQuery operations, all operations from a single API).

Toolset Characteristics:

  • Aggregates multiple BaseTool instances
  • Provides unified configuration (e.g., shared authentication)
  • Enables batch tool operations
  • Simplifies agent configuration with logical grouping

Common Toolset Pattern:

Example: OpenAPI Toolset Structure

Diagram: Toolset Aggregation Pattern

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py51-239 tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py47-61


Tool Declaration and Schema

FunctionDeclaration

Tools declare their interface to LLMs using FunctionDeclaration, a Gemini-compatible schema format that describes:

  • Tool name and description
  • Input parameters with types and constraints
  • Optional output schema

Declaration Structure:

ComponentTypePurpose
namestrFunction identifier (max 60 characters)
descriptionstrUsage instructions and purpose
parametersSchemaInput parameter schema in Gemini format

Schema Example:

Declaration Method in RestApiTool:

src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py185-193

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py185-193 src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py241-253


Schema Conversion

The tool framework provides utilities to convert between different schema formats, primarily in _gemini_schema_util.py.

Key Conversion Functions:

FunctionPurpose
_to_gemini_schema()Converts OpenAPI v3.1 schema to Gemini Schema
_sanitize_schema_formats_for_gemini()Filters unsupported fields and formats
_dereference_schema()Resolves $ref pointers in JSON schemas
_to_snake_case()Converts naming conventions (camelCase → snake_case)

Schema Conversion Pipeline:

Diagram: Schema Conversion Pipeline

Supported Type Mappings:

OpenAPI TypeGemini TypeNotes
stringSTRINGSupports enum, date-time formats
integerINTEGERSupports int32, int64 formats
numberNUMBERFloat/double values
booleanBOOLEAN-
arrayARRAYWith items schema
objectOBJECTWith properties map
["string", "null"]STRING (nullable=true)Nullable types

Format Sanitization:

src/google/adk/tools/_gemini_schema_util.py115-165

Name Conversion:

src/google/adk/tools/_gemini_schema_util.py35-74

Sources: src/google/adk/tools/_gemini_schema_util.py35-182 tests/unittests/tools/test_gemini_schema_util.py23-461


Tool Parameter Processing

ApiParameter

The ApiParameter class represents a single function parameter, handling type conversion and validation.

Parameter Properties:

PropertyTypePurpose
original_namestrName from source (OpenAPI, etc.)
py_namestrPython-compatible identifier (snake_case)
param_locationstrWhere parameter appears (query, body, header, path, cookie)
param_schemaSchemaType and validation rules
descriptionstrHuman-readable explanation
type_valuetypePython type for runtime (str, int, List[str], etc.)
type_hintstrType hint string for documentation
requiredboolWhether parameter is mandatory

Parameter Location Types:

LocationDescriptionExample Use Case
queryURL query parameters?location=SF&units=C
bodyRequest body (JSON)POST/PUT payload
headerHTTP headersAuthentication, content-type
pathURL path segments/users/{userId}
cookieCookie valuesSession tokens

Type Inference:

src/google/adk/tools/openapi_tool/common/common.py106-176

Parameter Name Sanitization:

src/google/adk/tools/openapi_tool/common/common.py33-51

Sources: src/google/adk/tools/openapi_tool/common/common.py54-104 tests/unittests/tools/openapi_tool/common/test_common.py49-157


OperationParser

For OpenAPI-based tools, OperationParser extracts parameters from OpenAPI Operation objects and converts them to ApiParameter instances.

Parsing Pipeline:

Diagram: OperationParser Processing Flow

Parameter Extraction Methods:

MethodSourceOutput
_process_operation_parameters()operation.parametersQuery, path, header, cookie params
_process_request_body()operation.requestBodyBody parameters
_process_return_value()operation.responses['2xx']Return type schema
_dedupe_param_names()All parametersUnique parameter names

Request Body Handling:

src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py105-152

Parameter Deduplication:

src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py153-163

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py35-270 tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py26-401


Tool Execution

ToolContext

ToolContext provides execution environment information to tools, including access to session state, authentication, and agent context.

Context Properties:

PropertyTypePurpose
stateStateSession and app-level state
get_auth_response()methodRetrieve authentication responses
request_credential()methodRequest user credentials

Context Usage in Tools:

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py358-436 tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py42-49


Execution Contract

All tools must implement the run_async() method, which defines the execution contract.

Method Signature:

Return Value Conventions:

PatternPurposeExample
Success resultNormal execution{"result": "success", "data": {...}}
Error resultExecution failure{"error": "Error message with details"}
Pending resultAwaiting user input{"pending": True, "message": "Need auth"}
Text fallbackNon-JSON response{"text": "Plain text response"}

Execution Error Handling:

RestApiTool Execution Example:

src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py364-436

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py358-436 tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py199-269


Tool Integration with Agents

Agent Tool Assignment

Tools are assigned to agents via the tools parameter during agent initialization.

Assignment Patterns:

Tool Execution Flow in Agents

Diagram: Tool Execution in Agent Context

Tool Declaration Injection:

During agent execution, the flow preprocesses the LLM request to include tool declarations:

Tool Result Handling:

After tool execution, results are formatted and sent back to the LLM:

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py68-193 tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py42-140


Tool Name Constraints

Tool names in ADK are subject to LLM provider constraints:

ConstraintValueReason
Max length60-64 charactersGemini function name limit
Formatsnake_casePython convention
Characters[a-z0-9_]Function identifier rules
UniquenessRequiredDisambiguation

Name Truncation:

src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py117-119

Name Conversion:

Tool names are automatically converted to snake_case using _to_snake_case():

src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py189-194

Sources: src/google/adk/tools/_gemini_schema_util.py35-74 src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py117-119 src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py189-194


Tool Framework Class Hierarchy

Diagram: Tool Framework Class Hierarchy

Sources: src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py68-450 src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py35-270 src/google/adk/tools/openapi_tool/common/common.py54-263


Summary

The Tool Framework provides:

  1. Core Abstractions: BaseTool, BaseAuthenticatedTool, BaseToolset define the tool interface contract
  2. Schema Conversion: Utilities to convert OpenAPI schemas to Gemini-compatible FunctionDeclaration format
  3. Parameter Processing: ApiParameter and OperationParser handle type inference and parameter extraction
  4. Execution Contract: Standardized run_async() method with ToolContext for state and authentication
  5. Agent Integration: Seamless tool declaration and execution within agent workflows

This framework enables ADK to support diverse tool types (REST APIs, MCP, BigQuery, custom Python functions) through a unified interface, while handling authentication, schema conversion, and error handling consistently.

For specific tool implementations, see the related pages listed at the beginning of this document.