chrome-devtools-mcp 1.10.1 → 1.11.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 +132 -13
- package/build/src/McpContext.js +188 -138
- package/build/src/McpPage.js +348 -96
- package/build/src/McpResponse.js +234 -76
- package/build/src/McpWorker.js +66 -0
- package/build/src/SlimMcpResponse.js +5 -1
- package/build/src/TextSnapshot.js +3 -2
- package/build/src/ToolHandler.js +80 -42
- package/build/src/bin/chrome-devtools-mcp-main.js +8 -7
- package/build/src/bin/chrome-devtools-mcp.js +1 -1
- package/build/src/bin/chrome-devtools.js +31 -41
- package/build/src/collectors/ServiceWorkerCollector.js +18 -11
- package/build/src/config/ConfigLocator.js +70 -0
- package/build/src/config/ConfigParser.js +264 -0
- package/build/src/config/ConfigWatcher.js +93 -0
- package/build/src/config/browser-options.js +2 -13
- package/build/src/config/category-options.js +15 -30
- package/build/src/config/cli-commands.js +165 -5
- package/build/src/config/cli-options.js +410 -43
- package/build/src/config/mcp-options.js +156 -234
- package/build/src/config/puppeteer-options.js +65 -0
- package/build/src/config/tool-options.js +60 -0
- package/build/src/daemon/client.js +81 -41
- package/build/src/daemon/daemon.js +55 -45
- package/build/src/daemon/utils.js +44 -11
- package/build/src/devtools/DevToolsCommentBridge.js +43 -6
- package/build/src/devtools/DevtoolsUtils.js +3 -3
- package/build/src/devtools/McpHostBindingAdapter.js +0 -1
- package/build/src/formatters/CommentFormatter.js +4 -13
- package/build/src/formatters/ConsoleFormatter.js +1 -1
- package/build/src/formatters/CssFormatter.js +3 -2
- package/build/src/formatters/HeapSnapshotFormatter.js +104 -46
- package/build/src/formatters/IssueFormatter.js +33 -32
- package/build/src/formatters/NetworkFormatter.js +12 -11
- package/build/src/formatters/SnapshotFormatter.js +31 -4
- package/build/src/index.js +54 -9
- package/build/src/processors/ChunkedTraceParser.js +11 -11
- package/build/src/processors/HeapSnapshotManager.js +121 -84
- package/build/src/processors/PerformanceTrace.js +9 -9
- package/build/src/telemetry/ClearcutLogger.js +1 -1
- package/build/src/telemetry/transformation.js +7 -11
- package/build/src/telemetry/watchdog/main.js +1 -1
- package/build/src/third_party/THIRD_PARTY_NOTICES +10 -397
- package/build/src/third_party/bundled-packages.json +7 -8
- package/build/src/third_party/devtools-formatter-worker.js +1193 -62
- package/build/src/third_party/devtools-heap-snapshot-worker.js +1126 -21
- package/build/src/third_party/index.js +8087 -19419
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantRead.md +0 -1
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantReadOptOut.md +5 -0
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantSet.md +0 -1
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantSetOptOut.md +5 -0
- package/build/src/third_party/issue-descriptions/deprecation.md +1 -1
- package/build/src/third_party/issue-descriptions/genericGeolocationPromptWithoutUserGesture.md +5 -0
- package/build/src/third_party/issue-descriptions/sriValidationFailedIntegrityMismatch.md +2 -0
- package/build/src/third_party/lighthouse-devtools-mcp-bundle.js +3 -3
- package/build/src/tools/ToolDefinition.js +16 -4
- package/build/src/tools/comments.js +20 -1
- package/build/src/tools/css.js +4 -2
- package/build/src/tools/emulation.js +17 -4
- package/build/src/tools/input.js +8 -3
- package/build/src/tools/lighthouse.js +112 -4
- package/build/src/tools/memory.js +129 -28
- package/build/src/tools/pages.js +5 -2
- package/build/src/tools/performance.js +21 -3
- package/build/src/tools/screencast.js +71 -32
- package/build/src/tools/screenshot.js +29 -14
- package/build/src/tools/script.js +90 -37
- package/build/src/tools/slim/tools.js +6 -8
- package/build/src/tools/thirdPartyDeveloper.js +6 -4
- package/build/src/tools/tools.js +35 -22
- package/build/src/tools/webmcp.js +9 -6
- package/build/src/types.js +11 -0
- package/build/src/utils/WaitForHelper.js +14 -1
- package/build/src/utils/check-for-updates.js +1 -1
- package/build/src/utils/errorHandling.js +20 -0
- package/build/src/utils/logger.js +15 -5
- package/build/src/utils/text.js +15 -0
- package/build/src/utils/url.js +99 -3
- package/build/src/version.js +1 -1
- package/package.json +14 -10
- package/skills/a11y-debugging/SKILL.md +4 -4
- package/skills/chrome-devtools/SKILL.md +2 -2
- package/skills/chrome-devtools-cli/SKILL.md +18 -15
- package/skills/cookie-debugging/SKILL.md +8 -5
- package/skills/debug-optimize-lcp/SKILL.md +1 -1
- package/skills/troubleshooting/SKILL.md +5 -6
package/build/src/McpContext.js
CHANGED
|
@@ -8,12 +8,13 @@ import os from 'node:os';
|
|
|
8
8
|
import path from 'node:path';
|
|
9
9
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
10
10
|
import { overrideDevToolsGlobals } from './devtools/DevtoolsUtils.js';
|
|
11
|
-
import { HeapSnapshotManager } from './processors/HeapSnapshotManager.js';
|
|
12
11
|
import { McpPage } from './McpPage.js';
|
|
13
12
|
import { ServiceWorkerConsoleCollector } from './collectors/ServiceWorkerCollector.js';
|
|
14
13
|
import { Locator, } from './third_party/index.js';
|
|
15
14
|
import { CLOSE_PAGE_ERROR } from './tools/ToolDefinition.js';
|
|
15
|
+
import { McpWorker } from './McpWorker.js';
|
|
16
16
|
import { getTempFilePath, resolveCanonicalPath } from './utils/files.js';
|
|
17
|
+
import { escapeForLog } from './utils/logger.js';
|
|
17
18
|
import { isAllowedUrl } from './utils/url.js';
|
|
18
19
|
// Page ids are handed out from a process-wide counter so they stay unique
|
|
19
20
|
// across all contexts, in particular across browser reconnects. An id issued
|
|
@@ -27,21 +28,19 @@ export class McpContext {
|
|
|
27
28
|
#isolatedContexts = new Map();
|
|
28
29
|
// Auto-generated name counter for when no name is provided.
|
|
29
30
|
#nextIsolatedContextId = 1;
|
|
30
|
-
#
|
|
31
|
+
// Cached McpWorker per target, reused across snapshots (mirrors #mcpPages) so
|
|
32
|
+
// referential identity holds and workers can carry per-worker state later.
|
|
33
|
+
#workers = new Map();
|
|
31
34
|
#mcpPages = new Map();
|
|
32
35
|
#selectedPage;
|
|
33
36
|
#selectedPageFallback;
|
|
34
37
|
#serviceWorkerConsoleCollector;
|
|
35
38
|
#isRunningTrace = false;
|
|
36
|
-
#screenRecorderData = null;
|
|
37
39
|
#reconnectNotice = false;
|
|
38
|
-
#extensionPages = new WeakMap();
|
|
39
|
-
#extensionServiceWorkerMap = new WeakMap();
|
|
40
|
-
#nextExtensionServiceWorkerId = 1;
|
|
41
40
|
#traceResults = [];
|
|
42
41
|
#locatorClass;
|
|
43
42
|
#options;
|
|
44
|
-
#heapSnapshotManager
|
|
43
|
+
#heapSnapshotManager;
|
|
45
44
|
#roots = undefined;
|
|
46
45
|
#allowUnrestrictedPaths;
|
|
47
46
|
constructor(browser, logger, options, locatorClass) {
|
|
@@ -54,13 +53,14 @@ export class McpContext {
|
|
|
54
53
|
this.logger = logger;
|
|
55
54
|
this.#locatorClass = locatorClass;
|
|
56
55
|
this.#options = options;
|
|
56
|
+
this.#heapSnapshotManager = options.heapSnapshotManager;
|
|
57
57
|
this.#allowUnrestrictedPaths = options.allowUnrestrictedPaths ?? false;
|
|
58
58
|
this.#reconnectNotice = options.reconnected ?? false;
|
|
59
59
|
this.#serviceWorkerConsoleCollector = new ServiceWorkerConsoleCollector(this.browser);
|
|
60
60
|
}
|
|
61
61
|
async #init() {
|
|
62
62
|
await this.createPagesSnapshot();
|
|
63
|
-
const workers =
|
|
63
|
+
const workers = this.createWorkersSnapshot();
|
|
64
64
|
await this.#serviceWorkerConsoleCollector.init(workers);
|
|
65
65
|
this.browser.on('targetcreated', this.#onTargetCreated);
|
|
66
66
|
this.browser.on('targetdestroyed', this.#onTargetDestroyed);
|
|
@@ -69,29 +69,23 @@ export class McpContext {
|
|
|
69
69
|
this.browser.off('targetcreated', this.#onTargetCreated);
|
|
70
70
|
this.browser.off('targetdestroyed', this.#onTargetDestroyed);
|
|
71
71
|
this.#serviceWorkerConsoleCollector.dispose();
|
|
72
|
-
this.#heapSnapshotManager.dispose();
|
|
73
72
|
for (const mcpPage of this.#mcpPages.values()) {
|
|
74
73
|
mcpPage.dispose();
|
|
75
74
|
}
|
|
75
|
+
this.#selectedPage?.dispose();
|
|
76
76
|
this.#mcpPages.clear();
|
|
77
|
+
this.#workers.clear();
|
|
77
78
|
// Isolated contexts are intentionally not closed here.
|
|
78
79
|
// Either the entire browser will be closed or we disconnect
|
|
79
80
|
// without destroying browser state.
|
|
80
81
|
this.#isolatedContexts.clear();
|
|
81
82
|
}
|
|
82
|
-
#onTargetCreated =
|
|
83
|
+
#onTargetCreated = (target) => {
|
|
83
84
|
try {
|
|
84
|
-
|
|
85
|
-
if (!isAllowedUrl(url, {
|
|
86
|
-
categoryExtensions: this.#options.categoryExtensions,
|
|
87
|
-
})) {
|
|
85
|
+
if (!this.#isPageTarget(target)) {
|
|
88
86
|
return;
|
|
89
87
|
}
|
|
90
|
-
|
|
91
|
-
if (!page) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
void this.#createMcpPage(page);
|
|
88
|
+
this.#createMcpPage(target);
|
|
95
89
|
}
|
|
96
90
|
catch (err) {
|
|
97
91
|
this.logger?.('Error handling targetcreated', err);
|
|
@@ -99,21 +93,12 @@ export class McpContext {
|
|
|
99
93
|
};
|
|
100
94
|
#onTargetDestroyed = (target) => {
|
|
101
95
|
try {
|
|
102
|
-
|
|
103
|
-
for (const page of this.#mcpPages.keys()) {
|
|
104
|
-
if (page.target() === target) {
|
|
105
|
-
foundPage = page;
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (!foundPage) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
const mcpPage = this.#mcpPages.get(foundPage);
|
|
96
|
+
const mcpPage = this.#mcpPages.get(target);
|
|
113
97
|
if (mcpPage) {
|
|
114
98
|
mcpPage.dispose();
|
|
115
|
-
this.#mcpPages.delete(
|
|
99
|
+
this.#mcpPages.delete(target);
|
|
116
100
|
}
|
|
101
|
+
this.#workers.delete(target);
|
|
117
102
|
}
|
|
118
103
|
catch (err) {
|
|
119
104
|
this.logger?.('Error handling targetdestroyed', err);
|
|
@@ -129,6 +114,9 @@ export class McpContext {
|
|
|
129
114
|
static resetPageIdsForTesting() {
|
|
130
115
|
nextPageId = 1;
|
|
131
116
|
}
|
|
117
|
+
static resetWorkerIdsForTesting() {
|
|
118
|
+
McpWorker.resetIdsForTesting();
|
|
119
|
+
}
|
|
132
120
|
roots() {
|
|
133
121
|
return [
|
|
134
122
|
...(this.#roots ?? []),
|
|
@@ -151,8 +139,8 @@ export class McpContext {
|
|
|
151
139
|
}
|
|
152
140
|
catch (err) {
|
|
153
141
|
const errMsg = err instanceof Error ? err.message : String(err);
|
|
154
|
-
console.error(`[MCP Context] Error resolving real path for ${filePath}: ${errMsg}`);
|
|
155
|
-
throw new Error(`Access denied: Cannot resolve base path for ${filePath}.`);
|
|
142
|
+
console.error(`[MCP Context] Error resolving real path for ${escapeForLog(filePath)}: ${escapeForLog(errMsg)}`);
|
|
143
|
+
throw new Error(`Access denied: Cannot resolve base path for ${escapeForLog(filePath)}.`);
|
|
156
144
|
}
|
|
157
145
|
// If the client never negotiated roots and the operator has explicitly
|
|
158
146
|
// opted into unrestricted access via --allow-unrestricted-paths, restore
|
|
@@ -187,12 +175,12 @@ export class McpContext {
|
|
|
187
175
|
else {
|
|
188
176
|
const rootErr = result.reason;
|
|
189
177
|
const errMsg = rootErr instanceof Error ? rootErr.message : String(rootErr);
|
|
190
|
-
console.warn(`[MCP Context] Could not resolve configured root ${root.uri}: ${errMsg}`);
|
|
178
|
+
console.warn(`[MCP Context] Could not resolve configured root ${escapeForLog(root.uri)}: ${escapeForLog(errMsg)}`);
|
|
191
179
|
// Skip this root if it cannot be resolved.
|
|
192
180
|
}
|
|
193
181
|
}
|
|
194
182
|
if (!allowed) {
|
|
195
|
-
throw new Error(`Access denied: path ${filePath} (canonical: ${canonicalPath}) is not within any of the configured workspace roots.`);
|
|
183
|
+
throw new Error(`Access denied: path ${escapeForLog(filePath)} (canonical: ${escapeForLog(canonicalPath)}) is not within any of the configured workspace roots.`);
|
|
196
184
|
}
|
|
197
185
|
return canonicalPath || path.resolve(filePath);
|
|
198
186
|
}
|
|
@@ -215,33 +203,50 @@ export class McpContext {
|
|
|
215
203
|
else {
|
|
216
204
|
page = await this.browser.newPage({ background });
|
|
217
205
|
}
|
|
218
|
-
const mcpPage =
|
|
219
|
-
await
|
|
206
|
+
const mcpPage = this.#createMcpPage(page.target());
|
|
207
|
+
await mcpPage.init();
|
|
220
208
|
this.selectPage(mcpPage);
|
|
209
|
+
await this.createPagesSnapshot();
|
|
221
210
|
return mcpPage;
|
|
222
211
|
}
|
|
223
212
|
async closePage(pageId) {
|
|
213
|
+
const page = this.getPageById(pageId);
|
|
224
214
|
if (this.#mcpPages.size === 1) {
|
|
225
215
|
throw new Error(CLOSE_PAGE_ERROR);
|
|
226
216
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
page.dispose();
|
|
230
|
-
this.#mcpPages.delete(page.pptrPage);
|
|
231
|
-
}
|
|
232
|
-
await page.pptrPage.close({ runBeforeUnload: false });
|
|
217
|
+
this.#mcpPages.delete(page.target);
|
|
218
|
+
await page.close();
|
|
233
219
|
}
|
|
234
220
|
get #hasNetworkBlockOrAllowlist() {
|
|
235
221
|
return !!(this.#options.allowlist || this.#options.blocklist);
|
|
236
222
|
}
|
|
237
|
-
installPWA(options) {
|
|
238
|
-
|
|
223
|
+
async installPWA(options) {
|
|
224
|
+
try {
|
|
225
|
+
return await this.browser.installPWA(options);
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
if (error instanceof Error &&
|
|
229
|
+
error.message.includes("Couldn't fetch install info")) {
|
|
230
|
+
return await this.browser.installPWA(options);
|
|
231
|
+
}
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
239
234
|
}
|
|
240
235
|
uninstallPWA(options) {
|
|
241
236
|
return this.browser.uninstallPWA(options);
|
|
242
237
|
}
|
|
243
|
-
launchPWA(options) {
|
|
244
|
-
|
|
238
|
+
async launchPWA(options) {
|
|
239
|
+
const page = await this.browser.launchPWA(options);
|
|
240
|
+
if (!page.url() || page.url() === 'about:blank') {
|
|
241
|
+
await page
|
|
242
|
+
.waitForNavigation({
|
|
243
|
+
timeout: this.#options.navigationTimeout ?? 10_000,
|
|
244
|
+
})
|
|
245
|
+
.catch(() => {
|
|
246
|
+
// Ignore timeout if navigation already completed or failed.
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
return page;
|
|
245
250
|
}
|
|
246
251
|
getPWAState(options) {
|
|
247
252
|
return this.browser.getPWAState(options);
|
|
@@ -252,24 +257,36 @@ export class McpContext {
|
|
|
252
257
|
isRunningPerformanceTrace() {
|
|
253
258
|
return this.#isRunningTrace;
|
|
254
259
|
}
|
|
255
|
-
getScreenRecorder() {
|
|
256
|
-
return this.#screenRecorderData;
|
|
257
|
-
}
|
|
258
|
-
setScreenRecorder(data) {
|
|
259
|
-
this.#screenRecorderData = data;
|
|
260
|
-
}
|
|
261
260
|
isCruxEnabled() {
|
|
262
261
|
return this.#options.performanceCrux;
|
|
263
262
|
}
|
|
264
263
|
getPages() {
|
|
265
264
|
return Array.from(this.#mcpPages.values());
|
|
266
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* Fetches current target info for all targets from the browser, keyed by
|
|
268
|
+
* target id. Unlike Puppeteer's cached target info, this reflects title
|
|
269
|
+
* changes, which Chrome does not report via `Target.targetInfoChanged`.
|
|
270
|
+
* Returns an empty map if the browser cannot be queried.
|
|
271
|
+
*/
|
|
272
|
+
async getTargetInfos() {
|
|
273
|
+
try {
|
|
274
|
+
// @ts-expect-error use existing browser connection (internal Puppeteer API).
|
|
275
|
+
const connection = this.browser._connection;
|
|
276
|
+
const { targetInfos } = await connection.send('Target.getTargets');
|
|
277
|
+
return new Map(targetInfos.map(info => [info.targetId, info]));
|
|
278
|
+
}
|
|
279
|
+
catch (err) {
|
|
280
|
+
this.logger?.('Failed to fetch target infos', err);
|
|
281
|
+
return new Map();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
267
284
|
getSelectedMcpPage() {
|
|
268
285
|
const page = this.#selectedPage;
|
|
269
286
|
if (!page) {
|
|
270
287
|
throw new Error('No page selected');
|
|
271
288
|
}
|
|
272
|
-
if (page.
|
|
289
|
+
if (page.isClosed()) {
|
|
273
290
|
throw new Error('The selected page has been closed. Call list_pages to see open pages.');
|
|
274
291
|
}
|
|
275
292
|
return page;
|
|
@@ -284,8 +301,8 @@ export class McpContext {
|
|
|
284
301
|
return undefined;
|
|
285
302
|
}
|
|
286
303
|
}
|
|
287
|
-
if (targetPage
|
|
288
|
-
return targetPage.
|
|
304
|
+
if (targetPage && !targetPage.isClosed()) {
|
|
305
|
+
return targetPage.url();
|
|
289
306
|
}
|
|
290
307
|
return undefined;
|
|
291
308
|
}
|
|
@@ -330,6 +347,11 @@ export class McpContext {
|
|
|
330
347
|
return this.#selectedPage === page;
|
|
331
348
|
}
|
|
332
349
|
selectPage(newPage) {
|
|
350
|
+
if (this.#selectedPage &&
|
|
351
|
+
this.#selectedPage !== newPage &&
|
|
352
|
+
!this.#mcpPages.has(this.#selectedPage.target)) {
|
|
353
|
+
this.#selectedPage.dispose();
|
|
354
|
+
}
|
|
333
355
|
this.#selectedPage = newPage;
|
|
334
356
|
newPage.updateTimeouts();
|
|
335
357
|
}
|
|
@@ -343,27 +365,30 @@ export class McpContext {
|
|
|
343
365
|
return this.#selectedPageFallback;
|
|
344
366
|
}
|
|
345
367
|
/**
|
|
346
|
-
* Creates a snapshot of the
|
|
368
|
+
* Creates a snapshot of the tracked workers. Today this is limited to
|
|
369
|
+
* extension service workers; the McpWorker abstraction lets dedicated and
|
|
370
|
+
* shared workers join the same snapshot later without changing consumers.
|
|
347
371
|
*/
|
|
348
|
-
|
|
349
|
-
const
|
|
350
|
-
const serviceWorkers = allTargets.filter(target => {
|
|
372
|
+
createWorkersSnapshot() {
|
|
373
|
+
const serviceWorkers = this.browser.targets().filter(target => {
|
|
351
374
|
return (target.type() === 'service_worker' &&
|
|
352
375
|
target.url().includes('chrome-extension://'));
|
|
353
376
|
});
|
|
354
|
-
for (
|
|
355
|
-
|
|
356
|
-
|
|
377
|
+
// Reuse the existing McpWorker for a target; only mint one (and an id) for
|
|
378
|
+
// targets seen for the first time.
|
|
379
|
+
for (const target of serviceWorkers) {
|
|
380
|
+
if (!this.#workers.has(target)) {
|
|
381
|
+
this.#workers.set(target, McpWorker.create('service_worker', target));
|
|
357
382
|
}
|
|
358
383
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
return this.#
|
|
384
|
+
// Prune workers whose target is gone (mirrors #mcpPages pruning).
|
|
385
|
+
const currentTargets = new Set(serviceWorkers);
|
|
386
|
+
for (const target of this.#workers.keys()) {
|
|
387
|
+
if (!currentTargets.has(target)) {
|
|
388
|
+
this.#workers.delete(target);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return Array.from(this.#workers.values());
|
|
367
392
|
}
|
|
368
393
|
getServiceWorkerConsoleData(extensionId) {
|
|
369
394
|
return this.#serviceWorkerConsoleCollector.getData(extensionId);
|
|
@@ -387,31 +412,35 @@ export class McpContext {
|
|
|
387
412
|
}
|
|
388
413
|
return contextToName;
|
|
389
414
|
}
|
|
390
|
-
|
|
391
|
-
let mcpPage = this.#mcpPages.get(
|
|
415
|
+
#createMcpPage(target) {
|
|
416
|
+
let mcpPage = this.#mcpPages.get(target) ??
|
|
417
|
+
(this.#selectedPage?.target === target ? this.#selectedPage : undefined);
|
|
392
418
|
if (!mcpPage) {
|
|
393
|
-
mcpPage = new McpPage(
|
|
419
|
+
mcpPage = new McpPage(target, nextPageId++, {
|
|
394
420
|
locatorClass: this.#locatorClass,
|
|
395
421
|
hasNetworkBlockOrAllowlist: this.#hasNetworkBlockOrAllowlist,
|
|
396
|
-
isolatedContextName: this.#getBrowserContextToNameMap().get(
|
|
422
|
+
isolatedContextName: this.#getBrowserContextToNameMap().get(target.browserContext()),
|
|
397
423
|
navigationTimeout: this.#options.navigationTimeout,
|
|
398
424
|
sourceMaps: this.#options.sourceMaps,
|
|
399
425
|
onNotification: this.#options.onNotification,
|
|
400
426
|
});
|
|
401
|
-
this.#mcpPages.set(page, mcpPage);
|
|
402
|
-
await mcpPage.init();
|
|
403
427
|
}
|
|
428
|
+
this.#mcpPages.set(target, mcpPage);
|
|
404
429
|
return mcpPage;
|
|
405
430
|
}
|
|
406
431
|
async createPagesSnapshot() {
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
432
|
+
const allTargets = this.#fetchPageTargets();
|
|
433
|
+
for (const target of allTargets) {
|
|
434
|
+
this.#createMcpPage(target);
|
|
435
|
+
}
|
|
436
|
+
// Prune orphaned #mcpPages entries (targets that no longer exist).
|
|
437
|
+
const currentTargets = new Set(allTargets);
|
|
438
|
+
for (const [target, mcpPage] of this.#mcpPages) {
|
|
439
|
+
if (!currentTargets.has(target)) {
|
|
440
|
+
if (mcpPage !== this.#selectedPage || mcpPage.isClosed()) {
|
|
441
|
+
mcpPage.dispose();
|
|
442
|
+
}
|
|
443
|
+
this.#mcpPages.delete(target);
|
|
415
444
|
}
|
|
416
445
|
}
|
|
417
446
|
const pages = Array.from(this.#mcpPages.values());
|
|
@@ -419,60 +448,56 @@ export class McpContext {
|
|
|
419
448
|
// `isClosed()` instead of `pages` membership avoids silently swapping a
|
|
420
449
|
// live page that is momentarily missing from the snapshot.
|
|
421
450
|
this.#selectedPageFallback = undefined;
|
|
422
|
-
if ((!this.#selectedPage || this.#selectedPage.
|
|
423
|
-
pages[0]) {
|
|
451
|
+
if ((!this.#selectedPage || this.#selectedPage.isClosed()) && pages[0]) {
|
|
424
452
|
// Record the automatic change so the response can surface it. Skipped on
|
|
425
453
|
// first connect, when there was no prior selection to replace.
|
|
426
454
|
if (this.#selectedPage) {
|
|
427
455
|
this.#selectedPageFallback = {
|
|
428
|
-
wasClosed: this.#selectedPage.
|
|
456
|
+
wasClosed: this.#selectedPage.isClosed(),
|
|
429
457
|
};
|
|
430
458
|
}
|
|
431
459
|
this.selectPage(pages[0]);
|
|
432
460
|
}
|
|
433
|
-
|
|
461
|
+
if (this.#selectedPage && !this.#selectedPage.isClosed()) {
|
|
462
|
+
await this.#selectedPage.init();
|
|
463
|
+
}
|
|
464
|
+
return pages;
|
|
434
465
|
}
|
|
435
|
-
|
|
436
|
-
const
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
})
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
if (!page) {
|
|
455
|
-
page = await target.asPage();
|
|
456
|
-
}
|
|
457
|
-
this.#extensionPages.set(target, page);
|
|
458
|
-
if (page &&
|
|
459
|
-
isAllowedUrl(page.url(), { categoryExtensions: true }) &&
|
|
460
|
-
!allPages.includes(page)) {
|
|
461
|
-
allPages.push(page);
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
catch (e) {
|
|
465
|
-
this.logger?.('Failed to get page for extension target', e);
|
|
466
|
-
}
|
|
467
|
-
}));
|
|
466
|
+
#isPageTarget(target) {
|
|
467
|
+
const existingMcpPage = this.#mcpPages.get(target);
|
|
468
|
+
const url = existingMcpPage ? existingMcpPage.url() : target.url();
|
|
469
|
+
if (!this.#options.experimentalDevToolsDebugging &&
|
|
470
|
+
url.startsWith('devtools://')) {
|
|
471
|
+
return false;
|
|
472
|
+
}
|
|
473
|
+
if (!isAllowedUrl(url, {
|
|
474
|
+
categoryExtensions: this.#options.categoryExtensions,
|
|
475
|
+
})) {
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
const type = target.type();
|
|
479
|
+
if (type === 'page') {
|
|
480
|
+
return true;
|
|
481
|
+
}
|
|
482
|
+
if (this.#options.experimentalIncludeAllPages &&
|
|
483
|
+
(type === 'background_page' || type === 'webview')) {
|
|
484
|
+
return true;
|
|
468
485
|
}
|
|
469
|
-
|
|
486
|
+
if (this.#options.experimentalDevToolsDebugging &&
|
|
487
|
+
type === 'other' &&
|
|
488
|
+
url.startsWith('devtools://')) {
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
#fetchPageTargets() {
|
|
494
|
+
return this.browser.targets().filter(target => this.#isPageTarget(target));
|
|
470
495
|
}
|
|
471
|
-
|
|
472
|
-
return this.#
|
|
496
|
+
getWorkers() {
|
|
497
|
+
return Array.from(this.#workers.values());
|
|
473
498
|
}
|
|
474
|
-
|
|
475
|
-
return this.#
|
|
499
|
+
getWorkerById(id) {
|
|
500
|
+
return this.#workers.values().find(worker => worker.id === id);
|
|
476
501
|
}
|
|
477
502
|
async #writeFile(filepath, data) {
|
|
478
503
|
const resolved = await this.validatePath(filepath);
|
|
@@ -493,7 +518,9 @@ export class McpContext {
|
|
|
493
518
|
});
|
|
494
519
|
}
|
|
495
520
|
catch (err) {
|
|
496
|
-
throw new Error(`Could not write ${filepath}`, {
|
|
521
|
+
throw new Error(`Could not write ${escapeForLog(filepath)}`, {
|
|
522
|
+
cause: err,
|
|
523
|
+
});
|
|
497
524
|
}
|
|
498
525
|
}
|
|
499
526
|
async saveTemporaryFile(data, filename) {
|
|
@@ -573,11 +600,11 @@ export class McpContext {
|
|
|
573
600
|
async getHeapSnapshotObjectDetails(filePath, nodeId) {
|
|
574
601
|
return await this.#heapSnapshotManager.getObjectInfo(filePath, nodeId);
|
|
575
602
|
}
|
|
576
|
-
async
|
|
577
|
-
return this.#heapSnapshotManager.
|
|
603
|
+
async analyzeHeapSnapshotContexts(filePath) {
|
|
604
|
+
return await this.#heapSnapshotManager.analyzeContexts(filePath);
|
|
578
605
|
}
|
|
579
|
-
|
|
580
|
-
return this.#heapSnapshotManager.
|
|
606
|
+
async closeHeapSnapshot(filePath) {
|
|
607
|
+
return await this.#heapSnapshotManager.disposeSnapshot(filePath);
|
|
581
608
|
}
|
|
582
609
|
async getHeapSnapshotRetainingPaths(filePath, nodeId, maxDepth, maxNodes, maxSiblings) {
|
|
583
610
|
return await this.#heapSnapshotManager.getRetainingPaths(filePath, nodeId, maxDepth, maxNodes, maxSiblings);
|
|
@@ -608,18 +635,41 @@ export class McpContext {
|
|
|
608
635
|
}
|
|
609
636
|
throw new Error(`Not allowed by allowlist: ${url}`);
|
|
610
637
|
}
|
|
638
|
+
async #loadNetworkResource(url) {
|
|
639
|
+
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
|
640
|
+
const maxRedirects = 20;
|
|
641
|
+
let currentUrl = url;
|
|
642
|
+
for (let redirectCount = 0;; redirectCount++) {
|
|
643
|
+
this.#validateUrlNotBlocked(currentUrl);
|
|
644
|
+
this.#validateUrlAllowed(currentUrl);
|
|
645
|
+
const response = await fetch(currentUrl, { redirect: 'manual' });
|
|
646
|
+
if (!redirectStatuses.has(response.status)) {
|
|
647
|
+
if (!response.ok) {
|
|
648
|
+
throw new Error(`Failed to load resource: ${currentUrl}`);
|
|
649
|
+
}
|
|
650
|
+
return await response.text();
|
|
651
|
+
}
|
|
652
|
+
const location = response.headers.get('location');
|
|
653
|
+
if (!location) {
|
|
654
|
+
throw new Error(`Failed to load resource: ${currentUrl}`);
|
|
655
|
+
}
|
|
656
|
+
if (redirectCount >= maxRedirects) {
|
|
657
|
+
throw new Error(`Too many redirects loading resource: ${url}`);
|
|
658
|
+
}
|
|
659
|
+
await response.body?.cancel();
|
|
660
|
+
currentUrl = new URL(location, currentUrl);
|
|
661
|
+
if (currentUrl.protocol !== 'https:' && currentUrl.protocol !== 'http:') {
|
|
662
|
+
throw new Error(`Unsupported protocol for: ${currentUrl}`);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
611
666
|
async loadResource(path) {
|
|
612
667
|
const url = new URL(path);
|
|
613
668
|
this.#validateUrlNotBlocked(url);
|
|
614
669
|
switch (url.protocol) {
|
|
615
670
|
case 'https:':
|
|
616
671
|
case 'http:': {
|
|
617
|
-
this.#
|
|
618
|
-
const response = await fetch(url);
|
|
619
|
-
if (!response.ok) {
|
|
620
|
-
throw new Error(`Failed to load resource: ${url}`);
|
|
621
|
-
}
|
|
622
|
-
return response.text();
|
|
672
|
+
return await this.#loadNetworkResource(url);
|
|
623
673
|
}
|
|
624
674
|
case 'file:': {
|
|
625
675
|
const resolved = await this.validatePath(fileURLToPath(url));
|