@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
package/build/src/cli.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { yargs, hideBin } from './third_party/index.js';
|
|
7
|
+
export const cliOptions = {
|
|
8
|
+
browserUrl: {
|
|
9
|
+
type: 'string',
|
|
10
|
+
description: 'Connect to a running, debuggable Chrome instance (e.g. `http://127.0.0.1:9222`). For more details see: https://github.com/ChromeDevTools/chrome-devtools-mcp#connecting-to-a-running-chrome-instance.',
|
|
11
|
+
alias: 'u',
|
|
12
|
+
conflicts: 'wsEndpoint',
|
|
13
|
+
coerce: (url) => {
|
|
14
|
+
if (!url) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
new URL(url);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new Error(`Provided browserUrl ${url} is not valid URL.`);
|
|
22
|
+
}
|
|
23
|
+
return url;
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
wsEndpoint: {
|
|
27
|
+
type: 'string',
|
|
28
|
+
description: 'WebSocket endpoint to connect to a running Chrome instance (e.g., ws://127.0.0.1:9222/devtools/browser/<id>). Alternative to --browserUrl.',
|
|
29
|
+
alias: 'w',
|
|
30
|
+
conflicts: 'browserUrl',
|
|
31
|
+
coerce: (url) => {
|
|
32
|
+
if (!url) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const parsed = new URL(url);
|
|
37
|
+
if (parsed.protocol !== 'ws:' && parsed.protocol !== 'wss:') {
|
|
38
|
+
throw new Error(`Provided wsEndpoint ${url} must use ws:// or wss:// protocol.`);
|
|
39
|
+
}
|
|
40
|
+
return url;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error.message.includes('ws://')) {
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`Provided wsEndpoint ${url} is not valid URL.`);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
wsHeaders: {
|
|
51
|
+
type: 'string',
|
|
52
|
+
description: 'Custom headers for WebSocket connection in JSON format (e.g., \'{"Authorization":"Bearer token"}\'). Only works with --wsEndpoint.',
|
|
53
|
+
implies: 'wsEndpoint',
|
|
54
|
+
coerce: (val) => {
|
|
55
|
+
if (!val) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const parsed = JSON.parse(val);
|
|
60
|
+
if (typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
61
|
+
throw new Error('Headers must be a JSON object');
|
|
62
|
+
}
|
|
63
|
+
return parsed;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new Error(`Invalid JSON for wsHeaders: ${error.message}`);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
headless: {
|
|
71
|
+
type: 'boolean',
|
|
72
|
+
description: 'Whether to run in headless (no UI) mode.',
|
|
73
|
+
default: false,
|
|
74
|
+
},
|
|
75
|
+
executablePath: {
|
|
76
|
+
type: 'string',
|
|
77
|
+
description: 'Path to custom Chrome executable.',
|
|
78
|
+
conflicts: ['browserUrl', 'wsEndpoint'],
|
|
79
|
+
alias: 'e',
|
|
80
|
+
},
|
|
81
|
+
isolated: {
|
|
82
|
+
type: 'boolean',
|
|
83
|
+
description: 'If specified, creates a temporary user-data-dir that is automatically cleaned up after the browser is closed. Defaults to false.',
|
|
84
|
+
},
|
|
85
|
+
userDataDir: {
|
|
86
|
+
type: 'string',
|
|
87
|
+
description: 'Path to the user data directory for Chrome. Default is $HOME/.cache/chrome-devtools-mcp/chrome-profile$CHANNEL_SUFFIX_IF_NON_STABLE',
|
|
88
|
+
conflicts: ['browserUrl', 'wsEndpoint', 'isolated'],
|
|
89
|
+
},
|
|
90
|
+
channel: {
|
|
91
|
+
type: 'string',
|
|
92
|
+
description: 'Specify a different Chrome channel that should be used. The default is the stable channel version.',
|
|
93
|
+
choices: ['stable', 'canary', 'beta', 'dev'],
|
|
94
|
+
conflicts: ['browserUrl', 'wsEndpoint', 'executablePath'],
|
|
95
|
+
},
|
|
96
|
+
logFile: {
|
|
97
|
+
type: 'string',
|
|
98
|
+
describe: 'Path to a file to write debug logs to. Set the env variable `DEBUG` to `*` to enable verbose logs. Useful for submitting bug reports.',
|
|
99
|
+
},
|
|
100
|
+
viewport: {
|
|
101
|
+
type: 'string',
|
|
102
|
+
describe: 'Initial viewport size for the Chrome instances started by the server. For example, `1280x720`. In headless mode, max size is 3840x2160px.',
|
|
103
|
+
coerce: (arg) => {
|
|
104
|
+
if (arg === undefined) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const [width, height] = arg.split('x').map(Number);
|
|
108
|
+
if (!width || !height || Number.isNaN(width) || Number.isNaN(height)) {
|
|
109
|
+
throw new Error('Invalid viewport. Expected format is `1280x720`.');
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
width,
|
|
113
|
+
height,
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
proxyServer: {
|
|
118
|
+
type: 'string',
|
|
119
|
+
description: `Proxy server configuration for Chrome passed as --proxy-server when launching the browser. See https://www.chromium.org/developers/design-documents/network-settings/ for details.`,
|
|
120
|
+
},
|
|
121
|
+
acceptInsecureCerts: {
|
|
122
|
+
type: 'boolean',
|
|
123
|
+
description: `If enabled, ignores errors relative to self-signed and expired certificates. Use with caution.`,
|
|
124
|
+
},
|
|
125
|
+
experimentalDevtools: {
|
|
126
|
+
type: 'boolean',
|
|
127
|
+
describe: 'Whether to enable automation over DevTools targets',
|
|
128
|
+
hidden: true,
|
|
129
|
+
},
|
|
130
|
+
experimentalIncludeAllPages: {
|
|
131
|
+
type: 'boolean',
|
|
132
|
+
describe: 'Whether to include all kinds of pages such as webviews or background pages as pages.',
|
|
133
|
+
hidden: true,
|
|
134
|
+
},
|
|
135
|
+
chromeArg: {
|
|
136
|
+
type: 'array',
|
|
137
|
+
describe: 'Additional arguments for Chrome. Only applies when Chrome is launched by chrome-devtools-mcp.',
|
|
138
|
+
},
|
|
139
|
+
categoryEmulation: {
|
|
140
|
+
type: 'boolean',
|
|
141
|
+
default: true,
|
|
142
|
+
describe: 'Set to false to exclude tools related to emulation.',
|
|
143
|
+
},
|
|
144
|
+
categoryPerformance: {
|
|
145
|
+
type: 'boolean',
|
|
146
|
+
default: true,
|
|
147
|
+
describe: 'Set to false to exclude tools related to performance.',
|
|
148
|
+
},
|
|
149
|
+
categoryNetwork: {
|
|
150
|
+
type: 'boolean',
|
|
151
|
+
default: true,
|
|
152
|
+
describe: 'Set to false to exclude tools related to network.',
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
export function parseArguments(version, argv = process.argv) {
|
|
156
|
+
const yargsInstance = yargs(hideBin(argv))
|
|
157
|
+
.scriptName('npx chrome-devtools-mcp@latest')
|
|
158
|
+
.options(cliOptions)
|
|
159
|
+
.check(args => {
|
|
160
|
+
// We can't set default in the options else
|
|
161
|
+
// Yargs will complain
|
|
162
|
+
if (!args.channel &&
|
|
163
|
+
!args.browserUrl &&
|
|
164
|
+
!args.wsEndpoint &&
|
|
165
|
+
!args.executablePath) {
|
|
166
|
+
args.channel = 'stable';
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
})
|
|
170
|
+
.example([
|
|
171
|
+
[
|
|
172
|
+
'$0 --browserUrl http://127.0.0.1:9222',
|
|
173
|
+
'Connect to an existing browser instance via HTTP',
|
|
174
|
+
],
|
|
175
|
+
[
|
|
176
|
+
'$0 --wsEndpoint ws://127.0.0.1:9222/devtools/browser/abc123',
|
|
177
|
+
'Connect to an existing browser instance via WebSocket',
|
|
178
|
+
],
|
|
179
|
+
[
|
|
180
|
+
`$0 --wsEndpoint ws://127.0.0.1:9222/devtools/browser/abc123 --wsHeaders '{"Authorization":"Bearer token"}'`,
|
|
181
|
+
'Connect via WebSocket with custom headers',
|
|
182
|
+
],
|
|
183
|
+
['$0 --channel beta', 'Use Chrome Beta installed on this system'],
|
|
184
|
+
['$0 --channel canary', 'Use Chrome Canary installed on this system'],
|
|
185
|
+
['$0 --channel dev', 'Use Chrome Dev installed on this system'],
|
|
186
|
+
['$0 --channel stable', 'Use stable Chrome installed on this system'],
|
|
187
|
+
['$0 --logFile /tmp/log.txt', 'Save logs to a file'],
|
|
188
|
+
['$0 --help', 'Print CLI options'],
|
|
189
|
+
[
|
|
190
|
+
'$0 --viewport 1280x720',
|
|
191
|
+
'Launch Chrome with the initial viewport size of 1280x720px',
|
|
192
|
+
],
|
|
193
|
+
[
|
|
194
|
+
`$0 --chrome-arg='--no-sandbox' --chrome-arg='--disable-setuid-sandbox'`,
|
|
195
|
+
'Launch Chrome without sandboxes. Use with caution.',
|
|
196
|
+
],
|
|
197
|
+
['$0 --no-category-emulation', 'Disable tools in the emulation category'],
|
|
198
|
+
[
|
|
199
|
+
'$0 --no-category-performance',
|
|
200
|
+
'Disable tools in the performance category',
|
|
201
|
+
],
|
|
202
|
+
['$0 --no-category-network', 'Disable tools in the network category'],
|
|
203
|
+
[
|
|
204
|
+
'$0 --user-data-dir=/tmp/user-data-dir',
|
|
205
|
+
'Use a custom user data directory',
|
|
206
|
+
],
|
|
207
|
+
]);
|
|
208
|
+
return yargsInstance
|
|
209
|
+
.wrap(Math.min(120, yargsInstance.terminalWidth()))
|
|
210
|
+
.help()
|
|
211
|
+
.version(version)
|
|
212
|
+
.parseSync();
|
|
213
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
// The short format for a console message, based on a previous format.
|
|
7
|
+
export function formatConsoleEventShort(msg) {
|
|
8
|
+
if (msg.type === 'issue') {
|
|
9
|
+
return `msgid=${msg.consoleMessageStableId} [${msg.type}] ${msg.message} (count: ${msg.count})`;
|
|
10
|
+
}
|
|
11
|
+
return `msgid=${msg.consoleMessageStableId} [${msg.type}] ${msg.message} (${msg.args?.length ?? 0} args)`;
|
|
12
|
+
}
|
|
13
|
+
function getArgs(msg) {
|
|
14
|
+
const args = [...(msg.args ?? [])];
|
|
15
|
+
// If there is no text, the first argument serves as text (see formatMessage).
|
|
16
|
+
if (!msg.message) {
|
|
17
|
+
args.shift();
|
|
18
|
+
}
|
|
19
|
+
return args;
|
|
20
|
+
}
|
|
21
|
+
// The verbose format for a console message, including all details.
|
|
22
|
+
export function formatConsoleEventVerbose(msg, context) {
|
|
23
|
+
const aggregatedIssue = msg.item;
|
|
24
|
+
const result = [
|
|
25
|
+
`ID: ${msg.consoleMessageStableId}`,
|
|
26
|
+
`Message: ${msg.type}> ${aggregatedIssue ? formatIssue(aggregatedIssue, msg.description, context) : msg.message}`,
|
|
27
|
+
aggregatedIssue ? undefined : formatArgs(msg),
|
|
28
|
+
].filter(line => !!line);
|
|
29
|
+
return result.join('\n');
|
|
30
|
+
}
|
|
31
|
+
function formatArg(arg) {
|
|
32
|
+
return typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
|
|
33
|
+
}
|
|
34
|
+
function formatArgs(consoleData) {
|
|
35
|
+
const args = getArgs(consoleData);
|
|
36
|
+
if (!args.length) {
|
|
37
|
+
return '';
|
|
38
|
+
}
|
|
39
|
+
const result = ['### Arguments'];
|
|
40
|
+
for (const [key, arg] of args.entries()) {
|
|
41
|
+
result.push(`Arg #${key}: ${formatArg(arg)}`);
|
|
42
|
+
}
|
|
43
|
+
return result.join('\n');
|
|
44
|
+
}
|
|
45
|
+
export function formatIssue(issue, description, context) {
|
|
46
|
+
const result = [];
|
|
47
|
+
let processedMarkdown = description?.trim();
|
|
48
|
+
// Remove heading in order not to conflict with the whole console message response markdown
|
|
49
|
+
if (processedMarkdown?.startsWith('# ')) {
|
|
50
|
+
processedMarkdown = processedMarkdown.substring(2).trimStart();
|
|
51
|
+
}
|
|
52
|
+
if (processedMarkdown)
|
|
53
|
+
result.push(processedMarkdown);
|
|
54
|
+
const links = issue.getDescription()?.links;
|
|
55
|
+
if (links && links.length > 0) {
|
|
56
|
+
result.push('Learn more:');
|
|
57
|
+
for (const link of links) {
|
|
58
|
+
result.push(`[${link.linkTitle}](${link.link})`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const issues = issue.getAllIssues();
|
|
62
|
+
const affectedResources = [];
|
|
63
|
+
for (const singleIssue of issues) {
|
|
64
|
+
const details = singleIssue.details();
|
|
65
|
+
if (!details)
|
|
66
|
+
continue;
|
|
67
|
+
// We send the remaining details as untyped JSON because the DevTools
|
|
68
|
+
// frontend code is currently not re-usable.
|
|
69
|
+
// eslint-disable-next-line
|
|
70
|
+
const data = structuredClone(details);
|
|
71
|
+
let uid;
|
|
72
|
+
let request;
|
|
73
|
+
if ('violatingNodeId' in details && details.violatingNodeId && context) {
|
|
74
|
+
uid = context.resolveCdpElementId(details.violatingNodeId);
|
|
75
|
+
delete data.violatingNodeId;
|
|
76
|
+
}
|
|
77
|
+
if ('nodeId' in details && details.nodeId && context) {
|
|
78
|
+
uid = context.resolveCdpElementId(details.nodeId);
|
|
79
|
+
delete data.nodeId;
|
|
80
|
+
}
|
|
81
|
+
if ('documentNodeId' in details && details.documentNodeId && context) {
|
|
82
|
+
uid = context.resolveCdpElementId(details.documentNodeId);
|
|
83
|
+
delete data.documentNodeId;
|
|
84
|
+
}
|
|
85
|
+
if ('request' in details && details.request) {
|
|
86
|
+
request = details.request.url;
|
|
87
|
+
if (details.request.requestId && context) {
|
|
88
|
+
const resolvedId = context.resolveCdpRequestId(details.request.requestId);
|
|
89
|
+
if (resolvedId) {
|
|
90
|
+
request = resolvedId;
|
|
91
|
+
delete data.request.requestId;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// These fields has no use for the MCP client (redundant or irrelevant).
|
|
96
|
+
delete data.errorType;
|
|
97
|
+
delete data.frameId;
|
|
98
|
+
affectedResources.push({
|
|
99
|
+
uid,
|
|
100
|
+
data: data,
|
|
101
|
+
request,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (affectedResources.length) {
|
|
105
|
+
result.push('### Affected resources');
|
|
106
|
+
}
|
|
107
|
+
result.push(...affectedResources.map(item => {
|
|
108
|
+
const details = [];
|
|
109
|
+
if (item.uid)
|
|
110
|
+
details.push(`uid=${item.uid}`);
|
|
111
|
+
if (item.request) {
|
|
112
|
+
details.push((typeof item.request === 'number' ? `reqid=` : 'url=') + item.request);
|
|
113
|
+
}
|
|
114
|
+
if (item.data)
|
|
115
|
+
details.push(`data=${JSON.stringify(item.data)}`);
|
|
116
|
+
return details.join(' ');
|
|
117
|
+
}));
|
|
118
|
+
if (result.length === 0)
|
|
119
|
+
return 'No affected resources found';
|
|
120
|
+
return result.join('\n');
|
|
121
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { isUtf8 } from 'node:buffer';
|
|
7
|
+
const BODY_CONTEXT_SIZE_LIMIT = 10000;
|
|
8
|
+
export function getShortDescriptionForRequest(request, id, selectedInDevToolsUI = false) {
|
|
9
|
+
// TODO truncate the URL
|
|
10
|
+
return `reqid=${id} ${request.method()} ${request.url()} ${getStatusFromRequest(request)}${selectedInDevToolsUI ? ` [selected in the DevTools Network panel]` : ''}`;
|
|
11
|
+
}
|
|
12
|
+
export function getStatusFromRequest(request) {
|
|
13
|
+
const httpResponse = request.response();
|
|
14
|
+
const failure = request.failure();
|
|
15
|
+
let status;
|
|
16
|
+
if (httpResponse) {
|
|
17
|
+
const responseStatus = httpResponse.status();
|
|
18
|
+
status =
|
|
19
|
+
responseStatus >= 200 && responseStatus <= 299
|
|
20
|
+
? `[success - ${responseStatus}]`
|
|
21
|
+
: `[failed - ${responseStatus}]`;
|
|
22
|
+
}
|
|
23
|
+
else if (failure) {
|
|
24
|
+
status = `[failed - ${failure.errorText}]`;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
status = '[pending]';
|
|
28
|
+
}
|
|
29
|
+
return status;
|
|
30
|
+
}
|
|
31
|
+
export function getFormattedHeaderValue(headers) {
|
|
32
|
+
const response = [];
|
|
33
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
34
|
+
response.push(`- ${name}:${value}`);
|
|
35
|
+
}
|
|
36
|
+
return response;
|
|
37
|
+
}
|
|
38
|
+
export async function getFormattedResponseBody(httpResponse, sizeLimit = BODY_CONTEXT_SIZE_LIMIT) {
|
|
39
|
+
try {
|
|
40
|
+
const responseBuffer = await httpResponse.buffer();
|
|
41
|
+
if (isUtf8(responseBuffer)) {
|
|
42
|
+
const responseAsTest = responseBuffer.toString('utf-8');
|
|
43
|
+
if (responseAsTest.length === 0) {
|
|
44
|
+
return `<empty response>`;
|
|
45
|
+
}
|
|
46
|
+
return `${getSizeLimitedString(responseAsTest, sizeLimit)}`;
|
|
47
|
+
}
|
|
48
|
+
return `<binary data>`;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return `<not available anymore>`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export async function getFormattedRequestBody(httpRequest, sizeLimit = BODY_CONTEXT_SIZE_LIMIT) {
|
|
55
|
+
if (httpRequest.hasPostData()) {
|
|
56
|
+
const data = httpRequest.postData();
|
|
57
|
+
if (data) {
|
|
58
|
+
return `${getSizeLimitedString(data, sizeLimit)}`;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const fetchData = await httpRequest.fetchPostData();
|
|
62
|
+
if (fetchData) {
|
|
63
|
+
return `${getSizeLimitedString(fetchData, sizeLimit)}`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return `<not available anymore>`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
function getSizeLimitedString(text, sizeLimit) {
|
|
73
|
+
if (text.length > sizeLimit) {
|
|
74
|
+
return `${text.substring(0, sizeLimit) + '... <truncated>'}`;
|
|
75
|
+
}
|
|
76
|
+
return `${text}`;
|
|
77
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export function formatSnapshotNode(root, snapshot, depth = 0) {
|
|
7
|
+
const chunks = [];
|
|
8
|
+
if (depth === 0) {
|
|
9
|
+
// Top-level content of the snapshot.
|
|
10
|
+
if (snapshot?.verbose &&
|
|
11
|
+
snapshot?.hasSelectedElement &&
|
|
12
|
+
!snapshot.selectedElementUid) {
|
|
13
|
+
chunks.push(`Note: there is a selected element in the DevTools Elements panel but it is not included into the current a11y tree snapshot.
|
|
14
|
+
Get a verbose snapshot to include all elements if you are interested in the selected element.\n\n`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const attributes = getAttributes(root);
|
|
18
|
+
const line = ' '.repeat(depth * 2) +
|
|
19
|
+
attributes.join(' ') +
|
|
20
|
+
(root.id === snapshot?.selectedElementUid
|
|
21
|
+
? ' [selected in the DevTools Elements panel]'
|
|
22
|
+
: '') +
|
|
23
|
+
'\n';
|
|
24
|
+
chunks.push(line);
|
|
25
|
+
for (const child of root.children) {
|
|
26
|
+
chunks.push(formatSnapshotNode(child, snapshot, depth + 1));
|
|
27
|
+
}
|
|
28
|
+
return chunks.join('');
|
|
29
|
+
}
|
|
30
|
+
function getAttributes(serializedAXNodeRoot) {
|
|
31
|
+
const attributes = [`uid=${serializedAXNodeRoot.id}`];
|
|
32
|
+
if (serializedAXNodeRoot.role) {
|
|
33
|
+
// To match representation in DevTools.
|
|
34
|
+
attributes.push(serializedAXNodeRoot.role === 'none'
|
|
35
|
+
? 'ignored'
|
|
36
|
+
: serializedAXNodeRoot.role);
|
|
37
|
+
}
|
|
38
|
+
if (serializedAXNodeRoot.name) {
|
|
39
|
+
attributes.push(`"${serializedAXNodeRoot.name}"`);
|
|
40
|
+
}
|
|
41
|
+
const excluded = new Set([
|
|
42
|
+
'id',
|
|
43
|
+
'role',
|
|
44
|
+
'name',
|
|
45
|
+
'elementHandle',
|
|
46
|
+
'children',
|
|
47
|
+
'backendNodeId',
|
|
48
|
+
]);
|
|
49
|
+
const booleanPropertyMap = {
|
|
50
|
+
disabled: 'disableable',
|
|
51
|
+
expanded: 'expandable',
|
|
52
|
+
focused: 'focusable',
|
|
53
|
+
selected: 'selectable',
|
|
54
|
+
};
|
|
55
|
+
for (const attr of Object.keys(serializedAXNodeRoot).sort()) {
|
|
56
|
+
if (excluded.has(attr)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const value = serializedAXNodeRoot[attr];
|
|
60
|
+
if (typeof value === 'boolean') {
|
|
61
|
+
if (booleanPropertyMap[attr]) {
|
|
62
|
+
attributes.push(booleanPropertyMap[attr]);
|
|
63
|
+
}
|
|
64
|
+
if (value) {
|
|
65
|
+
attributes.push(attr);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (typeof value === 'string' || typeof value === 'number') {
|
|
69
|
+
attributes.push(`${attr}="${value}"`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return attributes;
|
|
73
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2025 Google LLC
|
|
5
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { version } from 'node:process';
|
|
8
|
+
const [major, minor] = version.substring(1).split('.').map(Number);
|
|
9
|
+
if (major === 20 && minor < 19) {
|
|
10
|
+
console.error(`ERROR: \`chrome-devtools-mcp\` does not support Node ${process.version}. Please upgrade to Node 20.19.0 LTS or a newer LTS.`);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
if (major === 22 && minor < 12) {
|
|
14
|
+
console.error(`ERROR: \`chrome-devtools-mcp\` does not support Node ${process.version}. Please upgrade to Node 22.12.0 LTS or a newer LTS.`);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
if (major < 20) {
|
|
18
|
+
console.error(`ERROR: \`chrome-devtools-mcp\` does not support Node ${process.version}. Please upgrade to Node 20.19.0 LTS or a newer LTS.`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
await import('./main.js');
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import * as fs from 'node:fs';
|
|
7
|
+
import * as path from 'node:path';
|
|
8
|
+
const DESCRIPTIONS_PATH = path.join(import.meta.dirname, '../node_modules/chrome-devtools-frontend/front_end/models/issues_manager/descriptions');
|
|
9
|
+
let issueDescriptions = {};
|
|
10
|
+
/**
|
|
11
|
+
* Reads all issue descriptions from the filesystem into memory.
|
|
12
|
+
*/
|
|
13
|
+
export async function loadIssueDescriptions() {
|
|
14
|
+
if (Object.keys(issueDescriptions).length > 0) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const files = await fs.promises.readdir(DESCRIPTIONS_PATH);
|
|
18
|
+
const descriptions = {};
|
|
19
|
+
for (const file of files) {
|
|
20
|
+
if (!file.endsWith('.md')) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const content = await fs.promises.readFile(path.join(DESCRIPTIONS_PATH, file), 'utf-8');
|
|
24
|
+
descriptions[file] = content;
|
|
25
|
+
}
|
|
26
|
+
issueDescriptions = descriptions;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Gets an issue description from the in-memory cache.
|
|
30
|
+
* @param fileName The file name of the issue description.
|
|
31
|
+
* @returns The description of the issue, or null if it doesn't exist.
|
|
32
|
+
*/
|
|
33
|
+
export function getIssueDescription(fileName) {
|
|
34
|
+
return issueDescriptions[fileName] ?? null;
|
|
35
|
+
}
|
|
36
|
+
export const ISSUE_UTILS = {
|
|
37
|
+
loadIssueDescriptions,
|
|
38
|
+
getIssueDescription,
|
|
39
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import { debug } from './third_party/index.js';
|
|
8
|
+
const mcpDebugNamespace = 'mcp:log';
|
|
9
|
+
const namespacesToEnable = [
|
|
10
|
+
mcpDebugNamespace,
|
|
11
|
+
...(process.env['DEBUG'] ? [process.env['DEBUG']] : []),
|
|
12
|
+
];
|
|
13
|
+
export function saveLogsToFile(fileName) {
|
|
14
|
+
// Enable overrides everything so we need to add them
|
|
15
|
+
debug.enable(namespacesToEnable.join(','));
|
|
16
|
+
const logFile = fs.createWriteStream(fileName, { flags: 'a+' });
|
|
17
|
+
debug.log = function (...chunks) {
|
|
18
|
+
logFile.write(`${chunks.join(' ')}\n`);
|
|
19
|
+
};
|
|
20
|
+
logFile.on('error', function (error) {
|
|
21
|
+
console.error(`Error when opening/writing to log file: ${error.message}`);
|
|
22
|
+
logFile.end();
|
|
23
|
+
process.exit(1);
|
|
24
|
+
});
|
|
25
|
+
return logFile;
|
|
26
|
+
}
|
|
27
|
+
export const logger = debug(mcpDebugNamespace);
|