Skip to main content
Glama
README.md
<div align="center">

<img src="https://capsule-render.vercel.app/api?type=waving&color=0:0f172a,100:2563eb&height=220&section=header&text=multi-vm-mcp&fontSize=54&fontColor=ffffff&animation=fadeIn&desc=One%20MCP%20server%20for%20VMware%20and%20VirtualBox&descAlignY=74&descSize=18" alt="multi-vm-mcp" />

<img src="https://readme-typing-svg.demolab.com?font=JetBrains+Mono&weight=600&size=20&duration=2800&pause=900&color=2563EB&center=true&vCenter=true&width=920&lines=Observe+the+VM+console;Move%2C+click%2C+type%2C+run+commands;Manage+power+and+snapshots;VMware+Workstation+%2B+VirtualBox" alt="typing animation" />

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
![Python](https://img.shields.io/badge/Python-3.11%2B-3776AB?logo=python&logoColor=white)
![MCP](https://img.shields.io/badge/MCP-stdio-6E56CF)
![VMware](https://img.shields.io/badge/VMware-Workstation-607078)
![VirtualBox](https://img.shields.io/badge/VirtualBox-7.x-183A61)
![Tests](https://img.shields.io/badge/tests-37%20passed-brightgreen)
![Platform](https://img.shields.io/badge/platform-Windows-0078D6?logo=windows&logoColor=white)

[![GitHub stars](https://img.shields.io/github/stars/ZiYyyun/multi-vm-mcp?style=social)](https://github.com/ZiYyyun/multi-vm-mcp/stargazers)
[![Last commit](https://img.shields.io/github/last-commit/ZiYyyun/multi-vm-mcp?color=2563eb&label=last%20commit)](https://github.com/ZiYyyun/multi-vm-mcp/commits/main)

</div>

> 一个本地 MCP Server,统一操作 **VMware Workstation/Player** 与 **Oracle VirtualBox**。  
> 让 Agent 看虚拟机画面、动鼠标键盘、执行 Guest 命令、管理电源与快照。  
> **ESXi / vCenter Provider 接口已预留,v1 暂未实现。**

---

## 为什么是 multi-vm-mcp

| 设计 | 解决的问题 |
|---|---|
| **一个协议,多个后端** | Provider 注册表按 `vmware` / `virtualbox` 选择实现,后续可扩展 ESXi/vCenter |
| **观察编号(observation_id)** | GUI 动作必须基于最新画面,防止 Agent 盲点、重放和坐标错位 |
| **异步命令任务(job_id)** | 长命令不阻塞 MCP;服务重启后仍可继续读取输出 |
| **异步 VM 操作(operation_id)** | 电源/快照操作可追踪;结果不明时先查状态,不盲目重试 |
| **本地凭据库** | Guest 密码与 VNC 密码存系统 keyring,不写进配置文件 |
| **安全默认值** | 破坏性操作要求 `confirm=true`;VNC 默认绑定 `127.0.0.1` |
| **stdio 本地运行** | 不监听公网端口,适合 Codex、Claude Code、Cursor、Trae 等客户端 |

---

## 能做什么

| 能力 | 说明 |
|---|---|
| 控制台画面 | 返回虚拟机当前 PNG/JPEG 画面、分辨率与 `observation_id` |
| GUI 操作 | 鼠标移动、单击、双击、右键、拖拽、滚轮、按键、快捷键、文本输入 |
| 连续动作 | 一次最多 50 个短动作,返回最终画面 |
| Unicode 粘贴 | 向已聚焦输入框粘贴中文或长文本;依赖 Guest Tools / 剪贴板工具 |
| 登录框输入 | 使用已保存用户名或密码输入,不向 Agent 返回凭据文本 |
| Guest 命令 | Windows CMD/PowerShell、Linux Bash/Sh,异步任务与输出回读 |
| 电源管理 | 启动、正常关机、强制断电、重启、暂停、恢复、挂起、保存状态 |
| 快照管理 | 列出、创建、恢复、删除;恢复后旧观察编号自动失效 |
| 状态查询 | 运行状态、VMware Tools / Guest Additions、Guest IP |
| 多虚拟机 | 按名称选择目标,每台机器独立配置与凭据 |
| 客户端接入 | 生成 Codex / Claude Code / Claude Desktop / Cursor 的 stdio 配置 |

---

## 架构

```mermaid
flowchart LR
    A["MCP Client<br/>Codex / Claude / Cursor / Trae"] -->|stdio| B["multi-vm-mcp<br/>FastMCP"]
    B --> C["LabService<br/>观察 / 任务 / 操作编排"]
    C --> D["Provider Registry"]
    D --> E["VMware Provider<br/>vmrun + VNC"]
    D --> F["VirtualBox Provider<br/>VBoxManage + COM SDK"]
    D -.->|预留| G["ESXi / vCenter<br/>future"]
    E --> H["VMware 虚拟机"]
    F --> I["VirtualBox 虚拟机"]
```

- **MCP 层**:12 个工具,输入校验、结构化错误、图像内容返回。
- **服务层**:观察编号、每 VM 串行锁、后台任务、操作状态机。
- **Provider 层**:统一接口,能力声明(截图、鼠标、Unicode 粘贴、Guest 命令等)。
- **存储层**:SQLite 保存命令任务与操作记录;系统 keyring 保存凭据。

---

## 快速开始

### 环境要求

- 宿主机:Windows 10/11 x64
- Python 3.11+
- VMware Workstation/Player(提供 `vmrun.exe`),或 Oracle VirtualBox 7.x(提供 `VBoxManage.exe`)
- Guest 内已安装 VMware Tools / open-vm-tools / Guest Additions(命令执行与 IP 查询需要)
- macOS/Linux 宿主机代码保持可移植,但 v1 未做完整验证

### 安装

```powershell
git clone https://github.com/ZiYyyun/multi-vm-mcp.git
cd multi-vm-mcp

# 推荐:uv
uv venv --python 3.12 .venv
uv pip install -e ".[dev]" --python .venv\Scripts\python.exe

# 或者:venv + pip
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -e ".[dev]"
```

### 配置与注册

```powershell
# 1. 生成配置
.\.venv\Scripts\vm-mcp.exe init

# 2. 发现本机虚拟机并写入配置
.\.venv\Scripts\vm-mcp.exe discover --register

# 3. 编辑配置,填写 guest_user
#    %APPDATA%\vmboxs-mcp\config.toml

# 4. 保存 Guest 登录密码(交互输入,不写配置文件)
.\.venv\Scripts\vm-mcp.exe credential set <虚拟机名>

# 5. VMware 虚拟机首次使用控制台前:先关机,再准备 VNC
.\.venv\Scripts\vm-mcp.exe console prepare <虚拟机名>

# 6. 检查 Provider、配置、凭据和虚拟机状态
.\.venv\Scripts\vm-mcp.exe doctor
```

### 接入 MCP 客户端

```powershell
# 写入 Codex 配置(~/.codex/config.toml)
.\.venv\Scripts\vm-mcp.exe install codex

# 或 Claude Code / Claude Desktop / Cursor
.\.venv\Scripts\vm-mcp.exe install claude-code
.\.venv\Scripts\vm-mcp.exe install claude-desktop
.\.venv\Scripts\vm-mcp.exe install cursor

# 只输出配置,不写文件
.\.venv\Scripts\vm-mcp.exe install cursor --print
```

重启客户端后,应该能看到 `multi-vm-mcp` 提供的 12 个工具。

### 配置示例

```toml
[server]
state_dir = "C:/Users/you/AppData/Local/vmboxs-mcp"
default_command_timeout = 120
command_output_limit = 200000
vnc_host = "127.0.0.1"
log_level = "INFO"

[[vms]]
name = "win11-lab"
provider = "vmware"
path = "D:/VMs/win11-lab/win11-lab.vmx"
os = "windows"
guest_user = "Administrator"
credential = "win11-lab"
console = { vnc_port = 5901, vnc_address = "127.0.0.1", display = 0 }

[[vms]]
name = "ubuntu-lab"
provider = "virtualbox"
uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
os = "linux"
guest_user = "lab"
credential = "ubuntu-lab"
```

密码不写在配置文件里。`vm-mcp credential set` 会把密码写入 Windows Credential Manager / macOS Keychain / Linux Secret Service。

---

## 工具清单

| # | 工具 | 用途 | 是否修改 VM |
|---|---|---|---|
| 1 | `lab_status` | 配置、Provider 版本、运行状态、Tools、控制台能力 | 否 |
| 2 | `vm_discover` | 发现 VMware/VirtualBox 虚拟机;可注册进配置 | 仅注册时写配置 |
| 3 | `lab_reload` | 重新加载配置与凭据,重置控制台连接 | 否 |
| 4 | `console_observe` | 获取画面、分辨率与新的 `observation_id` | 否 |
| 5 | `console_action` | 执行一次鼠标/键盘动作,返回新画面 | 是(Guest 输入) |
| 6 | `console_batch` | 执行 1–50 个动作,返回最终画面 | 是(Guest 输入) |
| 7 | `console_paste` | 向已聚焦输入框粘贴 Unicode/长文本 | 是(Guest 输入) |
| 8 | `console_type_saved` | 输入已保存的用户名/密码,不返回凭据 | 是(Guest 输入) |
| 9 | `guest_command` | 通过 Guest Tools 执行命令,返回 `job_id` | 是(Guest 命令) |
| 10 | `command_read` | 读取同一命令任务的状态与输出 | 否 |
| 11 | `vm_action` | 电源、快照、Tools、IP、运行状态 | 电源/快照会修改 VM |
| 12 | `vm_operation_read` | 查询电源/快照操作的最终结果 | 否 |

### `vm_action` 的 action

`status` · `start` · `shutdown` · `power_off` · `reboot` · `pause` · `resume` · `suspend` · `save_state` · `tools_status` · `ip` · `snapshot_list` · `snapshot_create` · `snapshot_restore` · `snapshot_delete`

破坏性操作 `power_off`、`snapshot_restore`、`snapshot_delete` 必须显式传入 `confirm=true`。

---

## 工作流

### GUI:先观察,再动作

```mermaid
sequenceDiagram
    participant AI as AI Agent
    participant MCP as multi-vm-mcp
    participant VM as VM Console

    AI->>MCP: console_observe(vm)
    MCP->>VM: 截取当前帧
    VM-->>MCP: PNG
    MCP-->>AI: image + observation_id

    AI->>MCP: console_action(observation_id, click)
    MCP->>VM: 注入鼠标
    MCP->>VM: 截取动作后的帧
    MCP-->>AI: new image + new observation_id
```

规则:

1. 每次 GUI 动作必须使用**最新** `observation_id`。
2. 任何动作、快照恢复、电源变化都会让旧编号失效。
3. 出现未知结果时先重新 `console_observe`,不要盲目重放上一组动作。
4. 界面加载较慢时,用 `console_batch` 的 `interval_ms` 或动作后重新观察。

### 命令:job_id 生命周期

```mermaid
stateDiagram-v2
    [*] --> running: guest_command
    running --> running: command_read(未完成)
    running --> succeeded: exit_code = 0
    running --> failed: exit_code != 0
    running --> timed_out: 超过 timeout_sec
```

长命令原则:**继续读同一个 `job_id`,不要重新执行命令**。MCP 重启后仍可用同一 `job_id` 读取。

### 电源/快照:operation_id 生命周期

```mermaid
stateDiagram-v2
    [*] --> running: vm_action
    running --> succeeded: 操作完成
    running --> failed: 操作失败
    running --> unknown: MCP 重启,无法确认
    unknown --> [*]: 先查 lab_status / vm_action status
```

结果不明时**不要直接重试**,先查询虚拟机当前状态。

---

## 给 AI 的使用说明

把 [AI_GUIDE.md](AI_GUIDE.md) 交给你的 Agent,它会知道:

- 12 个工具的参数、返回结构和调用顺序
- 观察编号、命令任务、VM 操作三套状态机
- 常见错误码与恢复动作
- 哪些操作必须先征得用户确认
- 如何在不泄露密码的前提下完成登录框输入

也可以直接把下面这段放进系统提示词:

```text
You have access to multi-vm-mcp. Always call lab_status first to discover configured VM names.
For GUI work: call console_observe, inspect the returned image, then call console_action or
console_batch with the latest observation_id. Never reuse an observation_id; if you receive
STALE_OBSERVATION, observe again. For commands: use guest_command and poll command_read with
the returned job_id; never re-run a long command. For power/snapshot: use vm_action, then poll
vm_operation_read; if the state is unknown, check the VM state instead of retrying. Ask the user
before destructive actions and pass confirm=true only after approval. Never reveal credentials;
use console_type_saved for saved usernames and passwords.
```

---

## Provider 能力矩阵

| 能力 | VMware Workstation/Player | VirtualBox |
|---|---|---|
| 虚拟机发现 | `vmrun list` + VMX 扫描 | `VBoxManage list vms` |
| 控制台截图 | VNC;失败时降级 `vmrun captureScreen` | `controlvm screenshotpng` |
| 绝对鼠标 | VNC 支持 | Windows COM SDK;依赖 Guest Additions 鼠标集成 |
| 键盘/快捷键 | VNC keysym | `keyboardputstring` / `keyboardputscancode` |
| Unicode 粘贴 | Guest 剪贴板助手(需 Tools) | `keyboardputstring` + 剪贴板助手回退 |
| Guest 命令 | `vmrun runProgramInGuest` | `VBoxManage guestcontrol` |
| Guest Tools 状态 | `checkToolsState` | Guest Properties |
| Guest IP | `getGuestIPAddress` | Guest Properties |
| 电源 | `vmrun start/stop/reset/suspend/pause/unpause` | `startvm` / `controlvm` |
| 快照 | `vmrun snapshot` 系列 | `VBoxManage snapshot` |
| 内存快照 | 由 VMware 默认策略决定 | 运行中快照默认包含内存状态 |

### 当前验证范围

- **VMware Workstation**:已在 Windows 宿主机的 CentOS 8 虚拟机上验证 VNC 截图、鼠标移动、批量动作、过期观察拒绝、快照列表。
- **VirtualBox 7.2**:Provider、截图、键盘、Guest 控制、电源与快照代码路径已实现;真实鼠标/键盘和 Guest 命令仍需要在运行中的 VBox 虚拟机上做进一步实测。
- **Guest 命令**:单元测试与命令构造测试通过;真机执行需要配置 Guest 用户名和密码。
- **测试**:`pytest` 37 项通过,`ruff` 无告警。

---

## 安全边界

- 仅使用 **stdio**,不监听外部端口;VMware VNC 默认绑定 `127.0.0.1`。
- Guest 密码与 VNC 密码存系统 keyring,不写入配置文件、日志或工具返回。
- `console_type_saved` 只输入凭据,不返回凭据文本。
- `guest_command` 等价于在虚拟机内执行任意代码,只应在授权虚拟机上使用。
- `power_off`、`snapshot_restore`、`snapshot_delete` 需要 `confirm=true`。
- 修改前建议先创建快照;恢复快照后必须重新 `console_observe`。

---

## 开发

```powershell
# 测试
.\.venv\Scripts\python.exe -m pytest -q

# 代码检查
.\.venv\Scripts\ruff.exe check src tests

# 本地启动 MCP Server(stdio)
.\.venv\Scripts\vm-mcp.exe serve
```

项目结构:

```text
src/vm_mcp/
  server.py              # 12 个 FastMCP 工具
  cli.py                 # init / discover / vm / credential / console / doctor / install
  config.py              # TOML 配置与路径
  credentials.py         # 系统 keyring
  jobs.py                # SQLite 任务与操作存储
  models.py              # 公共模型与动作校验
  errors.py              # 结构化错误码
  services/lab.py        # 观察、任务、操作编排
  providers/
    base.py              # Provider 接口与能力声明
    vmware.py            # vmrun + VNC
    virtualbox.py        # VBoxManage + COM
    guest_scripts.py     # Guest 端 wrapper 生成
  console/
    vnc.py               # VMware VNC 截图与输入
    vbox_sdk.py          # VirtualBox COM 鼠标注入
    vbox_keys.py         # VirtualBox scancode 映射
tests/                   # 单元测试与 FakeProvider
```

> 依赖固定为 `mcp>=1.9,<2`,使用官方 MCP Python SDK 中的 FastMCP API。

---

## Roadmap

- ESXi / vCenter Provider(pyvmomi + WebMKS/VNC)
- VirtualBox 真机集成测试(Windows/Linux Guest、Guest Additions)
- Linux/macOS 宿主机验证
- VMware 多显示器截图与显示选择
- 可选 HTTP 传输与远程部署模式

---

## License

[MIT](LICENSE) © 2026 ZiYyyun

<div align="center">

<img src="https://capsule-render.vercel.app/api?type=waving&color=0:2563eb,100:0f172a&height=110&section=footer" alt="footer" />

[![Star History Chart](https://api.star-history.com/svg?repos=ZiYyyun/multi-vm-mcp&type=Date)](https://star-history.com/#ZiYyyun/multi-vm-mcp&Date)

</div>

TDQS

A3.5/5.0

Scored across 12 tools

Disambiguation4/5

Each tool targets a distinct capability: console observation/action/batch/paste/type, guest command execution vs. reading, VM power/snapshot actions vs. reading operation results. The only soft overlaps are console_action vs console_batch (granularity, but the 1-50 range makes the split clear) and vm_action action=status vs lab_status, which descriptions largely resolve.

Naming Consistency4/5

Consistent snake_case with category prefixes (console_*, lab_*, vm_*) and a predictable *_read pair for async results. Minor deviation: command_read pairs with guest_command rather than sharing a prefix, so its association is slightly less obvious.

Tool Count5/5

Twelve tools is well within the sweet spot, and every tool earns its place: separate observe/action flows, a batch variant for latency, a saved-credential variant for security, and explicit async read-backs for commands and operations.

Completeness4/5

The surface covers discovery, config reload, health/status, console interaction, guest execution, and power/snapshot lifecycle with proper async read-back, avoiding dead ends after MCP restarts. Minor gaps remain: no guest file transfer or clipboard read, and no explicit unregister/remove for discovered VMs (only register via vm_discover).

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues