mcp-ht-better-hyperv
# mcp-ht-better-hyperv
简体中文 | [English](README.en.md)
一个把 Windows Hyper-V 的日常运维能力提供给任意 MCP 客户端的 server。
[](https://www.npmjs.com/package/@huantian/mcp-ht-better-hyperv)
[](LICENSE)
[](package.json)
## 项目介绍
一个独立的 **MCP server**,把 Windows Hyper-V 的日常运维能力交给任意支持 MCP 的客户端。通过 **stdio** 或 **Streamable HTTP** 提供 12 个 `hyperv_*` 工具:
- 列出虚拟机、电源状态、代次、CPU/内存、客机心跳与是否已保存凭证
- 截取客机当前屏幕,存成 PNG,并把图片本身返回给客户端
- 开关机与重启(干净关机 / 断电)
- 在宿主机上提权执行 PowerShell
- 在客机内以管理员身份执行 PowerShell(PowerShell Direct)
- 用合成鼠标/键盘操作客机桌面,并能把一串动作编成一次调用
> **⚠️ 风险声明(部署前必读)**
> 本 server 面向“在本机管理本机 Hyper-V”的单一操作者,能力等价于把宿主机与客机的管理员权限交给调用它的模型。
>
> - 运行条件:Windows + 已启用 Hyper-V + 运行账户在本地 `Administrators` 组内;首次提权会弹出 UAC(`ConsentPromptBehaviorAdmin` 为静默模式时除外)。
> - `hyperv_host_exec` 在**宿主机**上以管理员权限执行**任意 PowerShell**;`hyperv_exec` 在客机内是**完整管理员令牌**,不受客机 UAC 约束。
> - `hyperv_mouse` / `hyperv_keyboard` / `hyperv_sequence` 能操作客机桌面;非 ASCII 输入会走剪贴板注入,**改写并还原宿主机剪贴板**(只备份还原文本)。
> - `hyperv_vm_screenshot` 会把客机画面交给 MCP 客户端,并默认落盘到数据目录的 `screenshots\`。
> - 只在**本机或可信网络**运行,不要暴露到公网;HTTP 传输**不提供 TLS**,跨机器使用请放在 HTTPS 反向代理或 VPN 之后。
> - 客机凭证以**明文**保存在数据目录的 `config.json`;`HYPERV_KEEP_SCRIPTS=true` 会把**含明文凭据**的临时脚本留在系统临时目录。
>
> 完整清单与漏洞报告方式见 [SECURITY.md](SECURITY.md)。
## 环境要求
- Windows 10/11 Pro、Enterprise/Education 或 Windows Server,且已启用 Hyper-V
- Windows PowerShell 5.1(优先)或 PowerShell 7(回退)
- Node.js >= 20
- 运行 server 的账户在本地 `Administrators` 组内
- 需要交互桌面才能弹出/完成 UAC;若 `ConsentPromptBehaviorAdmin=0`,提权静默
## 安装
```powershell
# 免安装直接跑(stdio,由客户端负责拉起)
npx @huantian/mcp-ht-better-hyperv
# 或全局安装,之后直接用命令名
npm i -g @huantian/mcp-ht-better-hyperv
mcp-ht-better-hyperv
```
从源码仓库运行时才需要在仓库目录 `npm install`,然后用 `node src/index.js`。
## 启动
stdio(默认):
```powershell
npx @huantian/mcp-ht-better-hyperv
```
HTTP:
```powershell
$env:MCP_HTTP_AUTH_TOKEN = "请换成一个长随机串"
npx @huantian/mcp-ht-better-hyperv --transport http --host 127.0.0.1 --port 8787
```
端点 `http://127.0.0.1:8787/mcp`,健康检查 `GET /healthz`。`--port 0` 会选空闲端口并把实际地址打印到 stderr。
stdio 模式下 **stdout 只输出 MCP JSON-RPC**,所有日志写 stderr 与数据目录的日志文件。
## 客户端配置
最小 stdio 配置:
```json
{
"mcpServers": {
"hyperv": {
"command": "npx",
"args": ["-y", "@huantian/mcp-ht-better-hyperv"]
}
}
}
```
Claude Desktop、Claude Code、Cursor、VS Code、Codex 与通用 HTTP 的完整配置见 [docs/client-setup.md](docs/client-setup.md),环境变量表也在那里。
## 工具
| 工具 | 作用 | 需要客机 OS |
|---|---|---|
| `hyperv_vm_list` | 列出虚拟机:名称、电源状态、代次、CPU/内存、心跳、是否已存凭证 | 否 |
| `hyperv_vm_screenshot` | 截取当前屏幕,存成 PNG,返回路径 + `image` 内容块 | 否 |
| `hyperv_vm_start` | 启动并等待 `Running` | 否 |
| `hyperv_vm_stop` | 停止并等待 `Off`;`mode=shutdown`(默认)或 `turnoff` | 否 |
| `hyperv_vm_restart` | 停止 → 等待 `Off` → 启动 → 等待 `Running` | 否 |
| `hyperv_host_exec` | 在宿主机上以管理员权限执行 PowerShell | 否 |
| `hyperv_exec` | 在客机内以管理员权限执行 PowerShell | **是** |
| `hyperv_mouse` | 移动、单击/双击/右键、拖拽、滚轮(仅基本会话) | **是** |
| `hyperv_keyboard` | ASCII 文本、键名按键、组合键;非 ASCII 自动走剪贴板 | **是**(非 ASCII 还要凭证) |
| `hyperv_sequence` | 一次调用执行一串点击/输入/等待/等画面变化/截图,失败即停 | **是** |
| `hyperv_credential_set` | 保存客机管理员用户名/密码(允许空密码) | 否 |
| `hyperv_credential_clear` | 删除某台 VM 的凭证 | 否 |
### 截图
`hyperv_vm_screenshot(vm, region?, savePath?)` 从 Hyper-V 合成显卡取一帧,支持 `region={x1,y1,x2,y2}` 裁剪;越界返回 `INVALID_ARGUMENT`,不会自动裁边。默认保存到 `<数据目录>/screenshots/`,同时作为 `image` 内容块返回。
**可能得到全黑图片,这不是故障**:增强会话连接期间、没有任何基本会话连接时、或客机息屏时,Hyper-V 的帧缓冲就是空的。
### 鼠标与键盘
`hyperv_mouse(vm, action, x?, y?, ...)` 支持 `move / click / drag / scroll`;`hyperv_keyboard(vm, text?, key?, combo?, submit?)` 支持键名与组合键。
**只对基本会话有效**:增强会话(RDP / HvSocket)期间合成事件到不了客机,工具会明确报错。坐标是客机当前分辨率的像素坐标,原点左上,越界不夹边。含非 ASCII 字符的文本走剪贴板注入(需要该 VM 的凭证和客机上已登录的桌面),只备份还原文本剪贴板。
### 编排
`hyperv_sequence(vm, steps, defaultSettleMs?)` 把 `click / type / key / combo / scroll / drag / wait / waitForChange / screenshot` 编进同一次调用。`waitForChange` 用截图比对等待画面变化,超时报 `TIMEOUT`;任何一步失败立即停止,并附一张该时刻的现场截图。
### 两种执行工具的区别
| | `hyperv_host_exec` | `hyperv_exec` |
|---|---|---|
| 在哪跑 | 宿主机 | 客机 |
| 权限 | 提权到宿主管理员 | 客机内完整管理员(无 UAC) |
| 需要客机系统 | 否 | **是** |
| 超时行为 | 提权进程内部计时,到点真正停止 | 只限制宿主等待,客机里的命令会继续跑完 |
| 输出上限 | 200 000 字符,保留头尾 | 不截断 |
### 心跳与电源状态
`Running` 只说明通电,不说明有活着的操作系统。新建未装系统、开机中、崩溃的虚拟机都能列出、开关机和改配置,只有 `hyperv_exec` 需要活着的 Windows 客机。
| Heartbeat | `hyperv_exec` |
|---|---|
| `Ok` | 可以连接 |
| `NoContact` / `Lost` / `Error` | 无法连接 |
| `Disabled` / `Unknown` | 仍尝试连接 |
## 重要限制
- **`hyperv_exec` 只支持 Windows 客机**:PowerShell Direct 也支持 Linux,本工具未实现。
- **客机必须先装好系统**:没装系统的 VM 只能用列表、截图、开关机和 `hyperv_host_exec`。
- **截图可能全黑**:增强会话、没有基本会话连接、或客机息屏时都是这样,不是工具故障。
- **合成鼠标/键盘只对基本会话有效**:增强会话期间会明确报错,而不是“成功但没反应”。
- **非 ASCII 文本走剪贴板注入**:需要该 VM 已保存凭证,且客机上有已登录的交互桌面。
- **`hyperv_sequence` 失败即停**:不会继续执行后续步骤。
- **`hyperv_exec` 超时不中断客机命令**。
- **客机内是 Session 0 后台会话**:命令能跑,但弹出的 GUI 窗口用户看不见。
- **每次调用约 2–6 秒**:每次都要启动一个提权进程,未实现常驻通道。
- **凭证失效会自动清除**:仅在 `VM_NOT_FOUND` 与 `AUTH_FAILED` 时;`GUEST_UNAVAILABLE` 与 `VM_NOT_RUNNING` 不清除。
- **HTTP 是无内置 TLS 的单用户服务**:不是多租户网关。
## 文档
`docs/` 目录下的文档目前仅提供中文版。
- [docs/client-setup.md](docs/client-setup.md):各客户端完整配置与环境变量表
- [docs/troubleshooting.md](docs/troubleshooting.md):排查表与日志位置
- [docs/operations.md](docs/operations.md):数据目录、凭证、错误码全表、HTTP 安全
- [docs/architecture.md](docs/architecture.md):链路、模块职责、错误分层(面向贡献者)
- [CONTRIBUTING.md](CONTRIBUTING.md) / [English](CONTRIBUTING.en.md):开发环境与测试要求
- [SECURITY.md](SECURITY.md) / [English](SECURITY.en.md):权限清单、最危险的工具、漏洞报告方式
## 开发与测试
```powershell
npm test # 运行 test/ 下的全部测试
npm run check # 语法检查 + 全部测试
npm run live-check # 只读真机冒烟
npm run screenshot-check # 截图真机冒烟
npm run input-check # 输入真机冒烟(会操作客机桌面)
```
真机脚本的注意事项见 [CONTRIBUTING.md](CONTRIBUTING.md)。
## 许可证
[MIT](LICENSE)
TDQS
Scored across 12 tools
The set is largely distinct: lifecycle (list/start/stop/restart), guest input (mouse/keyboard/sequence), screenshots, credentials, and execution are separate concerns. The only pair an agent could confuse is hyperv_exec (inside guest) vs hyperv_host_exec (host, elevated), and hyperv_sequence vs hyperv_mouse/hyperv_keyboard, but the descriptions make the boundaries explicit and hard to misselect.
All names are lowercase snake_case with a consistent hyperv_ prefix, and the dominant hyperv_vm_<action> and hyperv_credential_<action> patterns are predictable. Minor deviations exist (hyperv_host_exec and hyperv_exec place the verb last, and exec vs host_exec differ by one word), but nothing is ambiguous or chaotic.
12 tools is well within a healthy range and each one earns its place: VM lifecycle, guest execution, input primitives, batching, screenshot, and credential management. There is no filler or trivially redundant tool, and the batching tool (hyperv_sequence) exists for a real round-trip cost rather than convenience alone.
Core workflows are covered end to end: discover VMs, power cycle them, get credentials, run commands, see the screen, and drive input. Gaps like VM creation/deletion, snapshots/checkpoints, and guest file transfer are not native tools, but the elevated hyperv_host_exec escape hatch explicitly covers checkpoints, import/export, and host-side Hyper-V cmdlets, so agents can work around them.