Skip to main content
Glama
marttinm

windows-terminal-mcp

by marttinm

windows-terminal-mcp

一个 MCP (Model Context Protocol) 服务器,允许任何 LLM 在 Windows 终端上执行命令——包括本地 Shell 和远程 SSH 连接。

功能特性

  • 通过 cmd、powershell 或 bash (Git Bash) 运行命令

  • 通过 SSH 使用基于密钥的身份验证执行远程命令

  • 3 级安全模型(安全 / 确认 / 拦截)

  • 可配置每个命令的超时时间

  • 适用于 Claude Desktop 和任何兼容 MCP 的客户端

Related MCP server: Windows CLI MCP Server

安装

git clone https://github.com/marttinm/windows-terminal-mcp.git
cd windows-terminal-mcp
pip install -e .

需要 Windows 上的 Python 3.11+。

配置

将服务器添加到您的 Claude Desktop 配置文件 (%APPDATA%\Claude\claude_desktop_config.json) 中:

{
  "mcpServers": {
    "windows-terminal": {
      "command": "cmd",
      "args": ["/c", "python", "-m", "windows_terminal_mcp.server"]
    }
  }
}

或者直接指向脚本:

{
  "mcpServers": {
    "windows-terminal": {
      "command": "cmd",
      "args": ["/c", "python", "C:/path/to/windows-terminal-mcp/src/windows_terminal_mcp/server.py"]
    }
  }
}

工具:execute_command

参数

参数

类型

默认值

描述

command

string

必填

要运行的命令。对于 SSH:user@host command

shell

string

cmd

Shell 类型:cmd、powershell、bash、ssh

working_dir

string

—

工作目录(仅限本地命令)

timeout_ms

integer

120000

超时时间(毫秒)

ssh_key

string

—

SSH 私钥路径(例如 ~/.ssh/id_rsa)

响应

{
  "stdout": "...",
  "stderr": "...",
  "exit_code": 0,
  "execution_time": 0.42
}

使用示例

本地命令

Run: dir C:\Users
Shell: cmd

Run: Get-Process | Select-Object -First 5
Shell: powershell

Run: git status
Shell: bash

SSH 命令

Run: ubuntu@192.168.1.10 "df -h && uptime"
Shell: ssh
SSH key: ~/.ssh/id_rsa
Run: ubuntu@192.168.1.10 "docker ps"
Shell: ssh
SSH key: ~/.ssh/my_key
Timeout: 30000

安全性

命令分为三个级别:

安全 (Safe) — 直接执行
echo、dir、type、git、npm、node、python、pip、curl 以及大多数只读命令。

危险 (Dangerous) — Claude 在执行前会发出警告
del、rmdir、rm、taskkill、net stop、net start、sc delete、reg add 等。

拦截 (Blocked) — 始终拒绝
format、del /s /q、rd /s /q、reg delete、regedit、shutdown、net user、rm -rf、mkfs、fdisk 以及类似的破坏性操作。

开发

项目结构

windows-terminal-mcp/
├── src/windows_terminal_mcp/
│   ├── __init__.py
│   ├── server.py      # MCP server (main entry point)
│   ├── executor.py    # subprocess execution
│   ├── main.py        # simple stdin/stdout handler
│   └── security.py    # command safety checks
├── tests/
│   └── test_windows_terminal_mcp.py
├── pyproject.toml
├── LICENSE
└── README.md

运行测试

pip install pytest
pytest tests/ -v

作为 Python 模块使用

from windows_terminal_mcp.server import execute_command

result = execute_command("echo hello", shell="cmd")
print(result)
# {'stdout': 'hello\n', 'stderr': '', 'exit_code': 0, 'execution_time': 0.02}

result = execute_command(
    "ubuntu@192.168.1.10 uptime",
    shell="ssh",
    ssh_key="~/.ssh/id_rsa",
    timeout_ms=10000,
)
print(result)

更新日志

v0.2.0

  • 增加了对基于密钥的 SSH 身份验证的支持

  • 修复了 SSH 命令构建(主机和远程命令作为单独的参数传递)

  • 修复了 Windows 上 SSH 密钥路径中 ~ 的扩展问题

  • 修复了导致 SSH 在 MCP 服务器进程内挂起的 stdin 继承问题

v0.1.0

  • 通过 cmd、powershell、bash 进行基本命令执行

  • stdout/stderr/exit_code 捕获

  • 3 级安全模型

  • 单元测试

许可证

MIT — 参见 LICENSE

Related MCP Connectors

Related MCP Servers

  • A
    license
    B
    quality
    F
    maintenance
    A Model Context Protocol server that provides secure command-line access to Windows systems, allowing MCP clients like Claude Desktop to safely execute commands in PowerShell, CMD, and Git Bash shells with configurable security controls.
    9
    959 npm
    268
    MIT
  • F
    license
    A
    quality
    D
    maintenance
    A local Model Context Protocol server that allows LLMs to securely execute shell commands on remote Linux and Windows systems via SSH connections.
    6
    6 npm
    2
    -
  • A
    license
    B
    quality
    A
    maintenance
    A secure MCP server for shell operations, terminal management, and process control, enabling AI assistants to safely execute commands and manage interactive sessions.
    13
    264 npm
    6
    MIT