Command Executor MCP Server

local-only server

The server can only run on the client’s local machine because it depends on local resources.

命令执行器 MCP 服务器

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

🎥 演示

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

✨ 特点

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

🚀 安装

安装依赖项:

npm install

构建服务器:

npm run build

对于使用自动重建的开发:

npm run watch

⚙️ 配置

🔒 允许的命令

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

  • git
  • ls
  • mkdir
  • 光盘
  • npm
  • NPX
  • 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 Inspector

npm run inspector

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

🛠️ 工具 API

服务器提供一个单一工具:

执行命令

执行预先批准的命令。

参数:

  • command (字符串,必需):要执行的命令

示例请求:

{ "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 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。

You must be authenticated.

A
security – no known vulnerabilities
F
license - not found
A
quality - confirmed to work

模型上下文协议服务器允许安全执行预先批准的命令,使人工智能助手能够安全地与用户的系统交互。

  1. 🎥 Demo
    1. ✨ Features
      1. 🚀 Installation
        1. ⚙️ Configuration
          1. 🔒 Allowed Commands
          2. 🔌 Claude Desktop Integration
        2. 🛡️ Security Considerations
          1. 💻 Development
            1. 📁 Project Structure
            2. 🐛 Debugging
          2. 🛠️ Tool API
            1. execute_command
          3. ❌ Error Handling
            1. 🤝 Contributing
              1. 📄 License
                ID: db23ll92j5