playwright 1.56.0-alpha-2025-09-03 → 1.56.0-alpha-2025-09-04
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 +2 -2
- package/ThirdPartyNotices.txt +3 -3
- package/lib/index.js +2 -2
- package/lib/matchers/toBeTruthy.js +3 -3
- package/lib/matchers/toEqual.js +3 -3
- package/lib/matchers/toMatchText.js +3 -3
- package/lib/mcp/browser/actions.d.js +16 -0
- package/lib/mcp/browser/browserContextFactory.js +227 -0
- package/lib/mcp/browser/browserServerBackend.js +82 -0
- package/lib/mcp/browser/codegen.js +64 -0
- package/lib/mcp/browser/config.js +279 -0
- package/lib/mcp/browser/context.js +229 -0
- package/lib/mcp/browser/response.js +163 -0
- package/lib/mcp/browser/sessionLog.js +160 -0
- package/lib/mcp/browser/tab.js +256 -0
- package/lib/mcp/browser/tools/common.js +63 -0
- package/lib/mcp/browser/tools/console.js +41 -0
- package/lib/mcp/browser/tools/dialogs.js +55 -0
- package/lib/mcp/browser/tools/evaluate.js +70 -0
- package/lib/mcp/browser/tools/files.js +52 -0
- package/lib/mcp/browser/tools/form.js +73 -0
- package/lib/mcp/browser/tools/install.js +69 -0
- package/lib/mcp/browser/tools/keyboard.js +95 -0
- package/lib/mcp/browser/tools/mouse.js +107 -0
- package/lib/mcp/browser/tools/navigate.js +62 -0
- package/lib/mcp/browser/tools/network.js +49 -0
- package/lib/mcp/{sdk/call.js → browser/tools/pdf.js} +27 -18
- package/lib/mcp/browser/tools/screenshot.js +94 -0
- package/lib/mcp/browser/tools/snapshot.js +162 -0
- package/lib/mcp/browser/tools/tabs.js +67 -0
- package/lib/mcp/browser/tools/tool.js +49 -0
- package/lib/mcp/browser/tools/utils.js +90 -0
- package/lib/mcp/browser/tools/verify.js +154 -0
- package/lib/mcp/browser/tools/wait.js +63 -0
- package/lib/mcp/browser/tools.js +42 -83
- package/lib/mcp/config.d.js +16 -0
- package/lib/mcp/extension/cdpRelay.js +348 -0
- package/lib/mcp/extension/extensionContextFactory.js +75 -0
- package/lib/mcp/{browser/tool.js → extension/protocol.js} +6 -8
- package/lib/mcp/index.js +61 -0
- package/lib/mcp/log.js +35 -0
- package/lib/mcp/program.js +96 -0
- package/lib/mcp/sdk/bundle.js +11 -1
- package/lib/mcp/sdk/exports.js +2 -2
- package/lib/mcp/sdk/http.js +21 -6
- package/lib/mcp/sdk/mdb.js +16 -15
- package/lib/mcp/sdk/proxyBackend.js +7 -6
- package/lib/mcp/sdk/server.js +6 -6
- package/lib/mcp/sdk/tool.js +1 -1
- package/lib/mcp/{browser/backend.js → test/browserBackend.js} +7 -9
- package/lib/mcp/test/browserTool.js +30 -0
- package/lib/mcp/test/browserTools.js +120 -0
- package/lib/mcp/test/{backend.js → testBackend.js} +12 -12
- package/lib/mcp/test/{context.js → testContext.js} +6 -6
- package/lib/mcp/test/{tool.js → testTool.js} +6 -6
- package/lib/mcp/test/{tools.js → testTools.js} +7 -7
- package/lib/mcpBundleImpl.js +11 -11
- package/lib/program.js +4 -4
- package/package.json +6 -3
|
@@ -0,0 +1,279 @@
|
|
|
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 config_exports = {};
|
|
30
|
+
__export(config_exports, {
|
|
31
|
+
commaSeparatedList: () => commaSeparatedList,
|
|
32
|
+
configFromCLIOptions: () => configFromCLIOptions,
|
|
33
|
+
outputFile: () => outputFile,
|
|
34
|
+
resolveCLIConfig: () => resolveCLIConfig,
|
|
35
|
+
resolveConfig: () => resolveConfig,
|
|
36
|
+
semicolonSeparatedList: () => semicolonSeparatedList
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(config_exports);
|
|
39
|
+
var import_fs = __toESM(require("fs"));
|
|
40
|
+
var import_os = __toESM(require("os"));
|
|
41
|
+
var import_path = __toESM(require("path"));
|
|
42
|
+
var import_playwright_core = require("playwright-core");
|
|
43
|
+
const defaultConfig = {
|
|
44
|
+
browser: {
|
|
45
|
+
browserName: "chromium",
|
|
46
|
+
launchOptions: {
|
|
47
|
+
channel: "chrome",
|
|
48
|
+
headless: import_os.default.platform() === "linux" && !process.env.DISPLAY,
|
|
49
|
+
chromiumSandbox: true
|
|
50
|
+
},
|
|
51
|
+
contextOptions: {
|
|
52
|
+
viewport: null
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
network: {
|
|
56
|
+
allowedOrigins: void 0,
|
|
57
|
+
blockedOrigins: void 0
|
|
58
|
+
},
|
|
59
|
+
server: {},
|
|
60
|
+
saveTrace: false
|
|
61
|
+
};
|
|
62
|
+
async function resolveConfig(config) {
|
|
63
|
+
return mergeConfig(defaultConfig, config);
|
|
64
|
+
}
|
|
65
|
+
async function resolveCLIConfig(cliOptions) {
|
|
66
|
+
const configInFile = await loadConfig(cliOptions.config);
|
|
67
|
+
const envOverrides = configFromEnv();
|
|
68
|
+
const cliOverrides = configFromCLIOptions(cliOptions);
|
|
69
|
+
let result = defaultConfig;
|
|
70
|
+
result = mergeConfig(result, configInFile);
|
|
71
|
+
result = mergeConfig(result, envOverrides);
|
|
72
|
+
result = mergeConfig(result, cliOverrides);
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
function configFromCLIOptions(cliOptions) {
|
|
76
|
+
let browserName;
|
|
77
|
+
let channel;
|
|
78
|
+
switch (cliOptions.browser) {
|
|
79
|
+
case "chrome":
|
|
80
|
+
case "chrome-beta":
|
|
81
|
+
case "chrome-canary":
|
|
82
|
+
case "chrome-dev":
|
|
83
|
+
case "chromium":
|
|
84
|
+
case "msedge":
|
|
85
|
+
case "msedge-beta":
|
|
86
|
+
case "msedge-canary":
|
|
87
|
+
case "msedge-dev":
|
|
88
|
+
browserName = "chromium";
|
|
89
|
+
channel = cliOptions.browser;
|
|
90
|
+
break;
|
|
91
|
+
case "firefox":
|
|
92
|
+
browserName = "firefox";
|
|
93
|
+
break;
|
|
94
|
+
case "webkit":
|
|
95
|
+
browserName = "webkit";
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
const launchOptions = {
|
|
99
|
+
channel,
|
|
100
|
+
executablePath: cliOptions.executablePath,
|
|
101
|
+
headless: cliOptions.headless
|
|
102
|
+
};
|
|
103
|
+
if (cliOptions.sandbox === false)
|
|
104
|
+
launchOptions.chromiumSandbox = false;
|
|
105
|
+
if (cliOptions.proxyServer) {
|
|
106
|
+
launchOptions.proxy = {
|
|
107
|
+
server: cliOptions.proxyServer
|
|
108
|
+
};
|
|
109
|
+
if (cliOptions.proxyBypass)
|
|
110
|
+
launchOptions.proxy.bypass = cliOptions.proxyBypass;
|
|
111
|
+
}
|
|
112
|
+
if (cliOptions.device && cliOptions.cdpEndpoint)
|
|
113
|
+
throw new Error("Device emulation is not supported with cdpEndpoint.");
|
|
114
|
+
const contextOptions = cliOptions.device ? import_playwright_core.devices[cliOptions.device] : {};
|
|
115
|
+
if (cliOptions.storageState)
|
|
116
|
+
contextOptions.storageState = cliOptions.storageState;
|
|
117
|
+
if (cliOptions.userAgent)
|
|
118
|
+
contextOptions.userAgent = cliOptions.userAgent;
|
|
119
|
+
if (cliOptions.viewportSize) {
|
|
120
|
+
try {
|
|
121
|
+
const [width, height] = cliOptions.viewportSize.split(",").map((n) => +n);
|
|
122
|
+
if (isNaN(width) || isNaN(height))
|
|
123
|
+
throw new Error("bad values");
|
|
124
|
+
contextOptions.viewport = { width, height };
|
|
125
|
+
} catch (e) {
|
|
126
|
+
throw new Error('Invalid viewport size format: use "width,height", for example --viewport-size="800,600"');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (cliOptions.ignoreHttpsErrors)
|
|
130
|
+
contextOptions.ignoreHTTPSErrors = true;
|
|
131
|
+
if (cliOptions.blockServiceWorkers)
|
|
132
|
+
contextOptions.serviceWorkers = "block";
|
|
133
|
+
const result = {
|
|
134
|
+
browser: {
|
|
135
|
+
browserName,
|
|
136
|
+
isolated: cliOptions.isolated,
|
|
137
|
+
userDataDir: cliOptions.userDataDir,
|
|
138
|
+
launchOptions,
|
|
139
|
+
contextOptions,
|
|
140
|
+
cdpEndpoint: cliOptions.cdpEndpoint
|
|
141
|
+
},
|
|
142
|
+
server: {
|
|
143
|
+
port: cliOptions.port,
|
|
144
|
+
host: cliOptions.host
|
|
145
|
+
},
|
|
146
|
+
capabilities: cliOptions.caps,
|
|
147
|
+
network: {
|
|
148
|
+
allowedOrigins: cliOptions.allowedOrigins,
|
|
149
|
+
blockedOrigins: cliOptions.blockedOrigins
|
|
150
|
+
},
|
|
151
|
+
saveSession: cliOptions.saveSession,
|
|
152
|
+
saveTrace: cliOptions.saveTrace,
|
|
153
|
+
outputDir: cliOptions.outputDir,
|
|
154
|
+
imageResponses: cliOptions.imageResponses
|
|
155
|
+
};
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
function configFromEnv() {
|
|
159
|
+
const options = {};
|
|
160
|
+
options.allowedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_ALLOWED_ORIGINS);
|
|
161
|
+
options.blockedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_BLOCKED_ORIGINS);
|
|
162
|
+
options.blockServiceWorkers = envToBoolean(process.env.PLAYWRIGHT_MCP_BLOCK_SERVICE_WORKERS);
|
|
163
|
+
options.browser = envToString(process.env.PLAYWRIGHT_MCP_BROWSER);
|
|
164
|
+
options.caps = commaSeparatedList(process.env.PLAYWRIGHT_MCP_CAPS);
|
|
165
|
+
options.cdpEndpoint = envToString(process.env.PLAYWRIGHT_MCP_CDP_ENDPOINT);
|
|
166
|
+
options.config = envToString(process.env.PLAYWRIGHT_MCP_CONFIG);
|
|
167
|
+
options.device = envToString(process.env.PLAYWRIGHT_MCP_DEVICE);
|
|
168
|
+
options.executablePath = envToString(process.env.PLAYWRIGHT_MCP_EXECUTABLE_PATH);
|
|
169
|
+
options.headless = envToBoolean(process.env.PLAYWRIGHT_MCP_HEADLESS);
|
|
170
|
+
options.host = envToString(process.env.PLAYWRIGHT_MCP_HOST);
|
|
171
|
+
options.ignoreHttpsErrors = envToBoolean(process.env.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);
|
|
172
|
+
options.isolated = envToBoolean(process.env.PLAYWRIGHT_MCP_ISOLATED);
|
|
173
|
+
if (process.env.PLAYWRIGHT_MCP_IMAGE_RESPONSES === "omit")
|
|
174
|
+
options.imageResponses = "omit";
|
|
175
|
+
options.sandbox = envToBoolean(process.env.PLAYWRIGHT_MCP_SANDBOX);
|
|
176
|
+
options.outputDir = envToString(process.env.PLAYWRIGHT_MCP_OUTPUT_DIR);
|
|
177
|
+
options.port = envToNumber(process.env.PLAYWRIGHT_MCP_PORT);
|
|
178
|
+
options.proxyBypass = envToString(process.env.PLAYWRIGHT_MCP_PROXY_BYPASS);
|
|
179
|
+
options.proxyServer = envToString(process.env.PLAYWRIGHT_MCP_PROXY_SERVER);
|
|
180
|
+
options.saveTrace = envToBoolean(process.env.PLAYWRIGHT_MCP_SAVE_TRACE);
|
|
181
|
+
options.storageState = envToString(process.env.PLAYWRIGHT_MCP_STORAGE_STATE);
|
|
182
|
+
options.userAgent = envToString(process.env.PLAYWRIGHT_MCP_USER_AGENT);
|
|
183
|
+
options.userDataDir = envToString(process.env.PLAYWRIGHT_MCP_USER_DATA_DIR);
|
|
184
|
+
options.viewportSize = envToString(process.env.PLAYWRIGHT_MCP_VIEWPORT_SIZE);
|
|
185
|
+
return configFromCLIOptions(options);
|
|
186
|
+
}
|
|
187
|
+
async function loadConfig(configFile) {
|
|
188
|
+
if (!configFile)
|
|
189
|
+
return {};
|
|
190
|
+
try {
|
|
191
|
+
return JSON.parse(await import_fs.default.promises.readFile(configFile, "utf8"));
|
|
192
|
+
} catch (error) {
|
|
193
|
+
throw new Error(`Failed to load config file: ${configFile}, ${error}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
async function outputFile(config, rootPath, name) {
|
|
197
|
+
const outputDir = config.outputDir ?? (rootPath ? import_path.default.join(rootPath, ".playwright-mcp") : void 0) ?? import_path.default.join(import_os.default.tmpdir(), "playwright-mcp-output", sanitizeForFilePath((/* @__PURE__ */ new Date()).toISOString()));
|
|
198
|
+
await import_fs.default.promises.mkdir(outputDir, { recursive: true });
|
|
199
|
+
const fileName = sanitizeForFilePath(name);
|
|
200
|
+
return import_path.default.join(outputDir, fileName);
|
|
201
|
+
}
|
|
202
|
+
function pickDefined(obj) {
|
|
203
|
+
return Object.fromEntries(
|
|
204
|
+
Object.entries(obj ?? {}).filter(([_, v]) => v !== void 0)
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
function mergeConfig(base, overrides) {
|
|
208
|
+
const browser = {
|
|
209
|
+
...pickDefined(base.browser),
|
|
210
|
+
...pickDefined(overrides.browser),
|
|
211
|
+
browserName: overrides.browser?.browserName ?? base.browser?.browserName ?? "chromium",
|
|
212
|
+
isolated: overrides.browser?.isolated ?? base.browser?.isolated ?? false,
|
|
213
|
+
launchOptions: {
|
|
214
|
+
...pickDefined(base.browser?.launchOptions),
|
|
215
|
+
...pickDefined(overrides.browser?.launchOptions),
|
|
216
|
+
...{ assistantMode: true }
|
|
217
|
+
},
|
|
218
|
+
contextOptions: {
|
|
219
|
+
...pickDefined(base.browser?.contextOptions),
|
|
220
|
+
...pickDefined(overrides.browser?.contextOptions)
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
if (browser.browserName !== "chromium" && browser.launchOptions)
|
|
224
|
+
delete browser.launchOptions.channel;
|
|
225
|
+
return {
|
|
226
|
+
...pickDefined(base),
|
|
227
|
+
...pickDefined(overrides),
|
|
228
|
+
browser,
|
|
229
|
+
network: {
|
|
230
|
+
...pickDefined(base.network),
|
|
231
|
+
...pickDefined(overrides.network)
|
|
232
|
+
},
|
|
233
|
+
server: {
|
|
234
|
+
...pickDefined(base.server),
|
|
235
|
+
...pickDefined(overrides.server)
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function semicolonSeparatedList(value) {
|
|
240
|
+
if (!value)
|
|
241
|
+
return void 0;
|
|
242
|
+
return value.split(";").map((v) => v.trim());
|
|
243
|
+
}
|
|
244
|
+
function commaSeparatedList(value) {
|
|
245
|
+
if (!value)
|
|
246
|
+
return void 0;
|
|
247
|
+
return value.split(",").map((v) => v.trim());
|
|
248
|
+
}
|
|
249
|
+
function envToNumber(value) {
|
|
250
|
+
if (!value)
|
|
251
|
+
return void 0;
|
|
252
|
+
return +value;
|
|
253
|
+
}
|
|
254
|
+
function envToBoolean(value) {
|
|
255
|
+
if (value === "true" || value === "1")
|
|
256
|
+
return true;
|
|
257
|
+
if (value === "false" || value === "0")
|
|
258
|
+
return false;
|
|
259
|
+
return void 0;
|
|
260
|
+
}
|
|
261
|
+
function envToString(value) {
|
|
262
|
+
return value ? value.trim() : void 0;
|
|
263
|
+
}
|
|
264
|
+
function sanitizeForFilePath(s) {
|
|
265
|
+
const sanitize = (s2) => s2.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, "-");
|
|
266
|
+
const separator = s.lastIndexOf(".");
|
|
267
|
+
if (separator === -1)
|
|
268
|
+
return sanitize(s);
|
|
269
|
+
return sanitize(s.substring(0, separator)) + "." + sanitize(s.substring(separator + 1));
|
|
270
|
+
}
|
|
271
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
272
|
+
0 && (module.exports = {
|
|
273
|
+
commaSeparatedList,
|
|
274
|
+
configFromCLIOptions,
|
|
275
|
+
outputFile,
|
|
276
|
+
resolveCLIConfig,
|
|
277
|
+
resolveConfig,
|
|
278
|
+
semicolonSeparatedList
|
|
279
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
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 context_exports = {};
|
|
20
|
+
__export(context_exports, {
|
|
21
|
+
Context: () => Context,
|
|
22
|
+
InputRecorder: () => InputRecorder
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(context_exports);
|
|
25
|
+
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
26
|
+
var import_log = require("../log");
|
|
27
|
+
var import_tab = require("./tab");
|
|
28
|
+
var import_config = require("./config");
|
|
29
|
+
const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test");
|
|
30
|
+
class Context {
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this._tabs = [];
|
|
33
|
+
this._abortController = new AbortController();
|
|
34
|
+
this.tools = options.tools;
|
|
35
|
+
this.config = options.config;
|
|
36
|
+
this.sessionLog = options.sessionLog;
|
|
37
|
+
this.options = options;
|
|
38
|
+
this._browserContextFactory = options.browserContextFactory;
|
|
39
|
+
this._clientInfo = options.clientInfo;
|
|
40
|
+
testDebug("create context");
|
|
41
|
+
Context._allContexts.add(this);
|
|
42
|
+
}
|
|
43
|
+
static {
|
|
44
|
+
this._allContexts = /* @__PURE__ */ new Set();
|
|
45
|
+
}
|
|
46
|
+
static async disposeAll() {
|
|
47
|
+
await Promise.all([...Context._allContexts].map((context) => context.dispose()));
|
|
48
|
+
}
|
|
49
|
+
tabs() {
|
|
50
|
+
return this._tabs;
|
|
51
|
+
}
|
|
52
|
+
currentTab() {
|
|
53
|
+
return this._currentTab;
|
|
54
|
+
}
|
|
55
|
+
currentTabOrDie() {
|
|
56
|
+
if (!this._currentTab)
|
|
57
|
+
throw new Error('No open pages available. Use the "browser_navigate" tool to navigate to a page first.');
|
|
58
|
+
return this._currentTab;
|
|
59
|
+
}
|
|
60
|
+
async newTab() {
|
|
61
|
+
const { browserContext } = await this._ensureBrowserContext();
|
|
62
|
+
const page = await browserContext.newPage();
|
|
63
|
+
this._currentTab = this._tabs.find((t) => t.page === page);
|
|
64
|
+
return this._currentTab;
|
|
65
|
+
}
|
|
66
|
+
async selectTab(index) {
|
|
67
|
+
const tab = this._tabs[index];
|
|
68
|
+
if (!tab)
|
|
69
|
+
throw new Error(`Tab ${index} not found`);
|
|
70
|
+
await tab.page.bringToFront();
|
|
71
|
+
this._currentTab = tab;
|
|
72
|
+
return tab;
|
|
73
|
+
}
|
|
74
|
+
async ensureTab() {
|
|
75
|
+
const { browserContext } = await this._ensureBrowserContext();
|
|
76
|
+
if (!this._currentTab)
|
|
77
|
+
await browserContext.newPage();
|
|
78
|
+
return this._currentTab;
|
|
79
|
+
}
|
|
80
|
+
async closeTab(index) {
|
|
81
|
+
const tab = index === void 0 ? this._currentTab : this._tabs[index];
|
|
82
|
+
if (!tab)
|
|
83
|
+
throw new Error(`Tab ${index} not found`);
|
|
84
|
+
const url = tab.page.url();
|
|
85
|
+
await tab.page.close();
|
|
86
|
+
return url;
|
|
87
|
+
}
|
|
88
|
+
async outputFile(name) {
|
|
89
|
+
return (0, import_config.outputFile)(this.config, this._clientInfo.rootPath, name);
|
|
90
|
+
}
|
|
91
|
+
_onPageCreated(page) {
|
|
92
|
+
const tab = new import_tab.Tab(this, page, (tab2) => this._onPageClosed(tab2));
|
|
93
|
+
this._tabs.push(tab);
|
|
94
|
+
if (!this._currentTab)
|
|
95
|
+
this._currentTab = tab;
|
|
96
|
+
}
|
|
97
|
+
_onPageClosed(tab) {
|
|
98
|
+
const index = this._tabs.indexOf(tab);
|
|
99
|
+
if (index === -1)
|
|
100
|
+
return;
|
|
101
|
+
this._tabs.splice(index, 1);
|
|
102
|
+
if (this._currentTab === tab)
|
|
103
|
+
this._currentTab = this._tabs[Math.min(index, this._tabs.length - 1)];
|
|
104
|
+
if (!this._tabs.length)
|
|
105
|
+
void this.closeBrowserContext();
|
|
106
|
+
}
|
|
107
|
+
async closeBrowserContext() {
|
|
108
|
+
if (!this._closeBrowserContextPromise)
|
|
109
|
+
this._closeBrowserContextPromise = this._closeBrowserContextImpl().catch(import_log.logUnhandledError);
|
|
110
|
+
await this._closeBrowserContextPromise;
|
|
111
|
+
this._closeBrowserContextPromise = void 0;
|
|
112
|
+
}
|
|
113
|
+
isRunningTool() {
|
|
114
|
+
return this._runningToolName !== void 0;
|
|
115
|
+
}
|
|
116
|
+
setRunningTool(name) {
|
|
117
|
+
this._runningToolName = name;
|
|
118
|
+
}
|
|
119
|
+
async _closeBrowserContextImpl() {
|
|
120
|
+
if (!this._browserContextPromise)
|
|
121
|
+
return;
|
|
122
|
+
testDebug("close context");
|
|
123
|
+
const promise = this._browserContextPromise;
|
|
124
|
+
this._browserContextPromise = void 0;
|
|
125
|
+
await promise.then(async ({ browserContext, close }) => {
|
|
126
|
+
if (this.config.saveTrace)
|
|
127
|
+
await browserContext.tracing.stop();
|
|
128
|
+
await close();
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async dispose() {
|
|
132
|
+
this._abortController.abort("MCP context disposed");
|
|
133
|
+
await this.closeBrowserContext();
|
|
134
|
+
Context._allContexts.delete(this);
|
|
135
|
+
}
|
|
136
|
+
async _setupRequestInterception(context) {
|
|
137
|
+
if (this.config.network?.allowedOrigins?.length) {
|
|
138
|
+
await context.route("**", (route) => route.abort("blockedbyclient"));
|
|
139
|
+
for (const origin of this.config.network.allowedOrigins)
|
|
140
|
+
await context.route(`*://${origin}/**`, (route) => route.continue());
|
|
141
|
+
}
|
|
142
|
+
if (this.config.network?.blockedOrigins?.length) {
|
|
143
|
+
for (const origin of this.config.network.blockedOrigins)
|
|
144
|
+
await context.route(`*://${origin}/**`, (route) => route.abort("blockedbyclient"));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
_ensureBrowserContext() {
|
|
148
|
+
if (!this._browserContextPromise) {
|
|
149
|
+
this._browserContextPromise = this._setupBrowserContext();
|
|
150
|
+
this._browserContextPromise.catch(() => {
|
|
151
|
+
this._browserContextPromise = void 0;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return this._browserContextPromise;
|
|
155
|
+
}
|
|
156
|
+
async _setupBrowserContext() {
|
|
157
|
+
if (this._closeBrowserContextPromise)
|
|
158
|
+
throw new Error("Another browser context is being closed.");
|
|
159
|
+
const result = await this._browserContextFactory.createContext(this._clientInfo, this._abortController.signal, this._runningToolName);
|
|
160
|
+
const { browserContext } = result;
|
|
161
|
+
await this._setupRequestInterception(browserContext);
|
|
162
|
+
if (this.sessionLog)
|
|
163
|
+
await InputRecorder.create(this, browserContext);
|
|
164
|
+
for (const page of browserContext.pages())
|
|
165
|
+
this._onPageCreated(page);
|
|
166
|
+
browserContext.on("page", (page) => this._onPageCreated(page));
|
|
167
|
+
if (this.config.saveTrace) {
|
|
168
|
+
await browserContext.tracing.start({
|
|
169
|
+
name: "trace",
|
|
170
|
+
screenshots: false,
|
|
171
|
+
snapshots: true,
|
|
172
|
+
sources: false
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
class InputRecorder {
|
|
179
|
+
constructor(context, browserContext) {
|
|
180
|
+
this._context = context;
|
|
181
|
+
this._browserContext = browserContext;
|
|
182
|
+
}
|
|
183
|
+
static async create(context, browserContext) {
|
|
184
|
+
const recorder = new InputRecorder(context, browserContext);
|
|
185
|
+
await recorder._initialize();
|
|
186
|
+
return recorder;
|
|
187
|
+
}
|
|
188
|
+
async _initialize() {
|
|
189
|
+
const sessionLog = this._context.sessionLog;
|
|
190
|
+
await this._browserContext._enableRecorder({
|
|
191
|
+
mode: "recording",
|
|
192
|
+
recorderMode: "api"
|
|
193
|
+
}, {
|
|
194
|
+
actionAdded: (page, data, code) => {
|
|
195
|
+
if (this._context.isRunningTool())
|
|
196
|
+
return;
|
|
197
|
+
const tab = import_tab.Tab.forPage(page);
|
|
198
|
+
if (tab)
|
|
199
|
+
sessionLog.logUserAction(data.action, tab, code, false);
|
|
200
|
+
},
|
|
201
|
+
actionUpdated: (page, data, code) => {
|
|
202
|
+
if (this._context.isRunningTool())
|
|
203
|
+
return;
|
|
204
|
+
const tab = import_tab.Tab.forPage(page);
|
|
205
|
+
if (tab)
|
|
206
|
+
sessionLog.logUserAction(data.action, tab, code, true);
|
|
207
|
+
},
|
|
208
|
+
signalAdded: (page, data) => {
|
|
209
|
+
if (this._context.isRunningTool())
|
|
210
|
+
return;
|
|
211
|
+
if (data.signal.name !== "navigation")
|
|
212
|
+
return;
|
|
213
|
+
const tab = import_tab.Tab.forPage(page);
|
|
214
|
+
const navigateAction = {
|
|
215
|
+
name: "navigate",
|
|
216
|
+
url: data.signal.url,
|
|
217
|
+
signals: []
|
|
218
|
+
};
|
|
219
|
+
if (tab)
|
|
220
|
+
sessionLog.logUserAction(navigateAction, tab, `await page.goto('${data.signal.url}');`, false);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
226
|
+
0 && (module.exports = {
|
|
227
|
+
Context,
|
|
228
|
+
InputRecorder
|
|
229
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
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 response_exports = {};
|
|
20
|
+
__export(response_exports, {
|
|
21
|
+
Response: () => Response
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(response_exports);
|
|
24
|
+
var import_tab = require("./tab");
|
|
25
|
+
class Response {
|
|
26
|
+
constructor(context, toolName, toolArgs) {
|
|
27
|
+
this._result = [];
|
|
28
|
+
this._code = [];
|
|
29
|
+
this._images = [];
|
|
30
|
+
this._includeSnapshot = false;
|
|
31
|
+
this._includeTabs = false;
|
|
32
|
+
this._context = context;
|
|
33
|
+
this.toolName = toolName;
|
|
34
|
+
this.toolArgs = toolArgs;
|
|
35
|
+
}
|
|
36
|
+
addResult(result) {
|
|
37
|
+
this._result.push(result);
|
|
38
|
+
}
|
|
39
|
+
addError(error) {
|
|
40
|
+
this._result.push(error);
|
|
41
|
+
this._isError = true;
|
|
42
|
+
}
|
|
43
|
+
isError() {
|
|
44
|
+
return this._isError;
|
|
45
|
+
}
|
|
46
|
+
result() {
|
|
47
|
+
return this._result.join("\n");
|
|
48
|
+
}
|
|
49
|
+
addCode(code) {
|
|
50
|
+
this._code.push(code);
|
|
51
|
+
}
|
|
52
|
+
code() {
|
|
53
|
+
return this._code.join("\n");
|
|
54
|
+
}
|
|
55
|
+
addImage(image) {
|
|
56
|
+
this._images.push(image);
|
|
57
|
+
}
|
|
58
|
+
images() {
|
|
59
|
+
return this._images;
|
|
60
|
+
}
|
|
61
|
+
setIncludeSnapshot() {
|
|
62
|
+
this._includeSnapshot = true;
|
|
63
|
+
}
|
|
64
|
+
setIncludeTabs() {
|
|
65
|
+
this._includeTabs = true;
|
|
66
|
+
}
|
|
67
|
+
async finish() {
|
|
68
|
+
if (this._includeSnapshot && this._context.currentTab())
|
|
69
|
+
this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot();
|
|
70
|
+
for (const tab of this._context.tabs())
|
|
71
|
+
await tab.updateTitle();
|
|
72
|
+
}
|
|
73
|
+
tabSnapshot() {
|
|
74
|
+
return this._tabSnapshot;
|
|
75
|
+
}
|
|
76
|
+
serialize() {
|
|
77
|
+
const response = [];
|
|
78
|
+
if (this._result.length) {
|
|
79
|
+
response.push("### Result");
|
|
80
|
+
response.push(this._result.join("\n"));
|
|
81
|
+
response.push("");
|
|
82
|
+
}
|
|
83
|
+
if (this._code.length) {
|
|
84
|
+
response.push(`### Ran Playwright code
|
|
85
|
+
\`\`\`js
|
|
86
|
+
${this._code.join("\n")}
|
|
87
|
+
\`\`\``);
|
|
88
|
+
response.push("");
|
|
89
|
+
}
|
|
90
|
+
if (this._includeSnapshot || this._includeTabs)
|
|
91
|
+
response.push(...renderTabsMarkdown(this._context.tabs(), this._includeTabs));
|
|
92
|
+
if (this._tabSnapshot?.modalStates.length) {
|
|
93
|
+
response.push(...(0, import_tab.renderModalStates)(this._context, this._tabSnapshot.modalStates));
|
|
94
|
+
response.push("");
|
|
95
|
+
} else if (this._tabSnapshot) {
|
|
96
|
+
response.push(renderTabSnapshot(this._tabSnapshot));
|
|
97
|
+
response.push("");
|
|
98
|
+
}
|
|
99
|
+
const content = [
|
|
100
|
+
{ type: "text", text: response.join("\n") }
|
|
101
|
+
];
|
|
102
|
+
if (this._context.config.imageResponses !== "omit") {
|
|
103
|
+
for (const image of this._images)
|
|
104
|
+
content.push({ type: "image", data: image.data.toString("base64"), mimeType: image.contentType });
|
|
105
|
+
}
|
|
106
|
+
return { content, isError: this._isError };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function renderTabSnapshot(tabSnapshot) {
|
|
110
|
+
const lines = [];
|
|
111
|
+
if (tabSnapshot.consoleMessages.length) {
|
|
112
|
+
lines.push(`### New console messages`);
|
|
113
|
+
for (const message of tabSnapshot.consoleMessages)
|
|
114
|
+
lines.push(`- ${trim(message.toString(), 100)}`);
|
|
115
|
+
lines.push("");
|
|
116
|
+
}
|
|
117
|
+
if (tabSnapshot.downloads.length) {
|
|
118
|
+
lines.push(`### Downloads`);
|
|
119
|
+
for (const entry of tabSnapshot.downloads) {
|
|
120
|
+
if (entry.finished)
|
|
121
|
+
lines.push(`- Downloaded file ${entry.download.suggestedFilename()} to ${entry.outputFile}`);
|
|
122
|
+
else
|
|
123
|
+
lines.push(`- Downloading file ${entry.download.suggestedFilename()} ...`);
|
|
124
|
+
}
|
|
125
|
+
lines.push("");
|
|
126
|
+
}
|
|
127
|
+
lines.push(`### Page state`);
|
|
128
|
+
lines.push(`- Page URL: ${tabSnapshot.url}`);
|
|
129
|
+
lines.push(`- Page Title: ${tabSnapshot.title}`);
|
|
130
|
+
lines.push(`- Page Snapshot:`);
|
|
131
|
+
lines.push("```yaml");
|
|
132
|
+
lines.push(tabSnapshot.ariaSnapshot);
|
|
133
|
+
lines.push("```");
|
|
134
|
+
return lines.join("\n");
|
|
135
|
+
}
|
|
136
|
+
function renderTabsMarkdown(tabs, force = false) {
|
|
137
|
+
if (tabs.length === 1 && !force)
|
|
138
|
+
return [];
|
|
139
|
+
if (!tabs.length) {
|
|
140
|
+
return [
|
|
141
|
+
"### Open tabs",
|
|
142
|
+
'No open tabs. Use the "browser_navigate" tool to navigate to a page first.',
|
|
143
|
+
""
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
const lines = ["### Open tabs"];
|
|
147
|
+
for (let i = 0; i < tabs.length; i++) {
|
|
148
|
+
const tab = tabs[i];
|
|
149
|
+
const current = tab.isCurrentTab() ? " (current)" : "";
|
|
150
|
+
lines.push(`- ${i}:${current} [${tab.lastTitle()}] (${tab.page.url()})`);
|
|
151
|
+
}
|
|
152
|
+
lines.push("");
|
|
153
|
+
return lines;
|
|
154
|
+
}
|
|
155
|
+
function trim(text, maxLength) {
|
|
156
|
+
if (text.length <= maxLength)
|
|
157
|
+
return text;
|
|
158
|
+
return text.slice(0, maxLength) + "...";
|
|
159
|
+
}
|
|
160
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
161
|
+
0 && (module.exports = {
|
|
162
|
+
Response
|
|
163
|
+
});
|