AWS AgentCore CDK演示
用于在上部署AI代理的生产就绪参考架构 AWS基岩代理核心 使用AWS CDK。该项目演示了如何使用MCP(模型上下文协议)工具集成、OAuth2身份验证和CloudWatch监控来构建、部署和操作AI代理。
演示部署了一个 天气助理 使用Open-Meteo的免费API回答天气问题的代理,但基础结构模式适用于任何代理用例。
建筑
┌─────────────────────────────────────────────────────────┐
│ AWS Account │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Cognito │ │ AgentCore │ │ Monitoring │ │
│ │ User Pool │ │ │ │ CloudWatch │ │
│ │ + OAuth2 │──▶│ Gateway │ │ Dashboard │ │
│ │ Clients │ │ (MCP) │ │ + Alarms │ │
│ └──────────────┘ │ │ │ └──────────────┘ │
│ │ ▼ │ │
│ │ MCP Runtime │ │
│ │ (Weather │ │
│ │ Tools) │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ Agent │ │
│ │ Runtime │ │
│ │ (Strands) │ │
│ └──────────────┘ │
└─────────────────────────────────────────────────────────┘三层架构
| 堆栈 | 目的 |
|---|---|
| 认知 | OAuth2身份验证(用户池、M2M客户端凭据) |
| 代理商核心 | 网关+MCP运行时(工具)+代理运行时(Strands代理) |
| 监控 | CloudWatch仪表板、SNS警报(错误率、延迟、5xx) |
先决条件
- AWS帐户 使用基岩试剂进入岩芯
- AWS-CLI 已配置凭据
- Node.js 18+和npm
- 码头工人 (用于构建集装箱图像)
- python 3.12+(用于演示脚本)
- CDK-CLI:
npm install -g aws-cdk
快速开始
1.安装依赖项
cd infra
npm install2.Bootstrap CDK(仅限第一次)
npx cdk bootstrap -c env=dev3.构建和测试
npm run build
npm run test:unit4.部署
npx cdk deploy --all -c env=dev5.试试看
# Get the Agent Runtime ARN from stack outputs
export AGENT_RUNTIME_ARN=$(aws cloudformation describe-stacks \
--stack-name AcDemoAgentCoreStack-dev \
--query 'Stacks[0].Outputs[?OutputKey==`AgentRuntimeArn`].OutputValue' \
--output text)
# Ask a weather question
python scripts/ask-weather.py "What's the weather in Tel Aviv?"项目结构
.
├── infra/ # CDK Infrastructure
│ ├── bin/agentcore-demo.ts # CDK app entry point
│ ├── lib/
│ │ ├── config/
│ │ │ ├── constants.ts # Resource naming conventions
│ │ │ └── environments.ts # Environment configs (dev/prod)
│ │ └── stacks/
│ │ ├── cognito-stack.ts # OAuth2 authentication
│ │ ├── agentcore-stack.ts # Runtime + Gateway + MCP
│ │ └── monitoring-stack.ts # CloudWatch dashboards & alarms
│ └── test/
│ ├── unit/ # CDK unit tests
│ └── e2e/ # End-to-end tests
├── mcpServer/ # MCP Tools (Docker container)
│ ├── Dockerfile
│ ├── requirements.txt
│ └── src/
│ ├── server.py # FastMCP server
│ └── tools/
│ └── weather_tools.py
├── agent/ # Agent Runtime (Docker container)
│ ├── Dockerfile
│ ├── pyproject.toml
│ └── src/
│ ├── server.py # BedrockAgentCoreApp entrypoint
│ ├── agent.py # Strands weather agent
│ └── gateway_client.py # OAuth2 MCP Gateway client
├── scripts/
│ └── ask-weather.py # Demo CLI script
├── LICENSE
└── README.md定制
要将此演示改编为您自己的代理:
- 更换MCP工具:编辑
mcpServer/src/tools/使用您自己的工具实现 - 更新代理逻辑:修改
agent/src/agent.py使用您的系统提示和代理配置 - 添加资源:如果您的代理需要S3、数据库等,请按照现有模式添加新的堆栈
清理
cd infra
npx cdk destroy --all -c env=dev许可证
麻省理工学院
