跳转到内容

AgentCore Gateway 到 Agent

connection 生成器可以将代理(TypeScriptPython)注册为使用 protocol: http 生成的 AgentCore GatewayAgentCore Runtime 目标

连接后,Gateway 会在 <gatewayUrl>/<targetName>/invocations 下代理对代理的请求,使用 IAM SigV4 对发往运行时的流量进行签名。这为您的代理提供了一个统一的受管理入口点——由于调用者只需要访问 Gateway,代理运行时本身可以部署在其后面的 VPC 内部。

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

  1. 使用 protocol: http 生成的 agentcore-gateway 项目
  2. 使用 infra: agentcore 创建的代理组件(ts#agentpy#agent)。auth: iam(Gateway 使用其自己的角色调用它)或 auth: cognito(Gateway 转发调用者的 JWT——参见将调用者身份转发到运行时)都可以使用。
Terminal window
pnpm nx g @aws/nx-plugin:connection
您还可以执行试运行以查看哪些文件会被更改
Terminal window
pnpm nx g @aws/nx-plugin:connection --dry-run

选择 Gateway 项目作为源,代理项目作为目标。如果代理项目包含多个组件,请指定 targetComponent 以消除歧义。

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

生成器将现有项目连接在一起,而不是生成新的源文件。以下文件会被修改:

  • 文件夹packages/<gateway>
    • project.json Gateway 的 dev 目标获得对代理的 <agent>-dev 的依赖
    • local-dev.ts ATTACHED_AGENTS 更新,使本地 gateway 代理到代理

生成器无法自动将代理目标连接到您的基础设施,因为它不知道哪个堆栈或模块实例化了 Gateway。请自行添加一个对 gateway.addAgent(agent) 的调用。

在实例化 Gateway 的堆栈中,将代理注册为目标:

packages/infra/src/stacks/application-stack.ts
const myAgent = new MyAgent(this, 'MyAgent');
const myGateway = new MyGateway(this, 'MyGateway');
// Register the agent as a runtime target of the Gateway. The target name
// defaults to the agent's `agentName` (its class name in kebab-case,
// e.g. `MyAgent` -> `my-agent`), and forms the target's invocation path:
// <gatewayUrl>/my-agent/invocations
myGateway.addAgent(myAgent);

要覆盖默认目标名称,请传递 gatewayTargetName

myGateway.addAgent(myAgent, { gatewayTargetName: 'my-target' });

该构造授予 Gateway 的执行角色对代理运行时的调用访问权限,并使用 GATEWAY_IAM_ROLE 凭证提供程序配置目标,因此 Gateway 使用其自己的角色对出站调用进行签名。

<gatewayUrl origin>/<targetName>/invocations 的请求会被转发到代理运行时,无需协议转换,因此调用者使用与直接针对运行时相同的请求形式——SSE 流(AG-UI)、JSON 流(Python HTTP)和 A2A JSON-RPC 都可以代理通过。调用者使用 Gateway 进行身份验证(IAM SigV4 或 Cognito JWT,取决于 Gateway 的 auth),而不是使用代理进行身份验证。

要将网站连接到 Gateway 的代理,请使用 connection 生成器

默认情况下,Gateway 使用其自己的 IAM 角色(GATEWAY_IAM_ROLE 凭证提供程序)对出站调用进行签名,因此运行时看到的是 _Gateway 的_身份,而不是调用者的身份。如果您希望代理对调用者进行授权——例如读取用户的 subscope 声明——请使用 Cognito Gateway 前置 Cognito 代理。然后 Gateway 会将调用者的 JWT 原封不动地转发到运行时(JWT_PASSTHROUGH 凭证提供程序),运行时会重新验证它。

使用 auth: cognito 生成两端并按上述方式连接它们:

  • 使用 auth: cognito 创建的代理(ts#agentpy#agent),以及
  • 使用 auth: cognito 创建的 Gateway,前置相同的 Cognito 用户池。

其他一切都是自动的——gateway.addAgent(agent)(CDK)和生成的 Terraform 运行时模块会根据代理的 auth 为您处理连接:

  • 使用 JWT_PASSTHROUGH 凭证提供程序(而不是 GATEWAY_IAM_ROLE)创建目标,并且
  • 运行时将 Authorization 标头列入允许列表,以便转发的令牌到达您的代理代码。如果没有此允许列表,AgentCore 会验证令牌但在到达您的容器之前剥离标头。

调用者使用 Authorization: Bearer <jwt>(无 SigV4)调用 Gateway,代理从 Authorization 标头读取声明——跳过签名验证,因为运行时的入站授权器已经验证了令牌:

packages/py_project/.../my_agent/main.py
import jwt # PyJWT
@app.post('/invocations')
async def invoke(input: InvokeInput, request: Request):
token = request.headers['authorization'].removeprefix('Bearer ')
claims = jwt.decode(token, options={'verify_signature': False})
# authorize on claims['sub'], claims['scope'], ...

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

Terminal window
pnpm nx dev <gateway-name>

启动本地 gateway 以及每个附加代理在其分配的本地端口上。本地 gateway 将 /<targetName>/... 路径代理到每个代理的本地服务器,匹配已部署 Gateway 的基于路径的路由。