Skip to content

Python Strands Agent から A2A Agent への接続

connection ジェネレーターは、Python Strands Agent をリモートの A2A エージェント(TypeScript または Python)に接続し、エージェントが別のエージェントにツールとして委任できるようにします。

このジェネレーターは、AWS へのデプロイ時(Bedrock AgentCore 経由)とローカル実行時の両方で、エージェントがリモート A2A エージェントを検出して呼び出すために必要なすべての配線を設定します。

このジェネレーターを使用する前に、以下を確認してください:

  1. Strands Agent コンポーネント(任意のプロトコル)を持つ Python プロジェクト
  2. --protocol=A2A--auth=IAM で生成された Strands Agent コンポーネントを持つプロジェクト(ts#strands-agent または py#strands-agent のいずれか)
  3. 両方のコンポーネントが computeType: BedrockAgentCoreRuntime で作成されていること
  1. インストール Nx Console VSCode Plugin まだインストールしていない場合
  2. VSCodeでNxコンソールを開く
  3. クリック Generate (UI) "Common Nx Commands"セクションで
  4. 検索 @aws/nx-plugin - connection
  5. 必須パラメータを入力
    • クリック Generate

    ソースとしてホストエージェントプロジェクトを、ターゲットとして A2A エージェントプロジェクトを選択します。プロジェクトに複数のコンポーネントが含まれている場合は、sourceComponenttargetComponent オプションを指定して明確にしてください。

    パラメータ デフォルト 説明
    sourceProject 必須 string - ソース プロジェクト
    targetProject 必須 string - 接続先のターゲット プロジェクト
    sourceComponent string - 接続元のソース コンポーネント (コンポーネント名、ソース プロジェクト ルートからの相対パス、またはジェネレーター ID)。プロジェクトをソースとして明示的に選択するには '.' を使用します。
    targetComponent string - 接続先のターゲット コンポーネント (コンポーネント名、ターゲット プロジェクト ルートからの相対パス、またはジェネレーター ID)。プロジェクトをターゲットとして明示的に選択するには '.' を使用します。

    ジェネレーターは、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 エージェントをツールとしてラップします:

    packages/my-project/my_module/agent/agent.py
    from contextlib import contextmanager
    from strands import Agent, tool
    from my_scope_agent_connection import RemoteAgentClient
    @contextmanager
    def 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>/ エンドポイントを使用します。

    接続ジェネレーターを実行した後、ホストエージェントがリモート A2A エージェントを呼び出す権限を付与する必要があります。

    packages/infra/src/stacks/application-stack.ts
    const remoteAgent = new RemoteAgent(this, 'RemoteAgent');
    const myAgent = new MyAgent(this, 'MyAgent');
    // Grant the host agent permission to invoke the remote A2A agent
    remoteAgent.grantInvokeAccess(myAgent);

    A2A エージェントの grantInvokeAccess は、bedrock-agentcore:InvokeAgentRuntimebedrock-agentcore:GetAgentCard の両方を設定します。A2A クライアントは、エージェントカードを取得してメッセージを送信するために両方が必要です。

    リモートエージェントの AgentCore ランタイム ARN は、生成された CDK コンストラクトによって Runtime Configurationagentcore 名前空間に自動的に登録されるため、ホストエージェントは実行時にそれを検出できます。

    ジェネレーターは、ホストエージェントの serve-local ターゲットを以下のように構成します:

    1. 接続された A2A エージェントを自動的に起動
    2. SERVE_LOCAL=true を設定して、生成されたクライアントが AgentCore の代わりに http://localhost:<port>/ に直接接続するようにする

    エージェントをローカルで実行するには:

    Terminal window
    pnpm nx <agent-name>-serve-local <project-name>

    これにより、ホストエージェントとすべての接続された A2A エージェントが起動し、ホストエージェントは割り当てられたローカルポートでプレーン HTTP を介してリモートエージェントを呼び出します。