クイックスタート: A2A経由でリモートエージェントを利用する¶
このクイックスタートでは、開発者にとって最も一般的な出発点である「リモートエージェントがあり、ADKエージェントでA2A経由でそれを使用するにはどうすればよいですか?」について説明します。これは、さまざまなエージェントが連携して対話する必要がある複雑なマルチエージェントシステムを構築するために不可欠です。
概要¶
このサンプルは、Agent Development Kit(ADK)のAgent2Agent(A2A)アーキテクチャを示し、複数のエージェントが連携して複雑なタスクを処理する方法を紹介します。このサンプルでは、サイコロを振って数字が素数かどうかを確認できるエージェントを実装しています。
┌─────────────────┐ ┌──────────────────┐ ┌────────────────────┐
│ ルートエージェント │───▶│ ロールエージェント │ │ リモートプライム │
│ (ローカル) │ │ (ローカル) │ │ エージェント │
│ │ │ │ │ (localhost:8001) │
│ │───▶│ │◀───│ │
└─────────────────┘ └──────────────────┘ └────────────────────┘
A2A基本サンプルは、以下で構成されています。
- ルートエージェント(
root_agent):特殊なサブエージェントにタスクを委任するメインオーケストレーター - ロールエージェント(
roll_agent):サイコロを振る操作を処理するローカルサブエージェント - プライムエージェント(
prime_agent):数字が素数かどうかを確認するリモートA2Aエージェント。このエージェントは別のA2Aサーバーで実行されています。
ADKサーバーでエージェントを公開する¶
ADKには、A2Aプロトコルを使用してエージェントを公開するための組み込みCLIコマンドadk api_server --a2aが付属しています。
a2a_basicの例では、まずローカルルートエージェントが使用できるように、A2Aサーバー経由でcheck_prime_agentを公開する必要があります。
1. サンプルコードの取得¶
まず、必要な依存関係がインストールされていることを確認してください。
a2a_basic サンプルをクローンして移動できます。
ご覧のとおり、フォルダー構造は次のようになっています。
a2a_basic/
├── remote_a2a/
│ └── check_prime_agent/
│ ├── __init__.py
│ ├── agent.json
│ └── agent.py
├── README.md
├── __init__.py
└── agent.py # ローカルルートエージェント
メインエージェント(a2a_basic/agent.py)¶
roll_die(sides: int):サイコロを振るための関数ツールroll_agent:サイコロ振りに特化したローカルエージェントprime_agent:リモートA2Aエージェントの構成root_agent:委任ロジックを備えたメインオーケストレーター
リモートプライムエージェント(a2a_basic/remote_a2a/check_prime_agent/)¶
agent.py:素数判定サービスの実装agent.json:A2Aエージェントのエージェントカードcheck_prime(nums: list[int]):素数判定アルゴリズム
2. リモートプライムエージェントサーバーの起動¶
ADKエージェントがA2A経由でリモートエージェントを利用する方法を示すには、まずプライムエージェント(check_prime_agentの下)をホストするリモートエージェントサーバーを起動する必要があります。
# ポート8001でcheck_prime_agentを提供するリモートa2aサーバーを起動します
adk api_server --a2a --port 8001 contributing/samples/a2a_basic/remote_a2a
デバッグ用のロギングを追加する --log_level debug
デバッグレベルのロギングを有効にするには、adk api_serverに--log_level debugを追加します。
なぜポート8001を使用するのですか?
このクイックスタートでは、ローカルでテストする場合、エージェントはlocalhostを使用するため、公開されたエージェント(リモートのプライムエージェント)のA2Aサーバーのportは、利用側エージェントのポートとは異なる必要があります。利用側エージェントと対話するadk webのデフォルトポートは8000であるため、A2Aサーバーは別のポート8001を使用して作成されます。
実行すると、次のようなものが表示されます。
INFO: Started server process [56558]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
3. リモートエージェントの必須エージェントカード(agent-card.json)に注意してください¶
A2Aプロトコルでは、各エージェントが何をするかを記述したエージェントカードを持っている必要があります。
他の誰かがすでにエージェントで利用しようとしているリモートA2Aエージェントを構築している場合は、エージェントカード(agent-card.json)があることを確認する必要があります。
このサンプルでは、check_prime_agentにはすでにエージェントカードが提供されています。
{
"capabilities": {},
"defaultInputModes": ["text/plain"],
"defaultOutputModes": ["application/json"],
"description": "An agent specialized in checking whether numbers are prime. It can efficiently determine the primality of individual numbers or lists of numbers.",
"name": "check_prime_agent",
"skills": [
{
"id": "prime_checking",
"name": "Prime Number Checking",
"description": "Check if numbers in a list are prime using efficient mathematical algorithms",
"tags": ["mathematical", "computation", "prime", "numbers"]
}
],
"url": "http://localhost:8001/a2a/check_prime_agent",
"version": "1.0.0"
}
ADKのエージェントカードに関する詳細情報
ADKでは、to_a2a(root_agent)ラッパーを使用してエージェントカードを自動的に生成できます。他のユーザーが使用できるように既存のエージェントを公開する方法について詳しく知りたい場合は、A2Aクイックスタート(公開)チュートリアルをご覧ください。
4. メイン(利用側)エージェントの実行¶
仕組み¶
メインエージェントはRemoteA2aAgent()関数を使用してリモートエージェント(この例ではprime_agent)を利用します。以下に示すように、RemoteA2aAgent()にはname、description、およびagent_cardのURLが必要です。
<...code truncated...>
from google.adk.agents.remote_a2a_agent import AGENT_CARD_WELL_KNOWN_PATH
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
prime_agent = RemoteA2aAgent(
name="prime_agent",
description="Agent that handles checking if numbers are prime.",
agent_card=(
f"http://localhost:8001/a2a/check_prime_agent{AGENT_CARD_WELL_KNOWN_PATH}"
),
)
<...code truncated>
次に、エージェントでRemoteA2aAgentを単純に使用できます。この場合、prime_agentは以下のroot_agentのサブエージェントの1つとして使用されます。
from google.adk.agents.llm_agent import Agent
from google.genai import types
root_agent = Agent(
model="gemini-2.0-flash",
name="root_agent",
instruction="""
<You are a helpful assistant that can roll dice and check if numbers are prime.
You delegate rolling dice tasks to the roll_agent and prime checking tasks to the prime_agent.
Follow these steps:
1. If the user asks to roll a die, delegate to the roll_agent.
2. If the user asks to check primes, delegate to the prime_agent.
3. If the user asks to roll a die and then check if the result is prime, call roll_agent first, then pass the result to prime_agent.
Always clarify the results before proceeding.>
""",
global_instruction=(
"You are DicePrimeBot, ready to roll dice and check prime numbers."
),
sub_agents=[roll_agent, prime_agent],
tools=[example_tool],
generate_content_config=types.GenerateContentConfig(
safety_settings=[
types.SafetySetting( # avoid false alarm about rolling dice.
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=types.HarmBlockThreshold.OFF,
),
]
),
)
対話の例¶
メインエージェントとリモートエージェントの両方が実行されたら、ルートエージェントと対話して、A2A経由でリモートエージェントを呼び出す方法を確認できます。
単純なサイコロ振り: この対話では、ローカルエージェントであるロールエージェントを使用します。
素数判定:
この対話では、A2A経由でリモートエージェントであるプライムエージェントを使用します。
組み合わせた操作:
この対話では、ローカルのロールエージェントとリモートのプライムエージェントの両方を使用します。
次のステップ¶
A2Aサーバー経由でリモートエージェントを使用するエージェントを作成したので、次のステップは別のエージェントからそれに接続する方法を学ぶことです。
- A2Aクイックスタート(公開):他のエージェントがA2Aプロトコル経由で既存のエージェントを使用できるように公開する方法を学びます。