chrome-devtools-mcp 1.4.0 → 1.6.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/README.md +20 -3
- package/build/src/HeapSnapshotManager.js +83 -16
- package/build/src/McpContext.js +218 -367
- package/build/src/McpPage.js +242 -31
- package/build/src/McpResponse.js +165 -60
- package/build/src/PageCollector.js +48 -122
- package/build/src/TextSnapshot.js +20 -2
- package/build/src/ToolHandler.js +13 -3
- package/build/src/WaitForHelper.js +18 -8
- package/build/src/bin/chrome-devtools-cli-options.js +76 -4
- package/build/src/bin/chrome-devtools-mcp-cli-options.js +15 -6
- package/build/src/bin/chrome-devtools-mcp-main.js +2 -2
- package/build/src/bin/chrome-devtools.js +25 -1
- package/build/src/browser.js +1 -1
- package/build/src/daemon/client.js +33 -3
- package/build/src/daemon/daemon.js +3 -2
- package/build/src/daemon/utils.js +1 -1
- package/build/src/devtools/DevtoolsUtils.js +2 -63
- package/build/src/{issue-descriptions.js → devtools/issueDescriptions.js} +8 -6
- package/build/src/formatters/HeapSnapshotFormatter.js +43 -0
- package/build/src/formatters/IssueFormatter.js +2 -2
- package/build/src/index.js +14 -5
- package/build/src/telemetry/ClearcutLogger.js +1 -1
- package/build/src/telemetry/WatchdogClient.js +1 -1
- package/build/src/telemetry/metricsRegistry.js +2 -9
- package/build/src/telemetry/persistence.js +1 -1
- package/build/src/telemetry/transformation.js +20 -0
- package/build/src/telemetry/watchdog/ClearcutSender.js +1 -1
- package/build/src/telemetry/watchdog/main.js +1 -1
- package/build/src/third_party/THIRD_PARTY_NOTICES +13 -279
- package/build/src/third_party/bundled-packages.json +3 -5
- package/build/src/third_party/devtools-heap-snapshot-worker.js +81 -0
- package/build/src/third_party/index.js +2418 -2197
- package/build/src/third_party/lighthouse-devtools-mcp-bundle.js +3082 -6343
- package/build/src/tools/emulation.js +2 -2
- package/build/src/tools/extensions.js +1 -1
- package/build/src/tools/input.js +17 -8
- package/build/src/tools/lighthouse.js +1 -1
- package/build/src/tools/memory.js +76 -6
- package/build/src/tools/network.js +4 -4
- package/build/src/tools/pages.js +21 -89
- package/build/src/tools/performance.js +2 -2
- package/build/src/tools/screencast.js +1 -2
- package/build/src/tools/script.js +6 -10
- package/build/src/tools/snapshot.js +2 -2
- package/build/src/tools/thirdPartyDeveloper.js +7 -7
- package/build/src/tools/webmcp.js +1 -1
- package/build/src/trace-processing/parse.js +1 -1
- package/build/src/utils/files.js +0 -4
- package/build/src/{logger.js → utils/logger.js} +1 -1
- package/build/src/{polyfill.js → utils/polyfill.js} +1 -1
- package/build/src/version.js +1 -1
- package/package.json +17 -5
- package/skills/memory-leak-debugging/SKILL.md +22 -14
- package/build/src/utils/types.js +0 -7
- /package/build/src/{Mutex.js → utils/Mutex.js} +0 -0
package/build/src/McpPage.js
CHANGED
|
@@ -3,9 +3,14 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { createTargetUniverse, } from './devtools/DevtoolsUtils.js';
|
|
7
|
+
import { ConsoleCollector, NetworkCollector, } from './PageCollector.js';
|
|
7
8
|
import { TextSnapshot } from './TextSnapshot.js';
|
|
9
|
+
import { PredefinedNetworkConditions, } from './third_party/index.js';
|
|
8
10
|
import { takeSnapshot } from './tools/snapshot.js';
|
|
11
|
+
const DEFAULT_TIMEOUT = 5_000;
|
|
12
|
+
const NAVIGATION_TIMEOUT = 10_000;
|
|
13
|
+
import { logger } from './utils/logger.js';
|
|
9
14
|
import { getNetworkMultiplierFromString, WaitForHelper, } from './WaitForHelper.js';
|
|
10
15
|
/**
|
|
11
16
|
* Per-page state wrapper. Consolidates dialog, snapshot, emulation,
|
|
@@ -26,24 +31,69 @@ export class McpPage {
|
|
|
26
31
|
emulationSettings = {};
|
|
27
32
|
// Metadata
|
|
28
33
|
isolatedContextName;
|
|
29
|
-
|
|
34
|
+
#devtoolsUniverse;
|
|
30
35
|
// Dialog
|
|
31
36
|
#dialog;
|
|
32
37
|
#dialogHandler;
|
|
33
38
|
thirdPartyDeveloperTools = [];
|
|
34
|
-
|
|
39
|
+
networkCollector;
|
|
40
|
+
consoleCollector;
|
|
41
|
+
#hasNetworkBlockOrAllowlist;
|
|
42
|
+
#locatorClass;
|
|
43
|
+
constructor(page, id, options) {
|
|
44
|
+
this.#hasNetworkBlockOrAllowlist = options.hasNetworkBlockOrAllowlist;
|
|
45
|
+
this.#locatorClass = options.locatorClass;
|
|
35
46
|
this.pptrPage = page;
|
|
36
47
|
this.id = id;
|
|
48
|
+
this.isolatedContextName = options.isolatedContextName;
|
|
37
49
|
this.#dialogHandler = (dialog) => {
|
|
38
50
|
this.#dialog = dialog;
|
|
39
51
|
};
|
|
40
52
|
page.on('dialog', this.#dialogHandler);
|
|
53
|
+
this.networkCollector = new NetworkCollector(page);
|
|
54
|
+
this.consoleCollector = new ConsoleCollector(page, collect => {
|
|
55
|
+
return {
|
|
56
|
+
console: event => {
|
|
57
|
+
collect(event);
|
|
58
|
+
},
|
|
59
|
+
uncaughtError: event => {
|
|
60
|
+
collect(event);
|
|
61
|
+
},
|
|
62
|
+
devtoolsAggregatedIssue: event => {
|
|
63
|
+
collect(event);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
});
|
|
41
67
|
}
|
|
42
|
-
|
|
43
|
-
|
|
68
|
+
async init() {
|
|
69
|
+
await Promise.allSettled([
|
|
70
|
+
this.#initDevToolsUniverseNoThrow(),
|
|
71
|
+
this.#initFocusEmulationNoThrow(),
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
async #initFocusEmulationNoThrow() {
|
|
75
|
+
// We emulate a focused page for all pages to support multi-agent workflows.
|
|
76
|
+
void this.pptrPage.emulateFocusedPage(true).catch(error => {
|
|
77
|
+
logger?.('Error turning on focused page emulation', error);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async #initDevToolsUniverseNoThrow() {
|
|
81
|
+
if (this.#devtoolsUniverse) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const session = await this.pptrPage.createCDPSession();
|
|
86
|
+
this.#devtoolsUniverse = await createTargetUniverse(session);
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
logger?.('Failed to initialize DevTools universe', e);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
get devtoolsUniverse() {
|
|
93
|
+
return this.#devtoolsUniverse;
|
|
44
94
|
}
|
|
45
95
|
getDialog() {
|
|
46
|
-
return this
|
|
96
|
+
return this.#dialog;
|
|
47
97
|
}
|
|
48
98
|
clearDialog() {
|
|
49
99
|
this.#dialog = undefined;
|
|
@@ -59,6 +109,47 @@ export class McpPage {
|
|
|
59
109
|
getWebMcpTools() {
|
|
60
110
|
return this.pptrPage.webmcp.tools();
|
|
61
111
|
}
|
|
112
|
+
resolveCdpRequestId(cdpRequestId) {
|
|
113
|
+
if (!cdpRequestId) {
|
|
114
|
+
logger?.('no network request');
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const request = this.networkCollector.find(request => {
|
|
118
|
+
// @ts-expect-error id is internal.
|
|
119
|
+
return request.id === cdpRequestId;
|
|
120
|
+
});
|
|
121
|
+
if (!request) {
|
|
122
|
+
logger?.('no network request for ' + cdpRequestId);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
return this.networkCollector.getIdForResource(request);
|
|
126
|
+
}
|
|
127
|
+
getNetworkRequests(includePreservedRequests) {
|
|
128
|
+
return this.networkCollector.getData(includePreservedRequests);
|
|
129
|
+
}
|
|
130
|
+
async getDevToolsPage() {
|
|
131
|
+
try {
|
|
132
|
+
if (await this.pptrPage.hasDevTools()) {
|
|
133
|
+
return await this.pptrPage.openDevTools();
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// Prior to Chrome 144.0.7559.59, the command fails,
|
|
139
|
+
// Some Electron apps still use older version
|
|
140
|
+
// Fall back to not exposing DevTools at all.
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
getConsoleData(includePreservedMessages) {
|
|
145
|
+
return this.consoleCollector.getData(includePreservedMessages);
|
|
146
|
+
}
|
|
147
|
+
getConsoleMessageById(id) {
|
|
148
|
+
return this.consoleCollector.getById(id);
|
|
149
|
+
}
|
|
150
|
+
getNetworkRequestById(reqid) {
|
|
151
|
+
return this.networkCollector.getById(reqid);
|
|
152
|
+
}
|
|
62
153
|
get networkConditions() {
|
|
63
154
|
return this.emulationSettings.networkConditions ?? null;
|
|
64
155
|
}
|
|
@@ -87,6 +178,8 @@ export class McpPage {
|
|
|
87
178
|
}
|
|
88
179
|
dispose() {
|
|
89
180
|
this.pptrPage.off('dialog', this.#dialogHandler);
|
|
181
|
+
this.networkCollector.dispose();
|
|
182
|
+
this.consoleCollector.dispose();
|
|
90
183
|
}
|
|
91
184
|
async executeThirdPartyDeveloperTool(toolName, params, response) {
|
|
92
185
|
// Creates array of ElementHandles from the UIDs in the params.
|
|
@@ -203,7 +296,7 @@ export class McpPage {
|
|
|
203
296
|
logger?.(`No backendNodeId for stashed DOM element with index ${index}`);
|
|
204
297
|
return `stashed-${index}`;
|
|
205
298
|
}
|
|
206
|
-
const cdpElementId = this.resolveCdpElementId(backendNodeId);
|
|
299
|
+
const cdpElementId = this.textSnapshot?.resolveCdpElementId(backendNodeId);
|
|
207
300
|
if (!cdpElementId) {
|
|
208
301
|
logger?.(`Could not get cdpElementId for backend node ${backendNodeId}`);
|
|
209
302
|
return `stashed-${index}`;
|
|
@@ -261,33 +354,10 @@ export class McpPage {
|
|
|
261
354
|
getAXNodeByUid(uid) {
|
|
262
355
|
return this.textSnapshot?.idToNode.get(uid);
|
|
263
356
|
}
|
|
264
|
-
resolveCdpElementId(cdpBackendNodeId) {
|
|
265
|
-
if (!cdpBackendNodeId) {
|
|
266
|
-
logger?.('no cdpBackendNodeId');
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const snapshot = this.textSnapshot;
|
|
270
|
-
if (!snapshot) {
|
|
271
|
-
logger?.('no text snapshot');
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
// TODO: index by backendNodeId instead.
|
|
275
|
-
const queue = [snapshot.root];
|
|
276
|
-
while (queue.length) {
|
|
277
|
-
const current = queue.pop();
|
|
278
|
-
if (current.backendNodeId === cdpBackendNodeId) {
|
|
279
|
-
return current.id;
|
|
280
|
-
}
|
|
281
|
-
for (const child of current.children) {
|
|
282
|
-
queue.push(child);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
357
|
async getDevToolsData() {
|
|
288
358
|
try {
|
|
289
359
|
logger?.('Getting DevTools UI data');
|
|
290
|
-
const devtoolsPage = this.
|
|
360
|
+
const devtoolsPage = await this.getDevToolsPage();
|
|
291
361
|
if (!devtoolsPage) {
|
|
292
362
|
logger?.('No DevTools page detected');
|
|
293
363
|
return {};
|
|
@@ -311,5 +381,146 @@ export class McpPage {
|
|
|
311
381
|
}
|
|
312
382
|
return {};
|
|
313
383
|
}
|
|
384
|
+
async restoreEmulation() {
|
|
385
|
+
const currentSetting = this.emulationSettings;
|
|
386
|
+
await this.emulate(currentSetting);
|
|
387
|
+
}
|
|
388
|
+
async emulate(options) {
|
|
389
|
+
const page = this.pptrPage;
|
|
390
|
+
const newSettings = { ...this.emulationSettings };
|
|
391
|
+
// Skip network emulation if blocklist/allowlist is configured, as it conflicts with blocking rules in Puppeteer.
|
|
392
|
+
if (this.#hasNetworkBlockOrAllowlist) {
|
|
393
|
+
if (options.networkConditions !== undefined) {
|
|
394
|
+
throw new Error('Network throttling is not supported when network blocking (allowlist/blocklist) is configured.');
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
else if (!options.networkConditions) {
|
|
398
|
+
await page.emulateNetworkConditions(null);
|
|
399
|
+
delete newSettings.networkConditions;
|
|
400
|
+
}
|
|
401
|
+
else if (options.networkConditions === 'Offline') {
|
|
402
|
+
await page.emulateNetworkConditions({
|
|
403
|
+
offline: true,
|
|
404
|
+
download: 0,
|
|
405
|
+
upload: 0,
|
|
406
|
+
latency: 0,
|
|
407
|
+
});
|
|
408
|
+
newSettings.networkConditions = 'Offline';
|
|
409
|
+
}
|
|
410
|
+
else if (options.networkConditions in PredefinedNetworkConditions) {
|
|
411
|
+
const networkCondition = PredefinedNetworkConditions[options.networkConditions];
|
|
412
|
+
await page.emulateNetworkConditions(networkCondition);
|
|
413
|
+
newSettings.networkConditions = options.networkConditions;
|
|
414
|
+
}
|
|
415
|
+
const secondarySession = this.devtoolsUniverse?.session;
|
|
416
|
+
if (!options.cpuThrottlingRate) {
|
|
417
|
+
await page.emulateCPUThrottling(1);
|
|
418
|
+
if (secondarySession) {
|
|
419
|
+
await secondarySession.send('Emulation.setCPUThrottlingRate', {
|
|
420
|
+
rate: 1,
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
delete newSettings.cpuThrottlingRate;
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
await page.emulateCPUThrottling(options.cpuThrottlingRate);
|
|
427
|
+
if (secondarySession) {
|
|
428
|
+
await secondarySession.send('Emulation.setCPUThrottlingRate', {
|
|
429
|
+
rate: options.cpuThrottlingRate,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
newSettings.cpuThrottlingRate = options.cpuThrottlingRate;
|
|
433
|
+
}
|
|
434
|
+
if (!options.geolocation) {
|
|
435
|
+
await page.setGeolocation({ latitude: 0, longitude: 0 });
|
|
436
|
+
delete newSettings.geolocation;
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
await page.setGeolocation(options.geolocation);
|
|
440
|
+
newSettings.geolocation = options.geolocation;
|
|
441
|
+
}
|
|
442
|
+
if (!options.userAgent) {
|
|
443
|
+
await page.setUserAgent({ userAgent: undefined });
|
|
444
|
+
delete newSettings.userAgent;
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
await page.setUserAgent({ userAgent: options.userAgent });
|
|
448
|
+
newSettings.userAgent = options.userAgent;
|
|
449
|
+
}
|
|
450
|
+
if (!options.colorScheme || options.colorScheme === 'auto') {
|
|
451
|
+
await page.emulateMediaFeatures([
|
|
452
|
+
{ name: 'prefers-color-scheme', value: '' },
|
|
453
|
+
]);
|
|
454
|
+
delete newSettings.colorScheme;
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
await page.emulateMediaFeatures([
|
|
458
|
+
{ name: 'prefers-color-scheme', value: options.colorScheme },
|
|
459
|
+
]);
|
|
460
|
+
newSettings.colorScheme = options.colorScheme;
|
|
461
|
+
}
|
|
462
|
+
if (!options.viewport) {
|
|
463
|
+
delete newSettings.viewport;
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
const defaults = {
|
|
467
|
+
deviceScaleFactor: 1,
|
|
468
|
+
isMobile: false,
|
|
469
|
+
hasTouch: false,
|
|
470
|
+
isLandscape: false,
|
|
471
|
+
};
|
|
472
|
+
newSettings.viewport = { ...defaults, ...options.viewport };
|
|
473
|
+
}
|
|
474
|
+
if (options.extraHttpHeaders !== undefined) {
|
|
475
|
+
await page.setExtraHTTPHeaders(options.extraHttpHeaders);
|
|
476
|
+
newSettings.extraHttpHeaders = options.extraHttpHeaders;
|
|
477
|
+
if (Object.keys(options.extraHttpHeaders).length === 0) {
|
|
478
|
+
delete newSettings.extraHttpHeaders;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
this.emulationSettings = Object.keys(newSettings).length ? newSettings : {};
|
|
482
|
+
this.updateTimeouts();
|
|
483
|
+
// This should happen after updating the page timeouts.
|
|
484
|
+
// Setting the viewport can trigger a reload which we don't want to timeout.
|
|
485
|
+
await page.setViewport(newSettings.viewport ?? null);
|
|
486
|
+
}
|
|
487
|
+
updateTimeouts() {
|
|
488
|
+
// For waiters 5sec timeout should be sufficient.
|
|
489
|
+
// Increased in case we throttle the CPU
|
|
490
|
+
const cpuMultiplier = this.cpuThrottlingRate;
|
|
491
|
+
this.pptrPage.setDefaultTimeout(DEFAULT_TIMEOUT * cpuMultiplier);
|
|
492
|
+
// 10sec should be enough for the load event to be emitted during
|
|
493
|
+
// navigations.
|
|
494
|
+
// Increased in case we throttle the network requests or the CPU
|
|
495
|
+
const networkMultiplier = getNetworkMultiplierFromString(this.networkConditions);
|
|
496
|
+
this.pptrPage.setDefaultNavigationTimeout(NAVIGATION_TIMEOUT * networkMultiplier * cpuMultiplier);
|
|
497
|
+
}
|
|
498
|
+
waitForTextOnPage(text, timeout) {
|
|
499
|
+
const frames = this.pptrPage.frames();
|
|
500
|
+
let locator = this.#locatorClass.race(frames.flatMap(frame => text.flatMap(value => [
|
|
501
|
+
frame.locator(`aria/${value}`),
|
|
502
|
+
frame.locator(`text/${value}`),
|
|
503
|
+
])));
|
|
504
|
+
if (timeout) {
|
|
505
|
+
locator = locator.setTimeout(timeout);
|
|
506
|
+
}
|
|
507
|
+
return locator.wait();
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* We need to ignore favicon request as they make our test flaky
|
|
511
|
+
*/
|
|
512
|
+
async setUpNetworkCollectorForTesting() {
|
|
513
|
+
this.networkCollector.dispose();
|
|
514
|
+
this.networkCollector = new NetworkCollector(this.pptrPage, collect => {
|
|
515
|
+
return {
|
|
516
|
+
request: req => {
|
|
517
|
+
if (req.url().includes('favicon.ico')) {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
collect(req);
|
|
521
|
+
},
|
|
522
|
+
};
|
|
523
|
+
});
|
|
524
|
+
}
|
|
314
525
|
}
|
|
315
526
|
//# sourceMappingURL=McpPage.js.map
|