Windows CLI MCP 服务器
MCP 服务器用于在 Windows 系统上进行安全的命令行交互,支持通过 SSH 控制对 PowerShell、CMD、Git Bash shell 和远程系统的访问。它允许 MCP 客户端(例如Claude Desktop )在您的系统上执行类似于Open Interpreter的操作。
重要提示:此 MCP 服务器可通过 SSH 直接访问您系统的命令行界面和远程系统。启用后,它将授予您访问文件、环境变量、命令执行功能以及远程服务器管理的权限。
- 审查并限制允许的路径和 SSH 连接
- 启用目录限制
- 配置命令块
- 考虑安全影响
请参阅配置以了解更多详细信息。
特征
- 多 Shell 支持:在 PowerShell、命令提示符 (CMD) 和 Git Bash 中执行命令
- SSH 支持:通过 SSH 在远程系统上执行命令
- 资源公开:将 SSH 连接、当前目录和配置视为 MCP 资源
- 安全控制:
- 命令和 SSH 命令阻止(完整路径、大小写变化)
- 工作目录验证
- 最大命令长度限制
- 命令记录和历史跟踪
- 智能参数验证
- 可配置:
- 自定义安全规则
- Shell 特定设置
- SSH 连接配置文件
- 路径限制
- 阻止的命令列表
有关服务器向 MCP 客户端提供的工具和资源的更多详细信息,请参阅API部分。
注意:服务器只允许在配置的目录内、使用允许的命令以及在配置的 SSH 连接上进行操作。
与 Claude Desktop 一起使用
将其添加到您的claude_desktop_config.json
中:
{
"mcpServers": {
"windows-cli": {
"command": "npx",
"args": ["-y", "@simonb97/server-win-cli"]
}
}
}
要与特定的配置文件一起使用,请添加--config
标志:
{
"mcpServers": {
"windows-cli": {
"command": "npx",
"args": [
"-y",
"@simonb97/server-win-cli",
"--config",
"path/to/your/config.json"
]
}
}
}
配置完成后,您可以:
- 使用可用工具直接执行命令
- 在资源部分查看已配置的 SSH 连接和服务器配置
- 通过提供的工具管理 SSH 连接
配置
服务器使用 JSON 配置文件来自定义其行为。您可以指定安全控制、shell 配置和 SSH 连接的设置。
- 要创建默认配置文件,请执行以下操作之一:
**a)**将config.json.example
复制到config.json
,或者
**b)**运行:
npx @simonb97/server-win-cli --init-config ./config.json
- 然后设置
--config
标志以指向您的配置文件,如“使用 Claude Desktop”部分中所述。
配置位置
服务器在以下位置查找配置(按顺序):
--config
标志指定的路径- 当前目录中的 ./config.json
- 用户主目录中的 ~/.win-cli-mcp/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": ["User's home directory", "Current working directory"],
"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": {}
}
}
配置设置
配置文件分为三个主要部分: security
、 shells
和ssh
。
安全设置
{
"security": {
// Maximum allowed length for any command
"maxCommandLength": 1000,
// Commands to block - blocks both direct use and full paths
// Example: "rm" blocks both "rm" and "C:\\Windows\\System32\\rm.exe"
// Case-insensitive: "del" blocks "DEL.EXE", "del.cmd", etc.
"blockedCommands": [
"rm", // Delete files
"del", // Delete files
"rmdir", // Delete directories
"format", // Format disks
"shutdown", // Shutdown system
"restart", // Restart system
"reg", // Registry editor
"regedit", // Registry editor
"net", // Network commands
"netsh", // Network commands
"takeown", // Take ownership of files
"icacls" // Change file permissions
],
// Arguments that will be blocked when used with any command
// Note: Checks each argument independently - "cd warm_dir" won't be blocked just because "rm" is in blockedCommands
"blockedArguments": [
"--exec", // Execution flags
"-e", // Short execution flags
"/c", // Command execution in some shells
"-enc", // PowerShell encoded commands
"-encodedcommand", // PowerShell encoded commands
"-command", // Direct PowerShell command execution
"--interactive", // Interactive mode which might bypass restrictions
"-i", // Short form of interactive
"--login", // Login shells might have different permissions
"--system" // System level operations
],
// List of directories where commands can be executed
"allowedPaths": ["C:\\Users\\YourUsername", "C:\\Projects"],
// If true, commands can only run in allowedPaths
"restrictWorkingDirectory": true,
// If true, saves command history
"logCommands": true,
// Maximum number of commands to keep in history
"maxHistorySize": 1000,
// Timeout for command execution in seconds (default: 30)
"commandTimeout": 30,
// Enable or disable protection against command injection (covers ;, &, |, \`)
"enableInjectionProtection": true
}
}
Shell 配置
{
"shells": {
"powershell": {
// Enable/disable this shell
"enabled": true,
// Path to shell executable
"command": "powershell.exe",
// Default arguments for the shell
"args": ["-NoProfile", "-NonInteractive", "-Command"],
// Optional: Specify which command operators to block
"blockedOperators": ["&", "|", ";", "`"] // Block all command chaining
},
"cmd": {
"enabled": true,
"command": "cmd.exe",
"args": ["/c"],
"blockedOperators": ["&", "|", ";", "`"] // Block all command chaining
},
"gitbash": {
"enabled": true,
"command": "C:\\Program Files\\Git\\bin\\bash.exe",
"args": ["-c"],
"blockedOperators": ["&", "|", ";", "`"] // Block all command chaining
}
}
}
SSH 配置
{
"ssh": {
// Enable/disable SSH functionality
"enabled": false,
// Default timeout for SSH commands in seconds
"defaultTimeout": 30,
// Maximum number of concurrent SSH sessions
"maxConcurrentSessions": 5,
// Interval for sending keepalive packets (in milliseconds)
"keepaliveInterval": 10000,
// Maximum number of failed keepalive attempts before disconnecting
"keepaliveCountMax": 3,
// Timeout for establishing SSH connections (in milliseconds)
"readyTimeout": 20000,
// SSH connection profiles
"connections": {
// NOTE: these examples are not set in the default config!
// Example: Local Raspberry Pi
"raspberry-pi": {
"host": "raspberrypi.local", // Hostname or IP address
"port": 22, // SSH port
"username": "pi", // SSH username
"password": "raspberry", // Password authentication (if not using key)
"keepaliveInterval": 10000, // Override global keepaliveInterval
"keepaliveCountMax": 3, // Override global keepaliveCountMax
"readyTimeout": 20000 // Override global readyTimeout
},
// Example: Remote server with key authentication
"dev-server": {
"host": "dev.example.com",
"port": 22,
"username": "admin",
"privateKeyPath": "C:\\Users\\YourUsername\\.ssh\\id_rsa", // Path to private key
"keepaliveInterval": 10000,
"keepaliveCountMax": 3,
"readyTimeout": 20000
}
}
}
}
API
工具
- 执行命令
- 在指定的shell中执行命令
- 输入:
shell
(字符串):要使用的 Shell(“powershell”、“cmd”或“gitbash”)command
(字符串):要执行的命令workingDir
(可选字符串):工作目录
- 以文本形式返回命令输出,如果执行失败则返回错误消息
- 获取命令历史记录
- 获取已执行命令的历史记录
- 输入:
limit
(可选数字) - 返回带有输出的带时间戳的命令历史记录
- ssh_执行
- 通过 SSH 在远程系统上执行命令
- 输入:
connectionId
(字符串):要使用的 SSH 连接的 IDcommand
(字符串):要执行的命令
- 以文本形式返回命令输出,如果执行失败则返回错误消息
- ssh_断开连接
- 断开与 SSH 服务器的连接
- 输入:
connectionId
(字符串):要断开的 SSH 连接的 ID
- 返回确认消息
- 创建 SSH 连接
- 创建新的 SSH 连接
- 输入:
connectionId
(字符串):新 SSH 连接的 IDconnectionConfig
(对象):连接配置详细信息,包括主机、端口、用户名以及密码或 privateKeyPath
- 返回确认消息
- 读取 ssh 连接
- 读取所有已配置的 SSH 连接
- 返回配置中所有 SSH 连接的列表
- 更新 SSH 连接
- 更新现有的 SSH 连接
- 输入:
connectionId
(字符串):要更新的 SSH 连接的 IDconnectionConfig
(对象):新的连接配置详细信息
- 返回确认消息
- 删除 ssh 连接
- 删除 SSH 连接
- 输入:
connectionId
(字符串):要删除的 SSH 连接的 ID
- 返回确认消息
- 获取当前目录
资源
- SSH 连接
- URI 格式:
ssh://{connectionId}
- 包含隐藏敏感信息的连接详细信息
- 每个已配置的 SSH 连接对应一个资源
- 例如:
ssh://raspberry-pi
显示“raspberry-pi”连接的配置
- SSH 配置
- URI:
ssh://config
- 包含整体 SSH 配置和所有连接(带有屏蔽密码)
- 显示 defaultTimeout、maxConcurrentSessions 等设置以及连接列表
- 当前目录
- URI:
cli://currentdir
- 包含 CLI 服务器的当前工作目录
- 显示默认执行命令的路径
- CLI 配置
- URI:
cli://config
- 包含 CLI 服务器配置(不包括敏感数据)
- 显示安全设置、shell 配置和 SSH 设置
安全注意事项
内置安全功能(始终处于活动状态)
以下安全功能已硬编码到服务器中,无法禁用:
- 不区分大小写的命令阻止:所有命令阻止都是不区分大小写的(例如,如果 blockedCommands 中存在“del”,则“DEL.EXE”、“del.cmd”等都会被阻止)
- 智能路径解析:服务器解析完整的命令路径以防止绕过尝试(如果“rm”被阻止,则阻止“C:\Windows\System32\rm.exe”)
- 命令解析智能:避免误报(例如,“warm_dir”不会被阻止,只是因为“rm”在blockedCommands中)
- 输入验证:所有用户输入在执行前都经过验证
- Shell 进程管理:进程在执行或超时后正确终止
- 敏感数据屏蔽:资源中的密码会自动屏蔽(替换为********)
可配置的安全功能(默认激活)
这些安全功能可通过 config.json 文件进行配置:
- 命令阻止:
blockedCommands
数组中指定的命令被阻止(默认包括 rm、del、format 等危险命令) - 参数阻止:
blockedArguments
数组中指定的参数被阻止(默认包含潜在危险的标志) - 命令注入保护:防止命令链(通过
enableInjectionProtection: true
默认启用) - 工作目录限制:将命令执行限制在指定的目录中(通过
restrictWorkingDirectory: true
默认启用) - 命令长度限制:限制最大命令长度(默认值:2000 个字符)
- 命令超时:终止运行时间过长的命令(默认值:30 秒)
- 命令日志记录:记录命令历史记录(通过
logCommands: true
默认启用)
重要安全警告
这些不是功能,而是需要注意的重要安全事项:
- 环境访问:命令可能有权访问环境变量,其中可能包含敏感信息
- 文件系统访问:命令可以在允许的路径内读取/写入文件 - 仔细配置
allowedPaths
以防止访问敏感数据
执照
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅LICENSE文件。