chrome-devtools-mcp 0.22.0 → 0.23.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.
@@ -3,7 +3,7 @@
3
3
  * Copyright 2026 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { zod, ajv, } from '../third_party/index.js';
6
+ import { zod, ajv } from '../third_party/index.js';
7
7
  import { ToolCategory } from './categories.js';
8
8
  import { definePageTool } from './ToolDefinition.js';
9
9
  export const listInPageTools = definePageTool({
@@ -58,19 +58,6 @@ export const executeInPageTool = definePageTool({
58
58
  throw new Error(`Failed to parse params as JSON: ${errorMessage}`);
59
59
  }
60
60
  }
61
- // Creates array of ElementHandles from the UIDs in the params.
62
- // We do not replace the uids with the ElementsHandles yet, because
63
- // the `evaluate` function only turns them into DOM elements if they
64
- // are passed as non-nested arguments.
65
- const handles = [];
66
- for (const value of Object.values(params)) {
67
- if (value instanceof Object &&
68
- 'uid' in value &&
69
- typeof value.uid === 'string' &&
70
- Object.keys(value).length === 1) {
71
- handles.push(await request.page.getElementByUid(value.uid));
72
- }
73
- }
74
61
  const toolGroup = request.page.getInPageTools();
75
62
  const tool = toolGroup?.tools.find(t => t.name === toolName);
76
63
  if (!tool) {
@@ -82,24 +69,6 @@ export const executeInPageTool = definePageTool({
82
69
  if (!valid) {
83
70
  throw new Error(`Invalid parameters for tool ${toolName}: ${ajvInstance.errorsText(validate.errors)}`);
84
71
  }
85
- const result = await request.page.pptrPage.evaluate(async (name, args, ...elements) => {
86
- // Replace the UIDs with DOM elements.
87
- for (const [key, value] of Object.entries(args)) {
88
- if (value instanceof Object &&
89
- 'uid' in value &&
90
- typeof value.uid === 'string' &&
91
- Object.keys(value).length === 1) {
92
- args[key] = elements.shift();
93
- }
94
- }
95
- if (!window.__dtmcp?.executeTool) {
96
- throw new Error('No tools found on the page');
97
- }
98
- const toolResult = await window.__dtmcp.executeTool(name, args);
99
- return {
100
- result: toolResult,
101
- };
102
- }, toolName, params, ...handles);
103
- response.appendResponseLine(JSON.stringify(result, null, 2));
72
+ await request.page.executeInPageTool(toolName, params, response);
104
73
  },
105
74
  });
@@ -58,7 +58,7 @@ export const listNetworkRequests = definePageTool({
58
58
  .describe('Set to true to return the preserved requests over the last 3 navigations.'),
59
59
  },
60
60
  handler: async (request, response, context) => {
61
- const data = await context.getDevToolsData(request.page);
61
+ const data = await request.page.getDevToolsData();
62
62
  response.attachDevToolsData(data);
63
63
  const reqid = data?.cdpRequestId
64
64
  ? context.resolveCdpRequestId(request.page, data.cdpRequestId)
@@ -101,7 +101,7 @@ export const getNetworkRequest = definePageTool({
101
101
  });
102
102
  }
103
103
  else {
104
- const data = await context.getDevToolsData(request.page);
104
+ const data = await request.page.getDevToolsData();
105
105
  response.attachDevToolsData(data);
106
106
  const reqid = data?.cdpRequestId
107
107
  ? context.resolveCdpRequestId(request.page, data.cdpRequestId)
@@ -7,6 +7,51 @@ import { logger } from '../logger.js';
7
7
  import { zod } from '../third_party/index.js';
8
8
  import { ToolCategory } from './categories.js';
9
9
  import { CLOSE_PAGE_ERROR, definePageTool, defineTool, timeoutSchema, } from './ToolDefinition.js';
10
+ async function navigateWithInterception(page, action, allowListString, timeout) {
11
+ const allowList = allowListString
12
+ ? allowListString.split(',').map((p) => new URLPattern(p.trim()))
13
+ : undefined;
14
+ const requestHandler = (interceptedRequest) => {
15
+ if (!interceptedRequest.isNavigationRequest()) {
16
+ void interceptedRequest.continue();
17
+ return;
18
+ }
19
+ const requestUrl = interceptedRequest.url();
20
+ const isAllowed = allowList.some((pattern) => pattern.test(requestUrl));
21
+ if (isAllowed) {
22
+ void interceptedRequest.continue();
23
+ }
24
+ else {
25
+ logger(`Blocking request to: ${requestUrl}`);
26
+ void interceptedRequest.abort('blockedbyclient');
27
+ }
28
+ };
29
+ const cleanupInterception = async () => {
30
+ if (allowList) {
31
+ page.pptrPage.off('request', requestHandler);
32
+ await page.pptrPage.setRequestInterception(false).catch(error => {
33
+ logger(`Failed to disable request interception`, error);
34
+ });
35
+ }
36
+ };
37
+ if (allowList) {
38
+ await page.pptrPage.setRequestInterception(true);
39
+ page.pptrPage.on('request', requestHandler);
40
+ }
41
+ try {
42
+ await page.waitForEventsAfterAction(async () => {
43
+ try {
44
+ await action();
45
+ }
46
+ finally {
47
+ await cleanupInterception();
48
+ }
49
+ }, { timeout });
50
+ }
51
+ finally {
52
+ await cleanupInterception();
53
+ }
54
+ }
10
55
  export const listPages = defineTool(args => {
11
56
  return {
12
57
  name: 'list_pages',
@@ -78,159 +123,177 @@ export const closePage = defineTool({
78
123
  response.setListInPageTools();
79
124
  },
80
125
  });
81
- export const newPage = defineTool({
82
- name: 'new_page',
83
- description: `Open a new tab and load a URL. Use project URL if not specified otherwise.`,
84
- annotations: {
85
- category: ToolCategory.NAVIGATION,
86
- readOnlyHint: false,
87
- },
88
- schema: {
89
- url: zod.string().describe('URL to load in a new page.'),
90
- background: zod
91
- .boolean()
92
- .optional()
93
- .describe('Whether to open the page in the background without bringing it to the front. Default is false (foreground).'),
94
- isolatedContext: zod
95
- .string()
96
- .optional()
97
- .describe('If specified, the page is created in an isolated browser context with the given name. ' +
98
- 'Pages in the same browser context share cookies and storage. ' +
99
- 'Pages in different browser contexts are fully isolated.'),
100
- ...timeoutSchema,
101
- },
102
- handler: async (request, response, context) => {
103
- const page = await context.newPage(request.params.background, request.params.isolatedContext);
104
- await page.waitForEventsAfterAction(async () => {
105
- await page.pptrPage.goto(request.params.url, {
126
+ export const newPage = defineTool(args => {
127
+ return {
128
+ name: 'new_page',
129
+ description: `Open a new tab and load a URL. Use project URL if not specified otherwise.`,
130
+ annotations: {
131
+ category: ToolCategory.NAVIGATION,
132
+ readOnlyHint: false,
133
+ },
134
+ schema: {
135
+ url: zod.string().describe('URL to load in a new page.'),
136
+ background: zod
137
+ .boolean()
138
+ .optional()
139
+ .describe('Whether to open the page in the background without bringing it to the front. Default is false (foreground).'),
140
+ isolatedContext: zod
141
+ .string()
142
+ .optional()
143
+ .describe('If specified, the page is created in an isolated browser context with the given name. ' +
144
+ 'Pages in the same browser context share cookies and storage. ' +
145
+ 'Pages in different browser contexts are fully isolated.'),
146
+ ...(args?.experimentalNavigationAllowlist
147
+ ? {
148
+ allowList: zod
149
+ .string()
150
+ .optional()
151
+ .describe('Optional comma-separated list of URL patterns to allow. If provided, all other navigations will be blocked.'),
152
+ }
153
+ : {}),
154
+ ...timeoutSchema,
155
+ },
156
+ handler: async (request, response, context) => {
157
+ const page = await context.newPage(request.params.background, request.params.isolatedContext);
158
+ await navigateWithInterception(page, () => page.pptrPage.goto(request.params.url, {
106
159
  timeout: request.params.timeout,
107
- });
108
- }, { timeout: request.params.timeout });
109
- response.setIncludePages(true);
110
- response.setListInPageTools();
111
- },
160
+ }), request.params.allowList, request.params.timeout);
161
+ response.setIncludePages(true);
162
+ response.setListInPageTools();
163
+ },
164
+ };
112
165
  });
113
- export const navigatePage = definePageTool({
114
- name: 'navigate_page',
115
- description: `Go to a URL, or back, forward, or reload. Use project URL if not specified otherwise.`,
116
- annotations: {
117
- category: ToolCategory.NAVIGATION,
118
- readOnlyHint: false,
119
- },
120
- schema: {
121
- type: zod
122
- .enum(['url', 'back', 'forward', 'reload'])
123
- .optional()
124
- .describe('Navigate the page by URL, back or forward in history, or reload.'),
125
- url: zod.string().optional().describe('Target URL (only type=url)'),
126
- ignoreCache: zod
127
- .boolean()
128
- .optional()
129
- .describe('Whether to ignore cache on reload.'),
130
- handleBeforeUnload: zod
131
- .enum(['accept', 'decline'])
132
- .optional()
133
- .describe('Whether to auto accept or beforeunload dialogs triggered by this navigation. Default is accept.'),
134
- initScript: zod
135
- .string()
136
- .optional()
137
- .describe('A JavaScript script to be executed on each new document before any other scripts for the next navigation.'),
138
- ...timeoutSchema,
139
- },
140
- handler: async (request, response) => {
141
- const page = request.page;
142
- const options = {
143
- timeout: request.params.timeout,
144
- };
145
- if (!request.params.type && !request.params.url) {
146
- throw new Error('Either URL or a type is required.');
147
- }
148
- if (!request.params.type) {
149
- request.params.type = 'url';
150
- }
151
- const handleBeforeUnload = request.params.handleBeforeUnload ?? 'accept';
152
- const dialogHandler = (dialog) => {
153
- if (dialog.type() === 'beforeunload') {
154
- if (handleBeforeUnload === 'accept') {
155
- response.appendResponseLine(`Accepted a beforeunload dialog.`);
156
- void dialog.accept();
166
+ export const navigatePage = definePageTool(args => {
167
+ return {
168
+ name: 'navigate_page',
169
+ description: `Go to a URL, or back, forward, or reload. Use project URL if not specified otherwise.`,
170
+ annotations: {
171
+ category: ToolCategory.NAVIGATION,
172
+ readOnlyHint: false,
173
+ },
174
+ schema: {
175
+ type: zod
176
+ .enum(['url', 'back', 'forward', 'reload'])
177
+ .optional()
178
+ .describe('Navigate the page by URL, back or forward in history, or reload.'),
179
+ url: zod.string().optional().describe('Target URL (only type=url)'),
180
+ ignoreCache: zod
181
+ .boolean()
182
+ .optional()
183
+ .describe('Whether to ignore cache on reload.'),
184
+ handleBeforeUnload: zod
185
+ .enum(['accept', 'decline'])
186
+ .optional()
187
+ .describe('Whether to auto accept or beforeunload dialogs triggered by this navigation. Default is accept.'),
188
+ initScript: zod
189
+ .string()
190
+ .optional()
191
+ .describe('A JavaScript script to be executed on each new document before any other scripts for the next navigation.'),
192
+ ...(args?.experimentalNavigationAllowlist
193
+ ? {
194
+ allowList: zod
195
+ .string()
196
+ .optional()
197
+ .describe('Optional comma-separated list of URL patterns to allow. If provided, all other navigations will be blocked.'),
157
198
  }
158
- else {
159
- response.appendResponseLine(`Declined a beforeunload dialog.`);
160
- void dialog.dismiss();
199
+ : {}),
200
+ ...timeoutSchema,
201
+ },
202
+ handler: async (request, response) => {
203
+ const page = request.page;
204
+ const options = {
205
+ timeout: request.params.timeout,
206
+ };
207
+ if (!request.params.type && !request.params.url) {
208
+ throw new Error('Either URL or a type is required.');
209
+ }
210
+ if (!request.params.type) {
211
+ request.params.type = 'url';
212
+ }
213
+ const handleBeforeUnload = request.params.handleBeforeUnload ?? 'accept';
214
+ const dialogHandler = (dialog) => {
215
+ if (dialog.type() === 'beforeunload') {
216
+ if (handleBeforeUnload === 'accept') {
217
+ response.appendResponseLine(`Accepted a beforeunload dialog.`);
218
+ void dialog.accept();
219
+ }
220
+ else {
221
+ response.appendResponseLine(`Declined a beforeunload dialog.`);
222
+ void dialog.dismiss();
223
+ }
224
+ // We are not going to report the dialog like regular dialogs.
225
+ page.clearDialog();
161
226
  }
162
- // We are not going to report the dialog like regular dialogs.
163
- page.clearDialog();
227
+ };
228
+ let initScriptId;
229
+ if (request.params.initScript) {
230
+ const { identifier } = await page.pptrPage.evaluateOnNewDocument(request.params.initScript);
231
+ initScriptId = identifier;
164
232
  }
165
- };
166
- let initScriptId;
167
- if (request.params.initScript) {
168
- const { identifier } = await page.pptrPage.evaluateOnNewDocument(request.params.initScript);
169
- initScriptId = identifier;
170
- }
171
- page.pptrPage.on('dialog', dialogHandler);
172
- try {
173
- await page.waitForEventsAfterAction(async () => {
174
- switch (request.params.type) {
175
- case 'url':
176
- if (!request.params.url) {
177
- throw new Error('A URL is required for navigation of type=url.');
178
- }
179
- try {
180
- await page.pptrPage.goto(request.params.url, options);
181
- response.appendResponseLine(`Successfully navigated to ${request.params.url}.`);
182
- }
183
- catch (error) {
184
- response.appendResponseLine(`Unable to navigate in the selected page: ${error.message}.`);
185
- }
186
- break;
187
- case 'back':
188
- try {
189
- await page.pptrPage.goBack(options);
190
- response.appendResponseLine(`Successfully navigated back to ${page.pptrPage.url()}.`);
191
- }
192
- catch (error) {
193
- response.appendResponseLine(`Unable to navigate back in the selected page: ${error.message}.`);
194
- }
195
- break;
196
- case 'forward':
197
- try {
198
- await page.pptrPage.goForward(options);
199
- response.appendResponseLine(`Successfully navigated forward to ${page.pptrPage.url()}.`);
200
- }
201
- catch (error) {
202
- response.appendResponseLine(`Unable to navigate forward in the selected page: ${error.message}.`);
203
- }
204
- break;
205
- case 'reload':
206
- try {
207
- await page.pptrPage.reload({
208
- ...options,
209
- ignoreCache: request.params.ignoreCache,
210
- });
211
- response.appendResponseLine(`Successfully reloaded the page.`);
212
- }
213
- catch (error) {
214
- response.appendResponseLine(`Unable to reload the selected page: ${error.message}.`);
215
- }
216
- break;
233
+ page.pptrPage.on('dialog', dialogHandler);
234
+ try {
235
+ await navigateWithInterception(page, async () => {
236
+ switch (request.params.type) {
237
+ case 'url':
238
+ if (!request.params.url) {
239
+ throw new Error('A URL is required for navigation of type=url.');
240
+ }
241
+ try {
242
+ await page.pptrPage.goto(request.params.url, options);
243
+ response.appendResponseLine(`Successfully navigated to ${request.params.url}.`);
244
+ }
245
+ catch (error) {
246
+ response.appendResponseLine(`Unable to navigate in the selected page: ${error.message}.`);
247
+ }
248
+ break;
249
+ case 'back':
250
+ try {
251
+ await page.pptrPage.goBack(options);
252
+ response.appendResponseLine(`Successfully navigated back to ${page.pptrPage.url()}.`);
253
+ }
254
+ catch (error) {
255
+ response.appendResponseLine(`Unable to navigate back in the selected page: ${error.message}.`);
256
+ }
257
+ break;
258
+ case 'forward':
259
+ try {
260
+ await page.pptrPage.goForward(options);
261
+ response.appendResponseLine(`Successfully navigated forward to ${page.pptrPage.url()}.`);
262
+ }
263
+ catch (error) {
264
+ response.appendResponseLine(`Unable to navigate forward in the selected page: ${error.message}.`);
265
+ }
266
+ break;
267
+ case 'reload':
268
+ try {
269
+ await page.pptrPage.reload({
270
+ ...options,
271
+ ignoreCache: request.params.ignoreCache,
272
+ });
273
+ response.appendResponseLine(`Successfully reloaded the page.`);
274
+ }
275
+ catch (error) {
276
+ response.appendResponseLine(`Unable to reload the selected page: ${error.message}.`);
277
+ }
278
+ break;
279
+ }
280
+ }, request.params.allowList, request.params.timeout);
281
+ }
282
+ finally {
283
+ page.pptrPage.off('dialog', dialogHandler);
284
+ if (initScriptId) {
285
+ await page.pptrPage
286
+ .removeScriptToEvaluateOnNewDocument(initScriptId)
287
+ .catch(error => {
288
+ logger(`Failed to remove init script`, error);
289
+ });
217
290
  }
218
- }, { timeout: request.params.timeout });
219
- }
220
- finally {
221
- page.pptrPage.off('dialog', dialogHandler);
222
- if (initScriptId) {
223
- await page.pptrPage
224
- .removeScriptToEvaluateOnNewDocument(initScriptId)
225
- .catch(error => {
226
- logger(`Failed to remove init script`, error);
227
- });
228
291
  }
229
- }
230
- response.setIncludePages(true);
231
- response.setListInPageTools();
232
- response.setListWebMcpTools();
233
- },
292
+ response.setIncludePages(true);
293
+ response.setListInPageTools();
294
+ response.setListWebMcpTools();
295
+ },
296
+ };
234
297
  });
235
298
  export const resizePage = definePageTool({
236
299
  name: 'resize_page',
@@ -14,33 +14,44 @@ async function generateTempFilePath() {
14
14
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chrome-devtools-mcp-'));
15
15
  return path.join(dir, `screencast.mp4`);
16
16
  }
17
- export const startScreencast = definePageTool({
17
+ const supportedExtensions = ['.webm', '.mp4'];
18
+ export const startScreencast = definePageTool(args => ({
18
19
  name: 'screencast_start',
19
- description: 'Starts recording a screencast (video) of the selected page in mp4 format.',
20
+ description: `Starts recording a screencast (video) of the selected page in specified format.`,
20
21
  annotations: {
21
22
  category: ToolCategory.DEBUGGING,
22
23
  readOnlyHint: false,
23
24
  conditions: ['screencast'],
24
25
  },
25
26
  schema: {
26
- path: zod
27
+ filePath: zod
27
28
  .string()
28
29
  .optional()
29
- .describe('Output path. Uses mkdtemp to generate a unique path if not provided.'),
30
+ .describe(`Output file path (${supportedExtensions.join(',')} are supported). Uses mkdtemp to generate a unique path if not provided.`),
30
31
  },
31
32
  handler: async (request, response, context) => {
32
33
  if (context.getScreenRecorder() !== null) {
33
34
  response.appendResponseLine('Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.');
34
35
  return;
35
36
  }
36
- const filePath = request.params.path ?? (await generateTempFilePath());
37
- const resolvedPath = ensureExtension(path.resolve(filePath), '.mp4');
37
+ const filePath = request.params.filePath ?? (await generateTempFilePath());
38
+ let enforcedExtension = '.mp4';
39
+ let format = 'mp4';
40
+ for (const supportedExtension of supportedExtensions) {
41
+ if (filePath.endsWith(supportedExtension)) {
42
+ enforcedExtension = supportedExtension;
43
+ format = supportedExtension.substring(1);
44
+ break;
45
+ }
46
+ }
47
+ const resolvedPath = ensureExtension(path.resolve(filePath), enforcedExtension);
38
48
  const page = request.page;
39
49
  let recorder;
40
50
  try {
41
51
  recorder = await page.pptrPage.screencast({
42
52
  path: resolvedPath,
43
- format: 'mp4',
53
+ format: format,
54
+ ffmpegPath: args?.experimentalFfmpegPath,
44
55
  });
45
56
  }
46
57
  catch (err) {
@@ -54,7 +65,7 @@ export const startScreencast = definePageTool({
54
65
  context.setScreenRecorder({ recorder, filePath: resolvedPath });
55
66
  response.appendResponseLine(`Screencast recording started. The recording will be saved to ${resolvedPath}. Use ${stopScreencast.name} to stop recording.`);
56
67
  },
57
- });
68
+ }));
58
69
  export const stopScreencast = definePageTool({
59
70
  name: 'screencast_stop',
60
71
  description: 'Stops the active screencast recording on the selected page.',
@@ -5,5 +5,5 @@
5
5
  */
6
6
  // If moved update release-please config
7
7
  // x-release-please-start-version
8
- export const VERSION = '0.22.0';
8
+ export const VERSION = '0.23.0';
9
9
  // x-release-please-end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chrome-devtools-mcp",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "MCP server for Chrome DevTools",
5
5
  "type": "module",
6
6
  "bin": {
@@ -80,6 +80,7 @@
80
80
  "tiktoken": "^1.0.22",
81
81
  "typescript": "^6.0.2",
82
82
  "typescript-eslint": "^8.43.0",
83
+ "urlpattern-polyfill": "^10.1.0",
83
84
  "yargs": "18.0.0"
84
85
  },
85
86
  "engines": {