@cotestdev/mcp_playwright 0.0.28 → 0.0.29
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/lib/mcp/browser/browserContextFactory.js +10 -2
- package/lib/mcp/browser/tab.js +3 -1
- package/lib/mcp/browser/tools/runCode.js +1 -2
- package/lib/mcp/browser/tools/snapshot.js +1 -1
- package/lib/mcp/program.js +13 -1
- package/lib/mcp/sdk/tool.js +1 -1
- package/lib/mcp/terminal/cli.js +219 -0
- package/lib/mcp/terminal/daemon.js +114 -0
- package/lib/mcp/terminal/socketConnection.js +80 -0
- package/lib/mcpBundleImpl/index.js +81 -64
- package/lib/utilsBundle.js +7 -0
- package/package.json +1 -1
|
@@ -141,7 +141,10 @@ class CdpContextFactory extends BaseContextFactory {
|
|
|
141
141
|
super("cdp", config);
|
|
142
142
|
}
|
|
143
143
|
async _doObtainBrowser() {
|
|
144
|
-
return playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint, {
|
|
144
|
+
return playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint, {
|
|
145
|
+
headers: this.config.browser.cdpHeaders,
|
|
146
|
+
timeout: this.config.browser.cdpTimeout
|
|
147
|
+
});
|
|
145
148
|
}
|
|
146
149
|
async _doCreateContext(browser) {
|
|
147
150
|
return this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
|
|
@@ -199,7 +202,12 @@ class PersistentContextFactory {
|
|
|
199
202
|
} catch (error) {
|
|
200
203
|
if (error.message.includes("Executable doesn't exist"))
|
|
201
204
|
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
|
|
202
|
-
if (error.message.includes("
|
|
205
|
+
if (error.message.includes("cannot open shared object file: No such file or directory")) {
|
|
206
|
+
const browserName = launchOptions.channel ?? this.config.browser.browserName;
|
|
207
|
+
throw new Error(`Missing system dependencies required to run browser ${browserName}. Install them with: sudo npx playwright install-deps ${browserName}`);
|
|
208
|
+
}
|
|
209
|
+
if (error.message.includes("ProcessSingleton") || // On Windows the process exits silently with code 21 when the profile is in use.
|
|
210
|
+
error.message.includes("exitCode=21")) {
|
|
203
211
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
204
212
|
continue;
|
|
205
213
|
}
|
package/lib/mcp/browser/tab.js
CHANGED
|
@@ -237,7 +237,9 @@ class Tab extends import_events.EventEmitter {
|
|
|
237
237
|
await this._initializedPromise;
|
|
238
238
|
return Promise.all(params.map(async (param) => {
|
|
239
239
|
try {
|
|
240
|
-
|
|
240
|
+
let locator = this.page.locator(`aria-ref=${param.ref}`);
|
|
241
|
+
if (param.element)
|
|
242
|
+
locator = locator.describe(param.element);
|
|
241
243
|
const { resolvedSelector } = await locator._resolveSelector();
|
|
242
244
|
return { locator, resolved: (0, import_utils.asLocator)("javascript", resolvedSelector) };
|
|
243
245
|
} catch (e) {
|
|
@@ -74,7 +74,7 @@ const runCode = (0, import_tool.defineTabTool)({
|
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
76
|
});
|
|
77
|
-
const scriptSchema =
|
|
77
|
+
const scriptSchema = import_common.baseSchema.extend({
|
|
78
78
|
code: import_mcpBundle.z.string().describe(`A Typescript function containing Playwright code to execute. It will be invoked with a single argument, page, which you can use for any page interaction. For example: \`async (page) => { await page.getByRole('button', { name: 'Submit' }).click(); return await page.title(); }\``),
|
|
79
79
|
params: import_mcpBundle.z.record(import_mcpBundle.z.any()).optional().describe("Parameters to pass to the script.")
|
|
80
80
|
});
|
|
@@ -89,7 +89,6 @@ const runScript = (0, import_tool.defineTabTool)({
|
|
|
89
89
|
},
|
|
90
90
|
handle: async (tab, params, response) => {
|
|
91
91
|
response.setIncludeSnapshot();
|
|
92
|
-
response.addCode(`await (${params.code})(page);`);
|
|
93
92
|
const __end__ = new import_utils.ManualPromise();
|
|
94
93
|
const context = {
|
|
95
94
|
page: tab.page,
|
|
@@ -61,7 +61,7 @@ const snapshot = (0, import_tool.defineTool)({
|
|
|
61
61
|
}
|
|
62
62
|
});
|
|
63
63
|
const elementSchema = import_common.baseSchema.extend({
|
|
64
|
-
element: import_mcpBundle.z.string().describe("Human-readable element description used to obtain permission to interact with the element"),
|
|
64
|
+
element: import_mcpBundle.z.string().optional().describe("Human-readable element description used to obtain permission to interact with the element"),
|
|
65
65
|
ref: import_mcpBundle.z.string().describe("Exact target element reference from the page snapshot")
|
|
66
66
|
});
|
|
67
67
|
const clickSchema = elementSchema.extend({
|
package/lib/mcp/program.js
CHANGED
|
@@ -35,13 +35,14 @@ var import_fs = __toESM(require("fs"));
|
|
|
35
35
|
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
36
36
|
var import_server = require("playwright-core/lib/server");
|
|
37
37
|
var mcpServer = __toESM(require("./sdk/server"));
|
|
38
|
+
var import_daemon = require("./terminal/daemon");
|
|
38
39
|
var import_config = require("./browser/config");
|
|
39
40
|
var import_watchdog = require("./browser/watchdog");
|
|
40
41
|
var import_browserContextFactory = require("./browser/browserContextFactory");
|
|
41
42
|
var import_browserServerBackend = require("./browser/browserServerBackend");
|
|
42
43
|
var import_extensionContextFactory = require("./extension/extensionContextFactory");
|
|
43
44
|
function decorateCommand(command, version) {
|
|
44
|
-
command.option("--allowed-hosts <hosts...>", "comma-separated list of hosts this server is allowed to serve from. Defaults to the host the server is bound to. Pass '*' to disable the host check.", import_config.commaSeparatedList).option("--allowed-origins <origins>", "semicolon-separated list of TRUSTED origins to allow the browser to request. Default is to allow all.\nImportant: *does not* serve as a security boundary and *does not* affect redirects. ", import_config.semicolonSeparatedList).option("--allow-unrestricted-file-access", "allow access to files outside of the workspace roots. Also allows unrestricted access to file:// URLs. By default access to file system is restricted to workspace root directories (or cwd if no roots are configured) only, and navigation to file:// URLs is blocked.").option("--blocked-origins <origins>", "semicolon-separated list of origins to block the browser from requesting. Blocklist is evaluated before allowlist. If used without the allowlist, requests not matching the blocklist are still allowed.\nImportant: *does not* serve as a security boundary and *does not* affect redirects.", import_config.semicolonSeparatedList).option("--block-service-workers", "block service workers").option("--browser <browser>", "browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.").option("--caps <caps>", "comma-separated list of additional capabilities to enable, possible values: vision, pdf.", import_config.commaSeparatedList).option("--cdp-endpoint <endpoint>", "CDP endpoint to connect to.").option("--cdp-header <headers...>", "CDP headers to send with the connect request, multiple can be specified.", import_config.headerParser).option("--config <path>", "path to the configuration file.").option("--console-level <level>", 'level of console messages to return: "error", "warning", "info", "debug". Each level includes the messages of more severe levels.', import_config.enumParser.bind(null, "--console-level", ["error", "warning", "info", "debug"])).option("--device <device>", 'device to emulate, for example: "iPhone 15"').option("--executable-path <path>", "path to the browser executable.").option("--extension", 'Connect to a running browser instance (Edge/Chrome only). Requires the "Playwright MCP Bridge" browser extension to be installed.').option("--grant-permissions <permissions...>", 'List of permissions to grant to the browser context, for example "geolocation", "clipboard-read", "clipboard-write".', import_config.commaSeparatedList).option("--headless", "run browser in headless mode, headed by default").option("--host <host>", "host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.").option("--ignore-https-errors", "ignore https errors").option("--init-page <path...>", "path to TypeScript file to evaluate on Playwright page object").option("--init-script <path...>", "path to JavaScript file to add as an initialization script. The script will be evaluated in every page before any of the page's scripts. Can be specified multiple times.").option("--isolated", "keep the browser profile in memory, do not save it to disk.").option("--image-responses <mode>", 'whether to send image responses to the client. Can be "allow" or "omit", Defaults to "allow".', import_config.enumParser.bind(null, "--image-responses", ["allow", "omit"])).option("--no-sandbox", "disable the sandbox for all process types that are normally sandboxed.").option("--output-dir <path>", "path to the directory for output files.").option("--port <port>", "port to listen on for SSE transport.").option("--proxy-bypass <bypass>", 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"').option("--proxy-server <proxy>", 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"').option("--save-session", "Whether to save the Playwright MCP session into the output directory.").option("--save-trace", "Whether to save the Playwright Trace of the session into the output directory.").option("--save-video <size>", 'Whether to save the video of the session into the output directory. For example "--save-video=800x600"', import_config.resolutionParser.bind(null, "--save-video")).option("--secrets <path>", "path to a file containing secrets in the dotenv format", import_config.dotenvFileLoader).option("--shared-browser-context", "reuse the same browser context between all connected HTTP clients.").option("--snapshot-mode <mode>", 'when taking snapshots for responses, specifies the mode to use. Can be "incremental", "full", or "none". Default is incremental.').option("--storage-state <path>", "path to the storage state file for isolated sessions.").option("--test-id-attribute <attribute>", 'specify the attribute to use for test ids, defaults to "data-testid"').option("--timeout-action <timeout>", "specify action timeout in milliseconds, defaults to 5000ms", import_config.numberParser).option("--timeout-navigation <timeout>", "specify navigation timeout in milliseconds, defaults to 60000ms", import_config.numberParser).option("--user-agent <ua string>", "specify user agent string").option("--user-data-dir <path>", "path to the user data directory. If not specified, a temporary directory will be created.").option("--viewport-size <size>", 'specify browser viewport size in pixels, for example "1280x720"', import_config.resolutionParser.bind(null, "--viewport-size")).addOption(new import_utilsBundle.ProgramOption("--vision", "Legacy option, use --caps=vision instead").hideHelp()).action(async (options) => {
|
|
45
|
+
command.option("--allowed-hosts <hosts...>", "comma-separated list of hosts this server is allowed to serve from. Defaults to the host the server is bound to. Pass '*' to disable the host check.", import_config.commaSeparatedList).option("--allowed-origins <origins>", "semicolon-separated list of TRUSTED origins to allow the browser to request. Default is to allow all.\nImportant: *does not* serve as a security boundary and *does not* affect redirects. ", import_config.semicolonSeparatedList).option("--allow-unrestricted-file-access", "allow access to files outside of the workspace roots. Also allows unrestricted access to file:// URLs. By default access to file system is restricted to workspace root directories (or cwd if no roots are configured) only, and navigation to file:// URLs is blocked.").option("--blocked-origins <origins>", "semicolon-separated list of origins to block the browser from requesting. Blocklist is evaluated before allowlist. If used without the allowlist, requests not matching the blocklist are still allowed.\nImportant: *does not* serve as a security boundary and *does not* affect redirects.", import_config.semicolonSeparatedList).option("--block-service-workers", "block service workers").option("--browser <browser>", "browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.").option("--caps <caps>", "comma-separated list of additional capabilities to enable, possible values: vision, pdf.", import_config.commaSeparatedList).option("--cdp-endpoint <endpoint>", "CDP endpoint to connect to.").option("--cdp-header <headers...>", "CDP headers to send with the connect request, multiple can be specified.", import_config.headerParser).option("--config <path>", "path to the configuration file.").option("--console-level <level>", 'level of console messages to return: "error", "warning", "info", "debug". Each level includes the messages of more severe levels.', import_config.enumParser.bind(null, "--console-level", ["error", "warning", "info", "debug"])).option("--device <device>", 'device to emulate, for example: "iPhone 15"').option("--executable-path <path>", "path to the browser executable.").option("--extension", 'Connect to a running browser instance (Edge/Chrome only). Requires the "Playwright MCP Bridge" browser extension to be installed.').option("--grant-permissions <permissions...>", 'List of permissions to grant to the browser context, for example "geolocation", "clipboard-read", "clipboard-write".', import_config.commaSeparatedList).option("--headless", "run browser in headless mode, headed by default").option("--host <host>", "host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.").option("--ignore-https-errors", "ignore https errors").option("--init-page <path...>", "path to TypeScript file to evaluate on Playwright page object").option("--init-script <path...>", "path to JavaScript file to add as an initialization script. The script will be evaluated in every page before any of the page's scripts. Can be specified multiple times.").option("--isolated", "keep the browser profile in memory, do not save it to disk.").option("--image-responses <mode>", 'whether to send image responses to the client. Can be "allow" or "omit", Defaults to "allow".', import_config.enumParser.bind(null, "--image-responses", ["allow", "omit"])).option("--no-sandbox", "disable the sandbox for all process types that are normally sandboxed.").option("--output-dir <path>", "path to the directory for output files.").option("--port <port>", "port to listen on for SSE transport.").option("--proxy-bypass <bypass>", 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"').option("--proxy-server <proxy>", 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"').option("--save-session", "Whether to save the Playwright MCP session into the output directory.").option("--save-trace", "Whether to save the Playwright Trace of the session into the output directory.").option("--save-video <size>", 'Whether to save the video of the session into the output directory. For example "--save-video=800x600"', import_config.resolutionParser.bind(null, "--save-video")).option("--secrets <path>", "path to a file containing secrets in the dotenv format", import_config.dotenvFileLoader).option("--shared-browser-context", "reuse the same browser context between all connected HTTP clients.").option("--snapshot-mode <mode>", 'when taking snapshots for responses, specifies the mode to use. Can be "incremental", "full", or "none". Default is incremental.').option("--storage-state <path>", "path to the storage state file for isolated sessions.").option("--test-id-attribute <attribute>", 'specify the attribute to use for test ids, defaults to "data-testid"').option("--timeout-action <timeout>", "specify action timeout in milliseconds, defaults to 5000ms", import_config.numberParser).option("--timeout-navigation <timeout>", "specify navigation timeout in milliseconds, defaults to 60000ms", import_config.numberParser).option("--user-agent <ua string>", "specify user agent string").option("--user-data-dir <path>", "path to the user data directory. If not specified, a temporary directory will be created.").option("--viewport-size <size>", 'specify browser viewport size in pixels, for example "1280x720"', import_config.resolutionParser.bind(null, "--viewport-size")).addOption(new import_utilsBundle.ProgramOption("--vision", "Legacy option, use --caps=vision instead").hideHelp()).addOption(new import_utilsBundle.ProgramOption("--daemon <socket>", "run as daemon").hideHelp()).action(async (options) => {
|
|
45
46
|
(0, import_watchdog.setupExitWatchdog)();
|
|
46
47
|
if (options.vision) {
|
|
47
48
|
console.error("The --vision option is deprecated, use --caps=vision instead");
|
|
@@ -70,6 +71,17 @@ Please run the command below. It will install a local copy of ffmpeg and will no
|
|
|
70
71
|
await mcpServer.start(serverBackendFactory, config.server);
|
|
71
72
|
return;
|
|
72
73
|
}
|
|
74
|
+
if (options.daemon) {
|
|
75
|
+
const serverBackendFactory = {
|
|
76
|
+
name: "Playwright",
|
|
77
|
+
nameInConfig: "playwright-daemon",
|
|
78
|
+
version,
|
|
79
|
+
create: () => new import_browserServerBackend.BrowserServerBackend(config, browserContextFactory)
|
|
80
|
+
};
|
|
81
|
+
const socketPath = await (0, import_daemon.startMcpDaemonServer)(options.daemon, serverBackendFactory);
|
|
82
|
+
console.error(`Daemon server listening on ${socketPath}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
73
85
|
const factory = {
|
|
74
86
|
name: "Playwright",
|
|
75
87
|
nameInConfig: "playwright",
|
package/lib/mcp/sdk/tool.js
CHANGED
|
@@ -28,7 +28,7 @@ function toMcpTool(tool) {
|
|
|
28
28
|
return {
|
|
29
29
|
name: tool.name,
|
|
30
30
|
description: tool.description,
|
|
31
|
-
inputSchema:
|
|
31
|
+
inputSchema: import_mcpBundle.z.toJSONSchema(tool.inputSchema),
|
|
32
32
|
annotations: {
|
|
33
33
|
title: tool.title,
|
|
34
34
|
readOnlyHint: readOnly,
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var import_child_process = require("child_process");
|
|
25
|
+
var import_fs = __toESM(require("fs"));
|
|
26
|
+
var import_net = __toESM(require("net"));
|
|
27
|
+
var import_os = __toESM(require("os"));
|
|
28
|
+
var import_path = __toESM(require("path"));
|
|
29
|
+
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
30
|
+
var import_socketConnection = require("./socketConnection");
|
|
31
|
+
const debugCli = (0, import_utilsBundle.debug)("pw:cli");
|
|
32
|
+
const packageJSON = require("../../../package.json");
|
|
33
|
+
import_utilsBundle.program.version("Version " + (process.env.PW_CLI_DISPLAY_VERSION || packageJSON.version)).name("playwright-command");
|
|
34
|
+
function addCommand(name, description, action) {
|
|
35
|
+
import_utilsBundle.program.command(name).description(description).action(action);
|
|
36
|
+
}
|
|
37
|
+
addCommand("navigate <url>", "open url in the browser", async (url) => {
|
|
38
|
+
await runMcpCommand("browser_navigate", { url });
|
|
39
|
+
});
|
|
40
|
+
addCommand("close", "close the browser", async () => {
|
|
41
|
+
await runMcpCommand("browser_close", {});
|
|
42
|
+
});
|
|
43
|
+
addCommand("click <ref>", "click an element using a ref from a snapshot, e.g. e67", async (ref) => {
|
|
44
|
+
await runMcpCommand("browser_click", { ref });
|
|
45
|
+
});
|
|
46
|
+
addCommand("snapshot", "get accessible snapshot of the current page", async () => {
|
|
47
|
+
await runMcpCommand("browser_snapshot", {});
|
|
48
|
+
});
|
|
49
|
+
addCommand("drag <startRef> <endRef>", "drag from one element to another", async (startRef, endRef) => {
|
|
50
|
+
await runMcpCommand("browser_drag", { startRef, endRef });
|
|
51
|
+
});
|
|
52
|
+
addCommand("hover <ref>", "hover over an element", async (ref) => {
|
|
53
|
+
await runMcpCommand("browser_hover", { ref });
|
|
54
|
+
});
|
|
55
|
+
addCommand("select <ref> <values...>", "select option(s) in a dropdown", async (ref, values) => {
|
|
56
|
+
await runMcpCommand("browser_select_option", { ref, values });
|
|
57
|
+
});
|
|
58
|
+
addCommand("locator <ref>", "generate a locator for an element", async (ref) => {
|
|
59
|
+
await runMcpCommand("browser_generate_locator", { ref });
|
|
60
|
+
});
|
|
61
|
+
addCommand("press <key>", "press a key on the keyboard", async (key) => {
|
|
62
|
+
await runMcpCommand("browser_press_key", { key });
|
|
63
|
+
});
|
|
64
|
+
addCommand("type <ref> <text>", "type text into an element", async (ref, text) => {
|
|
65
|
+
await runMcpCommand("browser_type", { ref, text });
|
|
66
|
+
});
|
|
67
|
+
addCommand("back", "go back to the previous page", async () => {
|
|
68
|
+
await runMcpCommand("browser_navigate_back", {});
|
|
69
|
+
});
|
|
70
|
+
addCommand("wait <time>", "wait for a specified time in seconds", async (time) => {
|
|
71
|
+
await runMcpCommand("browser_wait_for", { time: parseFloat(time) });
|
|
72
|
+
});
|
|
73
|
+
addCommand("wait-for-text <text>", "wait for text to appear", async (text) => {
|
|
74
|
+
await runMcpCommand("browser_wait_for", { text });
|
|
75
|
+
});
|
|
76
|
+
addCommand("dialog-accept [promptText]", "accept a dialog", async (promptText) => {
|
|
77
|
+
await runMcpCommand("browser_handle_dialog", { accept: true, promptText });
|
|
78
|
+
});
|
|
79
|
+
addCommand("dialog-dismiss", "dismiss a dialog", async () => {
|
|
80
|
+
await runMcpCommand("browser_handle_dialog", { accept: false });
|
|
81
|
+
});
|
|
82
|
+
addCommand("screenshot [filename]", "take a screenshot of the current page", async (filename) => {
|
|
83
|
+
await runMcpCommand("browser_take_screenshot", { filename });
|
|
84
|
+
});
|
|
85
|
+
addCommand("resize <width> <height>", "resize the browser window", async (width, height) => {
|
|
86
|
+
await runMcpCommand("browser_resize", { width: parseInt(width, 10), height: parseInt(height, 10) });
|
|
87
|
+
});
|
|
88
|
+
addCommand("upload <paths...>", "upload files", async (paths) => {
|
|
89
|
+
await runMcpCommand("browser_file_upload", { paths });
|
|
90
|
+
});
|
|
91
|
+
addCommand("tabs", "list all browser tabs", async () => {
|
|
92
|
+
await runMcpCommand("browser_tabs", { action: "list" });
|
|
93
|
+
});
|
|
94
|
+
addCommand("tab-new", "create a new browser tab", async () => {
|
|
95
|
+
await runMcpCommand("browser_tabs", { action: "new" });
|
|
96
|
+
});
|
|
97
|
+
addCommand("tab-close [index]", "close a browser tab", async (index) => {
|
|
98
|
+
await runMcpCommand("browser_tabs", { action: "close", index: index !== void 0 ? parseInt(index, 10) : void 0 });
|
|
99
|
+
});
|
|
100
|
+
addCommand("tab-select <index>", "select a browser tab", async (index) => {
|
|
101
|
+
await runMcpCommand("browser_tabs", { action: "select", index: parseInt(index, 10) });
|
|
102
|
+
});
|
|
103
|
+
async function runMcpCommand(name, args) {
|
|
104
|
+
const session = await connectToDaemon();
|
|
105
|
+
const result = await session.callTool(name, args);
|
|
106
|
+
printResult(result);
|
|
107
|
+
session.dispose();
|
|
108
|
+
}
|
|
109
|
+
function printResult(result) {
|
|
110
|
+
for (const content of result.content) {
|
|
111
|
+
if (content.type === "text")
|
|
112
|
+
console.log(content.text);
|
|
113
|
+
else
|
|
114
|
+
console.log(`<${content.type} content>`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async function socketExists(socketPath) {
|
|
118
|
+
try {
|
|
119
|
+
const stat = await import_fs.default.promises.stat(socketPath);
|
|
120
|
+
if (stat?.isSocket())
|
|
121
|
+
return true;
|
|
122
|
+
} catch (e) {
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
class SocketSession {
|
|
127
|
+
constructor(connection) {
|
|
128
|
+
this._nextMessageId = 1;
|
|
129
|
+
this._callbacks = /* @__PURE__ */ new Map();
|
|
130
|
+
this._connection = connection;
|
|
131
|
+
this._connection.onmessage = (message) => this._onMessage(message);
|
|
132
|
+
this._connection.onclose = () => this.dispose();
|
|
133
|
+
}
|
|
134
|
+
async callTool(name, args) {
|
|
135
|
+
return this._send(name, args);
|
|
136
|
+
}
|
|
137
|
+
async _send(method, params = {}) {
|
|
138
|
+
const messageId = this._nextMessageId++;
|
|
139
|
+
const message = {
|
|
140
|
+
id: messageId,
|
|
141
|
+
method,
|
|
142
|
+
params
|
|
143
|
+
};
|
|
144
|
+
await this._connection.send(message);
|
|
145
|
+
return new Promise((resolve, reject) => {
|
|
146
|
+
this._callbacks.set(messageId, { resolve, reject, error: new Error(`Error in method: ${method}`) });
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
dispose() {
|
|
150
|
+
for (const callback of this._callbacks.values())
|
|
151
|
+
callback.reject(callback.error);
|
|
152
|
+
this._callbacks.clear();
|
|
153
|
+
this._connection.close();
|
|
154
|
+
}
|
|
155
|
+
_onMessage(object) {
|
|
156
|
+
if (object.id && this._callbacks.has(object.id)) {
|
|
157
|
+
const callback = this._callbacks.get(object.id);
|
|
158
|
+
this._callbacks.delete(object.id);
|
|
159
|
+
if (object.error) {
|
|
160
|
+
callback.error.cause = new Error(object.error);
|
|
161
|
+
callback.reject(callback.error);
|
|
162
|
+
} else {
|
|
163
|
+
callback.resolve(object.result);
|
|
164
|
+
}
|
|
165
|
+
} else if (object.id) {
|
|
166
|
+
throw new Error(`Unexpected message id: ${object.id}`);
|
|
167
|
+
} else {
|
|
168
|
+
throw new Error(`Unexpected message without id: ${JSON.stringify(object)}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function daemonSocketPath() {
|
|
173
|
+
if (import_os.default.platform() === "win32")
|
|
174
|
+
return import_path.default.join("\\\\.\\pipe", "pw-daemon.sock");
|
|
175
|
+
return import_path.default.join(import_os.default.homedir(), ".playwright", "pw-daemon.sock");
|
|
176
|
+
}
|
|
177
|
+
async function connectToDaemon() {
|
|
178
|
+
const socketPath = daemonSocketPath();
|
|
179
|
+
debugCli(`Connecting to daemon at ${socketPath}`);
|
|
180
|
+
if (await socketExists(socketPath)) {
|
|
181
|
+
debugCli(`Socket file exists, attempting to connect...`);
|
|
182
|
+
try {
|
|
183
|
+
return await connectToSocket(socketPath);
|
|
184
|
+
} catch (e) {
|
|
185
|
+
import_fs.default.unlinkSync(socketPath);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const cliPath = import_path.default.join(__dirname, "../../../cli.js");
|
|
189
|
+
debugCli(`Will launch daemon process: ${cliPath}`);
|
|
190
|
+
const child = (0, import_child_process.spawn)(process.execPath, [cliPath, "run-mcp-server", `--daemon=${socketPath}`], {
|
|
191
|
+
detached: true,
|
|
192
|
+
stdio: "ignore"
|
|
193
|
+
});
|
|
194
|
+
child.unref();
|
|
195
|
+
const maxRetries = 50;
|
|
196
|
+
const retryDelay = 100;
|
|
197
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
198
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
199
|
+
try {
|
|
200
|
+
return await connectToSocket(socketPath);
|
|
201
|
+
} catch (e) {
|
|
202
|
+
if (e.code !== "ENOENT")
|
|
203
|
+
throw e;
|
|
204
|
+
debugCli(`Retrying to connect to daemon at ${socketPath} (${i + 1}/${maxRetries})`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
throw new Error(`Failed to connect to daemon at ${socketPath} after ${maxRetries * retryDelay}ms`);
|
|
208
|
+
}
|
|
209
|
+
async function connectToSocket(socketPath) {
|
|
210
|
+
const socket = await new Promise((resolve, reject) => {
|
|
211
|
+
const socket2 = import_net.default.createConnection(socketPath, () => {
|
|
212
|
+
debugCli(`Connected to daemon at ${socketPath}`);
|
|
213
|
+
resolve(socket2);
|
|
214
|
+
});
|
|
215
|
+
socket2.on("error", reject);
|
|
216
|
+
});
|
|
217
|
+
return new SocketSession(new import_socketConnection.SocketConnection(socket));
|
|
218
|
+
}
|
|
219
|
+
void import_utilsBundle.program.parseAsync(process.argv);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var daemon_exports = {};
|
|
30
|
+
__export(daemon_exports, {
|
|
31
|
+
startMcpDaemonServer: () => startMcpDaemonServer
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(daemon_exports);
|
|
34
|
+
var import_promises = __toESM(require("fs/promises"));
|
|
35
|
+
var import_net = __toESM(require("net"));
|
|
36
|
+
var import_os = __toESM(require("os"));
|
|
37
|
+
var import_path = __toESM(require("path"));
|
|
38
|
+
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
39
|
+
var import_socketConnection = require("./socketConnection");
|
|
40
|
+
const daemonDebug = (0, import_utilsBundle.debug)("pw:daemon");
|
|
41
|
+
function normalizeSocketPath(path2) {
|
|
42
|
+
if (import_os.default.platform() === "win32") {
|
|
43
|
+
if (path2.startsWith("\\\\.\\pipe\\"))
|
|
44
|
+
return path2;
|
|
45
|
+
const name = path2.replace(/[^a-zA-Z0-9]/g, "-");
|
|
46
|
+
return `\\\\.\\pipe\\${name}`;
|
|
47
|
+
}
|
|
48
|
+
return path2;
|
|
49
|
+
}
|
|
50
|
+
async function socketExists(socketPath) {
|
|
51
|
+
try {
|
|
52
|
+
const stat = await import_promises.default.stat(socketPath);
|
|
53
|
+
if (stat?.isSocket())
|
|
54
|
+
return true;
|
|
55
|
+
} catch (e) {
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
async function startMcpDaemonServer(socketPath, serverBackendFactory) {
|
|
60
|
+
const normalizedPath = normalizeSocketPath(socketPath);
|
|
61
|
+
if (import_os.default.platform() !== "win32" && await socketExists(normalizedPath)) {
|
|
62
|
+
daemonDebug(`Socket already exists, removing: ${normalizedPath}`);
|
|
63
|
+
try {
|
|
64
|
+
await import_promises.default.unlink(normalizedPath);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
daemonDebug(`Failed to remove existing socket: ${error}`);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const backend = serverBackendFactory.create();
|
|
71
|
+
await backend.initialize?.({
|
|
72
|
+
name: "mcp-daemon",
|
|
73
|
+
version: "1.0.0",
|
|
74
|
+
roots: [],
|
|
75
|
+
timestamp: Date.now()
|
|
76
|
+
});
|
|
77
|
+
await import_promises.default.mkdir(import_path.default.dirname(normalizedPath), { recursive: true });
|
|
78
|
+
const server = import_net.default.createServer((socket) => {
|
|
79
|
+
daemonDebug("new client connection");
|
|
80
|
+
const connection = new import_socketConnection.SocketConnection(socket);
|
|
81
|
+
connection.onclose = () => {
|
|
82
|
+
daemonDebug("client disconnected");
|
|
83
|
+
};
|
|
84
|
+
connection.onmessage = async (message) => {
|
|
85
|
+
const { id, method, params } = message;
|
|
86
|
+
try {
|
|
87
|
+
daemonDebug("received command", method);
|
|
88
|
+
const response = await backend.callTool(method, params, () => {
|
|
89
|
+
});
|
|
90
|
+
daemonDebug("sending response", !!response);
|
|
91
|
+
if (response)
|
|
92
|
+
await connection.send({ id, result: response });
|
|
93
|
+
} catch (e) {
|
|
94
|
+
daemonDebug("command failed", e);
|
|
95
|
+
await connection.send({ id, error: e.message });
|
|
96
|
+
daemonDebug("error handling message", e);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
return new Promise((resolve, reject) => {
|
|
101
|
+
server.on("error", (error) => {
|
|
102
|
+
daemonDebug(`server error: ${error.message}`);
|
|
103
|
+
reject(error);
|
|
104
|
+
});
|
|
105
|
+
server.listen(normalizedPath, () => {
|
|
106
|
+
daemonDebug(`daemon server listening on ${normalizedPath}`);
|
|
107
|
+
resolve(normalizedPath);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
startMcpDaemonServer
|
|
114
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var socketConnection_exports = {};
|
|
20
|
+
__export(socketConnection_exports, {
|
|
21
|
+
SocketConnection: () => SocketConnection
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(socketConnection_exports);
|
|
24
|
+
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
25
|
+
const daemonDebug = (0, import_utilsBundle.debug)("pw:daemon");
|
|
26
|
+
class SocketConnection {
|
|
27
|
+
constructor(socket) {
|
|
28
|
+
this._pendingBuffers = [];
|
|
29
|
+
this._socket = socket;
|
|
30
|
+
socket.on("data", (buffer) => this._onData(buffer));
|
|
31
|
+
socket.on("close", () => {
|
|
32
|
+
this.onclose?.();
|
|
33
|
+
});
|
|
34
|
+
socket.on("error", (e) => daemonDebug(`error: ${e.message}`));
|
|
35
|
+
}
|
|
36
|
+
async send(message) {
|
|
37
|
+
await new Promise((resolve, reject) => {
|
|
38
|
+
this._socket.write(`${JSON.stringify(message)}
|
|
39
|
+
`, (error) => {
|
|
40
|
+
if (error)
|
|
41
|
+
reject(error);
|
|
42
|
+
else
|
|
43
|
+
resolve(void 0);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
close() {
|
|
48
|
+
this._socket.destroy();
|
|
49
|
+
}
|
|
50
|
+
_onData(buffer) {
|
|
51
|
+
let end = buffer.indexOf("\n");
|
|
52
|
+
if (end === -1) {
|
|
53
|
+
this._pendingBuffers.push(buffer);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this._pendingBuffers.push(buffer.slice(0, end));
|
|
57
|
+
const message = Buffer.concat(this._pendingBuffers).toString();
|
|
58
|
+
this._dispatchMessage(message);
|
|
59
|
+
let start = end + 1;
|
|
60
|
+
end = buffer.indexOf("\n", start);
|
|
61
|
+
while (end !== -1) {
|
|
62
|
+
const message2 = buffer.toString(void 0, start, end);
|
|
63
|
+
this._dispatchMessage(message2);
|
|
64
|
+
start = end + 1;
|
|
65
|
+
end = buffer.indexOf("\n", start);
|
|
66
|
+
}
|
|
67
|
+
this._pendingBuffers = [buffer.slice(start)];
|
|
68
|
+
}
|
|
69
|
+
_dispatchMessage(message) {
|
|
70
|
+
try {
|
|
71
|
+
this.onmessage?.(JSON.parse(message));
|
|
72
|
+
} catch (e) {
|
|
73
|
+
daemonDebug("failed to dispatch message", e);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
78
|
+
0 && (module.exports = {
|
|
79
|
+
SocketConnection
|
|
80
|
+
});
|