带有MCP服务器的AWS基岩代理
一个容器化的代理系统,使用AWS Bedrock与自定义MCP(模型上下文协议)服务器进行LLM推理,可部署在EKS上或通过Podman本地部署。
建筑
┌─────────────────┐
│ User/Client │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Agent Service │ ← Orchestrates agent loop, calls Bedrock API
└────────┬────────┘
│
├─────────────┬──────────────┐
▼ ▼ ▼
┌─────────────┐ ┌──────────┐ ┌──────────┐
│ AWS Bedrock │ │ MCP │ │ MCP │
│ (Claude) │ │FileSystem│ │Calculator│
└─────────────┘ └──────────┘ └──────────┘组件
代理服务(Python+FastAPI)
- 编排代理循环
- 调用AWS Bedrock Converse API
- 通过MCP服务器管理工具执行
- 为代理交互公开REST API
MCP服务器(Node.js+Express)
文件系统服务器
文件操作工具:
read_file-读取文件内容write_file-写入文件list_directory-列出目录内容create_directory-创建目录
计算器服务器
数学运算工具:
add,subtract,multiply,dividepower-指数化sqrt-平方根
先决条件
地方发展(波德曼)
- 播客和播客撰写(或使用
podman compose)-参见 PODMAN_SETUP.md - 具有Bedrock访问权限的AWS凭据
- AWS CLI已配置
注:
- 从Docker迁移?看
- 使用AWS SSO?看 AWS_SSO_SETUP.md 或 SSO_QUICK_REFERENCE.md
- 获取模型调用错误?看 卧室_MODEL_PDATE.md
用于EKS部署
- 带有EKS集群的AWS帐户
- kubectl已配置
- 服务帐户的IAM角色(IRSA)设置
- 容器映像的ECR存储库
- Podman用于构建和推送图像
- (可选)共享存储的EFS CSI驱动程序
使用Podman进行本地设置
1.配置AWS凭据
您有两种AWS身份验证选项:
选项A:AWS SSO(建议用于开发)
如果您使用AWS SSO进行身份验证(在组织中最常见):
快速启动(一个命令):
# Login to SSO first
aws sso login --profile your-profile
# Export credentials and start services
make up-sso PROFILE=your-profile这有什么作用:
- 将您登录到AWS SSO(如果需要)
- 提取临时凭据
- 创建
.env文件自动 - 启动所有容器
当凭据过期时 (通常为1-12小时),只需重新运行:
make up-sso PROFILE=your-profile看 AWS_SSO_SETUP.md 了解详细的SSO设置或 SSO_QUICK_REFERENCE.md 快速参考卡。
选项B:静态IAM凭据
如果您有永久IAM用户凭据:
创建一个 .env 文件:
cat > .env <<EOF
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
# AWS_SESSION_TOKEN=your_session_token # Only for temporary credentials
BEDROCK_MODEL_ID=us.anthropic.claude-3-5-sonnet-20241022-v2:0
MCP_FILESYSTEM_URL=http://mcp-filesystem:3001
MCP_CALCULATOR_URL=http://mcp-calculator:3002
EOF注: 静态凭据更容易上手,但在日常开发中不如SSO安全。
2.构建和运行
如果您使用了AWS SSO(选项A): 你已经完成了! make up-sso 开始了一切。跳到步骤3。
如果您使用静态凭据(选项B):
# Using Makefile (recommended)
make build
make up
# Or using podman-compose directly
podman-compose -f podman-compose.yml build
podman-compose -f podman-compose.yml up -d
# Or using podman compose (newer syntax)
podman compose -f podman-compose.yml up -d
# Alternative: Use native Podman pods
make pod-create
make pod-start服务将在以下地点提供:
- 代理服务:http://localhost:8000
- 文件系统MCP:http://localhost:3001
- 计算器MCP:http://localhost:3002
3.测试代理
# List available tools
curl http://localhost:8000/tools
# Run an agent query
curl -X POST http://localhost:8000/agent/run \
-H "Content-Type: application/json" \
-d '{
"message": "Calculate 25 * 4 and then add 10 to the result",
"max_turns": 10
}'4.请求示例
数学计算:
curl -X POST http://localhost:8000/agent/run \
-H "Content-Type: application/json" \
-d '{
"message": "What is the square root of 144?"
}'文件操作:
curl -X POST http://localhost:8000/agent/run \
-H "Content-Type: application/json" \
-d '{
"message": "Create a file called test.txt with the content Hello World"
}'联合作业:
curl -X POST http://localhost:8000/agent/run \
-H "Content-Type: application/json" \
-d '{
"message": "Calculate 10 + 5, then write the result to a file called result.txt"
}'5.查看日志
# All services
make logs
# Or with podman-compose
podman-compose -f podman-compose.yml logs -f
# Specific service
podman-compose -f podman-compose.yml logs -f agent-service
# With podman pod
podman logs agent-service
podman pod logs bedrock-agent-pod6.停止服务
# Using compose
make down
# Or with podman-compose
podman-compose -f podman-compose.yml down
# With podman pod
make pod-stop
make pod-remove7.日常开发工作流程
使用AWS SSO(推荐)
上午/会议开始:
# One command to start everything
make up-sso PROFILE=your-profile开发期间:
# View logs
make logs
# Test changes
./test-agent.sh
# Check status
podman ps凭据过期时: 您将看到“ExpiredToken”或“无法找到凭据”错误。重新运行:
make up-sso PROFILE=your-profile一天结束:
make down使用静态凭据
启动一次:
# First time only - create .env file
cp .env.example .env
# Edit .env with your credentials
# Start services
make build
make up稍后重新启动:
make up # Credentials persist in .env停止:
make downEKS部署
1.为服务帐户设置IAM角色(IRSA)
为基岩访问创建IAM策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": "arn:aws:bedrock:*::foundation-model/anthropic.claude-*"
}
]
}为您的EKS集群创建具有信任策略的IAM角色:
# Create the role (replace with your account ID and cluster details)
eksctl create iamserviceaccount \
--name bedrock-agent-sa \
--namespace bedrock-agent \
--cluster your-cluster-name \
--attach-policy-arn arn:aws:iam::YOUR_ACCOUNT_ID:policy/BedrockInvokePolicy \
--approve2.构建和推送容器映像
# Configure ECR login with Podman
aws ecr get-login-password --region us-east-1 | \
podman login --username AWS --password-stdin YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com
# Create ECR repositories
aws ecr create-repository --repository-name agent-service
aws ecr create-repository --repository-name mcp-filesystem
aws ecr create-repository --repository-name mcp-calculator
# Build and push agent service
cd agent-service
podman build -t YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/agent-service:latest .
podman push YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/agent-service:latest
# Build and push filesystem server
cd ../mcp-servers/filesystem
podman build -t YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/mcp-filesystem:latest .
podman push YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/mcp-filesystem:latest
# Build and push calculator server
cd ../calculator
podman build -t YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/mcp-calculator:latest .
podman push YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/mcp-calculator:latest
# Or use the Makefile
make push-images ECR_REGISTRY=YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com3.更新Kubernetes清单
编辑 k8s/serviceaccount.yaml:
eks.amazonaws.com/role-arn: arn:aws:iam::YOUR_ACCOUNT_ID:role/bedrock-agent-role编辑部署文件以使用ECR映像:
image: YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/agent-service:latest4.为共享存储设置EFS(可选)
如果您需要共享文件系统访问:
# Install EFS CSI driver
kubectl apply -k "github.com/kubernetes-sigs/aws-efs-csi-driver/deploy/kubernetes/overlays/stable/?ref=release-1.7"
# Create EFS filesystem and storage class
# See: https://docs.aws.amazon.com/eks/latest/userguide/efs-csi.html5.部署到EKS
# Apply all manifests
kubectl apply -k k8s/
# Or apply individually
kubectl apply -f k8s/namespace.yaml
kubectl apply -f k8s/serviceaccount.yaml
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/mcp-filesystem-deployment.yaml
kubectl apply -f k8s/mcp-calculator-deployment.yaml
kubectl apply -f k8s/agent-deployment.yaml
kubectl apply -f k8s/hpa.yaml6.验证部署
# Check all resources
kubectl get all -n bedrock-agent
# Check pod logs
kubectl logs -n bedrock-agent -l app=agent-service
# Get service endpoint
kubectl get svc -n bedrock-agent agent-service7.访问代理服务
# Get the LoadBalancer URL
AGENT_URL=$(kubectl get svc -n bedrock-agent agent-service -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
# Test the agent
curl -X POST http://$AGENT_URL/agent/run \
-H "Content-Type: application/json" \
-d '{
"message": "What is 100 divided by 4?"
}'项目结构
.
├── agent-service/ # Python agent service
│ ├── app.py # Main FastAPI application
│ ├── requirements.txt # Python dependencies
│ └── Dockerfile
├── mcp-servers/
│ ├── filesystem/ # Filesystem MCP server
│ │ ├── src/
│ │ │ └── index.ts
│ │ ├── package.json
│ │ ├── tsconfig.json
│ │ └── Dockerfile
│ └── calculator/ # Calculator MCP server
│ ├── src/
│ │ └── index.ts
│ ├── package.json
│ ├── tsconfig.json
│ └── Dockerfile
├── k8s/ # Kubernetes manifests
│ ├── namespace.yaml
│ ├── serviceaccount.yaml
│ ├── configmap.yaml
│ ├── agent-deployment.yaml
│ ├── mcp-filesystem-deployment.yaml
│ ├── mcp-calculator-deployment.yaml
│ ├── hpa.yaml
│ └── kustomization.yaml
├── podman-compose.yml # Podman Compose configuration
├── README.md # Full documentation
└── PODMAN_SETUP.md # Podman installation and setup guideAPI终点
代理服务
GET /-服务信息GET /health-健康检查GET /tools-列出所有可用工具POST /agent/run-运行带有消息的代理
{
"message": "Your query here",
"max_turns": 10
}MCP服务器
GET /-服务器信息GET /tools-列出可用工具POST /execute-执行工具
{
"tool": "tool_name",
"arguments": { "arg1": "value1" }
}添加新的MCP服务器
要添加新的MCP服务器,请执行以下操作:
- 在下创建新目录
mcp-servers/ - 使用以下工具实现服务器:
- GET /tools 端点返回工具定义 - POST /execute 工具执行端点
- 添加Dockerfile
- 更新
docker-compose.yml包括新服务 - 添加Kubernetes部署清单
- 更新代理服务环境以包含新的服务器URL
监控与调试
当地(波德曼)
# View real-time logs
make logs
# Or
podman-compose -f podman-compose.yml logs -f
# Check container status
podman ps
# Or with compose
podman-compose -f podman-compose.yml ps
# Check pod status (if using pods)
podman pod ps
podman pod inspect bedrock-agent-pod
# Restart a service
podman restart agent-service
# Or with compose
podman-compose -f podman-compose.yml restart agent-serviceEKS
# View logs
kubectl logs -n bedrock-agent -l app=agent-service -f
# Check pod status
kubectl get pods -n bedrock-agent
# Describe pod for troubleshooting
kubectl describe pod -n bedrock-agent
# Check HPA status
kubectl get hpa -n bedrock-agent
# Port forward for local testing
kubectl port-forward -n bedrock-agent svc/agent-service 8000:80成本优化
- 使用Bedrock的按需定价(无前期承诺)
- 使用HPA根据负载扩展MCP服务器
- 将点实例用于非生产EKS节点组
- 通过CloudWatch监控Bedrock API的使用情况
安全最佳实践
- 使用AWS SSO进行开发 -自动过期的临时凭据比静态密钥更安全
- 看 AWS_SSO_SETUP.md 用于设置 - 跑 make up-sso PROFILE=your-profile 用于自动凭证管理
- 从不提交AWS凭据 -
.env文件在.gitignore-永远不要覆盖此
- 将IRSA(服务帐户的IAM角色)用于EKS部署 - 使用SSO进行本地开发 - 仅将静态IAM用户凭据作为最后手段
- 限制基岩IAM权限 -只允许特定的模型调用
- 限制为 bedrock:InvokeModel 关于特定型号的ARN - 不使用 bedrock:* 权限
- 使用专用子网 -在生产专用子网中部署MCP服务器
- 启用pod安全标准 -使用Kubernetes安全上下文和pod安全准入
- 扫描集装箱图像 -使用ECR图像扫描或Trivy等工具
- 限制文件系统访问 -在容器中仅装载必要的目录
- 定期轮换凭据 -SSO凭据自动过期;旋转静态凭据(如果使用)
故障排除
特工无法连接到Bedrock
错误: “无法找到凭据”
解决:
- 如果使用AWS SSO:
# Check if logged in
aws sts get-caller-identity --profile your-profile
# If not logged in, login and re-run
aws sso login --profile your-profile
make up-sso PROFILE=your-profile- 如果使用静态凭据:
- 检查 .env 文件存在并具有AWS凭据 - 验证凭据是否为空 - 运行: ./troubleshoot-credentials.sh
错误: “ExpiredToken:请求中包含的安全令牌已过期”
解决方案: SSO凭据已过期,请重新运行:
make up-sso PROFILE=your-profile错误: “不支持按需吞吐量调用模型ID…”
解决方案: 型号ID格式已更改。运行:
./fix-model-id.sh
make down && make up看 卧室_MODEL_PDATE.md 了解详情。
其他检查:
- 验证IAM权限是否包括
bedrock:InvokeModel - 确保模型ID使用推理简档格式(例如。,
us.anthropic.claude-3-5-sonnet-20241022-v2:0) - 检查您所在地区是否有基岩
AWS SSO问题
SSO会话已过期:
# Re-login
aws sso login --profile your-profile
# Restart services with fresh credentials
make up-sso PROFILE=your-profile找不到SSO配置文件:
# List available profiles
aws configure list-profiles
# Configure SSO if needed
aws configure sso --profile your-profile容器中的凭据为空:
# Check what container sees
podman exec agent-service printenv | grep AWS
# If empty, re-export credentials
make up-sso PROFILE=your-profile看 AWS_SSO_SETUP.md 用于全面的SSO故障排除。
MCP服务器没有响应
- 检查MCP服务器日志是否有错误
- 验证服务之间的网络连接
- 确保环境变量设置正确
工具未出现
- 检查MCP服务器
/tools终结点返回有效的架构 - 验证代理服务是否可以访问MCP服务器URL
- 查看代理服务日志以查找工具获取错误
许可证
麻省理工学院
贡献
欢迎投稿!请打开问题或提交拉取请求。
