chrome-devtools-mcp 1.7.0 → 1.9.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 -874
- package/build/src/McpContext.js +98 -45
- package/build/src/McpPage.js +10 -4
- package/build/src/McpResponse.js +22 -4
- package/build/src/ToolHandler.js +81 -57
- package/build/src/bin/chrome-devtools-mcp-main.js +4 -4
- package/build/src/bin/chrome-devtools.js +29 -30
- package/build/src/browser.js +8 -17
- package/build/src/{PageCollector.js → collectors/PageCollector.js} +7 -7
- package/build/src/{ServiceWorkerCollector.js → collectors/ServiceWorkerCollector.js} +1 -1
- package/build/src/config/browser-options.js +123 -0
- package/build/src/config/category-options.js +80 -0
- package/build/src/{bin/chrome-devtools-cli-options.js → config/cli-options.js} +380 -25
- package/build/src/{bin/chrome-devtools-mcp-cli-options.js → config/mcp-options.js} +113 -148
- package/build/src/daemon/client.js +11 -2
- package/build/src/daemon/daemon.js +2 -2
- package/build/src/daemon/utils.js +6 -0
- package/build/src/devtools/DevtoolsUtils.js +29 -35
- package/build/src/formatters/ConsoleFormatter.js +43 -10
- package/build/src/formatters/HeapSnapshotFormatter.js +10 -2
- package/build/src/formatters/IssueFormatter.js +1 -1
- package/build/src/formatters/NetworkFormatter.js +12 -9
- package/build/src/index.js +185 -91
- package/build/src/{HeapSnapshotManager.js → processors/HeapSnapshotManager.js} +21 -5
- package/build/src/{trace-processing/parse.js → processors/PerformanceTrace.js} +1 -1
- package/build/src/telemetry/ClearcutLogger.js +18 -5
- package/build/src/telemetry/flagUtils.js +4 -3
- package/build/src/telemetry/types.js +4 -0
- package/build/src/third_party/THIRD_PARTY_NOTICES +8 -65
- package/build/src/third_party/bundled-packages.json +4 -4
- package/build/src/third_party/devtools-formatter-worker.js +22 -0
- package/build/src/third_party/devtools-heap-snapshot-worker.js +296 -42
- package/build/src/third_party/index.js +38728 -37176
- package/build/src/third_party/issue-descriptions/connectionAllowlistEmbeddingRequirementNotSatisfied.md +26 -0
- package/build/src/third_party/issue-descriptions/connectionAllowlistIFrameAttributeLoosensEmbeddingRequirement.md +19 -0
- package/build/src/third_party/issue-descriptions/connectionAllowlistInvalidAllowConnectionAllowlistFrom.md +21 -0
- package/build/src/third_party/issue-descriptions/federatedAuthRequestAccountsBlockedByConnectionAllowlist.md +1 -0
- package/build/src/third_party/issue-descriptions/federatedAuthRequestConfigBlockedByConnectionAllowlist.md +1 -0
- package/build/src/third_party/issue-descriptions/federatedAuthRequestIdTokenBlockedByConnectionAllowlist.md +1 -0
- package/build/src/third_party/issue-descriptions/federatedAuthRequestWellKnownBlockedByConnectionAllowlist.md +1 -0
- package/build/src/tools/ToolDefinition.js +15 -0
- package/build/src/tools/categories.js +2 -5
- package/build/src/tools/console.js +9 -3
- package/build/src/tools/emulation.js +2 -2
- package/build/src/tools/extensions.js +7 -5
- package/build/src/tools/input.js +26 -16
- package/build/src/tools/lighthouse.js +3 -1
- package/build/src/tools/memory.js +112 -16
- package/build/src/tools/network.js +7 -4
- package/build/src/tools/pages.js +34 -23
- package/build/src/tools/performance.js +16 -25
- package/build/src/tools/pwa.js +128 -0
- package/build/src/tools/screencast.js +7 -4
- package/build/src/tools/screenshot.js +42 -9
- package/build/src/tools/script.js +29 -8
- package/build/src/tools/slim/tools.js +43 -35
- package/build/src/tools/snapshot.js +5 -3
- package/build/src/tools/thirdPartyDeveloper.js +2 -2
- package/build/src/tools/tools.js +2 -0
- package/build/src/tools/webmcp.js +2 -2
- package/build/src/{WaitForHelper.js → utils/WaitForHelper.js} +74 -38
- package/build/src/utils/bytes.js +105 -0
- package/build/src/utils/logger.js +25 -11
- package/build/src/utils/url.js +73 -0
- package/build/src/version.js +1 -1
- package/package.json +21 -13
- package/skills/a11y-debugging/SKILL.md +2 -0
- package/skills/chrome-devtools/SKILL.md +5 -4
- package/skills/chrome-devtools-cli/SKILL.md +103 -64
- package/skills/cookie-debugging/SKILL.md +149 -0
- package/skills/debug-optimize-lcp/SKILL.md +9 -9
- package/skills/memory-leak-debugging/SKILL.md +6 -2
- package/skills/troubleshooting/SKILL.md +1 -0
- package/build/src/utils/string.js +0 -37
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { zod } from '../../third_party/index.js';
|
|
7
7
|
import { ToolCategory } from '../categories.js';
|
|
8
8
|
import { definePageTool } from '../ToolDefinition.js';
|
|
9
|
+
import { validateUrl } from '../../utils/url.js';
|
|
9
10
|
export const screenshot = definePageTool({
|
|
10
11
|
name: 'screenshot',
|
|
11
12
|
description: `Takes a screenshot`,
|
|
@@ -16,7 +17,7 @@ export const screenshot = definePageTool({
|
|
|
16
17
|
},
|
|
17
18
|
schema: {},
|
|
18
19
|
blockedByDialog: true,
|
|
19
|
-
verifyFilesSchema:
|
|
20
|
+
verifyFilesSchema: {},
|
|
20
21
|
handler: async (request, response, context) => {
|
|
21
22
|
const page = request.page;
|
|
22
23
|
const screenshot = await page.pptrPage.screenshot({
|
|
@@ -27,40 +28,46 @@ export const screenshot = definePageTool({
|
|
|
27
28
|
response.appendResponseLine(filepath);
|
|
28
29
|
},
|
|
29
30
|
});
|
|
30
|
-
export const navigate = definePageTool({
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
31
|
+
export const navigate = definePageTool(args => {
|
|
32
|
+
return {
|
|
33
|
+
name: 'navigate',
|
|
34
|
+
description: `Loads a URL`,
|
|
35
|
+
annotations: {
|
|
36
|
+
category: ToolCategory.NAVIGATION,
|
|
37
|
+
readOnlyHint: false,
|
|
38
|
+
},
|
|
39
|
+
schema: {
|
|
40
|
+
url: zod.string().describe('URL to navigate to'),
|
|
41
|
+
},
|
|
42
|
+
blockedByDialog: false,
|
|
43
|
+
verifyFilesSchema: {},
|
|
44
|
+
handler: async (request, response) => {
|
|
45
|
+
validateUrl(request.params.url, {
|
|
46
|
+
javascriptEvaluation: args?.javascriptEvaluation,
|
|
47
|
+
categoryExtensions: args?.categoryExtensions,
|
|
48
|
+
});
|
|
49
|
+
const page = request.page;
|
|
50
|
+
const options = {
|
|
51
|
+
timeout: 30_000,
|
|
52
|
+
};
|
|
53
|
+
const dialogHandler = (dialog) => {
|
|
54
|
+
if (dialog.type() === 'beforeunload') {
|
|
55
|
+
response.appendResponseLine(`Accepted a beforeunload dialog.`);
|
|
56
|
+
void dialog.accept();
|
|
57
|
+
// We are not going to report the dialog like regular dialogs.
|
|
58
|
+
page.clearDialog();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
page.pptrPage.on('dialog', dialogHandler);
|
|
62
|
+
try {
|
|
63
|
+
await page.pptrPage.goto(request.params.url, options);
|
|
64
|
+
response.appendResponseLine(`Navigated to ${page.pptrPage.url()}.`);
|
|
53
65
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
finally {
|
|
61
|
-
page.pptrPage.off('dialog', dialogHandler);
|
|
62
|
-
}
|
|
63
|
-
},
|
|
66
|
+
finally {
|
|
67
|
+
page.pptrPage.off('dialog', dialogHandler);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
64
71
|
});
|
|
65
72
|
export const evaluate = definePageTool({
|
|
66
73
|
name: 'evaluate',
|
|
@@ -68,12 +75,13 @@ export const evaluate = definePageTool({
|
|
|
68
75
|
annotations: {
|
|
69
76
|
category: ToolCategory.DEBUGGING,
|
|
70
77
|
readOnlyHint: false,
|
|
78
|
+
conditions: ['javascriptEvaluation'],
|
|
71
79
|
},
|
|
72
80
|
schema: {
|
|
73
81
|
script: zod.string().describe(`JS script to run on the page`),
|
|
74
82
|
},
|
|
75
83
|
blockedByDialog: true,
|
|
76
|
-
verifyFilesSchema:
|
|
84
|
+
verifyFilesSchema: {},
|
|
77
85
|
handler: async (request, response) => {
|
|
78
86
|
const page = request.page;
|
|
79
87
|
try {
|
|
@@ -8,7 +8,7 @@ import { ToolCategory } from './categories.js';
|
|
|
8
8
|
import { definePageTool, timeoutSchema } from './ToolDefinition.js';
|
|
9
9
|
export const takeSnapshot = definePageTool({
|
|
10
10
|
name: 'take_snapshot',
|
|
11
|
-
description: `Take a text snapshot of the
|
|
11
|
+
description: `Take a text snapshot of the target page based on the a11y tree. The snapshot lists page elements along with a unique
|
|
12
12
|
identifier (uid). Always use the latest snapshot. Prefer taking a snapshot over taking a screenshot. The snapshot indicates the element selected
|
|
13
13
|
in the DevTools Elements panel (if any).`,
|
|
14
14
|
annotations: {
|
|
@@ -27,7 +27,9 @@ in the DevTools Elements panel (if any).`,
|
|
|
27
27
|
.describe('The absolute path, or a path relative to the current working directory, to save the snapshot to instead of attaching it to the response.'),
|
|
28
28
|
},
|
|
29
29
|
blockedByDialog: true,
|
|
30
|
-
verifyFilesSchema:
|
|
30
|
+
verifyFilesSchema: {
|
|
31
|
+
filePath: true,
|
|
32
|
+
},
|
|
31
33
|
handler: async (request, response) => {
|
|
32
34
|
response.includeSnapshot({
|
|
33
35
|
verbose: request.params.verbose ?? false,
|
|
@@ -50,7 +52,7 @@ export const waitFor = definePageTool({
|
|
|
50
52
|
...timeoutSchema,
|
|
51
53
|
},
|
|
52
54
|
blockedByDialog: true,
|
|
53
|
-
verifyFilesSchema:
|
|
55
|
+
verifyFilesSchema: {},
|
|
54
56
|
handler: async (request, response) => {
|
|
55
57
|
const page = request.page;
|
|
56
58
|
await page.waitForTextOnPage(request.params.text, request.params.timeout);
|
|
@@ -21,7 +21,7 @@ third-party developer tools with additional functionality.`,
|
|
|
21
21
|
},
|
|
22
22
|
schema: {},
|
|
23
23
|
blockedByDialog: false,
|
|
24
|
-
verifyFilesSchema:
|
|
24
|
+
verifyFilesSchema: {},
|
|
25
25
|
handler: async (_request, response) => {
|
|
26
26
|
response.setListThirdPartyDeveloperTools();
|
|
27
27
|
},
|
|
@@ -41,7 +41,7 @@ export const executeThirdPartyDeveloperTool = definePageTool({
|
|
|
41
41
|
.describe('The JSON-stringified parameters to pass to the tool'),
|
|
42
42
|
},
|
|
43
43
|
blockedByDialog: false,
|
|
44
|
-
verifyFilesSchema:
|
|
44
|
+
verifyFilesSchema: {},
|
|
45
45
|
handler: async (request, response) => {
|
|
46
46
|
const toolName = request.params.toolName;
|
|
47
47
|
let params = {};
|
package/build/src/tools/tools.js
CHANGED
|
@@ -12,6 +12,7 @@ import * as memoryTools from './memory.js';
|
|
|
12
12
|
import * as networkTools from './network.js';
|
|
13
13
|
import * as pagesTools from './pages.js';
|
|
14
14
|
import * as performanceTools from './performance.js';
|
|
15
|
+
import * as pwaTools from './pwa.js';
|
|
15
16
|
import * as screencastTools from './screencast.js';
|
|
16
17
|
import * as screenshotTools from './screenshot.js';
|
|
17
18
|
import * as scriptTools from './script.js';
|
|
@@ -32,6 +33,7 @@ export const createTools = (args) => {
|
|
|
32
33
|
...Object.values(networkTools),
|
|
33
34
|
...Object.values(pagesTools),
|
|
34
35
|
...Object.values(performanceTools),
|
|
36
|
+
...Object.values(pwaTools),
|
|
35
37
|
...Object.values(screencastTools),
|
|
36
38
|
...Object.values(screenshotTools),
|
|
37
39
|
...Object.values(scriptTools),
|
|
@@ -15,7 +15,7 @@ export const listWebMcpTools = definePageTool({
|
|
|
15
15
|
},
|
|
16
16
|
schema: {},
|
|
17
17
|
blockedByDialog: false,
|
|
18
|
-
verifyFilesSchema:
|
|
18
|
+
verifyFilesSchema: {},
|
|
19
19
|
handler: async (_request, response) => {
|
|
20
20
|
response.setListWebMcpTools();
|
|
21
21
|
},
|
|
@@ -35,7 +35,7 @@ export const executeWebMcpTool = definePageTool({
|
|
|
35
35
|
.describe('The JSON-stringified parameters to pass to the WebMCP tool'),
|
|
36
36
|
},
|
|
37
37
|
blockedByDialog: false,
|
|
38
|
-
verifyFilesSchema:
|
|
38
|
+
verifyFilesSchema: {},
|
|
39
39
|
handler: async (request, response) => {
|
|
40
40
|
const toolName = request.params.toolName;
|
|
41
41
|
let input = {};
|
|
@@ -55,7 +55,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
|
|
|
55
55
|
var e = new Error(message);
|
|
56
56
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
57
57
|
});
|
|
58
|
-
import { logger } from './
|
|
58
|
+
import { logger } from './logger.js';
|
|
59
59
|
export class WaitForHelper {
|
|
60
60
|
#abortController = new AbortController();
|
|
61
61
|
#page;
|
|
@@ -143,32 +143,6 @@ export class WaitForHelper {
|
|
|
143
143
|
__disposeResources(env_1);
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
-
async waitForNavigationStarted() {
|
|
147
|
-
// Currently Puppeteer does not have API
|
|
148
|
-
// For when a navigation is about to start
|
|
149
|
-
const navigationStartedPromise = new Promise(resolve => {
|
|
150
|
-
const listener = (event) => {
|
|
151
|
-
if ([
|
|
152
|
-
'historySameDocument',
|
|
153
|
-
'historyDifferentDocument',
|
|
154
|
-
'sameDocument',
|
|
155
|
-
].includes(event.navigationType)) {
|
|
156
|
-
resolve(false);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
resolve(true);
|
|
160
|
-
};
|
|
161
|
-
this.#page._client().on('Page.frameStartedNavigating', listener);
|
|
162
|
-
this.#abortController.signal.addEventListener('abort', () => {
|
|
163
|
-
resolve(false);
|
|
164
|
-
this.#page._client().off('Page.frameStartedNavigating', listener);
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
return await Promise.race([
|
|
168
|
-
navigationStartedPromise,
|
|
169
|
-
this.timeout(this.#expectNavigationIn).then(() => false),
|
|
170
|
-
]);
|
|
171
|
-
}
|
|
172
146
|
timeout(time) {
|
|
173
147
|
return new Promise(res => {
|
|
174
148
|
const id = setTimeout(res, time);
|
|
@@ -211,17 +185,64 @@ export class WaitForHelper {
|
|
|
211
185
|
this.#abortController.signal.addEventListener('abort', () => {
|
|
212
186
|
this.#page.off('dialog', dialogHandler);
|
|
213
187
|
});
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
188
|
+
// A scoped AbortController used to clean up navigation probe listeners.
|
|
189
|
+
// When aborted (either after navigation detection finishes or if this.#abortController
|
|
190
|
+
// aborts), it removes CDP navigation probe listeners and automatically
|
|
191
|
+
// detaches the abort listener from this.#abortController.signal.
|
|
192
|
+
const navigationAbortController = new AbortController();
|
|
193
|
+
const navigationStartedResolvers = Promise.withResolvers();
|
|
194
|
+
const navigationListener = (event) => {
|
|
195
|
+
if (event.frameId !== this.#page.mainFrame()._id) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (event.navigationType === 'sameDocument' ||
|
|
199
|
+
event.navigationType === 'historySameDocument') {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
navigationStartedResolvers.resolve(true);
|
|
203
|
+
};
|
|
204
|
+
const requestedNavigationListener = (event) => {
|
|
205
|
+
if (event.frameId === this.#page.mainFrame()._id) {
|
|
206
|
+
navigationStartedResolvers.resolve(true);
|
|
221
207
|
}
|
|
222
|
-
|
|
208
|
+
};
|
|
209
|
+
this.#page._client().on('Page.frameStartedNavigating', navigationListener);
|
|
210
|
+
this.#page
|
|
211
|
+
._client()
|
|
212
|
+
.on('Page.frameRequestedNavigation', requestedNavigationListener);
|
|
213
|
+
navigationAbortController.signal.addEventListener('abort', () => {
|
|
214
|
+
this.#page
|
|
215
|
+
._client()
|
|
216
|
+
.off('Page.frameStartedNavigating', navigationListener);
|
|
217
|
+
this.#page
|
|
218
|
+
._client()
|
|
219
|
+
.off('Page.frameRequestedNavigation', requestedNavigationListener);
|
|
220
|
+
});
|
|
221
|
+
this.#abortController.signal.addEventListener('abort', () => {
|
|
222
|
+
navigationStartedResolvers.resolve(false);
|
|
223
|
+
navigationAbortController.abort();
|
|
224
|
+
}, { signal: navigationAbortController.signal });
|
|
225
|
+
// Puppeteer's waitForNavigation must be started before the action runs so that
|
|
226
|
+
// it captures the pre-action loader ID. If started after the action triggers navigation,
|
|
227
|
+
// it risks recording the new loader ID and hanging until timeout.
|
|
228
|
+
// If no navigation occurs, this.#abortController will cancel it in the finally block.
|
|
229
|
+
const navigationFinished = this.#page
|
|
230
|
+
.waitForNavigation({
|
|
231
|
+
timeout: options?.timeout ?? this.#navigationTimeout,
|
|
232
|
+
signal: this.#abortController.signal,
|
|
233
|
+
ignoreSameDocumentNavigation: true,
|
|
223
234
|
})
|
|
224
|
-
.
|
|
235
|
+
.then(result => {
|
|
236
|
+
navigationStartedResolvers.resolve(true);
|
|
237
|
+
return result;
|
|
238
|
+
})
|
|
239
|
+
.catch(error => {
|
|
240
|
+
if (this.#abortController.signal.aborted ||
|
|
241
|
+
(error instanceof Error && error.name === 'AbortError')) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
logger?.(error);
|
|
245
|
+
});
|
|
225
246
|
try {
|
|
226
247
|
await action();
|
|
227
248
|
}
|
|
@@ -230,14 +251,29 @@ export class WaitForHelper {
|
|
|
230
251
|
this.#abortController.abort();
|
|
231
252
|
throw error;
|
|
232
253
|
}
|
|
254
|
+
const expectNavigationIn = options?.expectNavigationIn ?? this.#expectNavigationIn;
|
|
255
|
+
const navigationStarted = await Promise.race([
|
|
256
|
+
navigationStartedResolvers.promise,
|
|
257
|
+
this.timeout(expectNavigationIn).then(() => {
|
|
258
|
+
navigationStartedResolvers.resolve(false);
|
|
259
|
+
return false;
|
|
260
|
+
}),
|
|
261
|
+
]);
|
|
262
|
+
navigationAbortController.abort();
|
|
233
263
|
try {
|
|
234
|
-
await
|
|
264
|
+
// Only await navigation if one was actually initiated; otherwise, the
|
|
265
|
+
// pending waitForNavigation promise will be cancelled when this.#abortController aborts.
|
|
266
|
+
if (navigationStarted) {
|
|
267
|
+
await navigationFinished;
|
|
268
|
+
}
|
|
235
269
|
if (this.#dialogDetected) {
|
|
236
270
|
return this.#getResult();
|
|
237
271
|
}
|
|
238
272
|
// Wait for stable dom after navigation so we execute in
|
|
239
273
|
// the correct context
|
|
240
|
-
|
|
274
|
+
if (options?.waitForStableDom !== false) {
|
|
275
|
+
await this.waitForStableDom();
|
|
276
|
+
}
|
|
241
277
|
}
|
|
242
278
|
catch (error) {
|
|
243
279
|
logger?.(error);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod } from '../third_party/index.js';
|
|
7
|
+
const BYTE_UNITS = {
|
|
8
|
+
b: 1,
|
|
9
|
+
byte: 1,
|
|
10
|
+
bytes: 1,
|
|
11
|
+
k: 1000,
|
|
12
|
+
kb: 1000,
|
|
13
|
+
kib: 1024,
|
|
14
|
+
m: 1000 * 1000,
|
|
15
|
+
mb: 1000 * 1000,
|
|
16
|
+
mib: 1024 * 1024,
|
|
17
|
+
g: 1000 * 1000 * 1000,
|
|
18
|
+
gb: 1000 * 1000 * 1000,
|
|
19
|
+
gib: 1024 * 1024 * 1024,
|
|
20
|
+
t: 1000 * 1000 * 1000 * 1000,
|
|
21
|
+
tb: 1000 * 1000 * 1000 * 1000,
|
|
22
|
+
tib: 1024 * 1024 * 1024 * 1024,
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Parses a byte size string (e.g. "1M", "1MB", "500KB", "1.5GB", "1024") into bytes.
|
|
26
|
+
*/
|
|
27
|
+
export function parseByteSize(value) {
|
|
28
|
+
const trimmed = value.trim();
|
|
29
|
+
if (trimmed === '') {
|
|
30
|
+
throw new Error(`Invalid byte size: "${value}"`);
|
|
31
|
+
}
|
|
32
|
+
const match = trimmed.match(/^(\d+(?:\.\d+)?)\s*([a-zA-Z]+)?$/);
|
|
33
|
+
if (!match) {
|
|
34
|
+
throw new Error(`Invalid byte size format: "${value}". Expected a number or format like "1024", "1M", "1MB", "1G", "1GB".`);
|
|
35
|
+
}
|
|
36
|
+
const numMatch = match[1];
|
|
37
|
+
if (numMatch === undefined) {
|
|
38
|
+
throw new Error(`Invalid byte size: "${value}"`);
|
|
39
|
+
}
|
|
40
|
+
const num = Number(numMatch);
|
|
41
|
+
if (!Number.isFinite(num) || num < 0) {
|
|
42
|
+
throw new Error(`Invalid byte size: "${value}"`);
|
|
43
|
+
}
|
|
44
|
+
const unitMatch = match[2];
|
|
45
|
+
if (unitMatch === undefined) {
|
|
46
|
+
return Math.round(num);
|
|
47
|
+
}
|
|
48
|
+
const unit = unitMatch.toLowerCase();
|
|
49
|
+
const multiplier = BYTE_UNITS[unit];
|
|
50
|
+
if (multiplier === undefined) {
|
|
51
|
+
throw new Error(`Unknown unit "${unitMatch}" in "${value}". Supported units: B, KB, KiB, MB, MiB, GB, GiB, TB, TiB.`);
|
|
52
|
+
}
|
|
53
|
+
const bytes = Math.round(num * multiplier);
|
|
54
|
+
if (!Number.isFinite(bytes)) {
|
|
55
|
+
throw new Error(`Invalid byte size: "${value}"`);
|
|
56
|
+
}
|
|
57
|
+
return bytes;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Parses an inclusive byte-size range (e.g. "1MB", "1MB-2MB", "-1MB", "1MB-").
|
|
61
|
+
*/
|
|
62
|
+
export function parseByteSizeRange(value) {
|
|
63
|
+
const trimmed = value.trim();
|
|
64
|
+
if (!trimmed.includes('-')) {
|
|
65
|
+
return { min: parseByteSize(trimmed), max: undefined };
|
|
66
|
+
}
|
|
67
|
+
const parts = trimmed.split('-');
|
|
68
|
+
if (parts.length !== 2) {
|
|
69
|
+
throw new Error(`Invalid byte size range: "${value}". Expected a size or range like "1MB", "1MB-2MB", "-1MB", or "1MB-".`);
|
|
70
|
+
}
|
|
71
|
+
const minValue = parts[0];
|
|
72
|
+
const maxValue = parts[1];
|
|
73
|
+
if (minValue === undefined || maxValue === undefined) {
|
|
74
|
+
throw new Error(`Invalid byte size range: "${value}"`);
|
|
75
|
+
}
|
|
76
|
+
const minText = minValue.trim();
|
|
77
|
+
const maxText = maxValue.trim();
|
|
78
|
+
if (minText === '' && maxText === '') {
|
|
79
|
+
throw new Error(`Invalid byte size range: "${value}". At least one bound is required.`);
|
|
80
|
+
}
|
|
81
|
+
const min = minText === '' ? 0 : parseByteSize(minText);
|
|
82
|
+
const max = maxText === '' ? undefined : parseByteSize(maxText);
|
|
83
|
+
if (max !== undefined && min > max) {
|
|
84
|
+
throw new Error(`Invalid byte size range: "${value}". The lower bound must not exceed the upper bound.`);
|
|
85
|
+
}
|
|
86
|
+
return { min, max };
|
|
87
|
+
}
|
|
88
|
+
export function byteSizeRangeSchema(description) {
|
|
89
|
+
return zod
|
|
90
|
+
.string()
|
|
91
|
+
.transform((value, context) => {
|
|
92
|
+
try {
|
|
93
|
+
return parseByteSizeRange(value);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
context.addIssue({
|
|
97
|
+
code: zod.ZodIssueCode.custom,
|
|
98
|
+
message: error instanceof Error ? error.message : 'Invalid byte size range',
|
|
99
|
+
});
|
|
100
|
+
return zod.NEVER;
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
.describe(description);
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=bytes.js.map
|
|
@@ -4,24 +4,18 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import fs from 'node:fs';
|
|
7
|
-
import
|
|
7
|
+
import util from 'node:util';
|
|
8
8
|
const mcpDebugNamespace = 'mcp:log';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
...(process.env['DEBUG'] ? [process.env['DEBUG']] : []),
|
|
12
|
-
];
|
|
9
|
+
let logFileStream;
|
|
10
|
+
const _debugLog = util.debuglog(mcpDebugNamespace);
|
|
13
11
|
export function saveLogsToFile(fileName) {
|
|
14
|
-
// Enable overrides everything so we need to add them
|
|
15
|
-
debug.enable(namespacesToEnable.join(','));
|
|
16
12
|
const logFile = fs.createWriteStream(fileName, { flags: 'a+' });
|
|
17
|
-
debug.log = function (...chunks) {
|
|
18
|
-
logFile.write(`${chunks.join(' ')}\n`);
|
|
19
|
-
};
|
|
20
13
|
logFile.on('error', function (error) {
|
|
21
14
|
console.error(`Error when opening/writing to log file: ${error.message}`);
|
|
22
15
|
logFile.end();
|
|
23
16
|
process.exit(1);
|
|
24
17
|
});
|
|
18
|
+
logFileStream = logFile;
|
|
25
19
|
return logFile;
|
|
26
20
|
}
|
|
27
21
|
export function flushLogs(logFile, timeoutMs = 2000) {
|
|
@@ -33,5 +27,25 @@ export function flushLogs(logFile, timeoutMs = 2000) {
|
|
|
33
27
|
});
|
|
34
28
|
});
|
|
35
29
|
}
|
|
36
|
-
export const logger =
|
|
30
|
+
export const logger = (...args) => {
|
|
31
|
+
if (logFileStream) {
|
|
32
|
+
logFileStream.write(`${new Date().toISOString()} ${mcpDebugNamespace} ${util.format(...args)}\n`);
|
|
33
|
+
}
|
|
34
|
+
else if (_debugLog.enabled) {
|
|
35
|
+
_debugLog('%s %s', new Date().toISOString(), util.format(...args));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
export const puppeteerLogger = (prefix) => {
|
|
39
|
+
if (logFileStream) {
|
|
40
|
+
return (...args) => {
|
|
41
|
+
logFileStream.write(`${new Date().toISOString()} ${prefix} ${util.format(...args)}\n`);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const dbg = util.debuglog(prefix);
|
|
45
|
+
return dbg.enabled
|
|
46
|
+
? (...args) => {
|
|
47
|
+
dbg('%s %s', new Date().toISOString(), util.format(...args));
|
|
48
|
+
}
|
|
49
|
+
: undefined;
|
|
50
|
+
};
|
|
37
51
|
//# sourceMappingURL=logger.js.map
|
package/build/src/utils/url.js
CHANGED
|
@@ -40,4 +40,77 @@ export function isLocalhost(url) {
|
|
|
40
40
|
}
|
|
41
41
|
return false;
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Determines whether a URL is allowed for navigation and page tracking.
|
|
45
|
+
*
|
|
46
|
+
* Disallowed schemes:
|
|
47
|
+
* - chrome: (except chrome://newtab/ and chrome://inspect*)
|
|
48
|
+
* - chrome-untrusted:
|
|
49
|
+
* - chrome-extension: (unless categoryExtensions is true)
|
|
50
|
+
*
|
|
51
|
+
* @param url The URL string to test.
|
|
52
|
+
* @param options Configuration specifying whether extensions are enabled.
|
|
53
|
+
* @returns true if the URL is allowed.
|
|
54
|
+
*/
|
|
55
|
+
export function isAllowedUrl(url, options) {
|
|
56
|
+
let parsed;
|
|
57
|
+
try {
|
|
58
|
+
parsed = new URL(url);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (parsed.protocol === 'chrome:') {
|
|
64
|
+
const host = parsed.hostname.toLowerCase();
|
|
65
|
+
const path = parsed.pathname.toLowerCase();
|
|
66
|
+
if (host === 'newtab' || host === 'new-tab-page' || host === 'inspect') {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
if (host === '' &&
|
|
70
|
+
(path === 'newtab' ||
|
|
71
|
+
path === 'new-tab-page' ||
|
|
72
|
+
path.startsWith('inspect'))) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (parsed.protocol === 'chrome:' ||
|
|
77
|
+
parsed.protocol === 'chrome-untrusted:') {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (!options.categoryExtensions && parsed.protocol === 'chrome-extension:') {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
const DISALLOWED_PROTOCOLS = new Set(['javascript:', 'data:', 'vbscript:']);
|
|
86
|
+
/**
|
|
87
|
+
* Validates a URL string by parsing it with `new URL` and checking for disallowed protocols and restricted schemes.
|
|
88
|
+
*
|
|
89
|
+
* @param url The URL string to validate.
|
|
90
|
+
* @param options Options object containing javascriptEvaluation and categoryExtensions.
|
|
91
|
+
* @returns The parsed URL.
|
|
92
|
+
* @throws Error if the URL does not parse with `new URL`, or if JavaScript evaluation is disabled and a disallowed URL is passed,
|
|
93
|
+
* or if navigating to a restricted scheme.
|
|
94
|
+
*/
|
|
95
|
+
export function validateUrl(url, options) {
|
|
96
|
+
const { javascriptEvaluation, categoryExtensions } = options;
|
|
97
|
+
let parsed;
|
|
98
|
+
try {
|
|
99
|
+
parsed = new URL(url);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
throw new Error(`Invalid URL: "${url}". URLs must be valid according to the URL standard.`);
|
|
103
|
+
}
|
|
104
|
+
if (javascriptEvaluation === false &&
|
|
105
|
+
DISALLOWED_PROTOCOLS.has(parsed.protocol)) {
|
|
106
|
+
throw new Error(`Navigating to ${parsed.protocol} URLs is not allowed when JavaScript evaluation is disabled.`);
|
|
107
|
+
}
|
|
108
|
+
if (!isAllowedUrl(url, { categoryExtensions })) {
|
|
109
|
+
if (parsed.protocol === 'chrome-extension:') {
|
|
110
|
+
throw new Error(`Navigating to chrome-extension: URLs is not allowed without --categoryExtensions.`);
|
|
111
|
+
}
|
|
112
|
+
throw new Error(`Navigating to ${parsed.protocol} URLs is not allowed.`);
|
|
113
|
+
}
|
|
114
|
+
return parsed;
|
|
115
|
+
}
|
|
43
116
|
//# sourceMappingURL=url.js.map
|