クイックスタート: A2A経由でリモートエージェントを利用する¶
このクイックスタートでは、開発者にとって最も一般的な出発点である「リモートエージェントがあり、ADKエージェントでA2A経由でそれを使用するにはどうすればよいですか?」について説明します。これは、さまざまなエージェントが連携して対話する必要がある複雑なマルチエージェントシステムを構築するために不可欠です。
概要¶
このサンプルは、Agent Development Kit(ADK)のAgent-to-Agent(A2A)アーキテクチャを示し、複数のエージェントが連携して複雑なタスクを処理する方法を紹介します。このサンプルでは、サイコロを振って数字が素数かどうかを確認できるエージェントを実装しています。
┌─────────────────┐ ┌──────────────────┐ ┌────────────────────┐
│ ルートエージェント │───▶│ ロールエージェント │ │ リモートプライム │
│ (ローカル) │ │ (ローカル) │ │ エージェント │
│ │ │ │ │ (localhost:8001) │
│ │───▶│ │◀───│ │
└─────────────────┘ └──────────────────┘ └────────────────────┘
A2A基本サンプルは、以下で構成されています。
- ルートエージェント(
root_agent):特殊なサブエージェントにタスクを委任するメインオーケストレーター - ロールエージェント(
roll_agent):サイコロを振る操作を処理するローカルサブエージェント - プライムエージェント(
prime_agent):数字が素数かどうかを確認するリモートA2Aエージェント。このエージェントは別のA2Aサーバーで実行されています。
ADKサーバーでエージェントを公開する¶
a2a_basicの例では、まずローカルルートエージェントが使用できるように、A2Aサーバー経由でcheck_prime_agentを公開する必要があります。
1. サンプルコードの取得¶
まず、Goがインストールされ、環境が設定されていることを確認してください。
a2a_basic サンプルをクローンして移動できます。
ご覧のとおり、フォルダー構造は次のようになっています。
a2a_basic/
├── remote_a2a/
│ └── check_prime_agent/
│ └── main.go
├── go.mod
├── go.sum
└── main.go # ローカルルートエージェント
メインエージェント(a2a_basic/main.go)¶
rollDieTool:サイコロを振るための関数ツールnewRollAgent:サイコロ振りに特化したローカルエージェントnewPrimeAgent:リモートA2Aエージェントの構成newRootAgent:委任ロジックを備えたメインオーケストレーター
リモートプライムエージェント(a2a_basic/remote_a2a/check_prime_agent/main.go)¶
checkPrimeTool:素数判定アルゴリズムmain:素数判定サービスとA2Aサーバーの実装
2. リモートプライムエージェントサーバーの起動¶
ADKエージェントがA2A経由でリモートエージェントを利用する方法を示すには、まずプライムエージェント(check_prime_agentの下)をホストするリモートエージェントサーバーを起動する必要があります。
実行すると、次のようなものが表示されます。
2025/11/06 11:00:19 Starting A2A prime checker server on port 8001
2025/11/06 11:00:19 Starting the web server: &{port:8001}
2025/11/06 11:00:19
2025/11/06 11:00:19 Web servers starts on http://localhost:8001
2025/11/06 11:00:19 a2a: you can access A2A using jsonrpc protocol: http://localhost:8001
3. リモートエージェントの必須エージェントカードに注意してください¶
A2Aプロトコルでは、各エージェントが何をするかを記述したエージェントカードを持っている必要があります。
Go ADKでは、A2Aランチャーを使用してエージェントを公開すると、エージェントカードが動的に生成されます。http://localhost:8001/.well-known/agent-card.jsonにアクセスして、生成されたカードを確認できます。
4. メイン(利用側)エージェントの実行¶
仕組み¶
メインエージェントはremoteagent.Newを使用してリモートエージェント(この例ではprime_agent)を利用します。以下に示すように、Name、Description、およびAgentCardSource URLが必要です。
func newPrimeAgent() (agent.Agent, error) {
remoteAgent, err := remoteagent.New(remoteagent.A2AConfig{
Name: "prime_agent",
Description: "Agent that handles checking if numbers are prime.",
AgentCardSource: "http://localhost:8001",
})
if err != nil {
return nil, fmt.Errorf("failed to create remote prime agent: %w", err)
}
return remoteAgent, nil
}
次に、ルートエージェントでリモートエージェントを単純に使用できます。この場合、primeAgentは以下のroot_agentのサブエージェントの1つとして使用されます。
func newRootAgent(ctx context.Context, rollAgent, primeAgent agent.Agent) (agent.Agent, error) {
model, err := gemini.NewModel(ctx, "gemini-2.0-flash", &genai.ClientConfig{})
if err != nil {
return nil, err
}
return llmagent.New(llmagent.Config{
Name: "root_agent",
Model: model,
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.
`,
SubAgents: []agent.Agent{rollAgent, primeAgent},
Tools: []tool.Tool{},
})
}
対話の例¶
メインエージェントとリモートエージェントの両方が実行されたら、ルートエージェントと対話して、A2A経由でリモートエージェントを呼び出す方法を確認できます。
単純なサイコロ振り: この対話では、ローカルエージェントであるロールエージェントを使用します。
ユーザー:6面体のサイコロを振ってください
ボットがツールを呼び出します:transfer_to_agent、引数:map[agent_name:roll_agent]
ボットがツールを呼び出します:roll_die、引数:map[sides:6]
ボット:6面体のサイコロを振った結果は6です。
素数判定:
この対話では、A2A経由でリモートエージェントであるプライムエージェントを使用します。
ユーザー:7は素数ですか?
ボットがツールを呼び出します:transfer_to_agent、引数:map[agent_name:prime_agent]
ボットがツールを呼び出します:prime_checking、引数:map[nums:[7]]
ボット:はい、7は素数です。
組み合わせた操作:
この対話では、ローカルのロールエージェントとリモートのプライムエージェントの両方を使用します。
ユーザー:サイコロを振って、それが素数かどうかを確認してください
ボット:わかりました。まずサイコロを振って、その結果が素数かどうかを確認します。
ボットがツールを呼び出します:transfer_to_agent、引数:map[agent_name:roll_agent]
ボTットがツールを呼び出します:roll_die、引数:map[sides:6]
ボットがツールを呼び出します:transfer_to_agent、引数:map[agent_name:prime_agent]
ボットがツールを呼び出します:prime_checking、引数:map[nums:[3]]
ボット:3は素数です。
次のステップ¶
A2Aサーバー経由でリモートエージェントを使用するエージェントを作成したので、次のステップは独自のエージェントを公開する方法を学ぶことです。
- A2Aクイックスタート(公開):他のエージェントがA2Aプロトコル経由で既存のエージェントを使用できるように公開する方法を学びます。