universal-screenshot-mcp 1.1.1 → 1.1.2

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.
@@ -2,6 +2,7 @@
2
2
  // Configuration Constants
3
3
  // ============================================================================
4
4
  import { join } from 'path';
5
+ import { homedir, tmpdir } from 'os';
5
6
  /**
6
7
  * Maximum number of concurrent Puppeteer browser instances.
7
8
  * Each instance spawns a Chromium process consuming 100-500MB of memory.
@@ -17,9 +18,9 @@ export const ALLOW_LOCAL = process.env.ALLOW_LOCAL === 'true';
17
18
  /**
18
19
  * Resolved allowed output directories for screenshots.
19
20
  * Only these directories (and their subdirectories) are permitted for file output.
20
- * Uses process.env.HOME at module load time (pure computation, no I/O side effects).
21
+ * os.homedir() works cross-platform (USERPROFILE on Windows, HOME on Unix).
21
22
  */
22
- export const homeDir = process.env.HOME || '/tmp';
23
+ export const homeDir = process.env.HOME || homedir();
23
24
  /**
24
25
  * Resolved default output directory for screenshots.
25
26
  * Priority: 1) SCREENSHOT_OUTPUT_DIR env var 2) ~/Documents/screenshots 3) ~/Desktop/Screenshots
@@ -30,7 +31,7 @@ export const configuredOutDir = process.env.SCREENSHOT_OUTPUT_DIR
30
31
  export const ALLOWED_OUTPUT_DIRS = [
31
32
  join(homeDir, 'Desktop', 'Screenshots'), // ~/Desktop/Screenshots (original default)
32
33
  configuredOutDir, // Configured default output directory
33
- '/tmp', // System temp directory
34
+ tmpdir(), // System temp directory (cross-platform)
34
35
  join(homeDir, 'Downloads'), // ~/Downloads
35
36
  join(homeDir, 'Documents'), // ~/Documents
36
37
  ];
@@ -8,7 +8,7 @@ import { ok, err, timestamp, ensureDir } from '../utils/helpers.js';
8
8
  // Semaphore protects expensive Puppeteer operations
9
9
  export function registerTakeScreenshot(server) {
10
10
  server.registerTool('take_screenshot', {
11
- description: 'Capture web page or element via headless browser. Saves to ~/Desktop/Screenshots by default.',
11
+ description: 'Capture web page or element via headless browser. Saves to ~/Documents/screenshots by default (configurable via SCREENSHOT_OUTPUT_DIR env var).',
12
12
  inputSchema: {
13
13
  url: z.string().describe('URL to capture'),
14
14
  width: z.number().min(1).max(3840).optional().describe('Viewport width'),
@@ -8,7 +8,7 @@ import { getScreenshotProvider } from '../utils/screenshot-provider.js';
8
8
  // No semaphore — system screenshot tools are lightweight (< 100ms, < 10MB)
9
9
  export function registerTakeSystemScreenshot(server) {
10
10
  server.registerTool('take_system_screenshot', {
11
- description: 'Capture desktop, window, or region screenshot. Cross-platform: macOS (screencapture), Linux (maim/scrot/gnome-screenshot/etc.), Windows (PowerShell+.NET). Saves to ~/Desktop/Screenshots by default. For window mode, provide windowName (app name like "Safari") or windowId.',
11
+ description: 'Capture desktop, window, or region screenshot. Cross-platform: macOS (screencapture), Linux (maim/scrot/gnome-screenshot/etc.), Windows (PowerShell+.NET). Saves to ~/Documents/screenshots by default (configurable via SCREENSHOT_OUTPUT_DIR env var). For window mode, provide windowName (app name like "Safari") or windowId.',
12
12
  inputSchema: {
13
13
  mode: z
14
14
  .enum(['fullscreen', 'window', 'region'])
@@ -27,7 +27,7 @@ export function registerTakeSystemScreenshot(server) {
27
27
  })
28
28
  .optional()
29
29
  .describe('Region {x,y,width,height}'),
30
- display: z.number().min(1).optional().describe('Display number'),
30
+ display: z.number().int().min(1).optional().describe('Display number'),
31
31
  includeCursor: z.boolean().optional().describe('Include cursor'),
32
32
  format: z.enum(['png', 'jpg']).optional().describe('Image format (png or jpg)'),
33
33
  delay: z.number().min(0).max(10).optional().describe('Delay seconds'),
@@ -16,6 +16,7 @@ export class LinuxProvider {
16
16
  }
17
17
  async captureFullscreen(opts) {
18
18
  const backend = await this.getBackend();
19
+ this.assertSupportedOptions(opts);
19
20
  if (opts.delay && opts.delay > 0)
20
21
  await sleep(opts.delay);
21
22
  switch (backend) {
@@ -41,6 +42,7 @@ export class LinuxProvider {
41
42
  }
42
43
  async captureWindow(opts) {
43
44
  const backend = await this.getBackend();
45
+ this.assertSupportedOptions(opts);
44
46
  if (opts.delay && opts.delay > 0)
45
47
  await sleep(opts.delay);
46
48
  // Try to resolve window ID from name using xdotool (X11 only)
@@ -92,6 +94,7 @@ export class LinuxProvider {
92
94
  }
93
95
  async captureRegion(opts) {
94
96
  const backend = await this.getBackend();
97
+ this.assertSupportedOptions(opts);
95
98
  if (opts.delay && opts.delay > 0)
96
99
  await sleep(opts.delay);
97
100
  const geometry = `${opts.width}x${opts.height}+${opts.x}+${opts.y}`;
@@ -121,6 +124,13 @@ export class LinuxProvider {
121
124
  }
122
125
  }
123
126
  // ── Private helpers ────────────────────────────────────────────────────
127
+ assertSupportedOptions(opts) {
128
+ if (opts.includeCursor) {
129
+ throw new Error('Linux provider does not support includeCursor — none of the supported backends ' +
130
+ '(maim, scrot, gnome-screenshot, spectacle, grim, import) expose a cursor flag in our argv. ' +
131
+ 'Use macOS or Windows for cursor capture.');
132
+ }
133
+ }
124
134
  async detectBackend() {
125
135
  if (this._detected)
126
136
  return;
@@ -2,12 +2,29 @@
2
2
  // Windows screenshot provider — PowerShell + .NET System.Drawing
3
3
  // ============================================================================
4
4
  import { execFileAsync, commandExists, sleep } from './screenshot-provider.js';
5
+ const POWERSHELL_FALLBACK_PATHS = [
6
+ 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
7
+ 'C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe',
8
+ ];
9
+ async function resolvePowerShell() {
10
+ if (await commandExists('powershell'))
11
+ return 'powershell';
12
+ for (const p of POWERSHELL_FALLBACK_PATHS) {
13
+ try {
14
+ await execFileAsync(p, ['-Command', 'echo ok']);
15
+ return p;
16
+ }
17
+ catch { /* try next */ }
18
+ }
19
+ return null;
20
+ }
5
21
  /**
6
22
  * Windows screenshot provider using PowerShell with .NET System.Drawing.
7
23
  * Zero external dependencies — PowerShell and .NET are built into all modern Windows.
8
24
  */
9
25
  export class WindowsProvider {
10
26
  platform = 'Windows';
27
+ powershellPath = null;
11
28
  static DPI_AWARE_SNIPPET = `
12
29
  Add-Type -TypeDefinition @"
13
30
  using System;
@@ -15,24 +32,35 @@ using System.Runtime.InteropServices;
15
32
  public class DpiAwareness {
16
33
  [DllImport("user32.dll")]
17
34
  public static extern bool SetProcessDPIAware();
35
+ [DllImport("user32.dll", SetLastError = true)]
36
+ public static extern bool SetProcessDpiAwarenessContext(IntPtr value);
18
37
  }
19
38
  "@
20
- [DpiAwareness]::SetProcessDPIAware() | Out-Null`.trim();
39
+ try { [DpiAwareness]::SetProcessDpiAwarenessContext([IntPtr]::new(-4)) | Out-Null } catch { [DpiAwareness]::SetProcessDPIAware() | Out-Null }`.trim();
21
40
  async isAvailable() {
22
- return commandExists('powershell');
41
+ this.powershellPath = await resolvePowerShell();
42
+ return this.powershellPath !== null;
23
43
  }
24
44
  async captureFullscreen(opts) {
25
45
  if (opts.delay && opts.delay > 0)
26
46
  await sleep(opts.delay);
27
47
  const format = this.dotNetFormat(opts.format);
28
- const displayIndex = (opts.display ?? 1) - 1; // Convert 1-based to 0-based
48
+ // No display specified → capture entire virtual screen (all monitors combined)
49
+ // display specified → capture that specific monitor
50
+ const captureAllDisplays = opts.display === undefined || opts.display === null;
51
+ const displayIndex = captureAllDisplays ? 0 : (opts.display - 1);
52
+ const boundsScript = captureAllDisplays
53
+ ? `
54
+ $bounds = [System.Windows.Forms.SystemInformation]::VirtualScreen`
55
+ : `
56
+ $screens = [System.Windows.Forms.Screen]::AllScreens
57
+ $screen = if (${displayIndex} -lt $screens.Length) { $screens[${displayIndex}] } else { [System.Windows.Forms.Screen]::PrimaryScreen }
58
+ $bounds = $screen.Bounds`;
29
59
  const script = `
30
60
  ${WindowsProvider.DPI_AWARE_SNIPPET}
31
61
  Add-Type -AssemblyName System.Windows.Forms
32
62
  Add-Type -AssemblyName System.Drawing
33
- $screens = [System.Windows.Forms.Screen]::AllScreens
34
- $screen = if (${displayIndex} -lt $screens.Length) { $screens[${displayIndex}] } else { [System.Windows.Forms.Screen]::PrimaryScreen }
35
- $bounds = $screen.Bounds
63
+ ${boundsScript}
36
64
  $bitmap = New-Object System.Drawing.Bitmap($bounds.Width, $bounds.Height)
37
65
  $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
38
66
  ${opts.includeCursor ? '$cursorPos = [System.Windows.Forms.Cursor]::Position\n' : ''}
@@ -111,11 +139,13 @@ $bitmap.Dispose()
111
139
  }
112
140
  // ── Private helpers ────────────────────────────────────────────────────
113
141
  async runPowerShell(script) {
114
- await execFileAsync('powershell', [
142
+ const cmd = this.powershellPath ?? 'powershell';
143
+ const encoded = Buffer.from(script, 'utf16le').toString('base64');
144
+ await execFileAsync(cmd, [
115
145
  '-ExecutionPolicy', 'Bypass',
116
146
  '-NoProfile',
117
147
  '-NonInteractive',
118
- '-Command', script,
148
+ '-EncodedCommand', encoded,
119
149
  ]);
120
150
  }
121
151
  dotNetFormat(format) {
@@ -1,5 +1,5 @@
1
1
  import { promises as fsPromises } from 'fs';
2
- import { dirname, join, resolve, relative } from 'path';
2
+ import { dirname, join, resolve, relative, isAbsolute } from 'path';
3
3
  const defaultFileSystem = {
4
4
  realpath: (path) => fsPromises.realpath(path),
5
5
  };
@@ -59,14 +59,15 @@ export async function validateOutputPath(customPath, defaultName, config, fileSy
59
59
  realAllowedDir = resolve(allowedDir);
60
60
  }
61
61
  const relativePath = relative(realAllowedDir, realPath);
62
- // If relative path doesn't start with '..' and isn't absolute, it's within the allowed dir
63
- if (!relativePath.startsWith('..') && !relativePath.startsWith('/')) {
62
+ // isAbsolute() catches the cross-drive case on Windows, where relative() returns
63
+ // an absolute path like 'D:\\foo' that doesn't start with '/' or '..'.
64
+ if (!relativePath.startsWith('..') && !isAbsolute(relativePath)) {
64
65
  return { valid: true, path: realPath };
65
66
  }
66
67
  }
67
68
  // Path is outside all allowed directories (after symlink resolution)
68
69
  return {
69
70
  valid: false,
70
- error: 'Output path must be within allowed directories (~/Desktop/Screenshots, ~/Downloads, ~/Documents, or /tmp). Symlinks to other locations are not permitted.',
71
+ error: 'Output path must be within allowed directories (~/Desktop/Screenshots, ~/Documents, ~/Downloads, or the system temp directory). Symlinks to other locations are not permitted.',
71
72
  };
72
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universal-screenshot-mcp",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "An MCP server for capturing web page screenshots via Puppeteer and cross-platform system screenshots via native OS tools",
5
5
  "author": "Seth Bang",
6
6
  "license": "Apache-2.0",