命令执行服务器

一个基于Model Context Protocol的安全命令执行服务器,用于执行预先批准的命令。

  • by Sunwood-ai-labs
  • JavaScript
  • 本地部署
  • 22 星标
  • 更新于
在 GitHub 查看 Sunwood-ai-labs/command-executor-mcp-server

使用说明

命令执行器MCP服务器

用于安全执行预先批准的命令的模型上下文协议服务器。

🎥 演示

https://github.com/user-attachments/assets/ed763a12-b685-4e0b-b9a5-bc948a590f51

✨ 特性

  • 使用预先批准的命令列表确保命令执行安全
  • 可通过环境变量配置允许的命令
  • 使用TypeScript和MCP SDK构建
  • 通过stdio进行通信,实现无缝集成
  • 错误处理和安全验证
  • 实时命令输出流

🚀 安装

安装依赖项:

npm install

构建服务器:

npm run build

对于自动重建的开发:

npm run watch

⚙️ 配置

🔒 允许的命令

默认情况下,允许使用以下命令:

  • 版本控制系统
  • ls
  • 创建目录
  • 光盘
  • npm
  • 新巴拿马型船舶
  • python

您可以通过设置 ALLOWED_COMMANDS 环境变量:

export ALLOWED_COMMANDS=git,ls,mkdir,python

🔌 Claude桌面集成

要与Claude Desktop一起使用,请添加服务器配置:

在MacOS上:

~/Library/Application Support/Claude/claude_desktop_config.json

在Windows上:

%APPDATA%/Claude/claude_desktop_config.json

配置示例:

{
  "mcpServers": {
    "command-executor": {
      "command": "/path/to/command-executor/build/index.js"
    }
  }
}

🛡️ 安全考虑

命令执行器服务器实现了多种安全措施:

  1. 预先批准的命令列表

    • 只能执行明确允许的命令
    • 默认列表具有限制性,并且以安全性为重点
    • 命令通过前缀进行验证,以防止注入
  2. 命令验证

    • 命令前缀验证可防止命令注入
    • 无需执行shell以提高安全性
    • 环境变量已得到适当的净化
  3. 错误处理

    • 对未经授权的命令进行全面的错误处理
    • 清除调试错误消息
    • 失败的命令不会使服务器崩溃
  4. 环境隔离

    • 服务器在自己的环境中运行
    • 环境变量可以控制
    • 系统访问受限

💻 发展

📁 项目结构

command-executor/
├─ src/
│  └─ index.ts      # Main server implementation
├─ build/
│  └─ index.js      # Compiled JavaScript
├─ assets/
│  └─ header.svg    # Project header image
└─ package.json     # Project configuration

🐛 调试

由于MCP服务器通过stdio进行通信,调试可能具有挑战性。我们建议使用 MCP检查员:

npm run inspector

检查器将提供一个URL,用于访问浏览器中的调试工具。

🛠️ 工具API

服务器提供了一个工具:

execute_命令

执行预先批准的命令。

参数:

  • command (string,必填):要执行的命令

请求示例:

{
  "name": "execute_command",
  "arguments": {
    "command": "git status"
  }
}

示例响应:

{
  "content": [
    {
      "type": "text",
      "text": "On branch main\nNothing to commit, working tree clean"
    }
  ]
}

错误响应:

{
  "content": [
    {
      "type": "text",
      "text": "Command execution failed: Command not allowed"
    }
  ],
  "isError": true
}

❌ 错误处理

服务器为各种场景提供详细的错误消息:

  1. 未经授权的命令

    {
      "code": "InvalidParams",
      "message": "Command not allowed: [command]. Allowed commands: git, ls, mkdir, cd, npm, npx, python"
    }
    
  2. 执行失败

    {
      "content": [
        {
          "type": "text",
          "text": "Command execution failed: [error message]"
        }
      ],
      "isError": true
    }
    

🤝 贡献

  1. 分叉存储库
  2. 创建功能分支
  3. 提交您的更改
  4. 推到分支
  5. 创建新的Pull请求

📄 许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。