TypeScript Agent 到 Gateway
connection 生成器可以将您的 TypeScript Agent 连接到 AgentCore Gateway。
该生成器会配置 agent,使其在部署时使用 IAM SigV4 向 Gateway 进行身份验证,并在本地运行时连接到由 Gateway 项目启动的本地 gateway。
在使用此生成器之前,请确保您具有:
- 一个包含 Agent 组件的 TypeScript 项目(
infra: agentcore) - 一个
agentcore-gateway项目
- 安装 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选择 agent 项目作为源,Gateway 项目作为目标。
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| sourceProject 必需 | string | - | 源项目 |
| targetProject 必需 | string | - | 要连接到的目标项目 |
| sourceComponent | string | - | 要从其连接的源组件(组件名称、相对于源项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为源。 |
| targetComponent | string | - | 要连接到的目标组件(组件名称、相对于目标项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为目标。 |
| preferInstallDependencies | boolean | true | 是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。 |
生成器会将共享的核心客户端文件输出到您的 agent-connection 包中,加上每个 Gateway 的包装器,并修改您的 agent:
文件夹packages/common/agent-connection
文件夹src
文件夹core/
- agentcore-endpoints.ts 框架无关的 ARN/URL 解析
- agentcore-gateway-mcp-transport.ts 框架无关的 Gateway MCP 传输
- agentcore-gateway-mcp-client-strands.ts 用于已部署 Gateway 的 Strands MCP 客户端
文件夹app/
- <gateway-kebab>-client-strands.ts 每个 Gateway 的 Strands 客户端包装器
- index.ts 重新导出 Gateway 客户端
此外,生成器还会:
- 修改您的 agent 的
agent.ts以导入 Gateway 客户端类,调用<Gateway>ClientStrands.create(),并在tools数组中注册返回的客户端 - 将 agent 的
<agent>-dev目标配置为依赖于 Gateway 的dev目标 - 安装所需的 SigV4 / MCP 依赖项
使用已连接的 Gateway
Section titled “使用已连接的 Gateway”生成器会转换您的 agent 的 agent.ts 以使用 Gateway 客户端:
import { Agent } from '@strands-agents/sdk';import { MyGatewayClientStrands } from ':my-scope/agent-connection';
export const getAgent = async () => { const myGateway = await MyGatewayClientStrands.create(); return new Agent({ systemPrompt: '...', tools: [myGateway], });};当部署时(未设置 LOCAL_DEV),客户端指向 Gateway 的 MCP 端点并使用 SigV4 进行身份验证。当 LOCAL_DEV=true 时,它指向由 Gateway 项目的 dev 目标启动的本地 gateway,因此相同的 agent.ts 在两种模式下都能统一工作。
会话 ID 会通过 X-Amzn-Bedrock-AgentCore-Runtime-Session-Id 标头自动传播到下游 MCP 服务器。
运行生成器后,您必须授予 agent 调用 Gateway 的权限。
const gateway = new MyGateway(this, 'MyGateway');const myAgent = new MyAgent(this, 'MyAgent');
// Grant the agent permissions to invoke the Gatewaygateway.grantInvokeAccess(myAgent);Gateway URL 会由生成的 CDK 构造自动注册到 运行时配置 的 agentcore.gateways.<ClassName> 命名空间中,以便 agent 可以在运行时发现它。
module "my_gateway" { source = "../../common/terraform/src/app/gateways/my-gateway"}
module "my_agent" { source = "../../common/terraform/src/app/agents/my-agent"}
# Grant the agent permission to invoke the Gatewayresource "aws_iam_policy" "agent_invoke_gateway" { name = "AgentInvokeGatewayPolicy" policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = "bedrock-agentcore:InvokeGateway" Resource = module.my_gateway.gateway_arn }] })}
resource "aws_iam_role_policy_attachment" "agent_invoke_gateway" { role = module.my_agent.agent_core_runtime_role_arn policy_arn = aws_iam_policy.agent_invoke_gateway.arn}Gateway URL 会由生成的 Terraform 模块自动注册到 运行时配置 的 agentcore.gateways.<ClassName> 命名空间中,以便 agent 可以在运行时发现它。
生成器会将 agent 的 serve-local 目标配置为:
- 启动已连接 Gateway 的本地 gateway 和每个附加的 MCP 服务器
- 设置
SERVE_LOCAL=true,以便生成的客户端指向本地 gateway,而不是已部署的 Gateway
使用以下命令在本地运行 agent:
pnpm nx <agent-name>-dev <project-name>yarn nx <agent-name>-dev <project-name>npx nx <agent-name>-dev <project-name>bunx nx <agent-name>-dev <project-name>要在本地运行 agent 针对已部署的 Gateway(例如,为了测试 Cedar 策略),请使用 agent 的 serve target。未设置 SERVE_LOCAL 时,客户端会从运行时配置解析已部署的 Gateway URL,并使用您的本地 AWS 凭证对请求进行 SigV4 签名:
pnpm nx <agent-name>-serve <project-name>yarn nx <agent-name>-serve <project-name>npx nx <agent-name>-serve <project-name>bunx nx <agent-name>-serve <project-name>本地 gateway 代替已部署的 Gateway,因此:
- 没有 Cedar 策略评估。 无论策略如何,agent 都可以看到每个工具。使用
serve目标针对已部署的 Gateway 测试策略。 - 保留工具名称前缀。 每个本地 MCP 服务器的工具都被包装以公开
<target-name>___<tool-name>形式的名称,与已部署的 Gateway 发出的名称相匹配。这使得 agent 的系统提示和您引用的 Cedar 操作名称在本地和已部署运行中保持一致。