M

MCP命令执行服务器

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

os-automationdeveloper-tools

918 查看 · 2026-07-07 更新

简介

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

简介

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

command-executor MCP 服务器

Command Executor MCP Server

EN doc JA doc

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

🎥 演示

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

✨ 特性

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

🚀 安装

安装依赖:

npm install

构建服务器:

npm run build

开发模式下自动重建:

npm run watch

⚙️ 配置

🔒 允许的命令

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

  • git
  • ls
  • mkdir
  • cd
  • npm
  • npx
  • python

你可以通过设置 ALLOWED_COMMANDS 环境变量来自定义允许的命令:

export ALLOWED_COMMANDS=git,ls,mkdir,python

🔌 Claude Desktop 集成

要与 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" } } }

🛡️ 安全注意事项

command-executor 服务器实现了多种安全措施:

  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 服务器通过标准输入输出进行通信,调试可能会比较困难。我们建议使用 MCP Inspector

npm run inspector

Inspector 将提供一个 URL,以便您在浏览器中访问调试工具。

🛠️ 工具 API

服务器提供了一个工具:

execute_command

执行预先批准的命令。

参数:

  • 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]。允许的命令: git, ls, mkdir, cd, npm, npx, python" }
  2. 执行失败

    { "content": [ { "type": "text", "text": "命令执行失败: [错误信息]" } ], "isError": true }

🤝 贡献

  1. Fork 仓库
  2. 创建你的功能分支
  3. 提交你的更改
  4. 推送到该分支
  5. 创建一个新的 Pull Request

📄 许可证

本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。

工具列表

  • execute_command: 事前に許可されたコマンドを実行します

服务配置

[{'mcpServers': {'command-executor': {'command': '/path/to/command-executor/build/index.js'}}}]

来源