mcpField
M
MCP Terminal Server

MCP Terminal Server

本地服务
github.com/RinardNick开发工具1192 周前更新

关于 MCP Terminal Server

MCP Terminal Server 为 Claude 等 LLM 提供受控的终端命令执行能力,适用于需要让模型运行命令的场景。它通过命令白名单、超时和输出大小限制管理执行风险。

主要功能

  1. 01执行 Shell 命令并捕获输出
  2. 02限制允许执行的命令
  3. 03阻止 Shell 操作符及注入
  4. 04设置执行超时和输出上限
  5. 05支持流式输出

适用场景

  1. 01让 LLM 在限制下运行终端命令
  2. 02限制自动化命令的执行范围
  3. 03避免长时间运行或过量输出

MCP 相关

该 MCP 服务向客户端公开终端命令执行能力,并支持标准 MCP 消息格式、能力声明和流式输出。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

MCP Terminal Server

一个安全的终端执行服务,实现了 Model Context Protocol(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"
          ]
        }
      }
    }
    

协议实现

此服务实现了 Model Context Protocol(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 许可,详情请参阅 LICENSE 文件。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。