Concept #171Mediumpython-for-gen-aigoogle-adk

What are all the tool types in Google ADK? Explain Function Tools, MCP Tools, OpenAPI Tools, and Agents-as-Tools with examples.

#google-adk#tools#function-tools#mcp#openapi#agents-as-tools

Answer

Tool Types in Google ADK

Agent Tool Call
Agent Tool Call

Google ADK provides a rich tool ecosystem with 6 tool types that let agents interact with external systems, APIs, and data sources.


Tool Types Overview


1. Function Tools

Any Python function automatically becomes a tool. ADK extracts the docstring as the tool description and type hints as parameters.

python
from google.adk.agents import Agent

def search_database(query: str, limit: int = 10) -> list[dict]:
    """Search the internal database for relevant documents.

    Args:
        query: The search query string.
        limit: Maximum number of results to return.

    Returns:
        list[dict]: List of matching documents with title and content.
    """
    # Your database logic here
    return [{"title": "Doc 1", "content": "..."}]

def calculate_cost(tokens: int, model: str = "gpt-4o") -> dict:
    """Calculate the cost of an LLM API call.

    Args:
        tokens: Number of tokens used.
        model: The model name for pricing lookup.

    Returns:
        dict: Cost breakdown with input/output pricing.
    """
    rates = {"gpt-4o": 0.005, "gemini-2.5-flash": 0.0001}
    cost = (tokens / 1000) * rates.get(model, 0.01)
    return {"model": model, "tokens": tokens, "cost": f"${cost:.4f}"}

agent = Agent(
    name="helper",
    model="gemini-2.5-flash",
    instruction="Help users search data and calculate costs.",
    tools=[search_database, calculate_cost],
)

2. Built-in Tools

ToolImportDescription
Google Search
text
google_search
Web search via Google
Code ExecutionBuilt-inExecute code using Gemini
RAG RetrievalBuilt-inPrivate data retrieval
python
from google.adk.tools import google_search

agent = Agent(
    name="search_agent",
    model="gemini-2.5-flash",
    instruction="Search the web to answer questions.",
    tools=[google_search],
)

3. MCP Tools

Connect to any Model Context Protocol server:

python
from google.adk.tools.mcp_tool import McpToolset, StdioServerParameters

# Stdio connection (local)
fs_tools = McpToolset(
    connection_params=StdioServerParameters(
        command="npx",
        args=["-y", "@modelcontextprotocol/server-filesystem", "./data"],
    )
)

# SSE connection (remote)
from google.adk.tools.mcp_tool import SseServerParams

remote_tools = McpToolset(
    connection_params=SseServerParams(
        url="https://mcp-server.example.com/sse"
    )
)

agent = Agent(
    name="file_agent",
    model="gemini-2.5-flash",
    tools=[fs_tools],
)

4. OpenAPI Tools

Auto-generate tools from OpenAPI v3.x specifications:

python
from google.adk.tools.openapi_tool import OpenAPIToolset

# Generate tools from API spec
toolset = OpenAPIToolset(
    spec_str=open("petstore.yaml").read(),
    spec_str_type="yaml",
)

agent = Agent(
    name="api_agent",
    model="gemini-2.5-flash",
    instruction="Help users interact with the Pet Store API.",
    tools=toolset.get_tools(),
)

5. Agents-as-Tools (AgentTool)

Use a specialized agent as a tool for another agent:

python
from google.adk.tools import AgentTool

# Specialist agent
math_agent = Agent(
    name="math_solver",
    model="gemini-2.5-flash",
    instruction="Solve math problems step by step.",
)

# Use it as a tool
main_agent = Agent(
    name="tutor",
    model="gemini-2.5-flash",
    instruction="Help students learn. Use the math solver for calculations.",
    tools=[AgentTool(agent=math_agent)],
)

6. Long Running Function Tools

For async operations that take time to complete:

python
from google.adk.tools import LongRunningFunctionTool

async def train_model(dataset: str, epochs: int = 10):
    """Train a model on the given dataset.

    Args:
        dataset: Path to training data.
        epochs: Number of training epochs.
    """
    for epoch in range(epochs):
        yield {"status": "training", "epoch": epoch + 1, "total": epochs}
    yield {"status": "complete", "accuracy": 0.95}

agent = Agent(
    name="ml_agent",
    model="gemini-2.5-flash",
    tools=[LongRunningFunctionTool(func=train_model)],
)

Tool Confirmation (Human-in-the-Loop)

Confirmation UI
Confirmation UI

Require user approval before executing sensitive tools:

python
agent = Agent(
    name="db_agent",
    model="gemini-2.5-flash",
    instruction="Help manage the database.",
    tools=[delete_record],   # dangerous operation
    tool_confirmation=True,  # ask user before executing
)

Summary

Tool TypeWhen to Use
Function ToolWrap any Python function
Built-in ToolGoogle Search, Code Exec, RAG
MCP ToolConnect to MCP servers
OpenAPI ToolAuto-generate from API specs
Agent-as-ToolUse agent as a callable tool
Long RunningAsync operations with progress

Learn more at Custom Tools and Function Tools.