R

Rinard安全终端

@RinardNick/mcp-terminal
0 Stars 422 次浏览 RinardNick 更新于 2026-08-23

一个安全的终端执行服务器,通过模型上下文协议(MCP)启用具有安全功能和资源限制的受控命令执行。

MCP 服务配置

复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用

{
  "mcpServers": {
    "terminal": {
      "args": [
        "pip",
        "run",
        "mcp-terminal",
        "--allowed-commands",
        "python,pip,git,ls,cd",
        "--timeout-ms",
        "30000",
        "--max-output-size",
        "1048576"
      ],
      "command": "uv"
    }
  }
}

该服务需要配置环境变量:allowed-commands、max-output-size、timeout-ms

可用工具 (1 个)

该服务在 MCP 协议中暴露的工具,AI 可按需调用

run_command 4 个参数 需填 1 项

Run a terminal command with security controls.

必填参数:command

服务介绍

MCP 终端服务器

一个实现模型上下文协议 (MCP) 的安全终端执行服务器。该服务器提供带有安全特性和资源限制的受控命令执行能力。

功能

  • 命令执行:执行 shell 命令并捕获输出和处理错误
  • 安全控制:限制允许的命令,防止命令注入
  • 资源控制
    • 命令超时
    • 最大输出大小限制
  • MCP 协议支持
    • 标准 MCP 消息格式
    • 能力广告
    • 流式输出支持

开发

本地设置

# Clone the repository
git clone https://github.com/RinardNick/mcp-terminal.git
cd mcp-terminal

# Create and activate virtual environment using uv
uv venv
source .venv/bin/activate  # or .venv\Scripts\activate on Windows

# Install development dependencies
uv pip install -e ".[dev]"

发布到 PyPI

# Build the package
uv pip install build
python -m build

# Upload to PyPI
uv pip install twine
python -m twine upload dist/*

使用 MCP Inspector 进行测试

可以使用 MCP Inspector 工具来测试服务器实现:

# Install inspector
npm install -g @modelcontextprotocol/inspector

# Test server
npx @modelcontextprotocol/inspector python3 src/mcp_terminal/server.py --allowed-commands "python,pip,git,ls,cd"

运行测试

# Run all tests
pytest tests/

# Run specific test file
pytest tests/test_terminal.py

# Run with coverage
pytest --cov=mcp_terminal tests/

与 Claude Desktop 一起使用

一旦将包发布到 PyPI:

  1. 安装 UV(如果尚未安装):

    pip install uv
    
  2. 使用 UV 安装包

    uv pip install mcp-terminal
    
  3. 配置 Claude Desktop
    编辑您的 Claude Desktop 配置文件(通常在 macOS 上位于 ~/Library/Application Support/Claude/claude_desktop_config.json):

    {
      "mcpServers": {
        "terminal": {
          "command": "uv",
          "args": [
            "pip",
            "run",
            "mcp-terminal",
            "--allowed-commands",
            "python,pip,git,ls,cd",
            "--timeout-ms",
            "30000",
            "--max-output-size",
            "1048576"
          ]
        }
      }
    }
    

协议实现

服务器实现了以下功能的模型上下文协议 (MCP):

能力广告

{
  "protocol": "1.0.0",
  "name": "terminal",
  "version": "1.1.0",
  "capabilities": {
    "execute": {
      "description": "Execute a terminal command",
      "parameters": {
        "command": {
          "type": "string",
          "description": "The command to execute"
        }
      },
      "returns": {
        "type": "object",
        "properties": {
          "exitCode": { "type": "number" },
          "stdout": { "type": "string" },
          "stderr": { "type": "string" },
          "startTime": { "type": "string" },
          "endTime": { "type": "string" }
        }
      }
    }
  }
}

消息格式

请求

{
  "type": "execute",
  "data": {
    "command": "echo 'hello world'"
  }
}

响应

{
  "type": "result",
  "data": {
    "command": "echo 'hello world'",
    "exitCode": 0,
    "stdout": "hello world\n",
    "stderr": "",
    "startTime": "2024-01-20T12:34:56.789Z",
    "endTime": "2024-01-20T12:34:56.790Z"
  }
}

错误

{
  "type": "error",
  "data": {
    "message": "command not allowed"
  }
}

安全注意事项

  1. 命令验证

    • 只能执行允许的命令
    • 禁止 shell 操作符
    • 防止命令注入尝试
  2. 资源保护

    • 命令超时防止挂起
    • 输出大小限制防止内存耗尽
    • 处理所有失败情况的错误处理
  3. 最佳实践

    • 在生产环境中始终设置 allowed-commands
    • 使用保守的超时和大小限制
    • 监控命令执行日志

贡献

  1. Fork 仓库
  2. 创建你的功能分支 (git checkout -b feature/amazing-feature)
  3. 提交你的更改 (git commit -m 'Add some amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 打开一个 Pull Request

许可证

该项目根据 MIT 许可证授权 - 详情请参阅 LICENSE 文件。

相关 MCP 服务