-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathlaunch-playwright-mcp.js
More file actions
99 lines (87 loc) · 2.91 KB
/
Copy pathlaunch-playwright-mcp.js
File metadata and controls
99 lines (87 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
// Launches the Playwright MCP server with the best available browser.
// Detects system-installed Chromium-based browsers in preference order,
// then falls back to Playwright's bundled Chromium.
// Self-contained — no external dependencies required.
const { spawn } = require('node:child_process');
const fs = require('node:fs');
const path = require('path');
const { detectBrowser } = require('./lib/detect-browser');
const PLAYWRIGHT_MCP_VERSION = '0.0.78';
const PLAYWRIGHT_MCP_PACKAGE = `@playwright/mcp@${PLAYWRIGHT_MCP_VERSION}`;
function buildMcpArgs(browser, {
configPath = path.join(__dirname, 'playwright-mcp-fullscreen.config.json'),
} = {}) {
// Marketplace installs copy only this plugin directory and do not run npm install,
// so a lockfile would not materialize a local executable. Keep the runtime package
// immutable, and disable lifecycle scripts while npx prepares the reviewed version.
return [
'--yes',
'--ignore-scripts',
`--package=${PLAYWRIGHT_MCP_PACKAGE}`,
'playwright-mcp',
'--browser',
browser,
'--config',
configPath,
];
}
function resolveNpxCli({
execPath = process.execPath,
platform = process.platform,
existsSync = fs.existsSync,
} = {}) {
// Windows exposes npx as a .cmd shim that cannot run with shell:false. Invoking
// npm's JavaScript entrypoint through Node preserves raw argv on every platform.
const pathApi = platform === 'win32' ? path.win32 : path.posix;
const nodeDir = pathApi.dirname(execPath);
const candidates = [
pathApi.resolve(nodeDir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npx-cli.js'),
pathApi.join(nodeDir, 'node_modules', 'npm', 'bin', 'npx-cli.js'),
];
const match = candidates.find((candidate) => existsSync(candidate));
if (!match) {
throw new Error(
'Could not locate npm/bin/npx-cli.js beside the current Node installation. Install Node.js with npm before starting the Playwright MCP server.',
);
}
return match;
}
function launch({
browser = detectBrowser(),
npxCliPath,
resolveNpxCliFn = resolveNpxCli,
spawnFn = spawn,
exitFn = (code) => process.exit(code),
writeError = (message) => process.stderr.write(message),
} = {}) {
let resolvedNpxCliPath = npxCliPath;
if (resolvedNpxCliPath === undefined) {
try {
resolvedNpxCliPath = resolveNpxCliFn();
} catch (error) {
writeError(`Failed to start Playwright MCP: ${error.message}\n`);
exitFn(1);
return null;
}
}
const child = spawnFn(process.execPath, [resolvedNpxCliPath, ...buildMcpArgs(browser)], {
stdio: 'inherit',
shell: false,
});
child.once('error', (error) => {
writeError(`Failed to start Playwright MCP: ${error.message}\n`);
exitFn(1);
});
child.once('exit', (code) => exitFn(code ?? 1));
return child;
}
if (require.main === module) {
launch();
}
module.exports = {
PLAYWRIGHT_MCP_PACKAGE,
buildMcpArgs,
launch,
resolveNpxCli,
};