本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
开始使用 G AgentCore ateway
在本快速入门指南中,您将学习如何设置网关并使用 AgentCore CLI 将其集成到您的代理中。有关更全面的指南和示例,请参阅亚马逊 Bedrock AgentCore Gateway GitHub 存储库
注意
AgentCore CLI 提供了用于管理 AgentCore 网关资源的命令和交互式 TUI 向导。有关完整的 AWS SDK API 参考,请参阅AgentCore 控制平面操作
先决条件
在开始之前,请确保您具备以下条件:
-
AWS 配置了凭据的账户。要配置凭证,您可以按照 AWS CLI 入门中的步骤安装和使用 AWS 命令行界面。
-
Node.js 已安装 20 多个(用于 AgentCore CLI)。
-
已安装 Python 3.10+(用于代理脚本)。
-
创建角色、Lambda 函数和使用亚马逊基岩 AgentCore的 IAM 权限。
-
模型访问权限。默认情况下,亚马逊 Bedrock 允许访问基础模型。要使用非基础模型,请按照模型访问步骤进行操作。
第 1 步:设置和安装
全局安装 AgentCore CLI:
npm install -g @aws/agentcore
使用代理和网关创建一个新 AgentCore 项目:
例
第 2 步:创建网关
使用 AgentCore CLI 为您的项目添加网关和目标:
例
要改用 JWT-based 授权,请--authorizer-type CUSTOM_JWT使用您的 OAuth 发现 URL 指定:
例
第 3 步:运行安装程序
将您的项目部署到 AWS:
agentcore deploy
CLI 合成 AWS CDK 堆栈并将您的网关、目标和代理部署到亚马逊 Bedrock。 AgentCore这大约需要 2-3 分钟。
第 4 步:将网关与代理一起使用
使用 CL AgentCore I 检索您的网关 URL:
agentcore status
创建一个名为的新文件run_agent.py并插入以下代码。首先安装 Python 依赖项:
pip install strands-agents mcp
""" Agent script to test the Gateway Run this after setup: python run_agent.py """ from strands import Agent from strands.models import BedrockModel from strands.tools.mcp.mcp_client import MCPClient from mcp.client.streamable_http import streamablehttp_client import json import sys def create_streamable_http_transport(mcp_url: str): return streamablehttp_client(mcp_url) def get_full_tools_list(client): """Get all tools with pagination support""" more_tools = True tools = [] pagination_token = None while more_tools: tmp_tools = client.list_tools_sync(pagination_token=pagination_token) tools.extend(tmp_tools) if tmp_tools.pagination_token is None: more_tools = False else: more_tools = True pagination_token = tmp_tools.pagination_token return tools def run_agent(): # Get the gateway URL from agentcore status gateway_url = "<YOUR_GATEWAY_URL>" # Replace with URL from 'agentcore status' # Model configuration - change if needed model_id = "anthropic.claude-3-7-sonnet-20250219-v1:0" print("Starting AgentCore Gateway Test Agent") print(f"Gateway URL: {gateway_url}") print(f"Model: {model_id}") print("-" * 60) # Setup Bedrock model bedrockmodel = BedrockModel( model_id=model_id, streaming=True, ) # Setup MCP client (no auth token needed for NONE authorizer) mcp_client = MCPClient(lambda: create_streamable_http_transport(gateway_url)) with mcp_client: # List available tools tools = get_full_tools_list(mcp_client) print(f"\nAvailable tools: {[tool.tool_name for tool in tools]}") print("-" * 60) # Create agent agent = Agent(model=bedrockmodel, tools=tools) # Interactive loop print("\nInteractive Agent Ready!") print("Try asking: 'What's the weather in Seattle?'") print("Type 'exit', 'quit', or 'bye' to end.\n") while True: user_input = input("You: ") if user_input.lower() in ["exit", "quit", "bye"]: print("Goodbye!") break print("\nThinking...\n") response = agent(user_input) print(f"\nAgent: {response.message.get('content', response)}\n") if __name__ == "__main__": run_agent()
运行你的代理
通过运行代理并与工具交互来测试您的网关。
python run_agent.py
仅此而已!代理将启动,你可以问一些问题,比如:
-
“西雅图的天气怎么样?”
-
“现在在纽约几点?”
你建造了什么
通过本入门教程,您创建了以下资源:
-
MCP 服务器(网关):托管端点
https://gateway-id.gateway.bedrock-agentcore.region.amazonaws.com/mcp -
Lambda 工具:返回测试数据的模拟函数(天气:“72°F,晴天”,时间:“下午 2:30”)
-
AI 代理:可以发现和使用您的工具的 Claude-powered 助手
问题排查
下表显示了一些可能的问题及其解决方案:
| 问题 | 解决方案 |
|---|---|
|
“没有名为'strands'的模块” |
运行: |
|
“模型访问被拒绝” |
默认情况下,亚马逊 Bedrock 允许访问基础模型。对于非基础模型,请按照模型访问步骤进行操作。 |
|
"AccessDeniedException" |
检查 bedrock-agentcore 的 IAM 权限:* |
|
网关没有响应 |
创建 DNS 传播后等待 30-60 秒 |
快速验证
在终端中运行以下命令以检查您的网关是否正常运行。
# Check your Gateway is working curl -X POST YOUR_GATEWAY_URL \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' # Watch live logs aws logs tail /aws/bedrock-agentcore/gateways/YOUR_GATEWAY_ID --follow
资源清理
要从您的项目中移除网关及其目标,请执行以下操作:
agentcore remove gateway --name TestGateway
要移除所有资源并重新部署,请执行以下操作:
agentcore remove all agentcore deploy
后续步骤
-
自定义 Lambda 工具:使用您的业务逻辑创建 Lambda 函数
-
添加您自己的 API:使用真实服务的 OpenAPI 规范扩展您的网关
-
生产设置:配置 VPC 终端节点、自定义域和监控