Python Strands Agent から A2A Agent への接続
connection ジェネレーターは、Python Strands Agent をリモートの A2A エージェント(TypeScript または Python)に接続し、エージェントが別のエージェントにツールとして委任できるようにします。
このジェネレーターは、AWS へのデプロイ時(Bedrock AgentCore 経由)とローカル実行時の両方で、エージェントがリモート A2A エージェントを検出して呼び出すために必要なすべての配線を設定します。
このジェネレーターを使用する前に、以下を確認してください:
- Strands Agent コンポーネント(任意のプロトコル)を持つ Python プロジェクト
--protocol=A2Aと--auth=IAMで生成された Strands Agent コンポーネントを持つプロジェクト(ts#strands-agentまたはpy#strands-agentのいずれか)- 両方のコンポーネントが
computeType: BedrockAgentCoreRuntimeで作成されていること
ジェネレーターの実行
Section titled “ジェネレーターの実行”- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - connection - 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:connectionyarn nx g @aws/nx-plugin:connectionnpx nx g @aws/nx-plugin:connectionbunx nx g @aws/nx-plugin:connectionソースとしてホストエージェントプロジェクトを、ターゲットとして A2A エージェントプロジェクトを選択します。プロジェクトに複数のコンポーネントが含まれている場合は、sourceComponent と targetComponent オプションを指定して明確にしてください。
| パラメータ | 型 | デフォルト | 説明 |
|---|---|---|---|
| sourceProject 必須 | string | - | ソース プロジェクト |
| targetProject 必須 | string | - | 接続先のターゲット プロジェクト |
| sourceComponent | string | - | 接続元のソース コンポーネント (コンポーネント名、ソース プロジェクト ルートからの相対パス、またはジェネレーター ID)。プロジェクトをソースとして明示的に選択するには '.' を使用します。 |
| targetComponent | string | - | 接続先のターゲット コンポーネント (コンポーネント名、ターゲット プロジェクト ルートからの相対パス、またはジェネレーター ID)。プロジェクトをターゲットとして明示的に選択するには '.' を使用します。 |
ジェネレーターの出力
Section titled “ジェネレーターの出力”ジェネレーターは、packages/common/agent_connection/ に共有の agent_connection Python プロジェクトを作成します(まだ存在しない場合)。接続ごとのクライアントモジュールがこの共有プロジェクトに生成されます:
Directorypackages/common/agent_connection
Directory<scope>_agent_connection
- __init__.py 接続ごとのクライアントを再エクスポート
Directorycore
- agentcore_a2a_client.py SigV4 認証を使用するコア AgentCore A2A クライアント
Directoryapp
- <target_agent_name>_client.py 各 A2A エージェント用の接続ごとのクライアント
さらに、ジェネレーターは以下を行います:
- エージェントの
agent.pyを変換して、リモート A2A エージェントを@toolを使用してツールとして登録 - エージェントプロジェクトのワークスペース依存関係として
agent_connectionプロジェクトを追加 - エージェントの
serve-localターゲットを更新して、ターゲットエージェントのserve-localターゲットに依存するようにする
接続された A2A エージェントの使用
Section titled “接続された A2A エージェントの使用”ジェネレーターは、エージェントの agent.py を変換して、リモート A2A エージェントをツールとしてラップします:
from contextlib import contextmanagerfrom strands import Agent, tool
from my_scope_agent_connection import RemoteAgentClient
@contextmanagerdef get_agent(session_id: str): remote_agent = RemoteAgentClient.create(session_id=session_id)
@tool def ask_remote_agent(prompt: str) -> str: """Delegate a question to the remote RemoteAgent A2A agent and return its reply.""" return str(remote_agent(prompt))
yield Agent( system_prompt="...", tools=[ask_remote_agent], )session_id パラメーターは呼び出し元から渡され、Bedrock AgentCore Observability の一貫性を確保します。
内部的には、RemoteAgentClient.create(session_id=...) は、AWS にデプロイされた際に SigV4 でリクエストに署名する httpx.AsyncClient で構成された Strands A2AAgent を返し、SERVE_LOCAL=true の場合はプレーンな http://localhost:<port>/ エンドポイントを使用します。
インフラストラクチャ
Section titled “インフラストラクチャ”接続ジェネレーターを実行した後、ホストエージェントがリモート A2A エージェントを呼び出す権限を付与する必要があります。
const remoteAgent = new RemoteAgent(this, 'RemoteAgent');const myAgent = new MyAgent(this, 'MyAgent');
// Grant the host agent permission to invoke the remote A2A agentremoteAgent.grantInvokeAccess(myAgent);A2A エージェントの grantInvokeAccess は、bedrock-agentcore:InvokeAgentRuntime と bedrock-agentcore:GetAgentCard の両方を設定します。A2A クライアントは、エージェントカードを取得してメッセージを送信するために両方が必要です。
リモートエージェントの AgentCore ランタイム ARN は、生成された CDK コンストラクトによって Runtime Configuration の agentcore 名前空間に自動的に登録されるため、ホストエージェントは実行時にそれを検出できます。
module "remote_agent" { source = "../../common/terraform/src/app/agents/remote-agent"}
module "my_agent" { source = "../../common/terraform/src/app/agents/my-agent"}
# Grant the host agent permission to invoke the remote A2A agentresource "aws_iam_policy" "agent_invoke_a2a" { name = "AgentInvokeA2aPolicy" policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = [ "bedrock-agentcore:InvokeAgentRuntime", "bedrock-agentcore:GetAgentCard", ] Resource = module.remote_agent.agent_core_runtime_arn }] })}
resource "aws_iam_role_policy_attachment" "agent_invoke_a2a" { role = module.my_agent.agent_core_runtime_role_arn policy_arn = aws_iam_policy.agent_invoke_a2a.arn}リモートエージェントの AgentCore ランタイム ARN は、生成された Terraform モジュールによって Runtime Configuration の agentcore 名前空間に自動的に登録されるため、ホストエージェントは実行時にそれを検出できます。
ローカル開発
Section titled “ローカル開発”ジェネレーターは、ホストエージェントの serve-local ターゲットを以下のように構成します:
- 接続された A2A エージェントを自動的に起動
SERVE_LOCAL=trueを設定して、生成されたクライアントが AgentCore の代わりにhttp://localhost:<port>/に直接接続するようにする
エージェントをローカルで実行するには:
pnpm nx <agent-name>-serve-local <project-name>yarn nx <agent-name>-serve-local <project-name>npx nx <agent-name>-serve-local <project-name>bunx nx <agent-name>-serve-local <project-name>これにより、ホストエージェントとすべての接続された A2A エージェントが起動し、ホストエージェントは割り当てられたローカルポートでプレーン HTTP を介してリモートエージェントを呼び出します。