AgentCore Gateway 到 MCP Server
connection 生成器可以将 MCP 服务器(TypeScript 或 Python)注册为 AgentCore Gateway 的目标。
连接后,Gateway 会将 MCP 服务器的工具聚合到其单一的 MCP 端点中,根据其 Cedar 策略引擎评估调用,并使用 IAM SigV4 对发往 MCP 服务器的出站流量进行签名。
在使用此生成器之前,请确保您具备:
- 一个
agentcore-gateway项目 - 一个使用
infra: agentcore和auth: iam创建的 MCP 服务器组件(ts#mcp-server或py#mcp-server)
Gateway 必须具有 protocol: mcp,MCP 服务器必须具有 auth: iam — 生成器会验证这两者。非 IAM MCP 服务器无法附加,因为 Gateway 使用 SigV4 对出站流量进行签名。
- 安装 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选择 Gateway 项目作为源,MCP 服务器项目作为目标。如果 MCP 服务器项目包含多个组件,请指定 targetComponent 以消除歧义。
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| sourceProject 必需 | string | - | 源项目 |
| targetProject 必需 | string | - | 要连接到的目标项目 |
| sourceComponent | string | - | 要从其连接的源组件(组件名称、相对于源项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为源。 |
| targetComponent | string | - | 要连接到的目标组件(组件名称、相对于目标项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为目标。 |
| preferInstallDependencies | boolean | true | 是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。 |
生成器将现有项目连接在一起,而不是生成新的源文件。以下文件会被修改:
文件夹packages/<gateway>
- project.json Gateway 的
dev目标获得对 MCP 服务器的<mcp>-dev的依赖 - local-dev.ts
ATTACHED_MCP_SERVERS更新,以便本地 gateway 聚合 MCP 服务器
- project.json Gateway 的
Gateway 项目的 <gateway>-dev 目标获得对 MCP 服务器的 <mcp>-dev 目标的依赖,因此在本地运行 Gateway 时也会启动 MCP 服务器。MCP 服务器还会在 Gateway 项目的 local-dev.ts 中注册,以便本地 gateway 聚合其工具。
将 MCP 服务器目标添加到您的堆栈
Section titled “将 MCP 服务器目标添加到您的堆栈”生成器无法自动将 MCP 服务器目标连接到您的基础设施,因为它不知道哪个堆栈或模块实例化了 Gateway。您需要自己添加一个对 gateway.addMcpServer(server) 的调用。
在实例化 Gateway 的堆栈中,将 MCP 服务器注册为目标:
const myMcpServer = new MyMcpServer(this, 'MyMcpServer');const myGateway = new MyGateway(this, 'MyGateway');
// Register the MCP server as a target of the Gateway. The target name// defaults to the MCP server construct's id in kebab-case (`my-mcp-server`).myGateway.addMcpServer(myMcpServer);Gateway 目标名称(默认为 MCP 服务器的 mcpServerName)用作 Cedar 操作名称的前缀 — 操作格式为 AgentCore::Action::"<targetName>___<toolName>"。请参阅 编写策略部分。保持目标名称简短且稳定;稍后更改它会使引用旧名称的任何 Cedar 策略失效。
要覆盖默认目标名称,请传递 gatewayTargetName:
myGateway.addMcpServer(myMcpServer, { gatewayTargetName: 'my-mcp' });该构造使用 iamCredentialProvider.service = 'bedrock-agentcore' 配置目标,以便 Gateway 使用其自己的执行角色对出站调用进行签名。
在实例化 Gateway 的 Terraform 文件中,连接 MCP 服务器目标:
module "my_mcp_server" { source = "../../common/terraform/src/app/mcp-servers/my-mcp-server"}
module "my_gateway" { source = "../../common/terraform/src/app/gateways/my-gateway" policy_dependencies = [aws_bedrockagentcore_gateway_target.my_mcp_server.target_id]}
# Register the MCP server as a target of the Gatewayresource "aws_bedrockagentcore_gateway_target" "my_mcp_server" { gateway_identifier = module.my_gateway.gateway_id name = "my-mcp-server"
target_configuration { mcp { mcp_server { endpoint = "https://bedrock-agentcore.${local.aws_region}.amazonaws.com/runtimes/${urlencode(module.my_mcp_server.agent_core_runtime_arn)}/invocations?qualifier=DEFAULT" } } }
credential_provider_configuration { gateway_iam_role { service = "bedrock-agentcore" } }}目标 name(上面的 my-mcp-server)用作 Cedar 操作名称的前缀 — 请参阅 编写策略部分。policy_dependencies 确保引用此目标操作的 Cedar 策略在目标注册它们之后创建。
使用以下命令在本地运行 Gateway:
pnpm nx dev <gateway-name>yarn nx dev <gateway-name>npx nx dev <gateway-name>bunx nx dev <gateway-name>会启动一个本地 gateway 以及在其分配的本地端口上的每个附加的 MCP 服务器。本地 gateway 公开一个单一的 MCP 端点,该端点聚合附加服务器的工具。通过 TypeScript 或 Python gateway-connection 生成器连接到 Gateway 的代理在使用 SERVE_LOCAL=true 运行时会指向它。