Shell命令执行服务

一个提供安全Shell命令执行功能的MCP服务器,适用于AI代理和工具如VS Code Copilot。

  • by kosiew
  • Python
  • 本地部署
  • 更新于

使用说明

Shell执行器MCP服务器

一个模型上下文协议(MCP)服务器,为AI代理和VS Code Copilot等工具提供安全的shell命令执行功能。

特性

  • 安全命令执行:基于白名单的命令筛选
  • 综合工具集:支持文件操作、文本处理、开发工具和系统实用程序
  • MCP兼容:完全支持模型上下文协议
  • 易于集成:通过stdio的简单JSON-RPC接口

安装

选项1:直接使用

git clone https://github.com/yourusername/shell-executor-mcp.git
cd shell-executor-mcp
pip install -e .

选项2:从PyPI安装(发布时)

pip install shell-executor-mcp

VS代码集成

1.全局配置

添加到您的VS Code settings.json或MCP配置中:

{
  "mcpServers": {
    "shell-executor": {
      "command": "python",
      "args": ["/path/to/shell-executor-mcp/src/mcp_server.py"],
      "env": {
        "MCP_API_TOKEN": "your-secret-token"
      }
    }
  }
}

2.工作空间配置

创建 .vscode/mcp_servers.json 在您的工作空间中:

{
  "shell-executor": {
    "command": "python",
    "args": ["${workspaceFolder}/path/to/mcp_server.py"],
    "env": {
      "MCP_API_TOKEN": "your-secret-token"
    }
  }
}

3.与VS代码副本一起使用

配置后,Copilot可以通过MCP发现和使用shell执行器:

@shell-executor execute ls -la
@shell-executor execute git status
@shell-executor execute cargo build

手动测试

启动服务器:

python src/mcp_server.py

发送测试请求:

  1. 初始化:
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test", "version": "1.0.0"}}}
  1. 列出工具:
{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}
  1. 执行命令:
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "execute_command", "arguments": {"command": "echo", "args": ["Hello World"]}}}

支持的命令

文件系统

  • ls, cat, head, tail, find, tree, wc, du, df
  • file, stat, pwd

文本处理

  • grep, sed, awk, sort, uniq, cut, tr

发展

  • git, npm, yarn, pip, python, node, cargo, make
  • rustc, go, java, javac

系统信息

  • ps, uptime, whoami, id, uname, which

网络

  • curl, wget, ping, dig, nslookup

档案

  • tar, zip, unzip, gzip, gunzip

公用事业

  • echo, date, cal, bc, expr, basename, dirname
  • 哈希工具: md5, sha256sum, shasum

安全

  • 命令通过白名单过滤
  • 30秒执行超时
  • 不允许进行破坏性操作(rm、mv、cp)
  • 不允许特权升级(sudo、su)
  • 环境变量可以控制

配置

设置环境变量:

  • MCP_API_TOKEN:身份验证令牌(默认值:“MYSECRET”)

许可证

MIT许可证-有关详细信息,请参阅许可证文件。