chrome-devtools-mcp 1.9.0 → 1.10.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.
- package/build/src/BrowserManager.js +401 -0
- package/build/src/McpContext.js +5 -5
- package/build/src/McpPage.js +107 -3
- package/build/src/McpResponse.js +68 -3
- package/build/src/ToolHandler.js +172 -118
- package/build/src/bin/chrome-devtools-mcp-main.js +11 -6
- package/build/src/bin/chrome-devtools.js +6 -12
- package/build/src/collectors/PageCollector.js +66 -3
- package/build/src/config/browser-options.js +2 -0
- package/build/src/config/category-options.js +13 -4
- package/build/src/config/cli-commands.js +26 -0
- package/build/src/config/cli-options.js +32 -0
- package/build/src/config/mcp-options.js +40 -1
- package/build/src/devtools/DevToolsCommentBridge.js +91 -0
- package/build/src/formatters/CommentFormatter.js +82 -0
- package/build/src/formatters/CssFormatter.js +812 -0
- package/build/src/index.js +53 -72
- package/build/src/processors/ChunkedTraceParser.js +361 -0
- package/build/src/processors/PerformanceTrace.js +60 -13
- package/build/src/telemetry/ClearcutLogger.js +91 -34
- package/build/src/telemetry/metricsRegistry.js +3 -0
- package/build/src/telemetry/persistence.js +17 -7
- package/build/src/telemetry/transformation.js +53 -4
- package/build/src/telemetry/types.js +1 -0
- package/build/src/third_party/THIRD_PARTY_NOTICES +685 -131
- package/build/src/third_party/bundled-packages.json +6 -3
- package/build/src/third_party/devtools-formatter-worker.js +136 -5
- package/build/src/third_party/devtools-heap-snapshot-worker.js +567 -6
- package/build/src/third_party/index.js +54945 -35497
- package/build/src/third_party/issue-descriptions/webInstallManifestMissingId.md +3 -0
- package/build/src/third_party/issue-descriptions/webInstallManifestMissingNameOrShortName.md +3 -0
- package/build/src/third_party/issue-descriptions/webInstallManifestParsingOrNetworkError.md +3 -0
- package/build/src/third_party/issue-descriptions/webInstallNoManifest.md +3 -0
- package/build/src/third_party/issue-descriptions/webInstallStartUrlInvalid.md +3 -0
- package/build/src/tools/ToolDefinition.js +13 -21
- package/build/src/tools/comments.js +169 -0
- package/build/src/tools/console.js +3 -3
- package/build/src/tools/css.js +45 -0
- package/build/src/tools/emulation.js +5 -5
- package/build/src/tools/extensions.js +10 -10
- package/build/src/tools/input.js +159 -87
- package/build/src/tools/lighthouse.js +18 -7
- package/build/src/tools/memory.js +26 -26
- package/build/src/tools/network.js +4 -4
- package/build/src/tools/pages.js +14 -14
- package/build/src/tools/performance.js +6 -6
- package/build/src/tools/pwa.js +8 -8
- package/build/src/tools/screencast.js +4 -4
- package/build/src/tools/screenshot.js +2 -2
- package/build/src/tools/script.js +7 -7
- package/build/src/tools/slim/tools.js +5 -5
- package/build/src/tools/snapshot.js +4 -4
- package/build/src/tools/thirdPartyDeveloper.js +4 -4
- package/build/src/tools/tools.js +6 -9
- package/build/src/tools/webmcp.js +4 -4
- package/build/src/types.js +4 -1
- package/build/src/utils/WaitForHelper.js +14 -8
- package/build/src/utils/logger.js +7 -6
- package/build/src/version.js +1 -1
- package/package.json +8 -5
- package/skills/chrome-devtools/SKILL.md +2 -1
- package/skills/chrome-devtools-cli/SKILL.md +2 -0
- package/build/src/browser.js +0 -233
- package/build/src/third_party/issue-descriptions/cookieExcludeBlockedWithinRelatedWebsiteSet.md +0 -4
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
7
|
+
if (value !== null && value !== void 0) {
|
|
8
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
9
|
+
var dispose, inner;
|
|
10
|
+
if (async) {
|
|
11
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
12
|
+
dispose = value[Symbol.asyncDispose];
|
|
13
|
+
}
|
|
14
|
+
if (dispose === void 0) {
|
|
15
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
16
|
+
dispose = value[Symbol.dispose];
|
|
17
|
+
if (async) inner = dispose;
|
|
18
|
+
}
|
|
19
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
20
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
21
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
22
|
+
}
|
|
23
|
+
else if (async) {
|
|
24
|
+
env.stack.push({ async: true });
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
};
|
|
28
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
29
|
+
return function (env) {
|
|
30
|
+
function fail(e) {
|
|
31
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
32
|
+
env.hasError = true;
|
|
33
|
+
}
|
|
34
|
+
var r, s = 0;
|
|
35
|
+
function next() {
|
|
36
|
+
while (r = env.stack.pop()) {
|
|
37
|
+
try {
|
|
38
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
39
|
+
if (r.dispose) {
|
|
40
|
+
var result = r.dispose.call(r.value);
|
|
41
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
42
|
+
}
|
|
43
|
+
else s |= 1;
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
fail(e);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
50
|
+
if (env.hasError) throw env.error;
|
|
51
|
+
}
|
|
52
|
+
return next();
|
|
53
|
+
};
|
|
54
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
55
|
+
var e = new Error(message);
|
|
56
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
57
|
+
});
|
|
58
|
+
var _a;
|
|
59
|
+
import { execSync } from 'node:child_process';
|
|
60
|
+
import fs from 'node:fs';
|
|
61
|
+
import os from 'node:os';
|
|
62
|
+
import path from 'node:path';
|
|
63
|
+
import { Mutex, puppeteer } from './third_party/index.js';
|
|
64
|
+
import { logger, puppeteerLogger } from './utils/logger.js';
|
|
65
|
+
import { isAllowedUrl } from './utils/url.js';
|
|
66
|
+
export class BrowserManager {
|
|
67
|
+
#browser;
|
|
68
|
+
#browserMode;
|
|
69
|
+
#initPromise;
|
|
70
|
+
#mutex = new Mutex();
|
|
71
|
+
#closingCount = 0;
|
|
72
|
+
#serverArgs;
|
|
73
|
+
#options;
|
|
74
|
+
constructor(serverArgs, options = {}) {
|
|
75
|
+
this.#serverArgs = serverArgs;
|
|
76
|
+
this.#options = options;
|
|
77
|
+
}
|
|
78
|
+
static makeTargetFilter(enableExtensions = false) {
|
|
79
|
+
return function targetFilter(target) {
|
|
80
|
+
const url = target.url();
|
|
81
|
+
if (!url) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return isAllowedUrl(url, { categoryExtensions: enableExtensions });
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
static detectDisplay() {
|
|
88
|
+
// Only detect display on Linux/UNIX.
|
|
89
|
+
if (os.platform() === 'win32' || os.platform() === 'darwin') {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!process.env['DISPLAY']) {
|
|
93
|
+
try {
|
|
94
|
+
const result = execSync(`ps -u $(id -u) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\\0' '\\n' | grep -m1 '^DISPLAY=' | cut -d= -f2`);
|
|
95
|
+
const display = result.toString('utf8').trim();
|
|
96
|
+
process.env['DISPLAY'] = display;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// no-op
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Chrome refuses to start as root unless the sandbox is explicitly disabled and
|
|
105
|
+
* only says so on its stderr. Because we launch with `pipe: true`, Puppeteer
|
|
106
|
+
* never surfaces that stderr and the failure reaches the client as an opaque
|
|
107
|
+
* `Protocol error (Target.setDiscoverTargets): Target closed`. Detect the
|
|
108
|
+
* situation and explain the way out instead. See https://crbug.com/638180.
|
|
109
|
+
*
|
|
110
|
+
* Returns `undefined` when the failure cannot be explained by running as root,
|
|
111
|
+
* including on platforms without uids and when the sandbox was already disabled
|
|
112
|
+
* through `--chrome-arg` (in which case root is not what stopped Chrome).
|
|
113
|
+
*/
|
|
114
|
+
static rootSandboxLaunchError(error, args, uid = process.getuid?.()) {
|
|
115
|
+
if (uid !== 0) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
if (args.some(arg => arg === '--no-sandbox' || arg.startsWith('--no-sandbox='))) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
return new Error(`Chrome failed to start: ${error.message}\n\n` +
|
|
122
|
+
'chrome-devtools-mcp is running as root and Chrome does not start as root ' +
|
|
123
|
+
'(https://crbug.com/638180). Run chrome-devtools-mcp as a non-root user; in a ' +
|
|
124
|
+
'container, create an unprivileged user in the image and switch to it with ' +
|
|
125
|
+
"USER. For the setup that Chrome's sandbox needs, see " +
|
|
126
|
+
'https://pptr.dev/troubleshooting#setting-up-chrome-linux-sandbox.', {
|
|
127
|
+
cause: error,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
async ensureBrowser() {
|
|
131
|
+
if (this.#initPromise) {
|
|
132
|
+
return await this.#initPromise;
|
|
133
|
+
}
|
|
134
|
+
const initPromise = this.#ensureBrowserLocked();
|
|
135
|
+
this.#initPromise = initPromise;
|
|
136
|
+
try {
|
|
137
|
+
return await initPromise;
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
if (this.#initPromise === initPromise) {
|
|
141
|
+
this.#initPromise = undefined;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async #ensureBrowserLocked() {
|
|
146
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
147
|
+
try {
|
|
148
|
+
if (this.#closingCount > 0) {
|
|
149
|
+
throw new Error('Browser was closed while initializing.');
|
|
150
|
+
}
|
|
151
|
+
const _guard = __addDisposableResource(env_1, await this.#mutex.acquire(), false);
|
|
152
|
+
if (this.#closingCount > 0) {
|
|
153
|
+
throw new Error('Browser was closed while initializing.');
|
|
154
|
+
}
|
|
155
|
+
if (!this.#browser?.connected) {
|
|
156
|
+
await this.#initBrowser();
|
|
157
|
+
}
|
|
158
|
+
if (this.#closingCount > 0 || !this.#browser) {
|
|
159
|
+
await this.#closeBrowser();
|
|
160
|
+
throw new Error('Browser was closed while initializing.');
|
|
161
|
+
}
|
|
162
|
+
return this.#browser;
|
|
163
|
+
}
|
|
164
|
+
catch (e_1) {
|
|
165
|
+
env_1.error = e_1;
|
|
166
|
+
env_1.hasError = true;
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
__disposeResources(env_1);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async #initBrowser() {
|
|
173
|
+
if (this.#serverArgs.browserUrl ||
|
|
174
|
+
this.#serverArgs.wsEndpoint ||
|
|
175
|
+
this.#serverArgs.autoConnect) {
|
|
176
|
+
return await this.#connect();
|
|
177
|
+
}
|
|
178
|
+
return await this.#launch();
|
|
179
|
+
}
|
|
180
|
+
async #launch() {
|
|
181
|
+
const { channel, executablePath, headless, isolated = false, categoryExtensions: enableExtensions, viaCli, viewport, acceptInsecureCerts, experimentalDevtools: devtools = false, blockedUrlPattern: blocklist, allowedUrlPattern: allowlist, ignoreDefaultChromeArg, proxyServer, } = this.#serverArgs;
|
|
182
|
+
const profileDirName = channel && channel !== 'stable'
|
|
183
|
+
? `chrome-profile-${channel}`
|
|
184
|
+
: 'chrome-profile';
|
|
185
|
+
let userDataDir = this.#serverArgs.userDataDir;
|
|
186
|
+
if (!isolated && !userDataDir) {
|
|
187
|
+
userDataDir = path.join(os.homedir(), '.cache', viaCli ? 'chrome-devtools-mcp-cli' : 'chrome-devtools-mcp', profileDirName);
|
|
188
|
+
await fs.promises.mkdir(userDataDir, {
|
|
189
|
+
recursive: true,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const args = [...(this.#serverArgs.chromeArg ?? [])];
|
|
193
|
+
if (proxyServer) {
|
|
194
|
+
args.push(`--proxy-server=${proxyServer}`);
|
|
195
|
+
}
|
|
196
|
+
args.push('--hide-crash-restore-bubble');
|
|
197
|
+
const ignoreDefaultArgs = ignoreDefaultChromeArg ?? false;
|
|
198
|
+
if (headless) {
|
|
199
|
+
args.push('--screen-info={3840x2160}');
|
|
200
|
+
}
|
|
201
|
+
let puppeteerChannel;
|
|
202
|
+
if (devtools) {
|
|
203
|
+
args.push('--auto-open-devtools-for-tabs');
|
|
204
|
+
}
|
|
205
|
+
if (!executablePath) {
|
|
206
|
+
puppeteerChannel =
|
|
207
|
+
channel && channel !== 'stable' ? `chrome-${channel}` : 'chrome';
|
|
208
|
+
}
|
|
209
|
+
if (!headless) {
|
|
210
|
+
_a.detectDisplay();
|
|
211
|
+
}
|
|
212
|
+
let browser;
|
|
213
|
+
try {
|
|
214
|
+
browser = await puppeteer.launch({
|
|
215
|
+
channel: puppeteerChannel,
|
|
216
|
+
targetFilter: _a.makeTargetFilter(enableExtensions),
|
|
217
|
+
executablePath,
|
|
218
|
+
defaultViewport: null,
|
|
219
|
+
userDataDir,
|
|
220
|
+
pipe: true,
|
|
221
|
+
headless,
|
|
222
|
+
args,
|
|
223
|
+
ignoreDefaultArgs,
|
|
224
|
+
acceptInsecureCerts,
|
|
225
|
+
handleDevToolsAsPage: true,
|
|
226
|
+
enableExtensions,
|
|
227
|
+
blocklist,
|
|
228
|
+
allowlist,
|
|
229
|
+
logger: puppeteerLogger,
|
|
230
|
+
});
|
|
231
|
+
if (this.#options.logFile) {
|
|
232
|
+
// FIXME: we are probably subscribing too late to catch startup logs. We
|
|
233
|
+
// should expose the process earlier or expose the getRecentLogs() getter.
|
|
234
|
+
browser.process()?.stderr?.pipe(this.#options.logFile);
|
|
235
|
+
browser.process()?.stdout?.pipe(this.#options.logFile);
|
|
236
|
+
}
|
|
237
|
+
if (viewport) {
|
|
238
|
+
const [page] = await browser.pages();
|
|
239
|
+
await page?.resize({
|
|
240
|
+
contentWidth: viewport.width,
|
|
241
|
+
contentHeight: viewport.height,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
this.#browserMode = 'launched';
|
|
245
|
+
this.#browser = browser;
|
|
246
|
+
return browser;
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
await browser?.close().catch(() => {
|
|
250
|
+
// Best-effort cleanup if post-launch setup failed.
|
|
251
|
+
});
|
|
252
|
+
if (userDataDir &&
|
|
253
|
+
error instanceof Error &&
|
|
254
|
+
error.message.includes('The browser is already running')) {
|
|
255
|
+
throw new Error(`The browser is already running for ${userDataDir}. Use --isolated to run multiple browser instances.`, {
|
|
256
|
+
cause: error,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (error instanceof Error) {
|
|
260
|
+
const rootError = _a.rootSandboxLaunchError(error, args);
|
|
261
|
+
if (rootError) {
|
|
262
|
+
throw rootError;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
throw error;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async #connect() {
|
|
269
|
+
const { browserUrl: browserURL, wsEndpoint, wsHeaders, autoConnect: isAutoConnect, userDataDir, blockedUrlPattern: blocklist, allowedUrlPattern: allowlist, } = this.#serverArgs;
|
|
270
|
+
// Important: only pass channel, if autoConnect is true.
|
|
271
|
+
const channel = isAutoConnect ? this.#serverArgs.channel : undefined;
|
|
272
|
+
const connectOptions = {
|
|
273
|
+
targetFilter: _a.makeTargetFilter(),
|
|
274
|
+
defaultViewport: null,
|
|
275
|
+
handleDevToolsAsPage: true,
|
|
276
|
+
blocklist,
|
|
277
|
+
allowlist,
|
|
278
|
+
logger: puppeteerLogger,
|
|
279
|
+
};
|
|
280
|
+
let autoConnect = false;
|
|
281
|
+
if (wsEndpoint) {
|
|
282
|
+
connectOptions.browserWSEndpoint = wsEndpoint;
|
|
283
|
+
if (wsHeaders) {
|
|
284
|
+
connectOptions.headers = wsHeaders;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else if (browserURL) {
|
|
288
|
+
connectOptions.browserURL = browserURL;
|
|
289
|
+
}
|
|
290
|
+
else if (channel || userDataDir) {
|
|
291
|
+
if (userDataDir) {
|
|
292
|
+
autoConnect = true;
|
|
293
|
+
// TODO: re-expose this logic via Puppeteer.
|
|
294
|
+
const portPath = path.join(userDataDir, 'DevToolsActivePort');
|
|
295
|
+
try {
|
|
296
|
+
const fileContent = await fs.promises.readFile(portPath, 'utf8');
|
|
297
|
+
const [rawPort, rawPath] = fileContent
|
|
298
|
+
.split('\n')
|
|
299
|
+
.map(line => {
|
|
300
|
+
return line.trim();
|
|
301
|
+
})
|
|
302
|
+
.filter(line => {
|
|
303
|
+
return !!line;
|
|
304
|
+
});
|
|
305
|
+
if (!rawPort || !rawPath) {
|
|
306
|
+
throw new Error(`Invalid DevToolsActivePort '${fileContent}' found`);
|
|
307
|
+
}
|
|
308
|
+
const port = parseInt(rawPort, 10);
|
|
309
|
+
if (isNaN(port) || port <= 0 || port > 65535) {
|
|
310
|
+
throw new Error(`Invalid port '${rawPort}' found`);
|
|
311
|
+
}
|
|
312
|
+
const browserWSEndpoint = `ws://127.0.0.1:${port}${rawPath}`;
|
|
313
|
+
connectOptions.browserWSEndpoint = browserWSEndpoint;
|
|
314
|
+
}
|
|
315
|
+
catch (error) {
|
|
316
|
+
throw new Error(`Could not connect to Chrome in ${userDataDir}. Check if Chrome is running and remote debugging is enabled by going to chrome://inspect/#remote-debugging.`, {
|
|
317
|
+
cause: error,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
if (!channel) {
|
|
323
|
+
throw new Error('Channel must be provided if userDataDir is missing');
|
|
324
|
+
}
|
|
325
|
+
connectOptions.channel =
|
|
326
|
+
channel === 'stable' ? 'chrome' : `chrome-${channel}`;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
throw new Error('Either browserURL, wsEndpoint, channel or userDataDir must be provided');
|
|
331
|
+
}
|
|
332
|
+
logger?.('Connecting Puppeteer to ', JSON.stringify(connectOptions));
|
|
333
|
+
try {
|
|
334
|
+
const connected = await puppeteer.connect(connectOptions);
|
|
335
|
+
logger?.('Connected Puppeteer');
|
|
336
|
+
this.#browserMode = 'connected';
|
|
337
|
+
this.#browser = connected;
|
|
338
|
+
return connected;
|
|
339
|
+
}
|
|
340
|
+
catch (err) {
|
|
341
|
+
throw new Error(`Could not connect to Chrome. ${autoConnect ? `Check if Chrome is running and remote debugging is enabled by going to chrome://inspect/#remote-debugging.` : `Check if Chrome is running.`}`, {
|
|
342
|
+
cause: err,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
async #closeBrowser() {
|
|
347
|
+
const browser = this.#browser;
|
|
348
|
+
const mode = this.#browserMode;
|
|
349
|
+
this.#browser = undefined;
|
|
350
|
+
this.#browserMode = undefined;
|
|
351
|
+
if (!browser || !browser.connected) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (mode === 'launched') {
|
|
355
|
+
await browser.close().catch(err => {
|
|
356
|
+
logger?.('Failed to close browser', err);
|
|
357
|
+
});
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
await browser.disconnect().catch(err => {
|
|
361
|
+
logger?.('Failed to disconnect from browser', err);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Shutdown hook for the active browser. Closes a launched browser (so the
|
|
366
|
+
* Chrome subprocess is reaped) or disconnects from an attached browser (so
|
|
367
|
+
* the user's Chrome instance stays alive). No-op if no browser is active or
|
|
368
|
+
* the connection has already been dropped.
|
|
369
|
+
*/
|
|
370
|
+
async close() {
|
|
371
|
+
const env_2 = { stack: [], error: void 0, hasError: false };
|
|
372
|
+
try {
|
|
373
|
+
this.#initPromise = undefined;
|
|
374
|
+
this.#closingCount++;
|
|
375
|
+
const _guard = __addDisposableResource(env_2, await this.#mutex.acquire(), false);
|
|
376
|
+
try {
|
|
377
|
+
await this.#closeBrowser();
|
|
378
|
+
}
|
|
379
|
+
finally {
|
|
380
|
+
this.#closingCount--;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
catch (e_2) {
|
|
384
|
+
env_2.error = e_2;
|
|
385
|
+
env_2.hasError = true;
|
|
386
|
+
}
|
|
387
|
+
finally {
|
|
388
|
+
__disposeResources(env_2);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
[Symbol.dispose]() {
|
|
392
|
+
this.close().catch(err => {
|
|
393
|
+
logger?.('Failed to dispose BrowserManager', err);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
async [Symbol.asyncDispose]() {
|
|
397
|
+
await this.close();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
_a = BrowserManager;
|
|
401
|
+
//# sourceMappingURL=BrowserManager.js.map
|
package/build/src/McpContext.js
CHANGED
|
@@ -12,7 +12,6 @@ import { HeapSnapshotManager } from './processors/HeapSnapshotManager.js';
|
|
|
12
12
|
import { McpPage } from './McpPage.js';
|
|
13
13
|
import { ServiceWorkerConsoleCollector } from './collectors/ServiceWorkerCollector.js';
|
|
14
14
|
import { Locator, } from './third_party/index.js';
|
|
15
|
-
import { listPages } from './tools/pages.js';
|
|
16
15
|
import { CLOSE_PAGE_ERROR } from './tools/ToolDefinition.js';
|
|
17
16
|
import { getTempFilePath, resolveCanonicalPath } from './utils/files.js';
|
|
18
17
|
import { isAllowedUrl } from './utils/url.js';
|
|
@@ -233,7 +232,7 @@ export class McpContext {
|
|
|
233
232
|
await page.pptrPage.close({ runBeforeUnload: false });
|
|
234
233
|
}
|
|
235
234
|
get #hasNetworkBlockOrAllowlist() {
|
|
236
|
-
return !!(this.#options.
|
|
235
|
+
return !!(this.#options.allowlist || this.#options.blocklist);
|
|
237
236
|
}
|
|
238
237
|
installPWA(options) {
|
|
239
238
|
return this.browser.installPWA(options);
|
|
@@ -271,7 +270,7 @@ export class McpContext {
|
|
|
271
270
|
throw new Error('No page selected');
|
|
272
271
|
}
|
|
273
272
|
if (page.pptrPage.isClosed()) {
|
|
274
|
-
throw new Error(
|
|
273
|
+
throw new Error('The selected page has been closed. Call list_pages to see open pages.');
|
|
275
274
|
}
|
|
276
275
|
return page;
|
|
277
276
|
}
|
|
@@ -397,6 +396,7 @@ export class McpContext {
|
|
|
397
396
|
isolatedContextName: this.#getBrowserContextToNameMap().get(page.browserContext()),
|
|
398
397
|
navigationTimeout: this.#options.navigationTimeout,
|
|
399
398
|
sourceMaps: this.#options.sourceMaps,
|
|
399
|
+
onNotification: this.#options.onNotification,
|
|
400
400
|
});
|
|
401
401
|
this.#mcpPages.set(page, mcpPage);
|
|
402
402
|
await mcpPage.init();
|
|
@@ -597,10 +597,10 @@ export class McpContext {
|
|
|
597
597
|
}
|
|
598
598
|
}
|
|
599
599
|
#validateUrlAllowed(url) {
|
|
600
|
-
if (!this.#options.
|
|
600
|
+
if (!this.#options.allowlist) {
|
|
601
601
|
return;
|
|
602
602
|
}
|
|
603
|
-
for (const allow of this.#options.
|
|
603
|
+
for (const allow of this.#options.allowlist) {
|
|
604
604
|
const pattern = new URLPattern(allow);
|
|
605
605
|
if (pattern.test(url)) {
|
|
606
606
|
return;
|
package/build/src/McpPage.js
CHANGED
|
@@ -101,15 +101,18 @@ export function replaceHtmlElementsWithUids(schema) {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
+
import { DevToolsCommentBridge } from './devtools/DevToolsCommentBridge.js';
|
|
104
105
|
import { createTargetUniverse, } from './devtools/DevtoolsUtils.js';
|
|
105
106
|
import { ConsoleCollector, NetworkCollector, } from './collectors/PageCollector.js';
|
|
106
107
|
import { TextSnapshot } from './TextSnapshot.js';
|
|
107
|
-
import { PredefinedNetworkConditions, } from './third_party/index.js';
|
|
108
|
-
import { takeSnapshot } from './tools/snapshot.js';
|
|
108
|
+
import { PredefinedNetworkConditions, DevTools, } from './third_party/index.js';
|
|
109
109
|
const DEFAULT_TIMEOUT = 5_000;
|
|
110
110
|
const NAVIGATION_TIMEOUT = 10_000;
|
|
111
111
|
import { logger } from './utils/logger.js';
|
|
112
112
|
import { getNetworkMultiplierFromString, WaitForHelper, } from './utils/WaitForHelper.js';
|
|
113
|
+
function isBackendNodeId(id) {
|
|
114
|
+
return typeof id === 'number';
|
|
115
|
+
}
|
|
113
116
|
/**
|
|
114
117
|
* Per-page state wrapper. Consolidates dialog, snapshot, emulation,
|
|
115
118
|
* and metadata that were previously scattered across Maps in McpContext.
|
|
@@ -140,11 +143,14 @@ export class McpPage {
|
|
|
140
143
|
#locatorClass;
|
|
141
144
|
#navigationTimeout;
|
|
142
145
|
#sourceMaps;
|
|
146
|
+
#commentBridge;
|
|
147
|
+
#onNotification;
|
|
143
148
|
constructor(page, id, options) {
|
|
144
149
|
this.#hasNetworkBlockOrAllowlist = options.hasNetworkBlockOrAllowlist;
|
|
145
150
|
this.#locatorClass = options.locatorClass;
|
|
146
151
|
this.#navigationTimeout = options.navigationTimeout ?? NAVIGATION_TIMEOUT;
|
|
147
152
|
this.#sourceMaps = options.sourceMaps ?? true;
|
|
153
|
+
this.#onNotification = options.onNotification;
|
|
148
154
|
this.pptrPage = page;
|
|
149
155
|
this.id = id;
|
|
150
156
|
this.isolatedContextName = options.isolatedContextName;
|
|
@@ -325,9 +331,29 @@ export class McpPage {
|
|
|
325
331
|
}
|
|
326
332
|
return this.networkCollector.getIdForResource(request);
|
|
327
333
|
}
|
|
334
|
+
resolveReqidToCdpRequestId(reqid) {
|
|
335
|
+
const request = this.networkCollector.getById(reqid);
|
|
336
|
+
if (!request) {
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
// @ts-expect-error id is internal.
|
|
340
|
+
return request.id;
|
|
341
|
+
}
|
|
328
342
|
getNetworkRequests(includePreservedRequests) {
|
|
329
343
|
return this.networkCollector.getData(includePreservedRequests);
|
|
330
344
|
}
|
|
345
|
+
get commentBridge() {
|
|
346
|
+
return this.#commentBridge;
|
|
347
|
+
}
|
|
348
|
+
async ensureDevToolsCommentBridge(devtoolsPage) {
|
|
349
|
+
if (!this.#commentBridge) {
|
|
350
|
+
this.#commentBridge = new DevToolsCommentBridge({
|
|
351
|
+
onNotification: this.#onNotification,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
await this.#commentBridge.attach(devtoolsPage);
|
|
355
|
+
return this.#commentBridge;
|
|
356
|
+
}
|
|
331
357
|
async getDevToolsPage() {
|
|
332
358
|
try {
|
|
333
359
|
if (await this.pptrPage.hasDevTools()) {
|
|
@@ -342,6 +368,11 @@ export class McpPage {
|
|
|
342
368
|
return undefined;
|
|
343
369
|
}
|
|
344
370
|
}
|
|
371
|
+
async openDevTools() {
|
|
372
|
+
const devtoolsPage = await this.pptrPage.openDevTools();
|
|
373
|
+
await this.ensureDevToolsCommentBridge(devtoolsPage);
|
|
374
|
+
return devtoolsPage;
|
|
375
|
+
}
|
|
345
376
|
getConsoleData(includePreservedMessages) {
|
|
346
377
|
return this.consoleCollector.getData(includePreservedMessages);
|
|
347
378
|
}
|
|
@@ -378,6 +409,8 @@ export class McpPage {
|
|
|
378
409
|
return helper.waitForEventsAfterAction(action, options);
|
|
379
410
|
}
|
|
380
411
|
dispose() {
|
|
412
|
+
this.#commentBridge?.dispose();
|
|
413
|
+
this.#commentBridge = undefined;
|
|
381
414
|
this.pptrPage.off('dialog', this.#dialogHandler);
|
|
382
415
|
this.networkCollector.dispose();
|
|
383
416
|
this.consoleCollector.dispose();
|
|
@@ -548,7 +581,7 @@ export class McpPage {
|
|
|
548
581
|
}
|
|
549
582
|
async getElementByUid(uid) {
|
|
550
583
|
if (!this.textSnapshot) {
|
|
551
|
-
throw new Error(`No snapshot found for page ${this.id ?? '?'}. Use
|
|
584
|
+
throw new Error(`No snapshot found for page ${this.id ?? '?'}. Use take_snapshot to capture one.`);
|
|
552
585
|
}
|
|
553
586
|
const node = this.textSnapshot.idToNode.get(uid);
|
|
554
587
|
if (!node) {
|
|
@@ -574,6 +607,77 @@ export class McpPage {
|
|
|
574
607
|
getAXNodeByUid(uid) {
|
|
575
608
|
return this.textSnapshot?.idToNode.get(uid);
|
|
576
609
|
}
|
|
610
|
+
async resolveBackendNodeId(backendNodeId) {
|
|
611
|
+
if (!this.textSnapshot) {
|
|
612
|
+
this.textSnapshot = await TextSnapshot.create(this);
|
|
613
|
+
}
|
|
614
|
+
let id = this.textSnapshot.resolveCdpElementId(backendNodeId);
|
|
615
|
+
if (!id) {
|
|
616
|
+
this.textSnapshot = await TextSnapshot.create(this);
|
|
617
|
+
id = this.textSnapshot.resolveCdpElementId(backendNodeId);
|
|
618
|
+
}
|
|
619
|
+
return id;
|
|
620
|
+
}
|
|
621
|
+
async resolveUidToBackendNodeId(uid) {
|
|
622
|
+
const target = this.pptrPage.target();
|
|
623
|
+
const targetId = Boolean(target) &&
|
|
624
|
+
'_targetId' in target &&
|
|
625
|
+
typeof target._targetId === 'string'
|
|
626
|
+
? target._targetId
|
|
627
|
+
: undefined;
|
|
628
|
+
const node = this.getAXNodeByUid(uid);
|
|
629
|
+
if (node?.backendNodeId !== undefined) {
|
|
630
|
+
return { backendNodeId: node.backendNodeId, targetId };
|
|
631
|
+
}
|
|
632
|
+
try {
|
|
633
|
+
const handle = await this.getElementByUid(uid);
|
|
634
|
+
const backendNodeId = await handle.backendNodeId();
|
|
635
|
+
return backendNodeId ? { backendNodeId, targetId } : undefined;
|
|
636
|
+
}
|
|
637
|
+
catch {
|
|
638
|
+
return undefined;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
async getMatchedStylesForUid(uid) {
|
|
642
|
+
if (!this.textSnapshot) {
|
|
643
|
+
throw new Error(`No snapshot found for page ${this.id ?? '?'}. Use take_snapshot to capture one.`);
|
|
644
|
+
}
|
|
645
|
+
const node = this.textSnapshot.idToNode.get(uid);
|
|
646
|
+
if (!node) {
|
|
647
|
+
throw new Error(`Element uid "${uid}" not found on page ${this.id}.`);
|
|
648
|
+
}
|
|
649
|
+
const backendNodeId = node.backendNodeId;
|
|
650
|
+
if (!isBackendNodeId(backendNodeId)) {
|
|
651
|
+
throw new Error(`Failed to resolve backend node ID for element with uid "${uid}".`);
|
|
652
|
+
}
|
|
653
|
+
if (!this.#devtoolsUniverse) {
|
|
654
|
+
throw new Error(`DevTools universe is not available for page ${this.id ?? '?'}.`);
|
|
655
|
+
}
|
|
656
|
+
const targetManager = this.#devtoolsUniverse.universe.context.get(DevTools.TargetManager);
|
|
657
|
+
let domNode;
|
|
658
|
+
let cssModel = null;
|
|
659
|
+
for (const dom of targetManager.models(DevTools.DOMModel.DOMModel)) {
|
|
660
|
+
const nodeMap = await dom.pushNodesByBackendIdsToFrontend(new Set([backendNodeId]));
|
|
661
|
+
const frontendNode = nodeMap?.get(backendNodeId);
|
|
662
|
+
if (frontendNode) {
|
|
663
|
+
domNode = frontendNode;
|
|
664
|
+
cssModel = dom.target().model(DevTools.CSSModel.CSSModel);
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
if (!domNode || !cssModel) {
|
|
669
|
+
throw new Error(`Element with uid "${uid}" was detached or no longer exists on the page. Please take a new snapshot with take_snapshot.`);
|
|
670
|
+
}
|
|
671
|
+
const targetElement = domNode.enclosingElementOrSelf();
|
|
672
|
+
if (!targetElement) {
|
|
673
|
+
throw new Error(`Element with uid "${uid}" is not an element node and has no parent element.`);
|
|
674
|
+
}
|
|
675
|
+
const matchedStyles = await cssModel.getMatchedStyles(targetElement.id);
|
|
676
|
+
if (!matchedStyles) {
|
|
677
|
+
throw new Error(`Could not retrieve matched styles for element with uid "${uid}".`);
|
|
678
|
+
}
|
|
679
|
+
return matchedStyles;
|
|
680
|
+
}
|
|
577
681
|
async getDevToolsData() {
|
|
578
682
|
try {
|
|
579
683
|
logger?.('Getting DevTools UI data');
|