AWS 模型上下文协议 (MCP) 服务器

一种轻量级服务,使 AI 助手能够通过模型上下文协议 (MCP) 执行 AWS CLI 命令。
概述
AWS MCP 服务器在支持 MCP 的 AI 助手(例如 Claude Desktop、Cursor、Windsurf)和 AWS CLI 之间搭建了桥梁。它使这些助手能够:
检索 AWS CLI 文档( aws_cli_help ) - 获取有关 AWS 服务和命令的详细帮助
执行 AWS CLI 命令( aws_cli_pipeline ) - 使用 Unix 管道运行命令并接收针对 AI 消费优化的格式化结果
flowchart LR
AI[AI Assistant] <-->|MCP Protocol| Server[AWS MCP Server]
Server <-->|Subprocess| AWS[AWS CLI]
AWS <-->|API| Cloud[AWS Cloud]
Related MCP server: MCP2Lambda
演示
演示
该视频演示了如何使用 Claude Desktop 和 AWS MCP Server 创建安装了 AWS SSM 代理的新 AWS EC2 实例。
特征
命令文档- AWS CLI 命令的详细帮助信息
命令执行- 执行 AWS CLI 命令并返回人类可读的结果
Unix 管道支持- 使用标准 Unix 管道和实用程序过滤和转换 AWS CLI 输出
AWS 资源上下文- 通过 MCP 资源访问 AWS 配置文件、区域、帐户信息和环境详细信息
提示模板- 遵循最佳实践的常见 AWS 任务的预定义提示模板
Docker 集成- 通过容器化实现简单部署,支持多架构(AMD64/x86_64 和 ARM64)
AWS 身份验证- 利用主机上现有的 AWS 凭证
要求
入门
**注意:**出于安全性和可靠性的考虑,强烈建议在 Docker 容器内运行服务器。请参阅“安全注意事项”部分,了解重要注意事项。
运行服务器选项 1:使用 Docker(推荐)
# Clone repository
git clone https://github.com/alexei-led/aws-mcp-server.git
cd aws-mcp-server
# Build and run Docker container
docker compose -f deploy/docker/docker-compose.yml up -d
Docker 镜像支持 AMD64/x86_64(Intel/AMD)和 ARM64(Apple Silicon M1-M4、AWS Graviton)架构。
注意:GitHub Packages 的官方镜像是多架构的,会自动使用适合您系统的版本。
# Use the latest stable version
docker pull ghcr.io/alexei-led/aws-mcp-server:latest
# Or pin to a specific version (recommended for production)
docker pull ghcr.io/alexei-led/aws-mcp-server:1.0.0
Docker 镜像标签:
运行服务器选项 2:使用 Python
**谨慎使用:**原生运行需要谨慎的环境设置,并且与推荐的 Docker 部署相比,存在更高的安全风险。请确保您了解“安全注意事项”部分中概述的影响。
# Clone repository
git clone https://github.com/alexei-led/aws-mcp-server.git
cd aws-mcp-server
# Set up virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e .
# Run the server
python -m aws_mcp_server
配置
可以使用环境变量配置 AWS MCP 服务器:
环境变量 | 描述 | 默认 |
AWS_MCP_TIMEOUT
| 命令执行超时(秒) | 300 |
AWS_MCP_MAX_OUTPUT
| 最大输出字符大小 | 100000 |
AWS_MCP_TRANSPORT
| 要使用的传输协议(“stdio”或“sse”) | 标准输入输出 |
AWS_PROFILE
| 要使用的 AWS 配置文件 | 默认 |
AWS_REGION
| 要使用的 AWS 区域 | 美国东部-1 |
AWS_MCP_SECURITY_MODE
| 安全模式(“严格”或“宽容”) | 严格的 |
AWS_MCP_SECURITY_CONFIG
| 自定义安全配置文件的路径 | “” |
**重要提示:**请安全地管理提供给服务器的 AWS 凭证,无论是通过挂载的~/.aws文件还是环境变量。确保凭证遵循“安全注意事项”部分中详述的最小特权原则。通过 Docker 运行时,请确保这些变量正确传递到容器环境(例如,使用docker run -e VAR=value ... )。
安全注意事项
在您的 AWS 环境上执行命令时,安全性至关重要。虽然 AWS MCP 服务器提供了功能,但您有责任安全地配置和运行它。请严格遵守以下规定:
1. 推荐部署:Docker 容器
2. AWS 凭证和 IAM 最小权限(关键)
**用户责任:**您向服务器提供 AWS 凭证(通过挂载的~/.aws或环境变量)。
最小权限至关重要:(最小权限),才能执行您打算通过此工具执行的 AWS 操作。
影响限制:。即使命令被操纵,它也只能执行特定 IAM 策略允许的操作。
3. 可信用户模型
4. 了解执行风险(当前实施)
**命令执行:**当前实现使用 shell 功能(在子进程调用中设置shell=True )来执行 AWS 命令并处理 Unix 管道。这种方法虽然方便,但如果输入的命令字符串被操纵(命令注入),则存在固有风险。
通过操作控制缓解:和Docker 部署的背景下,这些风险在操作上得到缓解:
假定可信用户不会对自己的环境故意发出恶意命令。
Docker 包含文件系统副作用。
至关重要的是,IAM 最小特权限制了可以执行的
**凭证泄露风险:**尽管有容器化和身份访问管理 (IAM),复杂的命令注入仍然可能试图读取容器内已挂载的凭证 ( ~/.aws ) 或环境变量,并将其泄露(例如,通过curl )。严格的 IAM 策略仍然是限制潜在凭证泄露风险的最重要防御措施。
5. 网络暴露(SSE 传输)
6. 共同责任摘要
AWS MCP Server 提供了该工具。
您(用户)负责:
在推荐的安全 Docker 环境中运行它。
提供并安全管理最低权限AWS 凭证。
确保只有受信任的用户才能与服务器/客户端交互。
如果适用,保护网络环境。
通过严格遵守 Docker 部署和细致的 IAM 最小权限配置,您可以建立必要的操作控制,以便在当前实施中安全地使用 AWS MCP 服务器。
与 Claude Desktop 集成
配置
要手动将 AWS MCP 服务器与 Claude Desktop 集成:
找到 Claude Desktop 配置文件:
编辑配置文件以包含 AWS MCP 服务器:
{
"mcpServers": {
"aws-mcp-server": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-v",
"/Users/YOUR_USER_NAME/.aws:/home/appuser/.aws:ro",
"ghcr.io/alexei-led/aws-mcp-server:latest"
]
}
}
}
重新启动 Claude Desktop以应用更改
flowchart TD
subgraph "User Device"
config[Edit claude_desktop_config.json]
claude[Claude Desktop]
docker[Docker Container]
aws_creds[AWS Credentials]
end
subgraph "AWS Cloud"
services[AWS Services]
end
subgraph "MCP Server Components"
tools[AWS CLI Tools]
resources[AWS Resources]
templates[Prompt Templates]
end
config -->|Add MCP Server Config| claude
claude -->|Docker Run Command| docker
docker --- tools
docker --- resources
docker --- templates
aws_creds -->|Mount Read-only| docker
resources -.->|Read| aws_creds
tools -->|API Calls| services
交互示例
获取 AWS CLI 文档:
User: What are the available options for the AWS S3 cp command?
Claude: Let me get that information for you.
The AWS S3 cp command has the following options:
NAME
cp - Copies a local file or S3 object to another location locally or in S3.
SYNOPSIS
cp <LocalPath> <S3Uri> or <S3Uri> <LocalPath> or <S3Uri> <S3Uri> [--acl <value>] [--cache-control <value>] ...
执行 AWS CLI 命令:
User: List my S3 buckets
Claude: I'll list your S3 buckets for you.
2023-10-15 14:30:45 my-bucket-1
2023-11-20 09:15:32 my-bucket-2
2024-01-05 11:22:18 my-backup-bucket
使用命令管道:
User: List my S3 buckets that have "backup" in their name and show them in alphabetical order
Claude: Let me find that for you.
2024-01-05 11:22:18 my-backup-bucket
2024-03-01 09:44:12 weekly-backup-bucket
2024-03-15 13:10:57 database-backup-bucket
访问 AWS 资源:
User: What AWS regions are available for me to use?
Claude: Let me check what AWS regions are available for you.
Available regions:
- us-east-1 (US East, N. Virginia) - Currently selected
- us-east-2 (US East, Ohio)
- us-west-1 (US West, N. California)
- us-west-2 (US West, Oregon)
- eu-west-1 (EU West, Ireland)
- eu-central-1 (EU Central, Frankfurt)
- ap-northeast-1 (Asia Pacific, Tokyo)
- ap-southeast-1 (Asia Pacific, Singapore)
- ap-southeast-2 (Asia Pacific, Sydney)
You're currently using the us-east-1 region.
使用提示模板:
User: I need to perform a security audit of my S3 buckets
Claude: I'll help you audit the security of your S3 buckets.
Here are AWS CLI commands to perform a comprehensive security audit:
# List all buckets with public access settings
aws s3api list-buckets --query 'Buckets[*].Name' --output text | xargs -I {} aws s3api get-public-access-block --bucket {}
# Check bucket policies for public access
aws s3api list-buckets --query 'Buckets[*].Name' --output text | xargs -I {} aws s3api get-bucket-policy --bucket {} 2>/dev/null
# Identify buckets without encryption
aws s3api list-buckets --query 'Buckets[*].Name' --output text | xargs -I {} aws s3api get-bucket-encryption --bucket {} 2>/dev/null
# Check for buckets without logging enabled
aws s3api list-buckets --query 'Buckets[*].Name' --output text | xargs -I {} aws s3api get-bucket-logging --bucket {} 2>/dev/null
# Verify bucket versioning status
aws s3api list-buckets --query 'Buckets[*].Name' --output text | xargs -I {} aws s3api get-bucket-versioning --bucket {}
可用的提示模板
AWS MCP 服务器包含以下预定义的提示模板:
核心运营
迅速的 | 描述 | 参数 |
create_resource
| 生成命令以使用最佳实践创建 AWS 资源 | resource_type
、 resource_name
|
resource_inventory
| 创建全面的资源清单 | service
、 region
(可选) |
troubleshoot_service
| 生成命令来排除服务问题 | service
、 resource_id
|
resource_cleanup
| 识别并安全清理资源 | service
、 criteria
(可选) |
安全与合规
迅速的 | 描述 | 参数 |
security_audit
| 审计特定 AWS 服务的安全设置 | service
|
security_posture_assessment
| 对您的 AWS 环境进行全面的安全评估 | 没有任何 |
iam_policy_generator
| 创建最小权限 IAM 策略 | service
、 actions
、 resource_pattern
(可选) |
compliance_check
| 检查是否符合标准 | compliance_standard
, service
(可选) |
成本与性能
迅速的 | 描述 | 参数 |
cost_optimization
| 寻找服务的成本优化机会 | service
|
performance_tuning
| 优化和调整 AWS 资源的性能 | service
、 resource_id
|
基础设施和架构
迅速的 | 描述 | 参数 |
serverless_deployment
| 使用最佳实践部署无服务器应用程序 | application_name
, runtime
(可选) |
container_orchestration
| 设置容器环境(ECS/EKS) | cluster_name
、 service_type
(可选) |
vpc_network_design
| 设计和实施安全的 VPC 网络 | vpc_name
、 cidr_block
(可选) |
infrastructure_automation
| 自动化基础设施管理 | resource_type
、 automation_scope
(可选) |
multi_account_governance
| 实施安全的多账户策略 | account_type
(可选) |
可靠性与监控
迅速的 | 描述 | 参数 |
service_monitoring
| 建立全面监控 | service
, metric_type
(可选) |
disaster_recovery
| 实施企业级灾难恢复解决方案 | service
, recovery_point_objective
(可选) |
安全
AWS MCP 服务器实施了全面的多层方法来实现命令验证和安全:
命令验证系统
服务器通过三层系统验证所有 AWS CLI 命令:
基本命令结构:
验证命令以“aws”前缀开头并包含有效服务
确保命令语法正确
以安全为中心的命令过滤:
管道命令安全性:
验证管道中使用的 Unix 命令
将命令限制在安全允许列表中
防止文件系统操纵和任意命令执行
默认安全配置
默认安全配置重点防止以下攻击媒介:
1.身份和访问管理(IAM)风险
阻止的命令 | 安全风险 |
aws iam create-user
| 创建具有持久访问权限的潜在后门帐户 |
aws iam create-access-key
| 创建可能被盗或滥用的长期凭证 |
aws iam attach-*-policy
| 通过策略附件进行潜在的权限提升 |
aws iam put-user-policy
| 内联策略可以授予过多的权限 |
aws iam create-policy
| 创建具有潜在危险权限的新策略 |
aws iam create-login-profile
| 为现有用户创建控制台密码 |
aws iam deactivate-mfa-device
| 禁用多因素身份验证,削弱安全性 |
aws iam update-assume-role-policy
| 修改信任关系,实现权限提升 |
2. 审计和日志篡改
阻止的命令 | 安全风险 |
aws cloudtrail delete-trail
| 删除 AWS 活动的审计跟踪 |
aws cloudtrail stop-logging
| 停止收集活动日志,造成盲点 |
aws cloudtrail update-trail
| 可以重定向或修改日志配置 |
aws config delete-configuration-recorder
| 禁用 AWS Config 对资源更改的记录 |
aws guardduty delete-detector
| 禁用威胁检测功能 |
3.敏感数据访问和保护
阻止的命令 | 安全风险 |
aws secretsmanager put-secret-value
| 修改敏感凭证 |
aws secretsmanager delete-secret
| 删除敏感凭证 |
aws kms schedule-key-deletion
| 安排删除加密密钥,存在数据丢失的风险 |
aws kms disable-key
| 禁用加密密钥,可能泄露数据 |
aws s3api put-bucket-policy
| 可以创建公共 S3 存储桶,公开数据 |
aws s3api delete-bucket-policy
| 从存储桶中删除保护策略 |
4.网络安全风险
阻止的命令 | 安全风险 |
aws ec2 authorize-security-group-ingress
| 打开入站网络访问,潜在暴露 |
aws ec2 authorize-security-group-egress
| 打开出站网络访问,可能造成数据泄露 |
aws ec2 modify-instance-attribute
| 可以改变实例的安全属性 |
许多符合这些模式的只读操作都通过安全模式明确允许:
配置选项
自定义安全规则示例
您可以通过定义 YAML 配置文件来创建自定义安全规则:
# Example custom security configuration
# Save to a file and set AWS_MCP_SECURITY_CONFIG environment variable
# Dangerous commands to block
dangerous_commands:
iam:
# Only block specific IAM operations for your environment
- "aws iam create-user"
- "aws iam attach-user-policy"
# Custom service restrictions for your organization
lambda:
- "aws lambda delete-function"
- "aws lambda remove-permission"
# Prevent accidental DynamoDB table deletion
dynamodb:
- "aws dynamodb delete-table"
# Safe patterns to explicitly allow
safe_patterns:
# Global safe patterns
general:
- "--help"
- "--dry-run"
# Allow read operations on IAM
iam:
- "aws iam get-"
- "aws iam list-"
# Allow specific Lambda operations
lambda:
- "aws lambda list-functions"
- "aws lambda get-function"
# Complex regex rules for security validation
regex_rules:
general:
# Prevent use of root credentials
- pattern: "aws .* --profile\\s+root"
description: "Prevent use of root profile"
error_message: "Using the root profile is not allowed for security reasons"
iam:
# Block creation of admin users
- pattern: "aws iam create-user.*--user-name\\s+.*admin.*"
description: "Prevent creation of admin users"
error_message: "Creating users with 'admin' in the name is restricted"
# Prevent wildcards in IAM policies
- pattern: "aws iam create-policy.*\"Effect\":\\s*\"Allow\".*\"Action\":\\s*\"\\*\".*\"Resource\":\\s*\"\\*\""
description: "Prevent wildcards in policies"
error_message: "Creating policies with '*' wildcards for both Action and Resource is not allowed"
s3:
# Prevent public bucket policies
- pattern: "aws s3api put-bucket-policy.*\"Effect\":\\s*\"Allow\".*\"Principal\":\\s*\"\\*\""
description: "Prevent public bucket policies"
error_message: "Creating bucket policies with public access is restricted"
安全示例
该系统遵循 IAM 最佳实践,重点防止权限升级:
# This command would be blocked (creates user)
aws iam create-user --user-name new-user
> Error: This command (aws iam create-user) is restricted for security reasons.
# This command would be blocked (attaches admin policy)
aws iam attach-user-policy --user-name any-user --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
> Error: Attaching Administrator policies is restricted for security reasons.
# This command would be blocked (opens SSH port globally)
aws ec2 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --port 22 --cidr 0.0.0.0/0
> Error: Opening non-web ports to the entire internet (0.0.0.0/0) is restricted.
# These commands are allowed (read-only operations)
aws iam list-users
aws s3 ls
aws ec2 describe-instances
安全最佳实践
在生产中始终使用默认的strict安全模式
遵循安全注意事项中的部署建议
使用最低权限 AWS 凭证运行
对于自定义配置,请关注您的安全要求
发展
设置开发环境
# Install only runtime dependencies using pip
pip install -e .
# Install all development dependencies using pip
pip install -e ".[dev]"
# Or use uv for faster dependency management
make uv-install # Install runtime dependencies
make uv-dev-install # Install development dependencies
Makefile 命令
该项目包括一个 Makefile,其中包含用于常见任务的各种目标:
# Test commands
make test # Run tests excluding integration tests
make test-unit # Run unit tests only (all tests except integration tests)
make test-integration # Run integration tests only (requires AWS credentials)
make test-all # Run all tests including integration tests
# Test coverage commands
make test-coverage # Run tests with coverage report (excluding integration tests)
make test-coverage-all # Run all tests with coverage report (including integration tests)
# Linting and formatting
make lint # Run linters (ruff check and format --check)
make lint-fix # Run linters and auto-fix issues where possible
make format # Format code with ruff
要获得可用命令的完整列表,请运行make help 。
代码覆盖率
该项目包含Codecov的配置,用于跟踪代码覆盖率指标。配置位于codecov.yml文件中,该文件包含以下内容:
将目标覆盖率阈值设定为 80%
从覆盖率报告中排除测试文件、设置文件和文档
配置 PR 注释和状态检查
覆盖率报告在 CI/CD 运行期间自动生成并上传到 Codecov。
集成测试
集成测试可验证 AWS MCP 服务器能否与实际 AWS 资源正常运行。运行测试的步骤如下:
设置AWS资源:
运行集成测试:
# Run all tests including integration tests
make test-all
# Run only integration tests
make test-integration
或者您可以直接运行 pytest 命令:
# Run all tests including integration tests
pytest --run-integration
# Run only integration tests
pytest --run-integration -m integration
故障排除
身份验证问题:确保您的 AWS 凭证已正确配置
连接错误:验证服务器是否正在运行以及 AI 助手连接设置是否正确
权限错误:检查您的 AWS 凭证是否具有必要的权限
超时错误:对于长时间运行的命令,增加AWS_MCP_TIMEOUT环境变量
为什么使用 Docker 进行部署
推荐使用 Docker 部署 AWS MCP Server,它具有显著的安全性和可靠性优势,构成了该工具安全使用模式的核心:
安全优势
隔离(主要缓解措施): Docker 容器提供必要的文件系统和进程隔离。AWS CLI 命令和管道 Unix 实用程序在封闭的环境中运行。影响文件系统的意外或误用命令仅限于容器内,从而保护您的主机。
**受控凭证访问:**安装凭证时,使用:ro (只读)标志可限制容器修改 AWS 配置文件的能力。
**无需本地安装:**避免直接在主机系统上安装 AWS CLI 及其依赖项。
**清洁环境:**每个容器运行都以已知的清洁状态开始。
可靠性优势
其他福利
多架构支持:可在 Intel/AMD(x86_64)和 ARM(Apple Silicon、AWS Graviton)处理器上运行
简单更新:使用单个拉取命令即可更新到新版本
无 Python 环境冲突:避免与系统上其他 Python 应用程序发生潜在冲突
版本固定:轻松固定到特定版本以确保生产环境的稳定性
版本控制
该项目使用setuptools_scm根据Git标签自动确定版本:
该版本自动包含在:
创建发布
要创建新的发布版本:
# Create and push a new tag
git tag -a 1.2.3 -m "Release version 1.2.3"
git push origin 1.2.3
CI/CD 管道将自动构建并发布具有适当版本标签的 Docker 镜像。
有关版本管理系统的更多详细信息,请参阅VERSION.md 。
执照
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。