跳转到内容

TypeScript Agent 到 A2A Agent

connection 生成器可以将您的 TypeScript Agent 连接到远程 A2A agent——无论是 TypeScript 还是 Python——这样您的 agent 就可以将任务委托给另一个 agent 作为工具。

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

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

  1. 一个包含 Strands Agent 组件的 TypeScript 项目(任何协议)
  2. 一个使用 --protocol=a2a--auth=iam 生成的 Agent 组件项目(ts#agentpy#agent
  3. 两个组件都使用 infra: agentcore 创建
Terminal window
pnpm nx g @aws/nx-plugin:connection
您还可以执行试运行以查看哪些文件会被更改
Terminal window
pnpm nx g @aws/nx-plugin:connection --dry-run

选择您的主机 agent 项目作为源,选择您的 A2A agent 项目作为目标。如果您的项目包含多个组件,请指定 sourceComponenttargetComponent 选项以消除歧义。

参数类型默认值描述
sourceProject 必需string-源项目
targetProject 必需string-要连接到的目标项目
sourceComponent string-要从其连接的源组件(组件名称、相对于源项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为源。
targetComponent string-要连接到的目标组件(组件名称、相对于目标项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为目标。
preferInstallDependencies booleantrue是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。

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

  • 文件夹packages/common/agent-connection
    • 文件夹src
      • 文件夹app
        • <target-agent-name>-client-strands.ts High-level Strands client for the connected A2A agent
      • 文件夹core
        • agentcore-endpoints.ts Framework-agnostic ARN/URL resolution
        • agentcore-fetch.ts Framework-agnostic SigV4 / JWT / session-forwarding fetch
        • agentcore-a2a-client-config.ts Framework-agnostic A2A client config (signed clientFactory)
        • agentcore-a2a-client-strands.ts Strands A2A client wrapping the config
      • index.ts Exports all clients
    • project.json
    • tsconfig.json

此外,它还会:

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

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

packages/example/src/my-agent/agent.ts
import { Agent, tool } from '@strands-agents/sdk';
import { RemoteAgentClientStrands } from '@my-scope/agent-connection';
import { z } from 'zod';
export const getAgent = async (sessionId: string) => {
const remoteAgent = await RemoteAgentClientStrands.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 的一致性。

在底层,RemoteAgentClientStrands.create(sessionId) 返回一个 Strands A2AAgent,当部署到 AWS 时配置了 SigV4 签名的 clientFactory,当 LOCAL_DEV=true 时使用普通的 http://localhost:<port>/ 端点。签名和端点解析位于与框架无关的 agentcore-a2a-client-config.ts 中;只有轻量级的 agentcore-a2a-client-strands.ts 依赖于 Strands。

运行连接生成器后,您需要授予主机代理调用远程 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 命名空间中,以便主机代理可以在运行时发现它。

生成器配置主机 agent 的 dev 目标以:

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

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

Terminal window
pnpm nx <agent-name>-dev <project-name>

这将启动主机 agent 和所有已连接的 A2A agent,主机 agent 通过分配的本地端口上的普通 HTTP 调用远程 agent。