chrome-devtools-mcp 1.10.1 → 1.12.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 +72 -20
- package/build/src/BrowserManager.js +155 -27
- package/build/src/McpContext.js +598 -200
- package/build/src/McpPage.js +513 -131
- package/build/src/McpResponse.js +291 -116
- package/build/src/McpWorker.js +66 -0
- package/build/src/SlimMcpResponse.js +5 -1
- package/build/src/TextSnapshot.js +40 -20
- package/build/src/ToolHandler.js +306 -147
- package/build/src/bin/chrome-devtools-mcp-main.js +13 -10
- package/build/src/bin/chrome-devtools-mcp.js +1 -1
- package/build/src/bin/chrome-devtools.js +34 -44
- package/build/src/collectors/PageCollector.js +13 -9
- package/build/src/collectors/ServiceWorkerCollector.js +20 -13
- package/build/src/collectors/constants.js +7 -0
- package/build/src/config/ConfigLocator.js +70 -0
- package/build/src/config/ConfigParser.js +159 -0
- package/build/src/config/ConfigWatcher.js +94 -0
- package/build/src/config/ServerConfig.js +251 -0
- package/build/src/config/browser-options.js +15 -17
- 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 +441 -58
- package/build/src/config/mcp-options.js +212 -264
- package/build/src/config/puppeteer-options.js +67 -0
- package/build/src/config/tool-options.js +60 -0
- package/build/src/daemon/client.js +84 -44
- package/build/src/daemon/daemon.js +59 -45
- package/build/src/daemon/utils.js +46 -12
- package/build/src/devtools/DevToolsCommentBridge.js +71 -11
- package/build/src/devtools/DevtoolsUtils.js +52 -15
- package/build/src/devtools/McpHostBindingAdapter.js +0 -1
- package/build/src/formatters/CommentFormatter.js +4 -13
- package/build/src/formatters/ConsoleFormatter.js +29 -2
- package/build/src/formatters/CssFormatter.js +3 -2
- package/build/src/formatters/HeapSnapshotFormatter.js +111 -57
- package/build/src/formatters/IssueFormatter.js +33 -32
- package/build/src/formatters/NetworkFormatter.js +62 -22
- package/build/src/formatters/RemoteObjectFormatter.js +231 -0
- package/build/src/formatters/SnapshotFormatter.js +44 -7
- package/build/src/index.js +285 -88
- package/build/src/processors/ChunkedTraceParser.js +11 -11
- package/build/src/processors/HeapSnapshotManager.js +455 -156
- 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 +399 -689
- package/build/src/third_party/bundled-packages.json +7 -8
- package/build/src/third_party/cli.js +9239 -0
- package/build/src/third_party/devtools-formatter-worker.js +4299 -2517
- package/build/src/third_party/devtools-heap-snapshot-worker.js +1298 -22
- package/build/src/third_party/index.js +132003 -146852
- 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 +54576 -52174
- package/build/src/tools/ToolDefinition.js +102 -12
- package/build/src/tools/comments.js +62 -4
- package/build/src/tools/console.js +6 -4
- package/build/src/tools/css.js +5 -2
- package/build/src/tools/emulation.js +22 -8
- package/build/src/tools/extensions.js +5 -0
- package/build/src/tools/input.js +29 -9
- package/build/src/tools/lighthouse.js +136 -12
- package/build/src/tools/memory.js +166 -59
- package/build/src/tools/network.js +5 -4
- package/build/src/tools/pages.js +52 -7
- package/build/src/tools/performance.js +28 -7
- package/build/src/tools/pwa.js +7 -0
- package/build/src/tools/screencast.js +74 -33
- package/build/src/tools/screenshot.js +52 -21
- package/build/src/tools/script.js +125 -40
- package/build/src/tools/slim/tools.js +27 -13
- package/build/src/tools/snapshot.js +13 -3
- package/build/src/tools/thirdPartyDeveloper.js +9 -5
- package/build/src/tools/tools.js +35 -22
- package/build/src/tools/webmcp.js +11 -6
- package/build/src/types.js +11 -0
- package/build/src/utils/TaskRunner.js +136 -0
- package/build/src/utils/WaitForHelper.js +15 -2
- package/build/src/utils/bytes.js +2 -12
- package/build/src/utils/check-for-updates.js +2 -2
- package/build/src/utils/disclaimers.js +27 -0
- package/build/src/utils/errorHandling.js +20 -0
- package/build/src/utils/keyboard.js +14 -2
- package/build/src/utils/logger.js +84 -5
- package/build/src/utils/resourceType.js +27 -0
- package/build/src/utils/text.js +44 -0
- package/build/src/utils/timeout.js +14 -0
- package/build/src/utils/url.js +140 -3
- package/build/src/version.js +1 -1
- package/package.json +16 -11
- package/skills/a11y-debugging/SKILL.md +7 -12
- 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
|
@@ -8,7 +8,7 @@ import { zod, DevTools } from '../third_party/index.js';
|
|
|
8
8
|
import { parseRawTraceBuffer, traceResultIsSuccess, } from '../processors/PerformanceTrace.js';
|
|
9
9
|
import { logger } from '../utils/logger.js';
|
|
10
10
|
import { ToolCategory } from './categories.js';
|
|
11
|
-
import { definePageTool } from './ToolDefinition.js';
|
|
11
|
+
import { definePageTool, } from './ToolDefinition.js';
|
|
12
12
|
const filePathSchema = zod
|
|
13
13
|
.string()
|
|
14
14
|
.optional()
|
|
@@ -29,9 +29,14 @@ export const startTrace = definePageTool(() => ({
|
|
|
29
29
|
.boolean()
|
|
30
30
|
.default(true)
|
|
31
31
|
.describe('Determines if the trace recording should be automatically stopped.'),
|
|
32
|
+
ignoreCache: zod
|
|
33
|
+
.boolean()
|
|
34
|
+
.optional()
|
|
35
|
+
.describe('Whether to bypass the HTTP cache when reloading the page, to measure a cold page load. Only applies if reload is true.'),
|
|
32
36
|
filePath: filePathSchema,
|
|
33
37
|
},
|
|
34
38
|
blockedByDialog: true,
|
|
39
|
+
concurrency: 'exclusive',
|
|
35
40
|
verifyFilesSchema: {
|
|
36
41
|
filePath: true,
|
|
37
42
|
},
|
|
@@ -40,8 +45,8 @@ export const startTrace = definePageTool(() => ({
|
|
|
40
45
|
response.appendResponseLine('Error: a performance trace is already running. Use performance_stop_trace to stop it. Only one trace can be running at any given time.');
|
|
41
46
|
return;
|
|
42
47
|
}
|
|
43
|
-
context.setIsRunningPerformanceTrace(true);
|
|
44
48
|
const page = request.page;
|
|
49
|
+
page.setIsRunningPerformanceTrace(true);
|
|
45
50
|
const pageUrlForTracing = page.pptrPage.url();
|
|
46
51
|
try {
|
|
47
52
|
if (request.params.reload) {
|
|
@@ -63,9 +68,22 @@ export const startTrace = definePageTool(() => ({
|
|
|
63
68
|
bufferSize: 1200 * 1000,
|
|
64
69
|
});
|
|
65
70
|
if (request.params.reload) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
71
|
+
const ignoreCache = request.params.ignoreCache ?? false;
|
|
72
|
+
if (ignoreCache) {
|
|
73
|
+
// Disable the HTTP cache so that the main document and all
|
|
74
|
+
// subresources are fetched from the network, like a hard reload.
|
|
75
|
+
await page.pptrPage.setCacheEnabled(false);
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
await page.pptrPage.goto(pageUrlForTracing, {
|
|
79
|
+
waitUntil: ['load'],
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
if (ignoreCache) {
|
|
84
|
+
await page.pptrPage.setCacheEnabled(true);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
69
87
|
}
|
|
70
88
|
if (request.params.autoStop) {
|
|
71
89
|
await new Promise(resolve => setTimeout(resolve, 5_000));
|
|
@@ -89,7 +107,7 @@ export const startTrace = definePageTool(() => ({
|
|
|
89
107
|
catch {
|
|
90
108
|
// Tracing may not have started yet; ignore.
|
|
91
109
|
}
|
|
92
|
-
|
|
110
|
+
page.setIsRunningPerformanceTrace(false);
|
|
93
111
|
}
|
|
94
112
|
throw error;
|
|
95
113
|
}
|
|
@@ -106,11 +124,13 @@ export const stopTrace = definePageTool(() => ({
|
|
|
106
124
|
filePath: filePathSchema,
|
|
107
125
|
},
|
|
108
126
|
blockedByDialog: true,
|
|
127
|
+
concurrency: 'exclusive',
|
|
109
128
|
verifyFilesSchema: {
|
|
110
129
|
filePath: true,
|
|
111
130
|
},
|
|
112
131
|
handler: async (request, response, context) => {
|
|
113
132
|
if (!context.isRunningPerformanceTrace()) {
|
|
133
|
+
response.appendResponseLine('Error: no active performance trace recording to stop.');
|
|
114
134
|
return;
|
|
115
135
|
}
|
|
116
136
|
const page = request.page;
|
|
@@ -133,6 +153,7 @@ export const analyzeInsight = definePageTool(() => ({
|
|
|
133
153
|
.describe('The name of the Insight you want more information on. For example: "DocumentLatency" or "LCPBreakdown"'),
|
|
134
154
|
},
|
|
135
155
|
blockedByDialog: false,
|
|
156
|
+
concurrency: 'exclusive',
|
|
136
157
|
verifyFilesSchema: {},
|
|
137
158
|
handler: async (request, response, context) => {
|
|
138
159
|
const lastRecording = context.recordedTraces().at(-1);
|
|
@@ -180,7 +201,7 @@ async function stopTracingAndAppendOutput(page, response, context, filePath) {
|
|
|
180
201
|
}
|
|
181
202
|
}
|
|
182
203
|
finally {
|
|
183
|
-
|
|
204
|
+
page.setIsRunningPerformanceTrace(false);
|
|
184
205
|
}
|
|
185
206
|
}
|
|
186
207
|
/** We tell CrUXManager to fetch data so it's available when DevTools.PerformanceTraceFormatter is invoked */
|
package/build/src/tools/pwa.js
CHANGED
|
@@ -38,12 +38,16 @@ export const installPwa = defineTool(() => ({
|
|
|
38
38
|
displayMode: displayModeSchema,
|
|
39
39
|
},
|
|
40
40
|
blockedByDialog: false,
|
|
41
|
+
concurrency: 'exclusive',
|
|
41
42
|
// We do not validate file paths for remote browser instances
|
|
42
43
|
// because they are on the remote host and not accessed by the MCP server.
|
|
43
44
|
verifyFilesSchema: {
|
|
44
45
|
installUrlOrBundleUrl: {
|
|
45
46
|
local: true,
|
|
46
47
|
remote: false,
|
|
48
|
+
// May be an http(s):// or file:// signed web bundle URL handed to the
|
|
49
|
+
// browser, so allow URLs through; a plain path is still validated.
|
|
50
|
+
allowUrl: true,
|
|
47
51
|
},
|
|
48
52
|
},
|
|
49
53
|
handler: async (request, response, context) => {
|
|
@@ -71,6 +75,7 @@ export const uninstallPwa = defineTool(() => ({
|
|
|
71
75
|
manifestId: manifestIdSchema,
|
|
72
76
|
},
|
|
73
77
|
blockedByDialog: false,
|
|
78
|
+
concurrency: 'exclusive',
|
|
74
79
|
verifyFilesSchema: {},
|
|
75
80
|
handler: async (request, response, context) => {
|
|
76
81
|
const { manifestId } = request.params;
|
|
@@ -96,6 +101,7 @@ export const launchPwa = defineTool(() => ({
|
|
|
96
101
|
.describe('Optional URL within the app to open instead of the default start URL.'),
|
|
97
102
|
},
|
|
98
103
|
blockedByDialog: false,
|
|
104
|
+
concurrency: 'exclusive',
|
|
99
105
|
verifyFilesSchema: {},
|
|
100
106
|
handler: async (request, response, context) => {
|
|
101
107
|
const { manifestId, url } = request.params;
|
|
@@ -116,6 +122,7 @@ export const getOsAppState = defineTool(() => ({
|
|
|
116
122
|
manifestId: manifestIdSchema,
|
|
117
123
|
},
|
|
118
124
|
blockedByDialog: false,
|
|
125
|
+
concurrency: 'exclusive',
|
|
119
126
|
verifyFilesSchema: {},
|
|
120
127
|
handler: async (request, response, context) => {
|
|
121
128
|
const { manifestId } = request.params;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import fs from 'node:fs/promises';
|
|
7
7
|
import os from 'node:os';
|
|
8
8
|
import path from 'node:path';
|
|
9
|
-
import { zod } from '../third_party/index.js';
|
|
9
|
+
import { zod, } from '../third_party/index.js';
|
|
10
10
|
import { ToolCategory } from './categories.js';
|
|
11
11
|
import { definePageTool } from './ToolDefinition.js';
|
|
12
12
|
async function generateTempFilePath() {
|
|
@@ -14,56 +14,93 @@ async function generateTempFilePath() {
|
|
|
14
14
|
return path.join(dir, `screencast.mp4`);
|
|
15
15
|
}
|
|
16
16
|
const supportedExtensions = ['.webm', '.mp4'];
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the output extension for the legacy ffmpeg-based recorder.
|
|
19
|
+
*
|
|
20
|
+
* Matches the extension case-insensitively so e.g. `.WEBM` is recognized as
|
|
21
|
+
* WebM. An explicitly requested but unsupported extension is rejected rather
|
|
22
|
+
* than being silently rewritten to `.mp4` (which would change both the format
|
|
23
|
+
* and the output path from what was requested). A missing extension falls
|
|
24
|
+
* back to `.mp4`. The matched extension is normalized to lower case.
|
|
25
|
+
*/
|
|
26
|
+
function getLegacyExtension(filePath) {
|
|
27
|
+
const requestedExtension = path.extname(filePath);
|
|
28
|
+
const matchedExtension = supportedExtensions.find(supportedExtension => supportedExtension === requestedExtension.toLowerCase());
|
|
29
|
+
if (!matchedExtension && requestedExtension !== '') {
|
|
30
|
+
throw new Error(`Unsupported screencast file extension "${requestedExtension}". ` +
|
|
31
|
+
`Supported formats: ${supportedExtensions.join(', ')} (case-insensitive).`);
|
|
32
|
+
}
|
|
33
|
+
return matchedExtension ?? '.mp4';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the size of the page viewport in device pixels.
|
|
37
|
+
*/
|
|
38
|
+
async function getPagePixelSize(pptrPage) {
|
|
39
|
+
const { width, height, dpr } = await pptrPage.evaluate(() => ({
|
|
40
|
+
width: window.innerWidth,
|
|
41
|
+
height: window.innerHeight,
|
|
42
|
+
dpr: window.devicePixelRatio,
|
|
43
|
+
}));
|
|
44
|
+
return {
|
|
45
|
+
maxWidth: Math.max(1, Math.round(width * dpr)),
|
|
46
|
+
maxHeight: Math.max(1, Math.round(height * dpr)),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
17
49
|
export const startScreencast = definePageTool((args) => ({
|
|
18
50
|
name: 'screencast_start',
|
|
19
|
-
description: `Starts recording a screencast (video) of the target page
|
|
51
|
+
description: `Starts recording a screencast (video) of the target page.`,
|
|
20
52
|
annotations: {
|
|
21
53
|
category: ToolCategory.DEBUGGING,
|
|
22
54
|
readOnlyHint: false,
|
|
23
|
-
conditions: ['experimentalScreencast'],
|
|
24
55
|
},
|
|
25
56
|
schema: {
|
|
26
57
|
filePath: zod
|
|
27
58
|
.string()
|
|
28
59
|
.optional()
|
|
29
|
-
.describe(
|
|
60
|
+
.describe(args?.experimentalScreencast
|
|
61
|
+
? `Output file path (${supportedExtensions.join(',')} are supported). Uses mkdtemp to generate a unique path if not provided.`
|
|
62
|
+
: 'Output file path (.mp4). Uses mkdtemp to generate a unique path if not provided.'),
|
|
30
63
|
},
|
|
31
64
|
blockedByDialog: false,
|
|
65
|
+
concurrency: 'exclusive',
|
|
32
66
|
verifyFilesSchema: {
|
|
33
67
|
filePath: true,
|
|
34
68
|
},
|
|
35
69
|
handler: async (request, response, context) => {
|
|
36
|
-
|
|
70
|
+
const page = request.page;
|
|
71
|
+
if (page.getScreenRecorder() !== null) {
|
|
37
72
|
response.appendResponseLine('Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.');
|
|
38
73
|
return;
|
|
39
74
|
}
|
|
40
75
|
const requestedFilePath = request.params.filePath;
|
|
41
76
|
const filePath = requestedFilePath ?? (await generateTempFilePath());
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// lower case.
|
|
48
|
-
const requestedExtension = path.extname(filePath);
|
|
49
|
-
const matchedExtension = supportedExtensions.find(supportedExtension => supportedExtension === requestedExtension.toLowerCase());
|
|
50
|
-
if (!matchedExtension && requestedExtension !== '') {
|
|
51
|
-
throw new Error(`Unsupported screencast file extension "${requestedExtension}". ` +
|
|
52
|
-
`Supported formats: ${supportedExtensions.join(', ')} (case-insensitive).`);
|
|
53
|
-
}
|
|
54
|
-
const enforcedExtension = matchedExtension ?? '.mp4';
|
|
55
|
-
const format = (matchedExtension?.substring(1) ??
|
|
56
|
-
'mp4');
|
|
57
|
-
const resolvedPath = await context.ensureExtension(filePath, enforcedExtension);
|
|
58
|
-
const page = request.page;
|
|
77
|
+
// The built-in Chrome recorder (page.record) always produces mp4. The
|
|
78
|
+
// legacy ffmpeg recorder (page.screencast) also supports WebM.
|
|
79
|
+
const useLegacy = Boolean(args?.experimentalScreencast);
|
|
80
|
+
const extension = useLegacy ? getLegacyExtension(filePath) : '.mp4';
|
|
81
|
+
const resolvedPath = await context.ensureExtension(filePath, extension);
|
|
59
82
|
let recorder;
|
|
60
83
|
try {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
84
|
+
if (useLegacy) {
|
|
85
|
+
const format = extension === '.webm' ? 'webm' : 'mp4';
|
|
86
|
+
recorder = await page.pptrPage.screencast({
|
|
87
|
+
path: resolvedPath,
|
|
88
|
+
format,
|
|
89
|
+
ffmpegPath: args?.experimentalFfmpegPath,
|
|
90
|
+
fps: args?.screencastFps,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
// Without explicit limits Chrome downscales frames to a small default
|
|
95
|
+
// size. Record at the actual device pixel size of the page instead.
|
|
96
|
+
const { maxWidth, maxHeight } = await getPagePixelSize(page.pptrPage);
|
|
97
|
+
recorder = await page.pptrPage.record({
|
|
98
|
+
path: resolvedPath,
|
|
99
|
+
fps: args?.screencastFps,
|
|
100
|
+
maxWidth,
|
|
101
|
+
maxHeight,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
67
104
|
}
|
|
68
105
|
catch (err) {
|
|
69
106
|
// If we generated a temporary directory for this recording, remove it so
|
|
@@ -84,9 +121,12 @@ export const startScreencast = definePageTool((args) => ({
|
|
|
84
121
|
throw new Error('ffmpeg is required for screencast recording but was not found. ' +
|
|
85
122
|
'Install ffmpeg (https://ffmpeg.org/) and ensure it is available in your PATH.');
|
|
86
123
|
}
|
|
124
|
+
if (message.includes('Page.startScreenRecording')) {
|
|
125
|
+
throw new Error('The connected Chrome does not support screen recording. Update Chrome to a newer version.');
|
|
126
|
+
}
|
|
87
127
|
throw err;
|
|
88
128
|
}
|
|
89
|
-
|
|
129
|
+
page.setScreenRecorder({ recorder, filePath: resolvedPath });
|
|
90
130
|
response.appendResponseLine(`Screencast recording started. The recording will be saved to ${resolvedPath}. Use ${stopScreencast(args).name} to stop recording.`);
|
|
91
131
|
},
|
|
92
132
|
}));
|
|
@@ -96,13 +136,14 @@ export const stopScreencast = definePageTool(() => ({
|
|
|
96
136
|
annotations: {
|
|
97
137
|
category: ToolCategory.DEBUGGING,
|
|
98
138
|
readOnlyHint: false,
|
|
99
|
-
conditions: ['experimentalScreencast'],
|
|
100
139
|
},
|
|
101
140
|
schema: {},
|
|
102
141
|
blockedByDialog: false,
|
|
142
|
+
concurrency: 'exclusive',
|
|
103
143
|
verifyFilesSchema: {},
|
|
104
|
-
handler: async (
|
|
105
|
-
const
|
|
144
|
+
handler: async (request, response) => {
|
|
145
|
+
const page = request.page;
|
|
146
|
+
const data = page.getScreenRecorder();
|
|
106
147
|
if (!data) {
|
|
107
148
|
response.appendResponseLine('Error: no active screencast recording to stop.');
|
|
108
149
|
return;
|
|
@@ -112,7 +153,7 @@ export const stopScreencast = definePageTool(() => ({
|
|
|
112
153
|
response.appendResponseLine(`The screencast recording has been stopped and saved to ${data.filePath}.`);
|
|
113
154
|
}
|
|
114
155
|
finally {
|
|
115
|
-
|
|
156
|
+
page.setScreenRecorder(null);
|
|
116
157
|
}
|
|
117
158
|
},
|
|
118
159
|
}));
|
|
@@ -55,19 +55,31 @@ 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 { zod } from '../third_party/index.js';
|
|
58
|
+
import { zod, } from '../third_party/index.js';
|
|
59
59
|
import { ToolCategory } from './categories.js';
|
|
60
60
|
import { definePageTool } from './ToolDefinition.js';
|
|
61
|
+
const MAX_CHROMIUM_SCREENSHOT_DIMENSION = 16384;
|
|
61
62
|
async function getSourceBox(page, element, fullPage) {
|
|
62
63
|
if (element) {
|
|
63
64
|
const viewport = page.viewport();
|
|
64
|
-
const [box,
|
|
65
|
+
const [box, metrics] = await Promise.all([
|
|
65
66
|
element.boundingBox(),
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
:
|
|
67
|
+
page.evaluate(() => ({
|
|
68
|
+
x: window.scrollX,
|
|
69
|
+
y: window.scrollY,
|
|
70
|
+
devicePixelRatio: window.devicePixelRatio,
|
|
71
|
+
})),
|
|
69
72
|
]);
|
|
70
|
-
return box
|
|
73
|
+
return box
|
|
74
|
+
? {
|
|
75
|
+
...box,
|
|
76
|
+
x: box.x + metrics.x,
|
|
77
|
+
y: box.y + metrics.y,
|
|
78
|
+
devicePixelRatio: viewport
|
|
79
|
+
? (viewport.deviceScaleFactor ?? 1)
|
|
80
|
+
: metrics.devicePixelRatio,
|
|
81
|
+
}
|
|
82
|
+
: undefined;
|
|
71
83
|
}
|
|
72
84
|
if (fullPage) {
|
|
73
85
|
const dims = await page.evaluate(() => ({
|
|
@@ -87,10 +99,19 @@ async function getSourceBox(page, element, fullPage) {
|
|
|
87
99
|
};
|
|
88
100
|
}
|
|
89
101
|
const viewport = page.viewport();
|
|
102
|
+
// Clips use page coordinates. Read the scroll offset alongside the native
|
|
103
|
+
// dimensions used when no viewport is emulated.
|
|
104
|
+
const dims = await page.evaluate(() => ({
|
|
105
|
+
x: window.scrollX,
|
|
106
|
+
y: window.scrollY,
|
|
107
|
+
width: window.innerWidth,
|
|
108
|
+
height: window.innerHeight,
|
|
109
|
+
devicePixelRatio: window.devicePixelRatio,
|
|
110
|
+
}));
|
|
90
111
|
if (viewport) {
|
|
91
112
|
return {
|
|
92
|
-
x:
|
|
93
|
-
y:
|
|
113
|
+
x: dims.x,
|
|
114
|
+
y: dims.y,
|
|
94
115
|
width: viewport.width,
|
|
95
116
|
height: viewport.height,
|
|
96
117
|
devicePixelRatio: viewport.deviceScaleFactor ?? 1,
|
|
@@ -99,17 +120,12 @@ async function getSourceBox(page, element, fullPage) {
|
|
|
99
120
|
// The browser is launched and connected with `defaultViewport: null`, so
|
|
100
121
|
// `page.viewport()` stays null until something emulates one. Fall back to the
|
|
101
122
|
// window's own dimensions, which is the area a viewport screenshot captures.
|
|
102
|
-
const dims = await page.evaluate(() => ({
|
|
103
|
-
width: window.innerWidth,
|
|
104
|
-
height: window.innerHeight,
|
|
105
|
-
devicePixelRatio: window.devicePixelRatio,
|
|
106
|
-
}));
|
|
107
123
|
if (dims.width <= 0 || dims.height <= 0) {
|
|
108
124
|
return undefined;
|
|
109
125
|
}
|
|
110
126
|
return {
|
|
111
|
-
x:
|
|
112
|
-
y:
|
|
127
|
+
x: dims.x,
|
|
128
|
+
y: dims.y,
|
|
113
129
|
width: dims.width,
|
|
114
130
|
height: dims.height,
|
|
115
131
|
devicePixelRatio: dims.devicePixelRatio,
|
|
@@ -175,6 +191,7 @@ export const screenshot = definePageTool((args) => {
|
|
|
175
191
|
.describe('The absolute path, or a path relative to the current working directory, to save the screenshot to instead of attaching it to the response.'),
|
|
176
192
|
},
|
|
177
193
|
blockedByDialog: true,
|
|
194
|
+
concurrency: 'page',
|
|
178
195
|
verifyFilesSchema: {
|
|
179
196
|
filePath: true,
|
|
180
197
|
},
|
|
@@ -202,12 +219,17 @@ export const screenshot = definePageTool((args) => {
|
|
|
202
219
|
// The smaller scale factor wins so both bounds are respected while
|
|
203
220
|
// preserving aspect ratio.
|
|
204
221
|
let clip;
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
222
|
+
const shouldInspectBox = fullPage ||
|
|
223
|
+
element !== undefined ||
|
|
224
|
+
screenshotMaxWidth !== undefined ||
|
|
225
|
+
screenshotMaxHeight !== undefined;
|
|
226
|
+
const box = shouldInspectBox
|
|
227
|
+
? await getSourceBox(page, element, fullPage)
|
|
228
|
+
: undefined;
|
|
229
|
+
if (box &&
|
|
230
|
+
(screenshotMaxWidth !== undefined ||
|
|
231
|
+
screenshotMaxHeight !== undefined)) {
|
|
232
|
+
clip = computeDownscaleClip(box, screenshotMaxWidth, screenshotMaxHeight);
|
|
211
233
|
}
|
|
212
234
|
if (clip) {
|
|
213
235
|
// page.screenshot with clip lets the CDP scale param downscale the
|
|
@@ -263,6 +285,15 @@ export const screenshot = definePageTool((args) => {
|
|
|
263
285
|
data: Buffer.from(screenshot).toString('base64'),
|
|
264
286
|
});
|
|
265
287
|
}
|
|
288
|
+
if (box) {
|
|
289
|
+
const effectiveScale = clip?.scale ?? 1;
|
|
290
|
+
const effectiveWidth = box.width * box.devicePixelRatio * effectiveScale;
|
|
291
|
+
const effectiveHeight = box.height * box.devicePixelRatio * effectiveScale;
|
|
292
|
+
if (effectiveWidth > MAX_CHROMIUM_SCREENSHOT_DIMENSION ||
|
|
293
|
+
effectiveHeight > MAX_CHROMIUM_SCREENSHOT_DIMENSION) {
|
|
294
|
+
response.appendResponseLine(`Warning: Screenshot dimensions exceed the maximum capturable dimension of ${MAX_CHROMIUM_SCREENSHOT_DIMENSION}px. Content beyond 16384px may repeat or be corrupted due to browser texture limits. Set --screenshotMaxHeight or --screenshotMaxWidth to downscale.`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
266
297
|
}
|
|
267
298
|
catch (e_1) {
|
|
268
299
|
env_1.error = e_1;
|