基础设施MCP服务器

一个用于安全执行Terraform命令的模型上下文协议(MCP)服务器,提供基础设施即代码管理、安全命令验证和全面日志记录功能。

  • by shoppingjaws
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 shoppingjaws/infrastructure-mcp

使用说明

基础架构MCP服务器

用于安全执行Terraform命令的模型上下文协议(MCP)服务器。

特性

  • 地形:将基础设施的Terraform命令作为代码管理执行
  • 安全:通过同种异体和危险模式检测进行命令验证
  • 日志记录:用于调试和审计跟踪的全面日志记录

安装

使用Bun安装依赖项:

bun install

配置

服务器使用 config/allowed-commands.yaml 定义允许使用哪些命令和标志。编辑此文件以自定义安全策略。

配置示例:

- command: terraform
  description: Terraform CLI for managing infrastructure as code
  requiresConfirmation: true
  allowedSubcommands:
    - init
    - plan
    - apply
  allowedFlags:
    - -var-file
    - -auto-approve

用法

启动服务器

bun start

或者用于自动重新加载的开发:

bun dev

使用Claude Desktop进行配置

添加到您的Claude Desktop配置(~/Library/Application Support/Claude/claude_desktop_config.json 在macOS上):

{
  "mcpServers": {
    "infrastructure": {
      "command": "bun",
      "args": ["run", "/path/to/infrastructure-mcp/src/index.ts"],
      "env": {
        "LOG_LEVEL": "info"
      }
    }
  }
}

可用工具

地形(terraform)

以代码形式管理基础设施:

{
  "subcommand": "plan",
  "workingDir": "/path/to/terraform",
  "varFile": "production.tfvars"
}
{
  "subcommand": "apply",
  "workingDir": "/path/to/terraform",
  "autoApprove": false,
  "target": ["aws_instance.example"]
}

安全

服务器实现了多个安全层:

  1. 命令允许列表:只能执行明确允许的命令
  2. 子命令验证:只允许使用指定的子命令
  3. 标志验证:只能使用允许的标志
  4. 模式检测:阻止危险模式(命令注入等)
  5. 确认要求:某些命令需要用户确认

发展

项目结构

infrastructure-mcp/
├── src/
│   ├── index.ts              # Main entry point
│   ├── server.ts             # MCP server implementation
│   ├── tools/                # Tool implementations
│   │   ├── terraform.ts      # Terraform tool
│   │   └── index.ts
│   ├── utils/                # Utility functions
│   │   ├── security.ts       # Security validation
│   │   ├── command.ts        # Command execution
│   │   └── logger.ts         # Logging
│   └── types/                # Type definitions
│       └── index.ts
├── config/                   # Configuration files
│   └── allowed-commands.yaml # Allowed commands config
└── tests/                    # Test files

运行测试

bun test

建筑

bun run build

环境变量

  • LOG_LEVEL:设置日志记录级别(debug, info, warn, error).违约: info

许可证

麻省理工学院

贡献

欢迎投稿!请在提交PR之前阅读投稿指南。


此项目是使用创建的 包子.