Google ADK

This section contains implementation using Google ADK.

Getting Started

Prerequisites

Setup

  1. Clone the repository:

    git clone https://github.com/GDP-ADMIN/gen-ai-examples.git 
  2. cd gen-ai-examples/examples/aip-agent-quickstart
  3. Install dependencies:

    poetry install
  4. Setup Google API key

    export GOOGLE_API_KEY='your-google-api-key-here'

Basic implementation

We can use GoogleADKAgent to utilize Google ADK's framework using our Agent Interface.

from gllm_agents.agent.google_adk_agent import GoogleADKAgent

from aip_agent_quickstart.config import CALCULATOR_AGENT_INSTRUCTION
from aip_agent_quickstart.tools import adk_sum_numbers

agent = GoogleADKAgent(
    name="GoogleADKCalculator",
    instruction=CALCULATOR_AGENT_INSTRUCTION,
    model="gemini-2.0-flash",
    tools=[adk_sum_numbers],
)

response = agent.run(query="What is the sum of 23 and 47? And then add 10 to that, then add 5 more.")
print(response.get("output"))

Running the Agent

You can run the following to test running the agent;

poetry run python hello_world_google_adk.py

You should see the following output:

The sum of 23 and 47 is 70. Adding 10 to that gives 80, and adding 5 more results in 85

Multi-agent

We provide multi-agent capabilities out of the box for Google ADK agents. The following example showcases:

  1. How to define multiple specialized agents (WeatherAgent, MathAgent).

  2. How to set up a CoordinatorAgent that can delegate to these specialized agents.

  3. How the CoordinatorAgent uses dynamically created tools to call sub-agents.

  4. How the CoordinatorAgent can delegate tasks to the appropriate sub-agents.

from gllm_agents.agent.google_adk_agent import GoogleADKAgent

from aip_agent_quickstart.config import (
    COORDINATOR_MULTI_AGENT_INSTRUCTION,
    MATH_AGENT_INSTRUCTION,
    WEATHER_AGENT_INSTRUCTION,
)
from aip_agent_quickstart.tools import adk_sum_numbers, adk_weather_tool

weather_agent = GoogleADKAgent(
    name="WeatherAgent",
    instruction=WEATHER_AGENT_INSTRUCTION,
    model="gemini-2.0-flash",
    tools=[adk_weather_tool],
)

math_agent = GoogleADKAgent(
    name="MathAgent",
    instruction=MATH_AGENT_INSTRUCTION,
    model="gemini-2.0-flash",
    tools=[adk_sum_numbers],
)

coordinator_agent = GoogleADKAgent(
    name="CoordinatorAgent",
    instruction=COORDINATOR_MULTI_AGENT_INSTRUCTION,
    model="gemini-2.0-flash",
    agents=[weather_agent, math_agent],
)

weather_response = coordinator_agent.run(query="What is the weather in Tokyo?")
math_response = coordinator_agent.run(query="What is 5 + 7?")

print(f"Weather Response: {weather_response.get('output')}\nMath Response: {math_response.get('output')}")

Running the Agent

You can run the following to test the capability:

poetry run python hello_world_multi_agent_google_adk.py

You should see the following output:

Agent 'CoordinatorAgent' (Coordinator) configured with 2 total tools. Can delegate to: ['WeatherAgent', 'MathAgent'].

Function calls:
name: transfer_to_agent, args: {'agent_name': 'WeatherAgent'}
Function calls:
name: weather_tool, args: {'city': 'Tokyo'}
Function calls:
name: transfer_to_agent, args: {'agent_name': 'MathAgent'}
Function calls:
name: sum_numbers, args: {'b': 7, 'a': 5}

Weather Response: The weather in Tokyo is clear skies with a temperature of 25°C and 65% humidity.
Math Response: 5 + 7 = 12.

MCP

We have the capability to connect to MCP client for Google ADK agents.

Start the MCP server

In a new terminal window, execute this

poetry run python aip_agent_quickstart/mcp_servers/mcp_server_stdio.py

Upon success, the window remains open without displaying any output.

Run agent using MCP-based tools

The following is how we can configure our GoogleADK Agent to run using MCP-based tools.

from gllm_agents.agent.google_adk_agent import GoogleADKAgent

from aip_agent_quickstart.config import DEFAULT_AGENT_INSTRUCTION
from aip_agent_quickstart.mcp_configs.configs import mcp_config_stdio

agent = GoogleADKAgent(
    name="ADK_Stdio_Weather_Agent",
    instruction=DEFAULT_AGENT_INSTRUCTION,
    model="gemini-2.0-flash",
)
agent.add_mcp_server(mcp_config_stdio)

response = agent.run(query="What's the weather forecast for monday?")
print(f"Response: {response.get('output')}")

To test the agent, in the current terminal window run the following command:

poetry run python hello_world_google_adk_mcp_stdio.py

You should see the following output:

Loaded 1 tools from MCP server 'weather_tools'
Re-initialized agent with 1 total tools (0 function tools + 1 MCP tools)

Response: OK. The weather forecast for Monday is Sunny with temperatures between 28°C and 32°C

A2A (Agent-to-Agent)

Google ADK agents can act as A2A agents and can also coordinate agents hosted on remote A2A servers.

Deploy Agent as A2A server

To initiate the specialized Weather Agent server, follow one of these instruction(s):

Start the Server

To start the Weather Agent server using Python and Poetry, first, we need to open a new terminal window. Then export the Google API key:

export GOOGLE_API_KEY='your-google-api-key-here'
export framework='google_adk'

Then run this command from your project's root directory:

poetry run python aip_agent_quickstart/agents/weather_agent/server.py

By default, the server will start and listen on http://localhost:8001

You can customize the host and port using the --host and --port command-line options.

This script performs the following key actions:

  1. Imports weather_tool.

  2. Builds a Agent Card with metadata.

  3. Wraps a Google ADK agent as an A2A app.

Call Agents via the A2A client

Once your Weather Agent server runs, then attempt one of the following approaches:

The following is how we can call A2A agent with non-streaming mode

from gllm_agents.agent.google_adk_agent import GoogleADKAgent
from gllm_agents.agent.types import A2AClientConfig

from aip_agent_quickstart.config import DEFAULT_AGENT_INSTRUCTION

agent = GoogleADKAgent(
    name="GoogleAssistantAgent",
    instruction=DEFAULT_AGENT_INSTRUCTION,
    model="gemini-2.0-flash",
)
client_a2a_config = A2AClientConfig(discovery_urls=["http://localhost:8001"])
agent_cards = agent.discover_agents(client_a2a_config)
response = agent.send_to_agent(agent_cards[0], message="What is the weather in Jakarta?")
print(response["content"])

Test the script by running

poetry run python hello_world_a2a_google_adk_client.py

These scripts will do the following:

  1. Instantiate GoogleADKAgent

  2. Discover the Weather Agent at its URL.

  3. Send What is the weather in Jakarta City?

  4. Receive the full response when it's ready.

Expected Output:

Discovered agents (1 Agents): 
1. WeatherAgent
...
The weather in Jakarta is 32°C, partly cloudy with high humidity.

Stopping the A2A Server

If the server is running in the foreground (as it will with the command above), press Ctrl+C in the terminal where it's running.

Last updated