TypeScript Strands Agent から A2A Agent への接続
connection ジェネレーターは、TypeScript Strands Agent をリモートの A2A エージェント(TypeScript または Python)に接続し、エージェントが別のエージェントにツールとして委譲できるようにします。
このジェネレーターは、AWS にデプロイされた場合(Bedrock AgentCore 経由)とローカルで実行される場合の両方で、エージェントがリモート A2A エージェントを検出して呼び出すために必要なすべての配線を設定します。
このジェネレーターを使用する前に、以下を確認してください:
- Strands Agent コンポーネント(任意のプロトコル)を持つ TypeScript プロジェクト
--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 “ジェネレーターの出力”ジェネレーターは共有の agent-connection パッケージを作成し、エージェントコードを変更します:
Directorypackages/common/agent-connection
Directorysrc
Directoryapp
- <target-agent-name>-client.ts 接続された A2A エージェント用の高レベルクライアント
Directorycore
- agentcore-a2a-client.ts SigV4 認証を使用した低レベル AgentCore A2A クライアント
- index.ts すべてのクライアントをエクスポート
- project.json
- tsconfig.json
さらに、以下を実行します:
- エージェントの
agent.tsを変換して、リモート A2A エージェントを Strandstoolとして登録 - エージェントの
serve-localターゲットを更新して、ターゲットエージェントのserve-localターゲットに依存するように設定 - 必要な依存関係をインストール
接続された A2A エージェントの使用
Section titled “接続された A2A エージェントの使用”ジェネレーターはエージェントの agent.ts を変換して、リモート A2A エージェントをツールとしてラップします:
import { Agent, tool } from '@strands-agents/sdk';import { RemoteAgentClient } from ':my-scope/agent-connection';import { z } from 'zod';
export const getAgent = async (sessionId: string) => { const remoteAgent = await RemoteAgentClient.create(sessionId); const remoteAgentTool = tool({ name: 'askRemoteAgent', description: 'Delegate a question to the remote RemoteAgent A2A agent and return its reply.', inputSchema: z.object({ prompt: z.string() }), callback: async ({ prompt }) => (await remoteAgent.invoke(prompt)).toString(), }); return new Agent({ systemPrompt: '...', tools: [remoteAgentTool], });};sessionId パラメータは呼び出し元から渡され、Bedrock AgentCore Observability の一貫性を確保します。
内部的には、RemoteAgentClient.create(sessionId) は、AWS にデプロイされた場合は SigV4 署名する clientFactory で構成された 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 を介してリモートエージェントを呼び出します。