utm-mcp 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -2
- package/agentdist.json +55 -0
- package/bin/utm-mcp.mjs +30 -0
- package/package.json +43 -4
- package/skills/utm/SKILL.md +119 -0
- package/skills/utm/references/configuration.md +131 -0
- package/skills/utm/references/linux-guests.md +87 -0
- package/skills/utm/references/utmctl.md +51 -0
- package/skills/utm/references/windows-guests.md +66 -0
- package/src/applescript.mjs +247 -0
- package/src/exec.mjs +81 -0
- package/src/feedback.mjs +103 -0
- package/src/media.mjs +194 -0
- package/src/policy.mjs +94 -0
- package/src/server.mjs +460 -0
- package/src/utm.mjs +162 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Windows guests
|
|
2
|
+
|
|
3
|
+
Checked with Windows 11 Pro 24H2 (ARM64, build 26100) on UTM 5.0.9, QEMU backend, installed by `create_windows_vm` from a Microsoft consumer ISO.
|
|
4
|
+
|
|
5
|
+
## Installing
|
|
6
|
+
|
|
7
|
+
`create_windows_vm` needs an ARM64 Windows 11 installer ISO in an allowed host directory, and UTM's Windows guest tools already downloaded on the Mac. It then:
|
|
8
|
+
|
|
9
|
+
1. Builds a small support disc holding an answer file (`autounattend.xml`) and UTM's guest tools installer.
|
|
10
|
+
2. Creates the VM: UEFI with TPM 2.0, a local-time clock, an NVMe system disk, a display, the installer ISO and the support disc.
|
|
11
|
+
3. Starts it and taps an arrow key through the firmware's "Press any key to boot from CD or DVD" prompt.
|
|
12
|
+
4. Returns. Windows Setup reads the answer file and does the rest: partitions the disk, installs, skips the first-run questions, creates a local administrator, logs it in, and runs the guest tools installer silently.
|
|
13
|
+
|
|
14
|
+
It took **20 minutes** from the call to the guest agent answering, with no input. Poll `wait_for_guest`; `ready: true` is the signal that it is finished. `capture_screen` shows the stage: "Installing Windows 11 … % complete" on blue, then "Installing … %" on black after the first restart, then the desktop.
|
|
15
|
+
|
|
16
|
+
The account is `agent` unless you chose another, with the password you gave or the generated one the tool returned. Tell the user what it is. Windows is installed with Microsoft's generic key and is not activated.
|
|
17
|
+
|
|
18
|
+
**When it is ready:** shut the guest down, then call `detach_media`. The support disc contains the account password in plain text; `detach_media` removes it from the VM and deletes it from the host.
|
|
19
|
+
|
|
20
|
+
### If the installation stalls
|
|
21
|
+
|
|
22
|
+
- **A firmware shell, or the "Press any key" prompt still showing.** The key tap missed. `stop_vm` with mode `force`, then `start_vm` with `boot_from_cd: true`.
|
|
23
|
+
- **A blue screen with only a Cancel button and no percentage, and it stays that way.** Setup was cancelled or has crashed. Restart the same way. This is what a stray `space` or `enter` does: the first Setup window has Cancel focused. Never send those keys to an installer without looking first.
|
|
24
|
+
- To investigate inside the installer, `send_keys` with `["shift+f10"]` opens a command prompt. Setup's logs are in `X:\$WINDOWS.~BT\Sources\Panther` (`setuperr.log`, `setupact.log`). The installer environment has `type`, `more`, `find` and `diskpart`, but no `findstr`, `tasklist` or PowerShell.
|
|
25
|
+
|
|
26
|
+
## Running commands
|
|
27
|
+
|
|
28
|
+
`guest_exec` and file transfer work exactly as on Linux. What differs is what you run:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
guest_exec command: ["cmd", "/c", "ver & whoami"]
|
|
32
|
+
guest_exec command: ["powershell", "-NoProfile", "-Command", "Get-Service | Where-Object Status -eq Running"]
|
|
33
|
+
guest_push_file guest_path: C:\Users\Public\in.zip
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- Commands run as `NT AUTHORITY\SYSTEM`, not as the logged-in user. They cannot see that user's mapped drives or desktop session.
|
|
37
|
+
- The guest agent is an x64 program running under emulation, so processes it starts report `PROCESSOR_ARCHITECTURE` as `AMD64` on an ARM64 Windows. Do not use that to detect the machine's architecture.
|
|
38
|
+
- Output uses CRLF line endings.
|
|
39
|
+
- Exit codes, stdout, stderr, stdin and environment variables all come through.
|
|
40
|
+
|
|
41
|
+
## Shutting down
|
|
42
|
+
|
|
43
|
+
`stop_vm` with mode `request` was ignored by Windows: the VM was still running two minutes later. Shut it down from inside, then let `stop_vm` wait for it:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
guest_exec command: ["shutdown", "/s", "/t", "0"]
|
|
47
|
+
stop_vm mode: request
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Shutdown took up to a minute and a half.
|
|
51
|
+
|
|
52
|
+
## Snapshots and suspend
|
|
53
|
+
|
|
54
|
+
A Windows VM made here has an NVMe system disk, and QEMU cannot save running state with an emulated NVMe device. So:
|
|
55
|
+
|
|
56
|
+
- `snapshot_create` on a **running** Windows VM fails with "Suspend is not supported when an emulated NVMe device is active."
|
|
57
|
+
- `snapshot_create` on a **stopped** Windows VM works. Shut down, snapshot, start.
|
|
58
|
+
- For a throwaway run, `start_vm` with `disposable: true` needs no snapshot at all. That was checked on Linux, not on a Windows VM.
|
|
59
|
+
|
|
60
|
+
## Shared folders
|
|
61
|
+
|
|
62
|
+
`set_shared_folder` with mode `WebDAV` is the Windows mode, and the guest tools install the service that serves it (`spice-webdavd`, which was running). Mapping the share inside the logged-in user's session was **not checked here**: `guest_exec` runs as SYSTEM and cannot see it. Prefer `guest_push_file` and `guest_pull_file`.
|
|
63
|
+
|
|
64
|
+
## A reusable base
|
|
65
|
+
|
|
66
|
+
Install once, shut down, `detach_media`, take a snapshot while stopped, and keep that VM as a template that is never used directly. `clone_vm` it for each task. Clones share the source's computer name and machine identity; whether two Windows clones coexist on the network without further changes was not checked.
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// The complete set of AppleScripts the broker can run. Each takes its values through `argv`.
|
|
2
|
+
// Results come back as lines of tab-separated fields or key=value pairs, parsed in utm.mjs.
|
|
3
|
+
|
|
4
|
+
/** Written into a VM's notes when the broker creates it; this is what makes a VM the broker's own. */
|
|
5
|
+
export const MANAGED_MARKER = "utm-mcp:managed";
|
|
6
|
+
|
|
7
|
+
const LOOKUP = `
|
|
8
|
+
on lookup(vmRef)
|
|
9
|
+
tell application "UTM"
|
|
10
|
+
-- A reference is not checked until it is used, so touch it to find out whether it exists.
|
|
11
|
+
try
|
|
12
|
+
set vm to virtual machine id vmRef
|
|
13
|
+
get name of vm
|
|
14
|
+
return vm
|
|
15
|
+
end try
|
|
16
|
+
set vm to virtual machine named vmRef
|
|
17
|
+
get name of vm
|
|
18
|
+
return vm
|
|
19
|
+
end tell
|
|
20
|
+
end lookup
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
const ROW = `
|
|
24
|
+
on row(vm)
|
|
25
|
+
tell application "UTM"
|
|
26
|
+
set managed to "0"
|
|
27
|
+
try
|
|
28
|
+
set cfg to configuration of vm
|
|
29
|
+
if (notes of cfg) contains "${MANAGED_MARKER}" then set managed to "1"
|
|
30
|
+
end try
|
|
31
|
+
return (id of vm) & tab & (status of vm as text) & tab & (backend of vm as text) & tab & managed & tab & (name of vm)
|
|
32
|
+
end tell
|
|
33
|
+
end row
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
export const LIST = `${ROW}
|
|
37
|
+
on run argv
|
|
38
|
+
set out to {}
|
|
39
|
+
tell application "UTM"
|
|
40
|
+
repeat with vm in virtual machines
|
|
41
|
+
set end of out to my row(vm)
|
|
42
|
+
end repeat
|
|
43
|
+
end tell
|
|
44
|
+
set AppleScript's text item delimiters to linefeed
|
|
45
|
+
return out as text
|
|
46
|
+
end run
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
export const RESOLVE = `${LOOKUP}${ROW}
|
|
50
|
+
on run argv
|
|
51
|
+
return my row(my lookup(item 1 of argv))
|
|
52
|
+
end run
|
|
53
|
+
`;
|
|
54
|
+
|
|
55
|
+
export const DESCRIBE = `${LOOKUP}
|
|
56
|
+
on run argv
|
|
57
|
+
set vm to my lookup(item 1 of argv)
|
|
58
|
+
set out to {}
|
|
59
|
+
tell application "UTM"
|
|
60
|
+
set cfg to configuration of vm
|
|
61
|
+
set end of out to "memory_mib=" & (memory of cfg)
|
|
62
|
+
set end of out to "cpus=" & (cpu cores of cfg)
|
|
63
|
+
try
|
|
64
|
+
set end of out to "architecture=" & (architecture of cfg)
|
|
65
|
+
set end of out to "share_mode=" & (directory share mode of cfg as text)
|
|
66
|
+
set shown to displays of cfg
|
|
67
|
+
set end of out to "displays=" & (count of shown)
|
|
68
|
+
end try
|
|
69
|
+
repeat with d in drives of cfg
|
|
70
|
+
set src to ""
|
|
71
|
+
try
|
|
72
|
+
set src to POSIX path of (source of d)
|
|
73
|
+
end try
|
|
74
|
+
set iface to ""
|
|
75
|
+
try
|
|
76
|
+
set iface to (interface of d as text)
|
|
77
|
+
end try
|
|
78
|
+
set end of out to "drive=" & (id of d) & tab & (removable of d) & tab & iface & tab & (host size of d) & tab & src
|
|
79
|
+
end repeat
|
|
80
|
+
repeat with n in network interfaces of cfg
|
|
81
|
+
set end of out to "network=" & (mode of n as text) & tab & (address of n)
|
|
82
|
+
end repeat
|
|
83
|
+
try
|
|
84
|
+
set shareList to registry of vm
|
|
85
|
+
repeat with i from 1 to (count of shareList)
|
|
86
|
+
set end of out to "shared_folder=" & (POSIX path of (item i of shareList))
|
|
87
|
+
end repeat
|
|
88
|
+
end try
|
|
89
|
+
end tell
|
|
90
|
+
set AppleScript's text item delimiters to linefeed
|
|
91
|
+
return out as text
|
|
92
|
+
end run
|
|
93
|
+
`;
|
|
94
|
+
|
|
95
|
+
// The cloud-init seed goes on a fixed VirtIO drive: removable drives are USB, which cut-down cloud
|
|
96
|
+
// kernels cannot read. A size given together with a source is ignored at creation, so the disk is
|
|
97
|
+
// grown in a second step, listing both drives because the list is the complete set.
|
|
98
|
+
export const CREATE_LINUX = `
|
|
99
|
+
on run argv
|
|
100
|
+
set {vmName, imagePath, seedPath, memoryMib, cpuCount, diskMib, vmNotes} to argv
|
|
101
|
+
tell application "UTM"
|
|
102
|
+
set vm to make new virtual machine with properties {backend:qemu, configuration:{name:vmName, notes:vmNotes, architecture:"aarch64", memory:(memoryMib as integer), cpu cores:(cpuCount as integer), drives:{{source:(POSIX file imagePath)}, {source:(POSIX file seedPath), interface:VirtIO}}}}
|
|
103
|
+
set cfg to configuration of vm
|
|
104
|
+
set ds to drives of cfg
|
|
105
|
+
set drives of cfg to {{id:(id of item 1 of ds), guest size:(diskMib as integer)}, {id:(id of item 2 of ds)}}
|
|
106
|
+
update configuration of vm with cfg
|
|
107
|
+
return id of vm
|
|
108
|
+
end tell
|
|
109
|
+
end run
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
// Windows 11 on ARM: UEFI with TPM 2.0, a local-time clock, an NVMe system disk, and a display for
|
|
113
|
+
// the installer. The installer ISO is first so the firmware offers to boot it.
|
|
114
|
+
export const CREATE_WINDOWS = `
|
|
115
|
+
on run argv
|
|
116
|
+
set {vmName, isoPath, supportPath, memoryMib, cpuCount, diskMib, vmNotes} to argv
|
|
117
|
+
tell application "UTM"
|
|
118
|
+
set vm to make new virtual machine with properties {backend:qemu, configuration:{name:vmName, notes:vmNotes, architecture:"aarch64", memory:(memoryMib as integer), cpu cores:(cpuCount as integer), uefi:true, tpm:true, rtc local time:true, drives:{{removable:true, source:(POSIX file isoPath)}, {guest size:(diskMib as integer), interface:NVMe}, {removable:true, source:(POSIX file supportPath)}}, displays:{{hardware:"virtio-ramfb"}}}}
|
|
119
|
+
return id of vm
|
|
120
|
+
end tell
|
|
121
|
+
end run
|
|
122
|
+
`;
|
|
123
|
+
|
|
124
|
+
export const SET_MAC = `${LOOKUP}
|
|
125
|
+
on run argv
|
|
126
|
+
set vm to my lookup(item 1 of argv)
|
|
127
|
+
tell application "UTM"
|
|
128
|
+
set cfg to configuration of vm
|
|
129
|
+
set nics to network interfaces of cfg
|
|
130
|
+
if (count of nics) > 0 then
|
|
131
|
+
set nic to item 1 of nics
|
|
132
|
+
set address of nic to (item 2 of argv)
|
|
133
|
+
set network interfaces of cfg to {nic} & rest of nics
|
|
134
|
+
update configuration of vm with cfg
|
|
135
|
+
end if
|
|
136
|
+
end tell
|
|
137
|
+
end run
|
|
138
|
+
`;
|
|
139
|
+
|
|
140
|
+
// memoryMib, cpuCount and diskMib are "" to leave a setting alone. diskMib applies to the first
|
|
141
|
+
// fixed drive. dropRemovable is "1" to remove every removable drive.
|
|
142
|
+
export const CONFIGURE = `${LOOKUP}
|
|
143
|
+
on run argv
|
|
144
|
+
set {vmRef, memoryMib, cpuCount, diskMib, dropRemovable} to argv
|
|
145
|
+
set vm to my lookup(vmRef)
|
|
146
|
+
tell application "UTM"
|
|
147
|
+
set cfg to configuration of vm
|
|
148
|
+
if memoryMib is not "" then set memory of cfg to (memoryMib as integer)
|
|
149
|
+
if cpuCount is not "" then set cpu cores of cfg to (cpuCount as integer)
|
|
150
|
+
if diskMib is not "" or dropRemovable is "1" then
|
|
151
|
+
set kept to {}
|
|
152
|
+
set resized to false
|
|
153
|
+
repeat with d in drives of cfg
|
|
154
|
+
if (removable of d) then
|
|
155
|
+
if dropRemovable is not "1" then set end of kept to {id:(id of d)}
|
|
156
|
+
else if diskMib is not "" and not resized then
|
|
157
|
+
set end of kept to {id:(id of d), guest size:(diskMib as integer)}
|
|
158
|
+
set resized to true
|
|
159
|
+
else
|
|
160
|
+
set end of kept to {id:(id of d)}
|
|
161
|
+
end if
|
|
162
|
+
end repeat
|
|
163
|
+
set drives of cfg to kept
|
|
164
|
+
end if
|
|
165
|
+
update configuration of vm with cfg
|
|
166
|
+
end tell
|
|
167
|
+
end run
|
|
168
|
+
`;
|
|
169
|
+
|
|
170
|
+
export const SET_SHARE = `${LOOKUP}
|
|
171
|
+
on run argv
|
|
172
|
+
set {vmRef, shareMode, sharePath} to argv
|
|
173
|
+
set vm to my lookup(vmRef)
|
|
174
|
+
tell application "UTM"
|
|
175
|
+
set cfg to configuration of vm
|
|
176
|
+
if shareMode is "VirtFS" then
|
|
177
|
+
set directory share mode of cfg to VirtFS
|
|
178
|
+
else
|
|
179
|
+
set directory share mode of cfg to WebDAV
|
|
180
|
+
end if
|
|
181
|
+
update configuration of vm with cfg
|
|
182
|
+
update registry of vm with {{POSIX file sharePath}}
|
|
183
|
+
end tell
|
|
184
|
+
end run
|
|
185
|
+
`;
|
|
186
|
+
|
|
187
|
+
export const CAPTURE = `${LOOKUP}
|
|
188
|
+
on run argv
|
|
189
|
+
set vm to my lookup(item 1 of argv)
|
|
190
|
+
tell application "UTM" to capture screen vm to (POSIX file (item 2 of argv))
|
|
191
|
+
end run
|
|
192
|
+
`;
|
|
193
|
+
|
|
194
|
+
// argv: vm, text, then any of shift / control / option / command.
|
|
195
|
+
export const KEYSTROKE = `${LOOKUP}
|
|
196
|
+
on run argv
|
|
197
|
+
set vm to my lookup(item 1 of argv)
|
|
198
|
+
tell application "UTM"
|
|
199
|
+
set mods to {}
|
|
200
|
+
repeat with m in (rest of rest of argv)
|
|
201
|
+
set m to m as text
|
|
202
|
+
if m is "shift" then set end of mods to shift
|
|
203
|
+
if m is "control" then set end of mods to control
|
|
204
|
+
if m is "option" then set end of mods to option
|
|
205
|
+
if m is "command" then set end of mods to command
|
|
206
|
+
end repeat
|
|
207
|
+
if (count of mods) > 0 then
|
|
208
|
+
input keystroke vm text (item 2 of argv) with modifiers mods
|
|
209
|
+
else
|
|
210
|
+
input keystroke vm text (item 2 of argv)
|
|
211
|
+
end if
|
|
212
|
+
end tell
|
|
213
|
+
end run
|
|
214
|
+
`;
|
|
215
|
+
|
|
216
|
+
// argv: vm, then PC AT set 1 scan codes as decimal integers.
|
|
217
|
+
export const SCAN_CODES = `${LOOKUP}
|
|
218
|
+
on run argv
|
|
219
|
+
set vm to my lookup(item 1 of argv)
|
|
220
|
+
set codeList to {}
|
|
221
|
+
repeat with c in (rest of argv)
|
|
222
|
+
set end of codeList to (c as integer)
|
|
223
|
+
end repeat
|
|
224
|
+
tell application "UTM" to input scan code vm codes codeList
|
|
225
|
+
end run
|
|
226
|
+
`;
|
|
227
|
+
|
|
228
|
+
export const CLICK = `${LOOKUP}
|
|
229
|
+
on run argv
|
|
230
|
+
set vm to my lookup(item 1 of argv)
|
|
231
|
+
set x to (item 2 of argv) as integer
|
|
232
|
+
set y to (item 3 of argv) as integer
|
|
233
|
+
tell application "UTM"
|
|
234
|
+
if (item 4 of argv) is "right" then
|
|
235
|
+
input mouse click vm at {x, y} with mouse button right
|
|
236
|
+
else
|
|
237
|
+
input mouse click vm at {x, y}
|
|
238
|
+
end if
|
|
239
|
+
end tell
|
|
240
|
+
end run
|
|
241
|
+
`;
|
|
242
|
+
|
|
243
|
+
export const UTM_VERSION = `
|
|
244
|
+
on run argv
|
|
245
|
+
tell application "UTM" to return UTM version
|
|
246
|
+
end run
|
|
247
|
+
`;
|
package/src/exec.mjs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Every local command the broker runs goes through here. Programs are named by absolute path and
|
|
2
|
+
// arguments are passed as an array, so nothing is ever interpreted by a shell.
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
5
|
+
|
|
6
|
+
export const UTMCTL = process.env.UTMCTL ?? "/Applications/UTM.app/Contents/MacOS/utmctl";
|
|
7
|
+
export const OSASCRIPT = "/usr/bin/osascript";
|
|
8
|
+
export const HDIUTIL = "/usr/bin/hdiutil";
|
|
9
|
+
|
|
10
|
+
const MAX_OUTPUT = 200_000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Run a program to completion. Never rejects for a non-zero exit: the caller reads `code`.
|
|
14
|
+
* `stdinFile` and `stdoutFile` stream a host file through the child without buffering it.
|
|
15
|
+
*/
|
|
16
|
+
export function run(file, args, { input, stdinFile, stdoutFile, timeoutMs = 60_000 } = {}) {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const child = spawn(file, args, { stdio: ["pipe", "pipe", "pipe"] });
|
|
19
|
+
let stdout = "";
|
|
20
|
+
let stderr = "";
|
|
21
|
+
let truncated = false;
|
|
22
|
+
let timedOut = false;
|
|
23
|
+
const timer = setTimeout(() => {
|
|
24
|
+
timedOut = true;
|
|
25
|
+
child.kill("SIGKILL");
|
|
26
|
+
}, timeoutMs);
|
|
27
|
+
|
|
28
|
+
const append = (current, chunk) => {
|
|
29
|
+
if (current.length >= MAX_OUTPUT) {
|
|
30
|
+
truncated = true;
|
|
31
|
+
return current;
|
|
32
|
+
}
|
|
33
|
+
return current + chunk.toString("utf8");
|
|
34
|
+
};
|
|
35
|
+
if (stdoutFile) child.stdout.pipe(createWriteStream(stdoutFile));
|
|
36
|
+
else child.stdout.on("data", (chunk) => { stdout = append(stdout, chunk); });
|
|
37
|
+
child.stderr.on("data", (chunk) => { stderr = append(stderr, chunk); });
|
|
38
|
+
|
|
39
|
+
child.stdin.on("error", () => {});
|
|
40
|
+
if (stdinFile) createReadStream(stdinFile).pipe(child.stdin);
|
|
41
|
+
else child.stdin.end(input ?? "");
|
|
42
|
+
|
|
43
|
+
child.on("error", (error) => { clearTimeout(timer); reject(error); });
|
|
44
|
+
child.on("close", (code) => {
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
resolve({ code: code ?? -1, stdout, stderr, truncated, timedOut });
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// utmctl writes this line to stderr during successful calls; it carries no information.
|
|
52
|
+
const UTMCTL_NOISE = /^Error from event: .*OSStatus error -10004\.\)\s*$/;
|
|
53
|
+
|
|
54
|
+
export async function utmctl(args, options) {
|
|
55
|
+
const result = await run(UTMCTL, args, options);
|
|
56
|
+
result.stderr = result.stderr.split("\n").filter((line) => !UTMCTL_NOISE.test(line)).join("\n").trim();
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** utmctl for calls that must succeed; the error carries what UTM said. */
|
|
61
|
+
export async function utmctlOk(args, options) {
|
|
62
|
+
const result = await utmctl(args, options);
|
|
63
|
+
if (result.timedOut) throw new Error(`utmctl ${args[0]} timed out.`);
|
|
64
|
+
if (result.code !== 0) {
|
|
65
|
+
const said = result.stderr.split("\n").filter((line) => !/^Error from event:/.test(line)).join(" ").trim();
|
|
66
|
+
throw new Error(said || `utmctl ${args[0]} failed with exit code ${result.code}.`);
|
|
67
|
+
}
|
|
68
|
+
return result.stdout;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Run one of the broker's own fixed AppleScripts. Values travel as arguments, never as script text. */
|
|
72
|
+
export async function osascript(script, args = [], { timeoutMs = 120_000 } = {}) {
|
|
73
|
+
const result = await run(OSASCRIPT, ["-", ...args.map(String)], { input: script, timeoutMs });
|
|
74
|
+
if (result.timedOut) throw new Error("UTM did not answer in time.");
|
|
75
|
+
if (result.code !== 0) {
|
|
76
|
+
// "12:34: execution error: UTM got an error: <message> (-2700)" -> "<message>"
|
|
77
|
+
const message = result.stderr.trim().replace(/^.*execution error: (UTM got an error: )?/s, "").replace(/ \(-?\d+\)\s*$/, "");
|
|
78
|
+
throw new Error(message || "UTM rejected the request.");
|
|
79
|
+
}
|
|
80
|
+
return result.stdout.replace(/\n$/, "");
|
|
81
|
+
}
|
package/src/feedback.mjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Agent feedback about this server, through mcp-feedback. A copy running on a user's Mac has no store
|
|
2
|
+
// its maintainer could read, so reports go to an mcp-feedback ingest hub, and only when the person
|
|
3
|
+
// running the server names one in UTM_MCP_FEEDBACK_ENDPOINT. The repository names no hub: without the
|
|
4
|
+
// variable there is no submit_feedback tool and nothing is sent anywhere. Every report is shown to the
|
|
5
|
+
// user and sent only after they confirm it.
|
|
6
|
+
import { readFile } from "node:fs/promises";
|
|
7
|
+
import { arch, release } from "node:os";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
import { feedbackConfirmInstructions, registerFeedback } from "mcp-feedback";
|
|
10
|
+
import { feedbackDisabled, remoteStore } from "mcp-feedback/remote";
|
|
11
|
+
import { UTMCTL } from "./exec.mjs";
|
|
12
|
+
import { audit, HOME, isManaged } from "./policy.mjs";
|
|
13
|
+
import * as utm from "./utm.mjs";
|
|
14
|
+
|
|
15
|
+
const ENDPOINT = process.env.UTM_MCP_FEEDBACK_ENDPOINT?.trim();
|
|
16
|
+
const enabled = Boolean(ENDPOINT) && !feedbackDisabled();
|
|
17
|
+
|
|
18
|
+
/** Appended to the server's instructions, or empty when feedback is off. */
|
|
19
|
+
export const feedbackGuidance = enabled
|
|
20
|
+
? ` When filing feedback, put the UUID of the VM involved in context.references.vmId if you have one. ${feedbackConfirmInstructions}`
|
|
21
|
+
: "";
|
|
22
|
+
|
|
23
|
+
const log = (error) => console.error("utm-mcp feedback:", error instanceof Error ? error.message : error);
|
|
24
|
+
|
|
25
|
+
/** Registers submit_feedback on the server when a hub is configured; otherwise does nothing. */
|
|
26
|
+
export function registerUtmFeedback(server, version) {
|
|
27
|
+
if (!enabled) return;
|
|
28
|
+
let remote;
|
|
29
|
+
try {
|
|
30
|
+
remote = remoteStore({ endpoint: ENDPOINT, project: "utm", stateDir: join(HOME, "feedback"), onError: log });
|
|
31
|
+
} catch (error) {
|
|
32
|
+
// A mistyped endpoint turns feedback off; it must not keep the VM tools from starting.
|
|
33
|
+
log(error);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
// Recorded in the audit log like every other tool call, by id, kind and title.
|
|
37
|
+
const store = {
|
|
38
|
+
async insert(record) {
|
|
39
|
+
const summary = { id: record.id, kind: record.kind, title: record.title };
|
|
40
|
+
try {
|
|
41
|
+
const receipt = await remote.insert(record);
|
|
42
|
+
audit("submit_feedback", summary, { ok: true, delivery: receipt.delivery });
|
|
43
|
+
return receipt;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
audit("submit_feedback", summary, { ok: false, error: error.message });
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
registerFeedback(server, {
|
|
51
|
+
store,
|
|
52
|
+
serverInfo: { name: "utm", version },
|
|
53
|
+
requireConfirmation: true,
|
|
54
|
+
destination: `the feedback service at ${new URL(ENDPOINT).host}`,
|
|
55
|
+
contextFields: { vmId: "UUID of the VM involved, from list_vms or get_vm" },
|
|
56
|
+
attachContext: (record) => situation(record, version),
|
|
57
|
+
onError: log,
|
|
58
|
+
});
|
|
59
|
+
// Renews this machine's token and sends reports queued while the hub was unreachable, without
|
|
60
|
+
// holding up startup. It contacts the hub only if this machine has filed feedback before.
|
|
61
|
+
void remote.start();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* What the server knows that the agent might not, stored with the report as `server_context`. It
|
|
66
|
+
* leaves the machine, so it holds versions and states only: no VM names, paths or guest content.
|
|
67
|
+
*/
|
|
68
|
+
async function situation(record, version) {
|
|
69
|
+
const context = { version, darwin: release(), arch: arch(), node: process.version, utm: await utmVersion() };
|
|
70
|
+
const vmId = record.context.references?.vmId;
|
|
71
|
+
if (vmId) context.vm = await within(2000, vmState(vmId), { lookup_timed_out: true });
|
|
72
|
+
return context;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function utmVersion() {
|
|
76
|
+
try {
|
|
77
|
+
const plist = await readFile(join(dirname(UTMCTL), "..", "Info.plist"), "utf8");
|
|
78
|
+
return /<key>CFBundleShortVersionString<\/key>\s*<string>([^<]+)<\/string>/.exec(plist)?.[1] ?? null;
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function vmState(ref) {
|
|
85
|
+
try {
|
|
86
|
+
const found = await utm.resolveVm(ref);
|
|
87
|
+
return { status: found.status, backend: found.backend, managed: isManaged(found) };
|
|
88
|
+
} catch {
|
|
89
|
+
return { found: false };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function within(ms, promise, fallback) {
|
|
94
|
+
let timer;
|
|
95
|
+
const timedOut = new Promise((resolve) => {
|
|
96
|
+
timer = setTimeout(() => resolve(fallback), ms);
|
|
97
|
+
});
|
|
98
|
+
try {
|
|
99
|
+
return await Promise.race([promise, timedOut]);
|
|
100
|
+
} finally {
|
|
101
|
+
clearTimeout(timer);
|
|
102
|
+
}
|
|
103
|
+
}
|