@tmhs/screencast-mcp 0.48.1 → 0.49.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.
Files changed (50) hide show
  1. package/README.md +12 -9
  2. package/dist/cli.js +8 -7
  3. package/dist/context.js +33 -0
  4. package/dist/index.js +42 -27
  5. package/dist/resources.js +26 -0
  6. package/dist/server.js +2 -0
  7. package/dist/setup.js +7 -6
  8. package/dist/tools/addMarker.js +2 -2
  9. package/dist/tools/callout.js +6 -7
  10. package/dist/tools/contactSheet.js +3 -2
  11. package/dist/tools/deleteRecording.js +119 -0
  12. package/dist/tools/indexRecording.js +6 -6
  13. package/dist/tools/pauseRecording.js +3 -3
  14. package/dist/tools/redactRegion.js +6 -7
  15. package/dist/tools/sampleFrames.js +10 -6
  16. package/dist/tools/startRecording.js +11 -5
  17. package/dist/tools/stepGuide.js +12 -8
  18. package/dist/tools/stopRecording.js +7 -3
  19. package/dist/tools/waitForChange.js +13 -45
  20. package/dist/utils/audioDevices.js +2 -2
  21. package/dist/utils/callout.js +6 -2
  22. package/dist/utils/capabilities.js +2 -1
  23. package/dist/utils/consent.js +2 -2
  24. package/dist/utils/errors.js +4 -4
  25. package/dist/utils/ffmpeg.js +14 -1
  26. package/dist/utils/images.js +4 -4
  27. package/dist/utils/jobs.js +2 -9
  28. package/dist/utils/lifecycle.js +13 -31
  29. package/dist/utils/media.js +29 -8
  30. package/dist/utils/monitors.js +2 -2
  31. package/dist/utils/narrate.js +3 -3
  32. package/dist/utils/ocr.js +3 -3
  33. package/dist/utils/ocrScan.js +33 -19
  34. package/dist/utils/paths.js +3 -0
  35. package/dist/utils/pause.js +3 -3
  36. package/dist/utils/pool.js +29 -0
  37. package/dist/utils/recorder.js +7 -6
  38. package/dist/utils/recordingFiles.js +32 -0
  39. package/dist/utils/replay.js +21 -34
  40. package/dist/utils/retention.js +14 -14
  41. package/dist/utils/roots.js +2 -1
  42. package/dist/utils/sessions.js +58 -14
  43. package/dist/utils/targetList.js +2 -1
  44. package/dist/utils/toolSpec.js +6 -0
  45. package/dist/utils/toolsets.js +1 -0
  46. package/dist/utils/waitLoop.js +50 -0
  47. package/dist/utils/wgc.js +27 -0
  48. package/dist/utils/windows.js +2 -2
  49. package/dist/wgcRecorder.js +7 -10
  50. package/package.json +2 -2
@@ -1,7 +1,7 @@
1
1
  import { spawnSync } from "node:child_process";
2
2
  import { readFileSync, writeFileSync, renameSync, existsSync, copyFileSync, openSync, closeSync, unlinkSync, statSync, linkSync, } from "node:fs";
3
3
  import { basename } from "node:path";
4
- import { ScreencastError } from "./errors.js";
4
+ import { errorMessage, ScreencastError } from "./errors.js";
5
5
  import { WGC_WINDOW_CLOSED_EXIT } from "./wgc.js";
6
6
  export const MAX_FINISHED_RECORDS = 100;
7
7
  export function isLive(status) {
@@ -23,6 +23,20 @@ export function isForeignLiveRecording(record, currentServerPid, ownerAlive) {
23
23
  record.serverPid !== currentServerPid &&
24
24
  ownerAlive);
25
25
  }
26
+ export function foreignStopRefusal(record, currentServerPid, ownerIsAlive) {
27
+ if (record.backend === "wgc" || !isForeignLiveRecording(record, currentServerPid, ownerIsAlive))
28
+ return null;
29
+ return (`Recording ${record.id} belongs to another running screencast-mcp instance (pid ${record.serverPid}). ` +
30
+ "Stop it from the client connected to that instance; closing that instance also finalizes it.");
31
+ }
32
+ export function ownerAlive(record, cache = new Map()) {
33
+ const pid = record.serverPid;
34
+ if (pid === undefined || pid === process.pid || !isAlive(pid))
35
+ return false;
36
+ if (!cache.has(pid))
37
+ cache.set(pid, processInfo(pid));
38
+ return isSameServer(record, cache.get(pid) ?? null);
39
+ }
26
40
  export function serverIdentity() {
27
41
  return {
28
42
  serverPid: process.pid,
@@ -82,6 +96,19 @@ export function isRecorderProcess(pid, image) {
82
96
  return false;
83
97
  return tasklistHasImage(res.stdout, image);
84
98
  }
99
+ export function isSameRecorder(record, running) {
100
+ if (!running)
101
+ return true;
102
+ if (running.image.toLowerCase() !== expectedImage(record).toLowerCase())
103
+ return false;
104
+ const ref = record.pidSpawnedAt;
105
+ return !(typeof ref === "number" && Number.isFinite(ref) && running.startMs > ref + OWNER_START_SLACK_MS);
106
+ }
107
+ export function isOwnRecorder(record) {
108
+ if (record.pid === null || !isRecorderProcess(record.pid, expectedImage(record)))
109
+ return false;
110
+ return record.pidSpawnedAt === undefined || isSameRecorder(record, processInfo(record.pid));
111
+ }
85
112
  export function classifyUnrequestedExit(opts) {
86
113
  const { code, backend, maxDurationSec, elapsedMs } = opts;
87
114
  if (backend === "wgc" && code === WGC_WINDOW_CLOSED_EXIT)
@@ -182,7 +209,7 @@ export function withRegistryLock(registry, fn, waitMs = LOCK_WAIT_MS) {
182
209
  const code = err.code;
183
210
  const busy = code === "EEXIST" || code === "EPERM" || code === "EACCES" || code === "EBUSY";
184
211
  if (!busy) {
185
- throw new ScreencastError(`Could not lock the session registry (${lock}): ${err.message}`);
212
+ throw new ScreencastError(`Could not lock the session registry (${lock}): ${errorMessage(err)}`);
186
213
  }
187
214
  if (code === "EEXIST") {
188
215
  let seen;
@@ -232,6 +259,7 @@ export class SessionStore {
232
259
  lockWaitMs;
233
260
  records = new Map();
234
261
  pending = new Map();
262
+ removed = new Set();
235
263
  seen = null;
236
264
  children = new Map();
237
265
  stopping = new Set();
@@ -278,6 +306,8 @@ export class SessionStore {
278
306
  if (base)
279
307
  disk.set(id, { ...base, ...patch });
280
308
  }
309
+ for (const id of this.removed)
310
+ disk.delete(id);
281
311
  return disk;
282
312
  }
283
313
  refresh() {
@@ -305,7 +335,7 @@ export class SessionStore {
305
335
  disk = withRegistryLock(this.path, read, this.lockWaitMs);
306
336
  }
307
337
  catch (err) {
308
- process.stderr.write(`${err.message} Reading the session registry without the lock.\n`);
338
+ process.stderr.write(`${errorMessage(err)} Reading the session registry without the lock.\n`);
309
339
  disk = read();
310
340
  }
311
341
  this.records = new Map(pruneRecords([...this.overlay(disk).values()]).map((r) => [r.id, r]));
@@ -319,6 +349,7 @@ export class SessionStore {
319
349
  this.seen = this.signature();
320
350
  this.records = new Map(pruned.map((r) => [r.id, r]));
321
351
  this.pending.clear();
352
+ this.removed.clear();
322
353
  }, this.lockWaitMs);
323
354
  for (const cb of this.listeners) {
324
355
  try {
@@ -340,6 +371,27 @@ export class SessionStore {
340
371
  throw err;
341
372
  }
342
373
  }
374
+ remove(ids) {
375
+ const before = ids.flatMap((id) => {
376
+ const r = this.records.get(id);
377
+ return r ? [r] : [];
378
+ });
379
+ for (const id of ids) {
380
+ this.removed.add(id);
381
+ this.pending.delete(id);
382
+ this.records.delete(id);
383
+ }
384
+ try {
385
+ this.persist();
386
+ }
387
+ catch (err) {
388
+ for (const id of ids)
389
+ this.removed.delete(id);
390
+ for (const r of before)
391
+ this.records.set(r.id, r);
392
+ throw err;
393
+ }
394
+ }
343
395
  get(id) {
344
396
  this.refresh();
345
397
  return this.records.get(id);
@@ -367,7 +419,7 @@ export class SessionStore {
367
419
  return this.update(id, patch);
368
420
  }
369
421
  catch (err) {
370
- process.stderr.write(`Could not save session ${id}: ${err instanceof Error ? err.message : String(err)}\n`);
422
+ process.stderr.write(`Could not save session ${id}: ${errorMessage(err)}\n`);
371
423
  return undefined;
372
424
  }
373
425
  }
@@ -411,24 +463,16 @@ export class SessionStore {
411
463
  this.refresh();
412
464
  const reaped = [];
413
465
  const owners = new Map();
414
- const ownerAlive = (record) => {
415
- const pid = record.serverPid;
416
- if (pid === undefined || pid === process.pid || !isAlive(pid))
417
- return false;
418
- if (!owners.has(pid))
419
- owners.set(pid, processInfo(pid));
420
- return isSameServer(record, owners.get(pid) ?? null);
421
- };
422
466
  for (const record of this.records.values()) {
423
467
  if (!isLive(record.status))
424
468
  continue;
425
- if (isForeignLiveRecording(record, process.pid, ownerAlive(record))) {
469
+ if (isForeignLiveRecording(record, process.pid, ownerAlive(record, owners))) {
426
470
  continue;
427
471
  }
428
472
  const alive = isAlive(record.pid);
429
473
  const verdict = classifyOrphan(record, alive);
430
474
  if (verdict.reaped && record.pid !== null) {
431
- if (isRecorderProcess(record.pid, expectedImage(record)))
475
+ if (isOwnRecorder(record))
432
476
  killPid(record.pid);
433
477
  reaped.push(record.id);
434
478
  }
@@ -1,5 +1,6 @@
1
1
  import { virtualDesktopBounds } from "./targets.js";
2
2
  import { parseWindowSelector, selectWindow } from "./windows.js";
3
+ import { errorMessage } from "./errors.js";
3
4
  function windowTarget(w) {
4
5
  return w.hwnd !== undefined ? `window:hwnd=${w.hwnd}` : `window:${w.title}`;
5
6
  }
@@ -13,7 +14,7 @@ function resolves(query, w, allowCloaked) {
13
14
  return { ok: true };
14
15
  }
15
16
  catch (err) {
16
- return { ok: false, reason: firstSentence(err.message) };
17
+ return { ok: false, reason: firstSentence(errorMessage(err)) };
17
18
  }
18
19
  }
19
20
  export function buildTargetList(monitors, query, opts) {
@@ -17,6 +17,12 @@ export const STOPS_SESSION = {
17
17
  idempotentHint: true,
18
18
  openWorldHint: false,
19
19
  };
20
+ export const DELETES_FILE = {
21
+ readOnlyHint: false,
22
+ destructiveHint: true,
23
+ idempotentHint: true,
24
+ openWorldHint: false,
25
+ };
20
26
  export function looseOutput(shape) {
21
27
  return z.looseObject(shape);
22
28
  }
@@ -7,6 +7,7 @@ export const TOOLSETS = {
7
7
  "resume_recording",
8
8
  "save_replay",
9
9
  "list_sessions",
10
+ "delete_recording",
10
11
  "get_session",
11
12
  "read_events",
12
13
  "add_marker",
@@ -0,0 +1,50 @@
1
+ import { diffFrames, SettleTracker } from "./diff.js";
2
+ const defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
3
+ export async function waitLoop(o) {
4
+ const now = o.now ?? Date.now;
5
+ const sleep = o.sleep ?? defaultSleep;
6
+ const startedAt = now();
7
+ const baseline = await o.grab();
8
+ const settle = new SettleTracker(baseline, o.width, o.height, o.diff);
9
+ let lastChangeAt = startedAt;
10
+ let everChanged = false;
11
+ let regions = [];
12
+ let outcome = "timeout";
13
+ while (now() - startedAt < o.timeoutMs) {
14
+ if (o.signal.aborted) {
15
+ outcome = "cancelled";
16
+ break;
17
+ }
18
+ await sleep(o.intervalMs);
19
+ const current = await o.grab().catch((error) => {
20
+ if (o.signal.aborted)
21
+ return null;
22
+ throw error;
23
+ });
24
+ if (!current) {
25
+ outcome = "cancelled";
26
+ break;
27
+ }
28
+ if (o.mode === "change") {
29
+ const d = diffFrames(baseline, current, o.width, o.height, o.diff);
30
+ if (d.regions.length > 0) {
31
+ regions = d.regions;
32
+ outcome = "changed";
33
+ break;
34
+ }
35
+ }
36
+ else {
37
+ const changed = settle.push(current);
38
+ if (changed.length > 0) {
39
+ everChanged = true;
40
+ lastChangeAt = now();
41
+ regions = changed;
42
+ }
43
+ else if (now() - lastChangeAt >= o.settleMs) {
44
+ outcome = "settled";
45
+ break;
46
+ }
47
+ }
48
+ }
49
+ return { outcome, regions, everChanged, startedAt };
50
+ }
package/dist/utils/wgc.js CHANGED
@@ -20,6 +20,33 @@ export function parseRawSize(stderr) {
20
20
  export function yuv420FrameBytes(width, height) {
21
21
  return (width * height * 3) / 2;
22
22
  }
23
+ export class FrameAssembler {
24
+ parts = [];
25
+ length = 0;
26
+ frameBytes = 0;
27
+ push(chunk) {
28
+ if (!chunk.length)
29
+ return;
30
+ this.parts.push(chunk);
31
+ this.length += chunk.length;
32
+ }
33
+ get buffered() {
34
+ return this.length;
35
+ }
36
+ take() {
37
+ const fb = this.frameBytes;
38
+ if (fb <= 0 || this.length < fb)
39
+ return null;
40
+ const frames = Math.floor(this.length / fb);
41
+ const all = this.parts.length === 1 && this.parts[0] ? this.parts[0] : Buffer.concat(this.parts, this.length);
42
+ const end = frames * fb;
43
+ const frame = all.subarray(end - fb, end);
44
+ const rest = this.length - end;
45
+ this.parts = rest ? [Buffer.from(all.subarray(end))] : [];
46
+ this.length = rest;
47
+ return frame;
48
+ }
49
+ }
23
50
  export function buildWgcEncodeArgs(width, height, cfg) {
24
51
  return [
25
52
  "-hide_banner", "-nostats", "-loglevel", "error", "-y",
@@ -1,4 +1,4 @@
1
- import { ScreencastError } from "./errors.js";
1
+ import { errorMessage, ScreencastError } from "./errors.js";
2
2
  import { runPowerShell } from "./monitors.js";
3
3
  export const MAX_CANDIDATES = 5;
4
4
  export function parseWindowSelector(wanted) {
@@ -180,7 +180,7 @@ export async function getRawWindows() {
180
180
  parsed = JSON.parse(stdout);
181
181
  }
182
182
  catch (err) {
183
- throw new ScreencastError(`Could not parse window geometry: ${err.message}`);
183
+ throw new ScreencastError(`Could not parse window geometry: ${errorMessage(err)}`);
184
184
  }
185
185
  const obj = parsed;
186
186
  const windows = Array.isArray(obj.windows) ? obj.windows : obj.windows ? [obj.windows] : [];
@@ -1,6 +1,6 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { existsSync, rmSync } from "node:fs";
3
- import { buildWgcEncodeArgs, buildWgcGrabArgs, framesDue, parseRawSize, nudgeScript, startBanner, yuv420FrameBytes, wgcStopFile, WGC_WINDOW_CLOSED_EXIT, } from "./utils/wgc.js";
3
+ import { buildWgcEncodeArgs, buildWgcGrabArgs, FrameAssembler, framesDue, parseRawSize, nudgeScript, startBanner, yuv420FrameBytes, wgcStopFile, WGC_WINDOW_CLOSED_EXIT, } from "./utils/wgc.js";
4
4
  const FIRST_FRAME_TIMEOUT_MS = 10_000;
5
5
  const MAX_QUEUED_FRAMES = 4;
6
6
  const cfg = JSON.parse(process.argv[2] ?? "{}");
@@ -13,7 +13,7 @@ const nudge = spawn("powershell", ["-NoProfile", "-NonInteractive", "-Command",
13
13
  let grabErr = "";
14
14
  let size = null;
15
15
  let frameBytes = 0;
16
- let carry = Buffer.alloc(0);
16
+ const frames = new FrameAssembler();
17
17
  let latest = null;
18
18
  let encoder = null;
19
19
  let startedAt = 0;
@@ -41,13 +41,9 @@ function exitWith(code) {
41
41
  function drain() {
42
42
  if (!size || finishing)
43
43
  return;
44
- let off = 0;
45
- while (carry.length - off >= frameBytes) {
46
- latest = Buffer.from(carry.subarray(off, off + frameBytes));
47
- off += frameBytes;
48
- }
49
- if (off)
50
- carry = Buffer.from(carry.subarray(off));
44
+ const frame = frames.take();
45
+ if (frame)
46
+ latest = frame;
51
47
  if (latest && !encoder)
52
48
  begin();
53
49
  }
@@ -58,11 +54,12 @@ grab.stderr.on("data", (d) => {
58
54
  size = parseRawSize(grabErr);
59
55
  if (size) {
60
56
  frameBytes = yuv420FrameBytes(size.width, size.height);
57
+ frames.frameBytes = frameBytes;
61
58
  drain();
62
59
  }
63
60
  });
64
61
  grab.stdout.on("data", (chunk) => {
65
- carry = carry.length ? Buffer.concat([carry, chunk]) : chunk;
62
+ frames.push(chunk);
66
63
  drain();
67
64
  });
68
65
  function begin() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmhs/screencast-mcp",
3
- "version": "0.48.1",
3
+ "version": "0.49.0",
4
4
  "description": "Let AI agents record the Windows screen, watch it as frames, and edit it with ffmpeg",
5
5
  "mcpName": "io.github.TMHSDigital/screencast-mcp",
6
6
  "type": "module",
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "scripts": {
21
21
  "build": "tsc",
22
- "typecheck": "tsc --noEmit && tsc -p src/__tests__",
22
+ "typecheck": "tsc --noEmit && tsc -p src/__tests__ && tsc -p evals/tsconfig.json",
23
23
  "lint": "eslint .",
24
24
  "dev": "tsx watch src/index.ts",
25
25
  "start": "node dist/index.js",