<工具 Claude Desktop>配置 Windows CLI MCP Server 安全的命令行交互功能 受控访问 PowerShell, CMD, Git Bash 与 SSH 5分钟搞定

原因:

今天再次打开: https://github.com/modelcontextprotocol/

看到社区服务器中,列出 win-cli-mcp

介绍:

Windows CLI 

为 Windows 系统上的安全命令行交互提供支持,能对 PowerShell、CMD、Git Bash shell 以及通过 SSH 连接的远程系统进行受控访问。它允许 MCP 客户端(Claude Desktop)在系统上执行操作,类似于 Open Interpreter。

注意:社区服务器是未经测试的,使用风险自负。它们与 Anthropic 无关联,也未获得 Anthropic 的认可。

能力:

  • 支持多 Shell: PowerShell、命令提示符 (CMD) 和 Git Bash
  • SSH 支持: 通过 SSH 在远程系统上执行命令
  • 资源可视与可访问: 将 SSH 连接、当前目录和配置视为 MCP 资源进行查看
  • 安全控制:
    • 命令和 SSH 命令阻止(完整路径,大小写变体)
    • 工作目录验证
    • 最大命令长度限制
    • 命令日志记录和历史跟踪
    • 智能参数验证
  • 可配置:
    • 自定义安全规则
    • 特定 Shell 设置
    • SSH 连接配置文件
    • 路径限制
    • 阻止的命令列表

注意: 服务器将仅允许在配置的目录内、使用允许的命令以及在配置的 SSH 连接上执行操作。

以自己的解理,大概翻译了上图。

觉等有用再往下做~

演示:

记得 以前是 27 个锤子,现在是 36 个

下面是读取文件【用的 filesystem server】:\\davens\Multimedia\2024-MyProgramFiles\27.1.xai-i.bestherbs.cn\requirements.txt

配置 Claude Desktop

1. 安装 Node.js 

确保你的 Windows 11 计算机上安装了 Node.js

因我计算机上有,过程略...

2. 修改 claude_desktop_config.json 文件

编辑 C:\Users\你的用户名\AppData\Roaming\Claude\claude_desktop_config.json

如果新纯新:

{
  "mcpServers": {
    "windows-cli": {
      "command": "npx",
      "args": ["-y", "@simonb97/server-win-cli"]
    }
  }
}

下面是我在用的配置文件,但里面使用 “我的用户名” 替代 原ID

{
    "mcpServers": {
      "filesystem": {
        "command": "npx",
        "args": [
          "-y",
          "@modelcontextprotocol/server-filesystem",
          "\\\\davens\\Multimedia\\2024-MyProgramFiles"
        ]
      },
      "brave-search": {
        "command": "npx",
        "args": [
          "-y",
          "@modelcontextprotocol/server-brave-search"
        ],
        "env": {
          "BRAVE_API_KEY": "BSAC9BzrEeWjl9DPXGsdXIzcfNUnyh5"
        }
      },
      "fetch": {
        "command": "python",
        "args": ["-m", "mcp_server_fetch"]
      },
      "blender": {
        "command": "uvx",
        "args": [
          "blender-mcp"
        ]
      },
      "windows-cli": {
        "command": "npx",
        "args": [
          "-y", 
          "@simonb97/server-win-cli",
          #"--config",
          #"C:\\Users\\我的用户名\\Documents\\win-cli-config.json"
          #后面有介绍
        ]
      }
    }
  }

3. 创建自定义配置文件

3.1 挑个文件存放位置

文件放在:C:\Users\我的用户名\Documents\win-cli-config.json

3.2 创建文件
npx @simonb97/server-win-cli --init-config C:\Users\我的用户名\Documents\win-cli-config.json

内容:

{

  "security": {

    "maxCommandLength": 2000,

    "blockedCommands": [

      "rm",

      "del",

      "rmdir",

      "format",

      "shutdown",

      "restart",

      "reg",

      "regedit",

      "net",

      "netsh",

      "takeown",

      "icacls"

    ],

    "blockedArguments": [

      "--exec",

      "-e",

      "/c",

      "-enc",

      "-encodedcommand",

      "-command",

      "--interactive",

      "-i",

      "--login",

      "--system"

    ],

    "allowedPaths": [

      "C:\\Users\\我的用户名",

      "C:\\Users\\我的用户名\\Documents",

      "Z:\\2024-MyProgramFiles"

    ],

    "restrictWorkingDirectory": true,

    "logCommands": true,

    "maxHistorySize": 1000,

    "commandTimeout": 30,

    "enableInjectionProtection": true

  },

  "shells": {

    "powershell": {

      "enabled": true,

      "command": "powershell.exe",

      "args": [

        "-NoProfile",

        "-NonInteractive",

        "-Command"

      ],

      "blockedOperators": [

        "&",

        "|",

        ";",

        "`"

      ]

    },

    "cmd": {

      "enabled": true,

      "command": "cmd.exe",

      "args": [

        "/c"

      ],

      "blockedOperators": [

        "&",

        "|",

        ";",

        "`"

      ]

    },

    "gitbash": {

      "enabled": true,

      "command": "C:\\Program Files\\Git\\bin\\bash.exe",

      "args": [

        "-c"

      ],

      "blockedOperators": [

        "&",

        "|",

        ";",

        "`"

      ]

    }

  },

  "ssh": {

    "enabled": false,

    "defaultTimeout": 30,

    "maxConcurrentSessions": 5,

    "keepaliveInterval": 10000,

    "keepaliveCountMax": 3,

    "readyTimeout": 20000,

    "connections": {}

  }

}

上面有两处用:我的用户名 替代原内容

4. 更新 Claude Desktop 配置

4.1 编辑文件 

编辑 C:\Users\你的用户名\AppData\Roaming\Claude\claude_desktop_config.json

      "windows-cli": {
        "command": "npx",
        "args": [
          "-y", 
          "@simonb97/server-win-cli",
          "--config",
          "C:\\Users\\我的用户名\\Documents\\win-cli-config.json"
        ]
      }

添加:"--config",
置文件的路径:"C:\\Users\\我的用户名\\Documents\\win-cli-config.json"

5. 安全设置 Windows 11 专有

allowedPaths 管理要访问的文件夹
5.1 修改配置文件 win-cli-config.json

例:"allowedPaths": ["C:\\Users\\你的用户名", "C:\\Projects"]

6. 重启 Claude Desktop

任务管理器中结束 Task

总结:

WIN-CLI MCP 是返回命令的运行结果。如果配置使用的不是默认设置,需要给详细的提示词。

社区项目有风险,使用需谨慎。

### 解决 npx 在 Windows 上运行失败问题 当遇到 `npx` 在 Windows 平台上无法正常运行的情况时,通常可能是由于环境变量未正确配置、Node.js 安装不完全或者权限不足等原因引起的。以下是针对该问题的具体分析和解决方案: #### 环境变量检查 确保 Node.js 已经被正确安装并添加到系统的 PATH 变量中。可以通过以下命令验证: ```bash node -v && npm -v ``` 如果上述命令返回版本号,则说明环境已正确配置;否则需要重新安装 Node.js 或者手动调整 PATH 路径[^3]。 #### 权限问题排查 某些情况下,可能是因为当前用户缺乏执行脚本所需的权限。尝试以管理员身份启动命令提示符或 PowerShell,并再次运行 `npx` 命令来测试是否仍然存在错误。 --- ### 配置 MCP Server 的方法 对于 MCP 报错问题:“Error executing MCP tool: Not connected”,可以按照如下方式完成 MCP Server配置: #### 打开 Cursor 设置界面 通过访问 Cursor 应用程序中的 **Settings > Features > MCP Servers** 页面,进入 MCP 服务管理区域[^1]。 #### 创建新的 MCP Server 点击“Add New Server”按钮创建一个新的服务器实例,在弹出窗口中填写必要的参数信息: - **名称 (Name)**:为新建立的服务指定一个易于辨识的名字,例如 “FileSystem MCP Server”。这一步骤有助于后续管理和区分不同类型的连接[^2]。 其他可选字段依据实际需求补充即可,比如端口号、认证密钥等高级选项可以根据官方文档进一步查阅设定指南。 #### 测试连通性 保存更改之后,利用内置工具发起一次简单的请求操作,确认新建好的 MCP Server 是否能够成功响应客户端指令。如果没有异常反馈,则表明整个流程已经顺利完成部署。 --- ### 综合处理建议 结合以上两部分内容来看,先解决基础开发环境中可能出现的基础障碍——即修复因路径缺失而导致的 `npx` 功能受限状况;再依照具体业务场景下的指导手册逐步完善关于远程协作平台方面的各项细节安排。两者相辅相成才能达到预期效果。 ```python import subprocess def run_npx_command(command): try: result = subprocess.run(['npx', command], check=True, text=True, capture_output=True) return f'Successfully executed {command}. Output:\n{result.stdout}' except subprocess.CalledProcessError as e: return f'Failed to execute {command}. Error message:\n{e.stderr}' print(run_npx_command('--version')) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值