@mcp-b/chrome-devtools-mcp 0.12.0-beta.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/LICENSE +202 -0
- package/README.md +554 -0
- package/build/src/DevToolsConnectionAdapter.js +69 -0
- package/build/src/DevtoolsUtils.js +206 -0
- package/build/src/McpContext.js +499 -0
- package/build/src/McpResponse.js +396 -0
- package/build/src/Mutex.js +37 -0
- package/build/src/PageCollector.js +283 -0
- package/build/src/WaitForHelper.js +139 -0
- package/build/src/browser.js +134 -0
- package/build/src/cli.js +213 -0
- package/build/src/formatters/consoleFormatter.js +121 -0
- package/build/src/formatters/networkFormatter.js +77 -0
- package/build/src/formatters/snapshotFormatter.js +73 -0
- package/build/src/index.js +21 -0
- package/build/src/issue-descriptions.js +39 -0
- package/build/src/logger.js +27 -0
- package/build/src/main.js +130 -0
- package/build/src/polyfill.js +7 -0
- package/build/src/third_party/index.js +16 -0
- package/build/src/tools/ToolDefinition.js +20 -0
- package/build/src/tools/categories.js +24 -0
- package/build/src/tools/console.js +85 -0
- package/build/src/tools/emulation.js +87 -0
- package/build/src/tools/input.js +268 -0
- package/build/src/tools/network.js +106 -0
- package/build/src/tools/pages.js +237 -0
- package/build/src/tools/performance.js +147 -0
- package/build/src/tools/screenshot.js +84 -0
- package/build/src/tools/script.js +71 -0
- package/build/src/tools/snapshot.js +52 -0
- package/build/src/tools/tools.js +31 -0
- package/build/src/tools/webmcp.js +233 -0
- package/build/src/trace-processing/parse.js +84 -0
- package/build/src/transports/WebMCPBridgeScript.js +196 -0
- package/build/src/transports/WebMCPClientTransport.js +276 -0
- package/build/src/transports/index.js +7 -0
- package/build/src/utils/keyboard.js +296 -0
- package/build/src/utils/pagination.js +49 -0
- package/build/src/utils/types.js +6 -0
- package/package.json +87 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod } from '../third_party/index.js';
|
|
7
|
+
import { ToolCategory } from './categories.js';
|
|
8
|
+
import { defineTool } from './ToolDefinition.js';
|
|
9
|
+
const FILTERABLE_RESOURCE_TYPES = [
|
|
10
|
+
'document',
|
|
11
|
+
'stylesheet',
|
|
12
|
+
'image',
|
|
13
|
+
'media',
|
|
14
|
+
'font',
|
|
15
|
+
'script',
|
|
16
|
+
'texttrack',
|
|
17
|
+
'xhr',
|
|
18
|
+
'fetch',
|
|
19
|
+
'prefetch',
|
|
20
|
+
'eventsource',
|
|
21
|
+
'websocket',
|
|
22
|
+
'manifest',
|
|
23
|
+
'signedexchange',
|
|
24
|
+
'ping',
|
|
25
|
+
'cspviolationreport',
|
|
26
|
+
'preflight',
|
|
27
|
+
'fedcm',
|
|
28
|
+
'other',
|
|
29
|
+
];
|
|
30
|
+
export const listNetworkRequests = defineTool({
|
|
31
|
+
name: 'list_network_requests',
|
|
32
|
+
description: `List all requests for the currently selected page since the last navigation.`,
|
|
33
|
+
annotations: {
|
|
34
|
+
category: ToolCategory.NETWORK,
|
|
35
|
+
readOnlyHint: true,
|
|
36
|
+
},
|
|
37
|
+
schema: {
|
|
38
|
+
pageSize: zod
|
|
39
|
+
.number()
|
|
40
|
+
.int()
|
|
41
|
+
.positive()
|
|
42
|
+
.optional()
|
|
43
|
+
.describe('Maximum number of requests to return. When omitted, returns all requests.'),
|
|
44
|
+
pageIdx: zod
|
|
45
|
+
.number()
|
|
46
|
+
.int()
|
|
47
|
+
.min(0)
|
|
48
|
+
.optional()
|
|
49
|
+
.describe('Page number to return (0-based). When omitted, returns the first page.'),
|
|
50
|
+
resourceTypes: zod
|
|
51
|
+
.array(zod.enum(FILTERABLE_RESOURCE_TYPES))
|
|
52
|
+
.optional()
|
|
53
|
+
.describe('Filter requests to only return requests of the specified resource types. When omitted or empty, returns all requests.'),
|
|
54
|
+
includePreservedRequests: zod
|
|
55
|
+
.boolean()
|
|
56
|
+
.default(false)
|
|
57
|
+
.optional()
|
|
58
|
+
.describe('Set to true to return the preserved requests over the last 3 navigations.'),
|
|
59
|
+
},
|
|
60
|
+
handler: async (request, response, context) => {
|
|
61
|
+
const data = await context.getDevToolsData();
|
|
62
|
+
response.attachDevToolsData(data);
|
|
63
|
+
const reqid = data?.cdpRequestId
|
|
64
|
+
? context.resolveCdpRequestId(data.cdpRequestId)
|
|
65
|
+
: undefined;
|
|
66
|
+
response.setIncludeNetworkRequests(true, {
|
|
67
|
+
pageSize: request.params.pageSize,
|
|
68
|
+
pageIdx: request.params.pageIdx,
|
|
69
|
+
resourceTypes: request.params.resourceTypes,
|
|
70
|
+
includePreservedRequests: request.params.includePreservedRequests,
|
|
71
|
+
networkRequestIdInDevToolsUI: reqid,
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
export const getNetworkRequest = defineTool({
|
|
76
|
+
name: 'get_network_request',
|
|
77
|
+
description: `Gets a network request by an optional reqid, if omitted returns the currently selected request in the DevTools Network panel.`,
|
|
78
|
+
annotations: {
|
|
79
|
+
category: ToolCategory.NETWORK,
|
|
80
|
+
readOnlyHint: true,
|
|
81
|
+
},
|
|
82
|
+
schema: {
|
|
83
|
+
reqid: zod
|
|
84
|
+
.number()
|
|
85
|
+
.optional()
|
|
86
|
+
.describe('The reqid of the network request. If omitted returns the currently selected request in the DevTools Network panel.'),
|
|
87
|
+
},
|
|
88
|
+
handler: async (request, response, context) => {
|
|
89
|
+
if (request.params.reqid) {
|
|
90
|
+
response.attachNetworkRequest(request.params.reqid);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const data = await context.getDevToolsData();
|
|
94
|
+
response.attachDevToolsData(data);
|
|
95
|
+
const reqid = data?.cdpRequestId
|
|
96
|
+
? context.resolveCdpRequestId(data.cdpRequestId)
|
|
97
|
+
: undefined;
|
|
98
|
+
if (reqid) {
|
|
99
|
+
response.attachNetworkRequest(reqid);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
response.appendResponseLine(`Nothing is currently selected in the DevTools Network panel.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
});
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { logger } from '../logger.js';
|
|
7
|
+
import { zod } from '../third_party/index.js';
|
|
8
|
+
import { ToolCategory } from './categories.js';
|
|
9
|
+
import { CLOSE_PAGE_ERROR, defineTool, timeoutSchema } from './ToolDefinition.js';
|
|
10
|
+
export const listPages = defineTool({
|
|
11
|
+
name: 'list_pages',
|
|
12
|
+
description: `Get a list of pages open in the browser.`,
|
|
13
|
+
annotations: {
|
|
14
|
+
category: ToolCategory.NAVIGATION,
|
|
15
|
+
readOnlyHint: true,
|
|
16
|
+
},
|
|
17
|
+
schema: {},
|
|
18
|
+
handler: async (_request, response) => {
|
|
19
|
+
response.setIncludePages(true);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
export const selectPage = defineTool({
|
|
23
|
+
name: 'select_page',
|
|
24
|
+
description: `Select a page as a context for future tool calls.`,
|
|
25
|
+
annotations: {
|
|
26
|
+
category: ToolCategory.NAVIGATION,
|
|
27
|
+
readOnlyHint: true,
|
|
28
|
+
},
|
|
29
|
+
schema: {
|
|
30
|
+
pageIdx: zod
|
|
31
|
+
.number()
|
|
32
|
+
.describe('The index of the page to select. Call list_pages to list pages.'),
|
|
33
|
+
},
|
|
34
|
+
handler: async (request, response, context) => {
|
|
35
|
+
const page = context.getPageByIdx(request.params.pageIdx);
|
|
36
|
+
await page.bringToFront();
|
|
37
|
+
context.selectPage(page);
|
|
38
|
+
response.setIncludePages(true);
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
export const closePage = defineTool({
|
|
42
|
+
name: 'close_page',
|
|
43
|
+
description: `Closes the page by its index. The last open page cannot be closed.`,
|
|
44
|
+
annotations: {
|
|
45
|
+
category: ToolCategory.NAVIGATION,
|
|
46
|
+
readOnlyHint: false,
|
|
47
|
+
},
|
|
48
|
+
schema: {
|
|
49
|
+
pageIdx: zod
|
|
50
|
+
.number()
|
|
51
|
+
.describe('The index of the page to close. Call list_pages to list pages.'),
|
|
52
|
+
},
|
|
53
|
+
handler: async (request, response, context) => {
|
|
54
|
+
try {
|
|
55
|
+
await context.closePage(request.params.pageIdx);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
if (err.message === CLOSE_PAGE_ERROR) {
|
|
59
|
+
response.appendResponseLine(err.message);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
response.setIncludePages(true);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
export const newPage = defineTool({
|
|
69
|
+
name: 'new_page',
|
|
70
|
+
description: `Creates a new page`,
|
|
71
|
+
annotations: {
|
|
72
|
+
category: ToolCategory.NAVIGATION,
|
|
73
|
+
readOnlyHint: false,
|
|
74
|
+
},
|
|
75
|
+
schema: {
|
|
76
|
+
url: zod.string().describe('URL to load in a new page.'),
|
|
77
|
+
...timeoutSchema,
|
|
78
|
+
},
|
|
79
|
+
handler: async (request, response, context) => {
|
|
80
|
+
const page = await context.newPage();
|
|
81
|
+
await context.waitForEventsAfterAction(async () => {
|
|
82
|
+
await page.goto(request.params.url, {
|
|
83
|
+
timeout: request.params.timeout,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
response.setIncludePages(true);
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
export const navigatePage = defineTool({
|
|
90
|
+
name: 'navigate_page',
|
|
91
|
+
description: `Navigates the currently selected page to a URL.`,
|
|
92
|
+
annotations: {
|
|
93
|
+
category: ToolCategory.NAVIGATION,
|
|
94
|
+
readOnlyHint: false,
|
|
95
|
+
},
|
|
96
|
+
schema: {
|
|
97
|
+
type: zod
|
|
98
|
+
.enum(['url', 'back', 'forward', 'reload'])
|
|
99
|
+
.optional()
|
|
100
|
+
.describe('Navigate the page by URL, back or forward in history, or reload.'),
|
|
101
|
+
url: zod.string().optional().describe('Target URL (only type=url)'),
|
|
102
|
+
ignoreCache: zod
|
|
103
|
+
.boolean()
|
|
104
|
+
.optional()
|
|
105
|
+
.describe('Whether to ignore cache on reload.'),
|
|
106
|
+
...timeoutSchema,
|
|
107
|
+
},
|
|
108
|
+
handler: async (request, response, context) => {
|
|
109
|
+
const page = context.getSelectedPage();
|
|
110
|
+
const options = {
|
|
111
|
+
timeout: request.params.timeout,
|
|
112
|
+
};
|
|
113
|
+
if (!request.params.type && !request.params.url) {
|
|
114
|
+
throw new Error('Either URL or a type is required.');
|
|
115
|
+
}
|
|
116
|
+
if (!request.params.type) {
|
|
117
|
+
request.params.type = 'url';
|
|
118
|
+
}
|
|
119
|
+
await context.waitForEventsAfterAction(async () => {
|
|
120
|
+
switch (request.params.type) {
|
|
121
|
+
case 'url':
|
|
122
|
+
if (!request.params.url) {
|
|
123
|
+
throw new Error('A URL is required for navigation of type=url.');
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
await page.goto(request.params.url, options);
|
|
127
|
+
response.appendResponseLine(`Successfully navigated to ${request.params.url}.`);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
response.appendResponseLine(`Unable to navigate in the selected page: ${error.message}.`);
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
case 'back':
|
|
134
|
+
try {
|
|
135
|
+
await page.goBack(options);
|
|
136
|
+
response.appendResponseLine(`Successfully navigated back to ${page.url()}.`);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
response.appendResponseLine(`Unable to navigate back in the selected page: ${error.message}.`);
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
case 'forward':
|
|
143
|
+
try {
|
|
144
|
+
await page.goForward(options);
|
|
145
|
+
response.appendResponseLine(`Successfully navigated forward to ${page.url()}.`);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
response.appendResponseLine(`Unable to navigate forward in the selected page: ${error.message}.`);
|
|
149
|
+
}
|
|
150
|
+
break;
|
|
151
|
+
case 'reload':
|
|
152
|
+
try {
|
|
153
|
+
await page.reload({
|
|
154
|
+
...options,
|
|
155
|
+
ignoreCache: request.params.ignoreCache,
|
|
156
|
+
});
|
|
157
|
+
response.appendResponseLine(`Successfully reloaded the page.`);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
response.appendResponseLine(`Unable to reload the selected page: ${error.message}.`);
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
response.setIncludePages(true);
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
export const resizePage = defineTool({
|
|
169
|
+
name: 'resize_page',
|
|
170
|
+
description: `Resizes the selected page's window so that the page has specified dimension`,
|
|
171
|
+
annotations: {
|
|
172
|
+
category: ToolCategory.EMULATION,
|
|
173
|
+
readOnlyHint: false,
|
|
174
|
+
},
|
|
175
|
+
schema: {
|
|
176
|
+
width: zod.number().describe('Page width'),
|
|
177
|
+
height: zod.number().describe('Page height'),
|
|
178
|
+
},
|
|
179
|
+
handler: async (request, response, context) => {
|
|
180
|
+
const page = context.getSelectedPage();
|
|
181
|
+
// @ts-expect-error internal API for now.
|
|
182
|
+
await page.resize({
|
|
183
|
+
contentWidth: request.params.width,
|
|
184
|
+
contentHeight: request.params.height,
|
|
185
|
+
});
|
|
186
|
+
response.setIncludePages(true);
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
export const handleDialog = defineTool({
|
|
190
|
+
name: 'handle_dialog',
|
|
191
|
+
description: `If a browser dialog was opened, use this command to handle it`,
|
|
192
|
+
annotations: {
|
|
193
|
+
category: ToolCategory.INPUT,
|
|
194
|
+
readOnlyHint: false,
|
|
195
|
+
},
|
|
196
|
+
schema: {
|
|
197
|
+
action: zod
|
|
198
|
+
.enum(['accept', 'dismiss'])
|
|
199
|
+
.describe('Whether to dismiss or accept the dialog'),
|
|
200
|
+
promptText: zod
|
|
201
|
+
.string()
|
|
202
|
+
.optional()
|
|
203
|
+
.describe('Optional prompt text to enter into the dialog.'),
|
|
204
|
+
},
|
|
205
|
+
handler: async (request, response, context) => {
|
|
206
|
+
const dialog = context.getDialog();
|
|
207
|
+
if (!dialog) {
|
|
208
|
+
throw new Error('No open dialog found');
|
|
209
|
+
}
|
|
210
|
+
switch (request.params.action) {
|
|
211
|
+
case 'accept': {
|
|
212
|
+
try {
|
|
213
|
+
await dialog.accept(request.params.promptText);
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
// Likely already handled by the user outside of MCP.
|
|
217
|
+
logger(err);
|
|
218
|
+
}
|
|
219
|
+
response.appendResponseLine('Successfully accepted the dialog');
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
case 'dismiss': {
|
|
223
|
+
try {
|
|
224
|
+
await dialog.dismiss();
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
// Likely already handled.
|
|
228
|
+
logger(err);
|
|
229
|
+
}
|
|
230
|
+
response.appendResponseLine('Successfully dismissed the dialog');
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
context.clearDialog();
|
|
235
|
+
response.setIncludePages(true);
|
|
236
|
+
},
|
|
237
|
+
});
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { logger } from '../logger.js';
|
|
7
|
+
import { zod } from '../third_party/index.js';
|
|
8
|
+
import { getInsightOutput, getTraceSummary, parseRawTraceBuffer, traceResultIsSuccess, } from '../trace-processing/parse.js';
|
|
9
|
+
import { ToolCategory } from './categories.js';
|
|
10
|
+
import { defineTool } from './ToolDefinition.js';
|
|
11
|
+
export const startTrace = defineTool({
|
|
12
|
+
name: 'performance_start_trace',
|
|
13
|
+
description: 'Starts a performance trace recording on the selected page. This can be used to look for performance problems and insights to improve the performance of the page. It will also report Core Web Vital (CWV) scores for the page.',
|
|
14
|
+
annotations: {
|
|
15
|
+
category: ToolCategory.PERFORMANCE,
|
|
16
|
+
readOnlyHint: true,
|
|
17
|
+
},
|
|
18
|
+
schema: {
|
|
19
|
+
reload: zod
|
|
20
|
+
.boolean()
|
|
21
|
+
.describe('Determines if, once tracing has started, the page should be automatically reloaded.'),
|
|
22
|
+
autoStop: zod
|
|
23
|
+
.boolean()
|
|
24
|
+
.describe('Determines if the trace recording should be automatically stopped.'),
|
|
25
|
+
},
|
|
26
|
+
handler: async (request, response, context) => {
|
|
27
|
+
if (context.isRunningPerformanceTrace()) {
|
|
28
|
+
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.');
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
context.setIsRunningPerformanceTrace(true);
|
|
32
|
+
const page = context.getSelectedPage();
|
|
33
|
+
const pageUrlForTracing = page.url();
|
|
34
|
+
if (request.params.reload) {
|
|
35
|
+
// Before starting the recording, navigate to about:blank to clear out any state.
|
|
36
|
+
await page.goto('about:blank', {
|
|
37
|
+
waitUntil: ['networkidle0'],
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
// Keep in sync with the categories arrays in:
|
|
41
|
+
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/timeline/TimelineController.ts
|
|
42
|
+
// https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/gather/gatherers/trace.js
|
|
43
|
+
const categories = [
|
|
44
|
+
'-*',
|
|
45
|
+
'blink.console',
|
|
46
|
+
'blink.user_timing',
|
|
47
|
+
'devtools.timeline',
|
|
48
|
+
'disabled-by-default-devtools.screenshot',
|
|
49
|
+
'disabled-by-default-devtools.timeline',
|
|
50
|
+
'disabled-by-default-devtools.timeline.invalidationTracking',
|
|
51
|
+
'disabled-by-default-devtools.timeline.frame',
|
|
52
|
+
'disabled-by-default-devtools.timeline.stack',
|
|
53
|
+
'disabled-by-default-v8.cpu_profiler',
|
|
54
|
+
'disabled-by-default-v8.cpu_profiler.hires',
|
|
55
|
+
'latencyInfo',
|
|
56
|
+
'loading',
|
|
57
|
+
'disabled-by-default-lighthouse',
|
|
58
|
+
'v8.execute',
|
|
59
|
+
'v8',
|
|
60
|
+
];
|
|
61
|
+
await page.tracing.start({
|
|
62
|
+
categories,
|
|
63
|
+
});
|
|
64
|
+
if (request.params.reload) {
|
|
65
|
+
await page.goto(pageUrlForTracing, {
|
|
66
|
+
waitUntil: ['load'],
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (request.params.autoStop) {
|
|
70
|
+
await new Promise(resolve => setTimeout(resolve, 5_000));
|
|
71
|
+
await stopTracingAndAppendOutput(page, response, context);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
response.appendResponseLine(`The performance trace is being recorded. Use performance_stop_trace to stop it.`);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
export const stopTrace = defineTool({
|
|
79
|
+
name: 'performance_stop_trace',
|
|
80
|
+
description: 'Stops the active performance trace recording on the selected page.',
|
|
81
|
+
annotations: {
|
|
82
|
+
category: ToolCategory.PERFORMANCE,
|
|
83
|
+
readOnlyHint: true,
|
|
84
|
+
},
|
|
85
|
+
schema: {},
|
|
86
|
+
handler: async (_request, response, context) => {
|
|
87
|
+
if (!context.isRunningPerformanceTrace()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const page = context.getSelectedPage();
|
|
91
|
+
await stopTracingAndAppendOutput(page, response, context);
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
export const analyzeInsight = defineTool({
|
|
95
|
+
name: 'performance_analyze_insight',
|
|
96
|
+
description: 'Provides more detailed information on a specific Performance Insight of an insight set that was highlighted in the results of a trace recording.',
|
|
97
|
+
annotations: {
|
|
98
|
+
category: ToolCategory.PERFORMANCE,
|
|
99
|
+
readOnlyHint: true,
|
|
100
|
+
},
|
|
101
|
+
schema: {
|
|
102
|
+
insightSetId: zod
|
|
103
|
+
.string()
|
|
104
|
+
.describe('The id for the specific insight set. Only use the ids given in the "Available insight sets" list.'),
|
|
105
|
+
insightName: zod
|
|
106
|
+
.string()
|
|
107
|
+
.describe('The name of the Insight you want more information on. For example: "DocumentLatency" or "LCPBreakdown"'),
|
|
108
|
+
},
|
|
109
|
+
handler: async (request, response, context) => {
|
|
110
|
+
const lastRecording = context.recordedTraces().at(-1);
|
|
111
|
+
if (!lastRecording) {
|
|
112
|
+
response.appendResponseLine('No recorded traces found. Record a performance trace so you have Insights to analyze.');
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const insightOutput = getInsightOutput(lastRecording, request.params.insightSetId, request.params.insightName);
|
|
116
|
+
if ('error' in insightOutput) {
|
|
117
|
+
response.appendResponseLine(insightOutput.error);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
response.appendResponseLine(insightOutput.output);
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
async function stopTracingAndAppendOutput(page, response, context) {
|
|
124
|
+
try {
|
|
125
|
+
const traceEventsBuffer = await page.tracing.stop();
|
|
126
|
+
const result = await parseRawTraceBuffer(traceEventsBuffer);
|
|
127
|
+
response.appendResponseLine('The performance trace has been stopped.');
|
|
128
|
+
if (traceResultIsSuccess(result)) {
|
|
129
|
+
context.storeTraceRecording(result);
|
|
130
|
+
const traceSummaryText = getTraceSummary(result);
|
|
131
|
+
response.appendResponseLine(traceSummaryText);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
response.appendResponseLine('There was an unexpected error parsing the trace:');
|
|
135
|
+
response.appendResponseLine(result.error);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
const errorText = e instanceof Error ? e.message : JSON.stringify(e);
|
|
140
|
+
logger(`Error stopping performance trace: ${errorText}`);
|
|
141
|
+
response.appendResponseLine('An error occurred generating the response for this trace:');
|
|
142
|
+
response.appendResponseLine(errorText);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
context.setIsRunningPerformanceTrace(false);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod } from '../third_party/index.js';
|
|
7
|
+
import { ToolCategory } from './categories.js';
|
|
8
|
+
import { defineTool } from './ToolDefinition.js';
|
|
9
|
+
export const screenshot = defineTool({
|
|
10
|
+
name: 'take_screenshot',
|
|
11
|
+
description: `Take a screenshot of the page or element.`,
|
|
12
|
+
annotations: {
|
|
13
|
+
category: ToolCategory.DEBUGGING,
|
|
14
|
+
// Not read-only due to filePath param.
|
|
15
|
+
readOnlyHint: false,
|
|
16
|
+
},
|
|
17
|
+
schema: {
|
|
18
|
+
format: zod
|
|
19
|
+
.enum(['png', 'jpeg', 'webp'])
|
|
20
|
+
.default('png')
|
|
21
|
+
.describe('Type of format to save the screenshot as. Default is "png"'),
|
|
22
|
+
quality: zod
|
|
23
|
+
.number()
|
|
24
|
+
.min(0)
|
|
25
|
+
.max(100)
|
|
26
|
+
.optional()
|
|
27
|
+
.describe('Compression quality for JPEG and WebP formats (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.'),
|
|
28
|
+
uid: zod
|
|
29
|
+
.string()
|
|
30
|
+
.optional()
|
|
31
|
+
.describe('The uid of an element on the page from the page content snapshot. If omitted takes a pages screenshot.'),
|
|
32
|
+
fullPage: zod
|
|
33
|
+
.boolean()
|
|
34
|
+
.optional()
|
|
35
|
+
.describe('If set to true takes a screenshot of the full page instead of the currently visible viewport. Incompatible with uid.'),
|
|
36
|
+
filePath: zod
|
|
37
|
+
.string()
|
|
38
|
+
.optional()
|
|
39
|
+
.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.'),
|
|
40
|
+
},
|
|
41
|
+
handler: async (request, response, context) => {
|
|
42
|
+
if (request.params.uid && request.params.fullPage) {
|
|
43
|
+
throw new Error('Providing both "uid" and "fullPage" is not allowed.');
|
|
44
|
+
}
|
|
45
|
+
let pageOrHandle;
|
|
46
|
+
if (request.params.uid) {
|
|
47
|
+
pageOrHandle = await context.getElementByUid(request.params.uid);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
pageOrHandle = context.getSelectedPage();
|
|
51
|
+
}
|
|
52
|
+
const format = request.params.format;
|
|
53
|
+
const quality = format === 'png' ? undefined : request.params.quality;
|
|
54
|
+
const screenshot = await pageOrHandle.screenshot({
|
|
55
|
+
type: format,
|
|
56
|
+
fullPage: request.params.fullPage,
|
|
57
|
+
quality,
|
|
58
|
+
optimizeForSpeed: true, // Bonus: optimize encoding for speed
|
|
59
|
+
});
|
|
60
|
+
if (request.params.uid) {
|
|
61
|
+
response.appendResponseLine(`Took a screenshot of node with uid "${request.params.uid}".`);
|
|
62
|
+
}
|
|
63
|
+
else if (request.params.fullPage) {
|
|
64
|
+
response.appendResponseLine('Took a screenshot of the full current page.');
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
response.appendResponseLine("Took a screenshot of the current page's viewport.");
|
|
68
|
+
}
|
|
69
|
+
if (request.params.filePath) {
|
|
70
|
+
const file = await context.saveFile(screenshot, request.params.filePath);
|
|
71
|
+
response.appendResponseLine(`Saved screenshot to ${file.filename}.`);
|
|
72
|
+
}
|
|
73
|
+
else if (screenshot.length >= 2_000_000) {
|
|
74
|
+
const { filename } = await context.saveTemporaryFile(screenshot, `image/${request.params.format}`);
|
|
75
|
+
response.appendResponseLine(`Saved screenshot to ${filename}.`);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
response.attachImage({
|
|
79
|
+
mimeType: `image/${request.params.format}`,
|
|
80
|
+
data: Buffer.from(screenshot).toString('base64'),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod } from '../third_party/index.js';
|
|
7
|
+
import { ToolCategory } from './categories.js';
|
|
8
|
+
import { defineTool } from './ToolDefinition.js';
|
|
9
|
+
export const evaluateScript = defineTool({
|
|
10
|
+
name: 'evaluate_script',
|
|
11
|
+
description: `Evaluate a JavaScript function inside the currently selected page. Returns the response as JSON
|
|
12
|
+
so returned values have to JSON-serializable.`,
|
|
13
|
+
annotations: {
|
|
14
|
+
category: ToolCategory.DEBUGGING,
|
|
15
|
+
readOnlyHint: false,
|
|
16
|
+
},
|
|
17
|
+
schema: {
|
|
18
|
+
function: zod.string().describe(`A JavaScript function declaration to be executed by the tool in the currently selected page.
|
|
19
|
+
Example without arguments: \`() => {
|
|
20
|
+
return document.title
|
|
21
|
+
}\` or \`async () => {
|
|
22
|
+
return await fetch("example.com")
|
|
23
|
+
}\`.
|
|
24
|
+
Example with arguments: \`(el) => {
|
|
25
|
+
return el.innerText;
|
|
26
|
+
}\`
|
|
27
|
+
`),
|
|
28
|
+
args: zod
|
|
29
|
+
.array(zod.object({
|
|
30
|
+
uid: zod
|
|
31
|
+
.string()
|
|
32
|
+
.describe('The uid of an element on the page from the page content snapshot'),
|
|
33
|
+
}))
|
|
34
|
+
.optional()
|
|
35
|
+
.describe(`An optional list of arguments to pass to the function.`),
|
|
36
|
+
},
|
|
37
|
+
handler: async (request, response, context) => {
|
|
38
|
+
const args = [];
|
|
39
|
+
try {
|
|
40
|
+
const frames = new Set();
|
|
41
|
+
for (const el of request.params.args ?? []) {
|
|
42
|
+
const handle = await context.getElementByUid(el.uid);
|
|
43
|
+
frames.add(handle.frame);
|
|
44
|
+
args.push(handle);
|
|
45
|
+
}
|
|
46
|
+
let pageOrFrame;
|
|
47
|
+
// We can't evaluate the element handle across frames
|
|
48
|
+
if (frames.size > 1) {
|
|
49
|
+
throw new Error("Elements from different frames can't be evaluated together.");
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
pageOrFrame = [...frames.values()][0] ?? context.getSelectedPage();
|
|
53
|
+
}
|
|
54
|
+
const fn = await pageOrFrame.evaluateHandle(`(${request.params.function})`);
|
|
55
|
+
args.unshift(fn);
|
|
56
|
+
await context.waitForEventsAfterAction(async () => {
|
|
57
|
+
const result = await pageOrFrame.evaluate(async (fn, ...args) => {
|
|
58
|
+
// @ts-expect-error no types.
|
|
59
|
+
return JSON.stringify(await fn(...args));
|
|
60
|
+
}, ...args);
|
|
61
|
+
response.appendResponseLine('Script ran on page and returned:');
|
|
62
|
+
response.appendResponseLine('```json');
|
|
63
|
+
response.appendResponseLine(`${result}`);
|
|
64
|
+
response.appendResponseLine('```');
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
void Promise.allSettled(args.map(arg => arg.dispose()));
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
});
|