Google ADK - adding A2A support for your agent rapidly

In this post, I am going to show how easy it is to convert your agentic app created using ADK to support A2A. What we will do is setup an agentic app that supports A2A. Then we will bring it up to serve new requests. Finally we will call it from ADK app.

Ensure we have the proper module installed

uv add google-adk[a2a]

And then once we have the dependencies, we have a agent.py file contains code to your hello world agent or get weather agent - something really basic. Notice we purposely put the core code in agent.py so we can have another other deployment options for example AppEngine we can create another file called app_engine.py. 


But in our case, we will call it a2a_agent.py as we wrapping our agent.py and expose it as a A2A server.  

agent.py code (your typical get weather sample)

import datetime
from zoneinfo import ZoneInfo

from google.adk.agents import Agent
from google.adk.apps import App
from google.adk.models import Gemini
from google.genai import types
from google.adk.a2a.utils.agent_to_a2a import to_a2a

import os
import google.auth

_, project_id = google.auth.default()
os.environ["GOOGLE_CLOUD_PROJECT"] = project_id
os.environ["GOOGLE_CLOUD_LOCATION"] = "global"
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"


def get_weather(query: str) -> str:
    """Simulates a web search. Use it get information on weather.

    Args:
        query: A string containing the location to get weather information for.

    Returns:
        A string with the simulated weather information for the queried location.
    """
    if "sf" in query.lower() or "san francisco" in query.lower():
        return "It's 60 degrees and foggy."
    return "It's 90 degrees and sunny."


def get_current_time(query: str) -> str:
    """Simulates getting the current time for a city.

    Args:
        city: The name of the city to get the current time for.

    Returns:
        A string with the current time information.
    """
    if "sf" in query.lower() or "san francisco" in query.lower():
        tz_identifier = "America/Los_Angeles"
    else:
        return f"Sorry, I don't have timezone information for query: {query}."

    tz = ZoneInfo(tz_identifier)
    now = datetime.datetime.now(tz)
    return f"The current time for query {query} is {now.strftime('%Y-%m-%d %H:%M:%S %Z%z')}"

# Here we are crafting the agent
root_agent = Agent(
    name="root_agent",
    model=Gemini(
        model="gemini-3-flash-preview",
        retry_options=types.HttpRetryOptions(attempts=3),
    ),
    instruction="You are a helpful AI assistant designed to provide accurate and useful information.",
    tools=[get_weather, get_current_time],
)

# This is setting up the app
app = App(
    root_agent=root_agent,
    name="agent_skills_tutorial",
)

Then we create a file to import and bring it all together called a2a_agent.py. Please update your agent card url to the endpoint you will be running your agent onn - in my case it is localhost:8001


import logging
import os
from typing import Any
from google.adk.a2a.utils.agent_to_a2a import to_a2a
import vertexai
from dotenv import load_dotenv
from google.adk.artifacts import GcsArtifactService, InMemoryArtifactService
from google.cloud import logging as google_cloud_logging

# import our agent in
from vertexai.agent_engines.templates.adk import AdkApp

from agent_skills_tutorial.agent import root_agent as adk_app
from agent_skills_tutorial.app_utils.telemetry import setup_telemetry
from agent_skills_tutorial.app_utils.typing import Feedback
from a2a.types import AgentCard

# Load environment variables from .env file at runtime
load_dotenv()

my_agent_card = AgentCard(
    name="file_agent",
    url="http://localhost:8001",
    description="Test agent from file",
    version="1.0.0",
    capabilities={"get_weather", "get_current_time"},
    skills=[],
    defaultInputModes=["text/plain"],
    defaultOutputModes=["text/plain"],
    supportsAuthenticatedExtendedCard=False,
)

# convert ADK agent to A2A agent
a2a_app = to_a2a(adk_app, port=8001, agent_card=my_agent_card)

We use to_a2a() to do all the magic and expose it as a startlet app. We even provided a card here. 

 uvicorn a2a_agent:a2a_app --host 0.0.0.0 --port 8001 --reload

Once you started the server, wait for a while and you should be able to hit: 

http://localhost:8001/.well-known/agent-card.json


It does take sometime to get it loaded. So we have set up a stand alone agent that supports A2A. Let's talk to it 

To do that, we need the following code

from google.adk.agents.remote_a2a_agent import AGENT_CARD_WELL_KNOWN_PATH
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent

root_agent = RemoteA2aAgent(
    name="hello_world_agent",
    description=(
        "Helpful assistant that can get weather and current time information."
    ),
    agent_card=f"http://localhost:8001/{AGENT_CARD_WELL_KNOWN_PATH}",
)


And then we can run the app - by running "make playground". Make sure "app" is selected. You should get the following outputs. 



One thing you will noticed is that we didn't even tried to construct the message and specify additional path.  

Here is the code repository if you wanted to try it out yourself
https://github.com/kepungnzai/adk-app-agentic-remote-A2A




Comments

Popular posts from this blog

vllm : Failed to infer device type

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

android studio kotlin source is null error