返回 MCP 目录
public公开dns本地运行

Secure Shell Executor

一个安全的Shell命令执行服务器,支持远程执行授权命令并返回完整输出

article

README

🚀 MCP Shell Server

MCP Shell Server 是一个安全的服务器,用于执行授权的 shell 命令,并支持通过 stdin 传递输入。它实现了模型上下文协议(MCP),允许远程执行经过验证的 shell 命令,为用户提供了安全、高效的命令执行环境。

✨ 主要特性

  • 安全命令执行:仅允许执行受信任的命令,保障系统安全。
  • 标准输入支持:可以通过 stdin 向命令提供输入,增强命令执行的灵活性。
  • 全面输出:返回 stdout、stderr、退出状态码和执行时间,方便用户了解命令执行情况。
  • shell 操作符验证:在使用 shell 操作符(;,&&,||,|)时仍需进行命令验证,确保命令的合法性。
  • 超时控制:为命令设置最大执行时间,避免命令长时间运行占用资源。

📦 安装指南

安装依赖项

pip install -e ".[test]"

启动服务器

uvicorn main:app --reload

验证安装

运行以下命令检查是否成功安装:

mcp-shell-server --version

💻 使用示例

基础用法

示例 1:执行基本命令

请求示例:

{
  "command": ["echo", "Hello, World!"],
  "stdin": null,
  "directory": "/",
  "timeout": 30
}

响应示例:

{
  "stdout": "Hello, World!\n",
  "stderr": "",
  "status": 0,
  "execution_time": 0.123456
}

示例 2:带有标准输入的命令

请求示例:

{
  "command": ["tr", "a-z", "A-Z"],
  "stdin": "hello world\n",
  "directory": "/tmp",
  "timeout": 60
}

📚 详细文档

客户端配置在 Claude.app 中

发布版本

code ~/Library/Application\ Support/Claude/claude.yaml
backend:
  class: mcp_shell_server.Backend
  config:
    server_uri: http://localhost:5000
    max_timeout: 3600

本地版本

poetry install
poetry run mcp-shell-server

请求格式

请求参数

| 参数名 | 类型 | 是否必须 | 描述 | |--------|------|----------|------| | command | string[] | 是 | 命令及其参数,作为数组元素 | | stdin | string | 否 | 提供给命令的标准输入 | | directory | string | 否 | 执行命令的工作目录 | | timeout | integer | 否 | 最大执行时间(秒) |

响应格式

| 参数名 | 类型 | 描述 | |--------|------|------| | stdout | string | 标准输出 | | stderr | string | 错误输出 | | status | integer | 退出状态码 | | execution_time | float | 执行时间(秒) | | error | string | 错误信息(仅在失败时出现) |

🔧 技术细节

开发说明

设置开发环境

  1. 克隆仓库
git clone https://github.com/yourusername/mcp-shell-server.git
cd mcp-shell-server
  1. 安装依赖项(包括测试要求)
pip install -e ".[test]"

执行测试

pytest

API 参考文档

请求参数

| 参数名 | 类型 | 是否必须 | 描述 | |--------|------|----------|------| | command | string[] | 是 | 命令及其参数,作为数组元素 | | stdin | string | 否 | 提供给命令的标准输入 | | directory | string | 否 | 执行命令的工作目录 | | timeout | integer | 否 | 最大执行时间(秒) |

响应参数

| 参数名 | 类型 | 描述 | |--------|------|------| | stdout | string | 标准输出 | | stderr | string | 错误输出 | | status | integer | 退出状态码 | | execution_time | float | 执行时间(秒) | | error | string | 错误信息(仅在失败时出现) |

系统要求

  • Python 3.11 或更高版本
  • mcp>=1.1.0

📄 许可证

MIT 许可证 - 请参阅 LICENSE 文件以获取详细信息。

help

运行方式说明

cloud

托管运行

托管运行通常表示这个 MCP Server 由服务方环境承载,用户一般按页面提供的连接方式或授权流程接入,不需要在本地长期启动一个 MCP 进程

  1. 打开服务方连接页
  2. 完成授权或复制端点
  3. 在 MCP 客户端中连接
terminal

本地运行 / 其它方式

本地运行通常需要用户在自己的电脑或服务器上安装依赖,把 server_config 复制到 MCP 客户端,并按 env_schema 补齐环境变量、密钥或其它配置

  1. 复制 server_config
  2. 安装所需依赖
  3. 补齐环境变量后重启客户端