跳转到内容

TypeScript Strands Agent 到 A2A Agent

connection 生成器可以将您的 TypeScript Strands Agent 连接到远程 A2A 代理 — 可以是 TypeScriptPython — 这样您的代理就可以将任务委托给另一个代理作为工具使用。

该生成器会设置所有必要的配置,使您的代理能够发现并调用远程 A2A 代理,无论是部署到 AWS(通过 Bedrock AgentCore)还是在本地运行时都可以。

在使用此生成器之前,请确保您具备:

  1. 一个包含 Strands Agent 组件的 TypeScript 项目(任何协议)
  2. 一个使用 --protocol=A2A--auth=IAM 生成的 Strands Agent 组件项目(可以是 ts#strands-agentpy#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)。使用 '.' 显式选择项目作为目标。

    生成器会创建一个共享的 agent-connection 包并修改您的代理代码:

    • 文件夹packages/common/agent-connection
      • 文件夹src
        • 文件夹app
          • <target-agent-name>-client.ts 连接的 A2A 代理的高级客户端
        • 文件夹core
          • agentcore-a2a-client.ts 带有 SigV4 身份验证的低级 AgentCore A2A 客户端
        • index.ts 导出所有客户端
      • project.json
      • tsconfig.json

    此外,它还会:

    • 转换您代理的 agent.ts 以将远程 A2A 代理注册为 Strands tool
    • 更新代理的 serve-local 目标以依赖于目标代理的 serve-local 目标
    • 安装所需的依赖项

    生成器会转换您代理的 agent.ts 以将远程 A2A 代理包装为工具:

    packages/example/src/my-agent/agent.ts
    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 可观测性的一致性。

    在底层,RemoteAgentClient.create(sessionId) 返回一个配置了 SigV4 签名 clientFactory 的 Strands A2AAgent(当部署到 AWS 时),以及一个普通的 http://localhost:<port>/ 端点(当 SERVE_LOCAL=true 时)。

    运行连接生成器后,您需要授予宿主代理调用远程 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 构造自动注册到运行时配置agentcore 命名空间中,以便宿主代理可以在运行时发现它。

    生成器会配置宿主代理的 serve-local 目标以:

    1. 自动启动连接的 A2A 代理
    2. 设置 SERVE_LOCAL=true,使生成的客户端直接连接到 http://localhost:<port>/ 而不是 AgentCore

    使用以下命令在本地运行代理:

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

    这将同时启动宿主代理和所有连接的 A2A 代理,宿主代理通过分配的本地端口使用普通 HTTP 调用远程代理。