chrome-devtools-mcp 1.10.1 → 1.11.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/build/src/BrowserManager.js +132 -13
- package/build/src/McpContext.js +188 -138
- package/build/src/McpPage.js +348 -96
- package/build/src/McpResponse.js +234 -76
- package/build/src/McpWorker.js +66 -0
- package/build/src/SlimMcpResponse.js +5 -1
- package/build/src/TextSnapshot.js +3 -2
- package/build/src/ToolHandler.js +80 -42
- package/build/src/bin/chrome-devtools-mcp-main.js +8 -7
- package/build/src/bin/chrome-devtools-mcp.js +1 -1
- package/build/src/bin/chrome-devtools.js +31 -41
- package/build/src/collectors/ServiceWorkerCollector.js +18 -11
- package/build/src/config/ConfigLocator.js +70 -0
- package/build/src/config/ConfigParser.js +264 -0
- package/build/src/config/ConfigWatcher.js +93 -0
- package/build/src/config/browser-options.js +2 -13
- 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 +410 -43
- package/build/src/config/mcp-options.js +156 -234
- package/build/src/config/puppeteer-options.js +65 -0
- package/build/src/config/tool-options.js +60 -0
- package/build/src/daemon/client.js +81 -41
- package/build/src/daemon/daemon.js +55 -45
- package/build/src/daemon/utils.js +44 -11
- package/build/src/devtools/DevToolsCommentBridge.js +43 -6
- package/build/src/devtools/DevtoolsUtils.js +3 -3
- package/build/src/devtools/McpHostBindingAdapter.js +0 -1
- package/build/src/formatters/CommentFormatter.js +4 -13
- package/build/src/formatters/ConsoleFormatter.js +1 -1
- package/build/src/formatters/CssFormatter.js +3 -2
- package/build/src/formatters/HeapSnapshotFormatter.js +104 -46
- package/build/src/formatters/IssueFormatter.js +33 -32
- package/build/src/formatters/NetworkFormatter.js +12 -11
- package/build/src/formatters/SnapshotFormatter.js +31 -4
- package/build/src/index.js +54 -9
- package/build/src/processors/ChunkedTraceParser.js +11 -11
- package/build/src/processors/HeapSnapshotManager.js +121 -84
- 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 +10 -397
- package/build/src/third_party/bundled-packages.json +7 -8
- package/build/src/third_party/devtools-formatter-worker.js +1193 -62
- package/build/src/third_party/devtools-heap-snapshot-worker.js +1126 -21
- package/build/src/third_party/index.js +8087 -19419
- 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 +3 -3
- package/build/src/tools/ToolDefinition.js +16 -4
- package/build/src/tools/comments.js +20 -1
- package/build/src/tools/css.js +4 -2
- package/build/src/tools/emulation.js +17 -4
- package/build/src/tools/input.js +8 -3
- package/build/src/tools/lighthouse.js +112 -4
- package/build/src/tools/memory.js +129 -28
- package/build/src/tools/pages.js +5 -2
- package/build/src/tools/performance.js +21 -3
- package/build/src/tools/screencast.js +71 -32
- package/build/src/tools/screenshot.js +29 -14
- package/build/src/tools/script.js +90 -37
- package/build/src/tools/slim/tools.js +6 -8
- package/build/src/tools/thirdPartyDeveloper.js +6 -4
- package/build/src/tools/tools.js +35 -22
- package/build/src/tools/webmcp.js +9 -6
- package/build/src/types.js +11 -0
- package/build/src/utils/WaitForHelper.js +14 -1
- package/build/src/utils/check-for-updates.js +1 -1
- package/build/src/utils/errorHandling.js +20 -0
- package/build/src/utils/logger.js +15 -5
- package/build/src/utils/text.js +15 -0
- package/build/src/utils/url.js +99 -3
- package/build/src/version.js +1 -1
- package/package.json +14 -10
- package/skills/a11y-debugging/SKILL.md +4 -4
- 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
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Options that change the default behavior of tools.
|
|
8
|
+
*/
|
|
9
|
+
export const toolOptions = {
|
|
10
|
+
screenshotFormat: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
default: 'png',
|
|
13
|
+
description: 'Override the default output format used by take_screenshot when the caller does not specify one. JPEG and WebP are ~3-5x smaller than PNG, which reduces transfer and storage size. To reduce context size use --screenshotMaxWidth / --screenshotMaxHeight, since image tokens scale with dimensions rather than encoded bytes. Unset preserves the existing default ("png").',
|
|
14
|
+
choices: ['jpeg', 'png', 'webp'],
|
|
15
|
+
reloadable: true,
|
|
16
|
+
},
|
|
17
|
+
screenshotQuality: {
|
|
18
|
+
type: 'number',
|
|
19
|
+
description: 'Override the default compression quality (0-100) used by take_screenshot for JPEG and WebP when the caller does not specify one. Lower values mean smaller files. Ignored for PNG. Unset preserves the Puppeteer default.',
|
|
20
|
+
reloadable: true,
|
|
21
|
+
coerce: (value) => {
|
|
22
|
+
if (value === undefined) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!Number.isInteger(value) || value < 0 || value > 100) {
|
|
26
|
+
throw new Error(`Invalid screenshotQuality ${value}. Expected an integer between 0 and 100.`);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
screenshotMaxWidth: {
|
|
32
|
+
type: 'number',
|
|
33
|
+
description: 'Maximum width in pixels for screenshots. If the captured image is wider, it is downscaled (preserving aspect ratio) before being returned. Reduces context size in AI conversations. Unset means no resize.',
|
|
34
|
+
reloadable: true,
|
|
35
|
+
coerce: (value) => {
|
|
36
|
+
if (value === undefined) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
40
|
+
throw new Error(`Invalid screenshotMaxWidth ${value}. Expected a positive integer.`);
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
screenshotMaxHeight: {
|
|
46
|
+
type: 'number',
|
|
47
|
+
description: 'Maximum height in pixels for screenshots. If the captured image is taller, it is downscaled (preserving aspect ratio) before being returned. Can be combined with --screenshot-max-width; the smaller scale factor wins. Unset means no resize.',
|
|
48
|
+
reloadable: true,
|
|
49
|
+
coerce: (value) => {
|
|
50
|
+
if (value === undefined) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
54
|
+
throw new Error(`Invalid screenshotMaxHeight ${value}. Expected a positive integer.`);
|
|
55
|
+
}
|
|
56
|
+
return value;
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=tool-options.js.map
|
|
@@ -6,55 +6,51 @@
|
|
|
6
6
|
import { spawn } from 'node:child_process';
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import net from 'node:net';
|
|
9
|
+
import path from 'node:path';
|
|
9
10
|
import { PipeTransport } from '../third_party/index.js';
|
|
10
11
|
import { getTempFilePath } from '../utils/files.js';
|
|
11
12
|
import { logger, puppeteerLogger } from '../utils/logger.js';
|
|
12
|
-
import { DAEMON_SCRIPT_PATH, getSocketPath, getPidFilePath, isDaemonRunning, } from './utils.js';
|
|
13
|
+
import { DAEMON_SCRIPT_PATH, getDaemonPid, getSocketPath, getPidFilePath, isDaemonRunning, } from './utils.js';
|
|
13
14
|
const FILE_TIMEOUT = 10_000;
|
|
14
15
|
const READY_CHECK_INTERVAL = 100;
|
|
15
16
|
const READY_CHECK_COMMAND_TIMEOUT = 1_000;
|
|
16
|
-
/**
|
|
17
|
-
* Waits for a file to be created and populated (removed = false) or removed (removed = true).
|
|
18
|
-
*/
|
|
19
|
-
function waitForFile(filePath, removed = false) {
|
|
20
|
-
return new Promise((resolve, reject) => {
|
|
21
|
-
const check = () => {
|
|
22
|
-
const exists = fs.existsSync(filePath);
|
|
23
|
-
if (removed) {
|
|
24
|
-
return !exists;
|
|
25
|
-
}
|
|
26
|
-
if (!exists) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
try {
|
|
30
|
-
return fs.statSync(filePath).size > 0;
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
if (check()) {
|
|
37
|
-
resolve();
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
const timer = setTimeout(() => {
|
|
41
|
-
fs.unwatchFile(filePath);
|
|
42
|
-
reject(new Error(`Timeout: file ${filePath} ${removed ? 'not removed' : 'not found'} within ${FILE_TIMEOUT}ms`));
|
|
43
|
-
}, FILE_TIMEOUT);
|
|
44
|
-
fs.watchFile(filePath, { interval: 500 }, () => {
|
|
45
|
-
if (check()) {
|
|
46
|
-
clearTimeout(timer);
|
|
47
|
-
fs.unwatchFile(filePath);
|
|
48
|
-
resolve();
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
17
|
function delay(ms) {
|
|
54
18
|
return new Promise(resolve => {
|
|
55
19
|
setTimeout(resolve, ms);
|
|
56
20
|
});
|
|
57
21
|
}
|
|
22
|
+
async function waitFor(check, timeoutMessage) {
|
|
23
|
+
const deadline = Date.now() + FILE_TIMEOUT;
|
|
24
|
+
while (Date.now() < deadline) {
|
|
25
|
+
if (check()) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const timeLeft = deadline - Date.now();
|
|
29
|
+
if (timeLeft > 0) {
|
|
30
|
+
await delay(Math.min(READY_CHECK_INTERVAL, timeLeft));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (check()) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
throw new Error(timeoutMessage);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Waits for a file to be created and populated.
|
|
40
|
+
*/
|
|
41
|
+
async function waitForFile(filePath) {
|
|
42
|
+
await waitFor(() => {
|
|
43
|
+
if (!fs.existsSync(filePath)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
return fs.statSync(filePath).size > 0;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}, `Timeout: file ${filePath} not found within ${FILE_TIMEOUT}ms`);
|
|
53
|
+
}
|
|
58
54
|
async function waitForDaemonReady(sessionId) {
|
|
59
55
|
const deadline = Date.now() + FILE_TIMEOUT;
|
|
60
56
|
let lastError;
|
|
@@ -84,6 +80,13 @@ export async function startDaemon(mcpArgs = [], sessionId) {
|
|
|
84
80
|
}
|
|
85
81
|
const pidFilePath = getPidFilePath(sessionId);
|
|
86
82
|
if (fs.existsSync(pidFilePath)) {
|
|
83
|
+
if (process.platform !== 'win32') {
|
|
84
|
+
const pidDir = path.dirname(pidFilePath);
|
|
85
|
+
if (fs.lstatSync(pidDir).isSymbolicLink() ||
|
|
86
|
+
fs.lstatSync(pidFilePath).isSymbolicLink()) {
|
|
87
|
+
throw new Error(`Refusing to remove daemon PID file through a symbolic link: ${pidFilePath}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
87
90
|
fs.unlinkSync(pidFilePath);
|
|
88
91
|
}
|
|
89
92
|
logger?.('Starting daemon...', ...mcpArgs);
|
|
@@ -111,7 +114,7 @@ export async function sendCommand(command, sessionId, timeout = SEND_COMMAND_TIM
|
|
|
111
114
|
const socket = net.createConnection({
|
|
112
115
|
path: socketPath,
|
|
113
116
|
});
|
|
114
|
-
return new Promise((resolve, reject) => {
|
|
117
|
+
return await new Promise((resolve, reject) => {
|
|
115
118
|
const timer = setTimeout(() => {
|
|
116
119
|
socket.destroy();
|
|
117
120
|
reject(new Error('Timeout waiting for daemon response'));
|
|
@@ -120,6 +123,7 @@ export async function sendCommand(command, sessionId, timeout = SEND_COMMAND_TIM
|
|
|
120
123
|
transport.onmessage = async (message) => {
|
|
121
124
|
clearTimeout(timer);
|
|
122
125
|
logger?.('onmessage', message);
|
|
126
|
+
socket.end();
|
|
123
127
|
resolve(JSON.parse(message));
|
|
124
128
|
};
|
|
125
129
|
socket.on('error', error => {
|
|
@@ -141,9 +145,45 @@ export async function stopDaemon(sessionId) {
|
|
|
141
145
|
logger?.('Daemon is not running');
|
|
142
146
|
return;
|
|
143
147
|
}
|
|
148
|
+
const pid = getDaemonPid(sessionId);
|
|
144
149
|
const pidFilePath = getPidFilePath(sessionId);
|
|
145
|
-
|
|
146
|
-
|
|
150
|
+
try {
|
|
151
|
+
await sendCommand({ method: 'stop' }, sessionId);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
logger?.('Failed to stop daemon via socket, sending SIGTERM:', error);
|
|
155
|
+
if (pid) {
|
|
156
|
+
try {
|
|
157
|
+
process.kill(pid, 'SIGTERM');
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
// Ignore if process already exited.
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
await waitFor(() => !isDaemonRunning(sessionId), `Timeout: daemon (pid ${pid}) did not exit within ${FILE_TIMEOUT}ms`);
|
|
166
|
+
fs.rmSync(pidFilePath, { force: true });
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
if (pid) {
|
|
170
|
+
try {
|
|
171
|
+
process.kill(pid, 'SIGKILL');
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
// Ignore if process already exited.
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
fs.unlinkSync(pidFilePath);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// Ignore if file already removed.
|
|
182
|
+
}
|
|
183
|
+
if (isDaemonRunning(sessionId)) {
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
147
187
|
}
|
|
148
188
|
export async function verifyDaemonVersion(sessionId, cliVersion) {
|
|
149
189
|
if (!isDaemonRunning(sessionId)) {
|
|
@@ -4,15 +4,23 @@
|
|
|
4
4
|
* Copyright 2026 Google LLC
|
|
5
5
|
* SPDX-License-Identifier: Apache-2.0
|
|
6
6
|
*/
|
|
7
|
+
import '../utils/polyfill.js';
|
|
7
8
|
import fs, { constants, openSync, writeSync, closeSync } from 'node:fs';
|
|
8
9
|
import { createServer } from 'node:net';
|
|
9
10
|
import os from 'node:os';
|
|
10
11
|
import path from 'node:path';
|
|
11
12
|
import process from 'node:process';
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
13
|
+
import { BrowserManager } from '../BrowserManager.js';
|
|
14
|
+
import { mcpOptions } from '../config/mcp-options.js';
|
|
15
|
+
import { ConfigParser } from '../config/ConfigParser.js';
|
|
16
|
+
import { McpServer } from '../index.js';
|
|
17
|
+
import { ClearcutLogger } from '../telemetry/ClearcutLogger.js';
|
|
18
|
+
import { computeFlagUsage } from '../telemetry/flagUtils.js';
|
|
19
|
+
import { PipeTransport } from '../third_party/index.js';
|
|
20
|
+
import { logger, puppeteerLogger, saveLogsToFile } from '../utils/logger.js';
|
|
21
|
+
import { setupUnhandledRejectionHandler } from '../utils/errorHandling.js';
|
|
14
22
|
import { VERSION } from '../version.js';
|
|
15
|
-
import { DAEMON_CLIENT_NAME, getPidFilePath, getSocketPath,
|
|
23
|
+
import { DAEMON_CLIENT_NAME, getPidFilePath, getSocketPath, IS_WINDOWS, isDaemonRunning, assertValidSessionId, } from './utils.js';
|
|
16
24
|
const sessionId = process.env.CHROME_DEVTOOLS_MCP_SESSION_ID || '';
|
|
17
25
|
assertValidSessionId(sessionId);
|
|
18
26
|
logger?.(`Daemon sessionId: ${sessionId}`);
|
|
@@ -28,13 +36,18 @@ try {
|
|
|
28
36
|
if (os.platform() !== 'win32') {
|
|
29
37
|
// POSIX specific checks
|
|
30
38
|
try {
|
|
31
|
-
const stats = fs.
|
|
32
|
-
// 1.
|
|
39
|
+
const stats = fs.lstatSync(pidDir);
|
|
40
|
+
// 1. Reject symlinked runtime directories before checking ownership.
|
|
41
|
+
if (stats.isSymbolicLink()) {
|
|
42
|
+
console.error(`[MCP Daemon] Critical error: PID directory ${pidDir} must not be a symbolic link. Possible tampering.`);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
// 2. Check Ownership: Ensure the directory is owned by the current user.
|
|
33
46
|
if (stats.uid !== currentUserUid) {
|
|
34
47
|
console.error(`[MCP Daemon] Critical error: PID directory ${pidDir} is not owned by the current user (Expected: ${currentUserUid}, Found: ${stats.uid}). Possible tampering.`);
|
|
35
48
|
process.exit(1);
|
|
36
49
|
}
|
|
37
|
-
//
|
|
50
|
+
// 3. Check Permissions: Ensure the directory is not group or world-writable.
|
|
38
51
|
// Mode is a number, e.g., 0o700. We check if bits for group/world write are set.
|
|
39
52
|
const mode = stats.mode;
|
|
40
53
|
if (mode & constants.S_IWGRP || mode & constants.S_IWOTH) {
|
|
@@ -85,43 +98,39 @@ logger?.(`Writing ${process.pid.toString()} to ${pidFilePath}`);
|
|
|
85
98
|
const socketPath = getSocketPath(sessionId);
|
|
86
99
|
const startDate = new Date();
|
|
87
100
|
const mcpServerArgs = process.argv.slice(2);
|
|
88
|
-
let
|
|
89
|
-
let mcpTransport = null;
|
|
101
|
+
let mcpServer = null;
|
|
90
102
|
let server = null;
|
|
91
|
-
async function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
103
|
+
async function setupMCPServer() {
|
|
104
|
+
logger?.(`Starting Chrome DevTools MCP Server v${VERSION}`);
|
|
105
|
+
const configParser = new ConfigParser(VERSION);
|
|
106
|
+
const args = configParser.parse();
|
|
107
|
+
const logFile = args.logFile ? saveLogsToFile(args.logFile) : undefined;
|
|
108
|
+
const browserManager = new BrowserManager(args, {
|
|
109
|
+
logFile,
|
|
98
110
|
});
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}, {
|
|
103
|
-
capabilities: {},
|
|
111
|
+
mcpServer = await McpServer.from(args, {
|
|
112
|
+
browserManager,
|
|
113
|
+
logFile,
|
|
104
114
|
});
|
|
105
|
-
|
|
106
|
-
|
|
115
|
+
ClearcutLogger.get()?.setClientName(DAEMON_CLIENT_NAME);
|
|
116
|
+
void ClearcutLogger.get()?.logDailyActiveIfNeeded();
|
|
117
|
+
void ClearcutLogger.get()?.logServerStart(computeFlagUsage(args, mcpOptions));
|
|
107
118
|
}
|
|
108
119
|
async function handleRequest(msg) {
|
|
109
120
|
try {
|
|
110
121
|
if (msg.method === 'invoke_tool') {
|
|
111
|
-
|
|
112
|
-
|
|
122
|
+
await started;
|
|
123
|
+
if (!mcpServer) {
|
|
124
|
+
throw new Error('MCP server not initialized');
|
|
113
125
|
}
|
|
114
126
|
const { tool, args } = msg;
|
|
115
|
-
const result =
|
|
116
|
-
name: tool,
|
|
117
|
-
arguments: args || {},
|
|
118
|
-
}));
|
|
127
|
+
const result = await mcpServer.callTool(tool, args);
|
|
119
128
|
return {
|
|
120
129
|
success: true,
|
|
121
130
|
result: JSON.stringify(result),
|
|
122
131
|
};
|
|
123
132
|
}
|
|
124
|
-
|
|
133
|
+
if (msg.method === 'stop') {
|
|
125
134
|
// Ensure we are not interrupting in-progress starting.
|
|
126
135
|
await started;
|
|
127
136
|
// Trigger cleanup asynchronously.
|
|
@@ -133,7 +142,7 @@ async function handleRequest(msg) {
|
|
|
133
142
|
message: 'stopping',
|
|
134
143
|
};
|
|
135
144
|
}
|
|
136
|
-
|
|
145
|
+
if (msg.method === 'status') {
|
|
137
146
|
await started;
|
|
138
147
|
const statusResult = {
|
|
139
148
|
pid: process.pid,
|
|
@@ -192,8 +201,7 @@ async function startSocketServer() {
|
|
|
192
201
|
}, async () => {
|
|
193
202
|
console.log(`Daemon server listening on ${socketPath}`);
|
|
194
203
|
try {
|
|
195
|
-
|
|
196
|
-
await setupMCPClient();
|
|
204
|
+
await setupMCPServer();
|
|
197
205
|
resolve();
|
|
198
206
|
}
|
|
199
207
|
catch (err) {
|
|
@@ -206,23 +214,23 @@ async function startSocketServer() {
|
|
|
206
214
|
});
|
|
207
215
|
});
|
|
208
216
|
}
|
|
217
|
+
let isCleaningUp = false;
|
|
209
218
|
async function cleanup(exitCode = 0) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
await mcpClient?.close();
|
|
213
|
-
}
|
|
214
|
-
catch (error) {
|
|
215
|
-
logger?.('Error closing MCP client:', error);
|
|
219
|
+
if (isCleaningUp) {
|
|
220
|
+
return;
|
|
216
221
|
}
|
|
222
|
+
isCleaningUp = true;
|
|
223
|
+
console.log('Cleaning up daemon...');
|
|
217
224
|
try {
|
|
218
|
-
await
|
|
225
|
+
await mcpServer?.close();
|
|
219
226
|
}
|
|
220
227
|
catch (error) {
|
|
221
|
-
logger?.('Error closing MCP
|
|
228
|
+
logger?.('Error closing MCP server:', error);
|
|
222
229
|
}
|
|
223
|
-
|
|
230
|
+
const activeServer = server;
|
|
231
|
+
if (activeServer) {
|
|
224
232
|
await new Promise(resolve => {
|
|
225
|
-
|
|
233
|
+
activeServer.close(() => resolve());
|
|
226
234
|
});
|
|
227
235
|
}
|
|
228
236
|
if (!IS_WINDOWS) {
|
|
@@ -234,9 +242,12 @@ async function cleanup(exitCode = 0) {
|
|
|
234
242
|
}
|
|
235
243
|
}
|
|
236
244
|
logger?.(`unlinking ${pidFilePath}`);
|
|
237
|
-
|
|
245
|
+
try {
|
|
238
246
|
fs.unlinkSync(pidFilePath);
|
|
239
247
|
}
|
|
248
|
+
catch {
|
|
249
|
+
// ignore errors
|
|
250
|
+
}
|
|
240
251
|
process.exit(exitCode);
|
|
241
252
|
}
|
|
242
253
|
// Handle shutdown signals
|
|
@@ -254,8 +265,7 @@ process.on('uncaughtException', error => {
|
|
|
254
265
|
logger?.('Uncaught exception:', error);
|
|
255
266
|
void cleanup(1);
|
|
256
267
|
});
|
|
257
|
-
|
|
258
|
-
logger?.('Unhandled rejection:', error);
|
|
268
|
+
setupUnhandledRejectionHandler(() => {
|
|
259
269
|
void cleanup(1);
|
|
260
270
|
});
|
|
261
271
|
// Start the server
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
* Copyright 2026 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
+
import childProcess from 'node:child_process';
|
|
6
7
|
import fs from 'node:fs';
|
|
7
8
|
import os from 'node:os';
|
|
8
9
|
import path from 'node:path';
|
|
9
10
|
import process from 'node:process';
|
|
10
11
|
import { logger } from '../utils/logger.js';
|
|
11
12
|
export const DAEMON_SCRIPT_PATH = path.join(import.meta.dirname, 'daemon.js');
|
|
12
|
-
export const INDEX_SCRIPT_PATH = path.join(import.meta.dirname, '..', 'bin', 'chrome-devtools-mcp.js');
|
|
13
13
|
const APP_NAME = 'chrome-devtools-mcp';
|
|
14
14
|
export const DAEMON_CLIENT_NAME = 'chrome-devtools-cli-daemon';
|
|
15
15
|
export function assertValidSessionId(sessionId) {
|
|
@@ -23,7 +23,6 @@ export function assertValidSessionId(sessionId) {
|
|
|
23
23
|
// Using these paths due to strict limits on the POSIX socket path length.
|
|
24
24
|
export function getSocketPath(sessionId) {
|
|
25
25
|
assertValidSessionId(sessionId);
|
|
26
|
-
const uid = os.userInfo().uid;
|
|
27
26
|
const username = os.userInfo().username;
|
|
28
27
|
const suffix = sessionId ? `-${sessionId}` : '';
|
|
29
28
|
const appName = APP_NAME + suffix;
|
|
@@ -33,14 +32,7 @@ export function getSocketPath(sessionId) {
|
|
|
33
32
|
// Append username to prevent cross-user named pipe squatting
|
|
34
33
|
return path.join('\\\\.\\pipe', `${appName}-${username}`, 'server.sock');
|
|
35
34
|
}
|
|
36
|
-
|
|
37
|
-
if (process.env.XDG_RUNTIME_DIR) {
|
|
38
|
-
return path.join(process.env.XDG_RUNTIME_DIR, appName, 'server.sock');
|
|
39
|
-
}
|
|
40
|
-
// 2. macOS/Unix Fallback: Use /tmp/
|
|
41
|
-
// We use /tmp/ because it is much shorter than ~/Library/Application Support/
|
|
42
|
-
// and keeps us well under the 104-character limit.
|
|
43
|
-
return path.join('/tmp', `${appName}-${uid}.sock`);
|
|
35
|
+
return path.join(getRuntimeHome(sessionId), 'server.sock');
|
|
44
36
|
}
|
|
45
37
|
export function getRuntimeHome(sessionId) {
|
|
46
38
|
assertValidSessionId(sessionId);
|
|
@@ -86,16 +78,57 @@ export function getDaemonPid(sessionId) {
|
|
|
86
78
|
return null;
|
|
87
79
|
}
|
|
88
80
|
}
|
|
81
|
+
const verifiedDaemonPids = new Map();
|
|
82
|
+
function getProcessCommandLine(pid) {
|
|
83
|
+
if (process.platform === 'linux' && fs.existsSync('/proc')) {
|
|
84
|
+
return fs
|
|
85
|
+
.readFileSync(`/proc/${pid}/cmdline`, 'utf-8')
|
|
86
|
+
.replaceAll('\0', ' ');
|
|
87
|
+
}
|
|
88
|
+
if (IS_WINDOWS) {
|
|
89
|
+
return childProcess.execFileSync('powershell.exe', [
|
|
90
|
+
'-NoProfile',
|
|
91
|
+
'-NonInteractive',
|
|
92
|
+
'-Command',
|
|
93
|
+
`(Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}").CommandLine`,
|
|
94
|
+
], {
|
|
95
|
+
encoding: 'utf-8',
|
|
96
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
97
|
+
windowsHide: true,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return childProcess.execFileSync('ps', ['-p', String(pid), '-o', 'command='], {
|
|
101
|
+
encoding: 'utf-8',
|
|
102
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function isDaemonProcess(pid) {
|
|
106
|
+
const cached = verifiedDaemonPids.get(pid);
|
|
107
|
+
if (cached !== undefined) {
|
|
108
|
+
return cached;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const commandLine = getProcessCommandLine(pid).trim();
|
|
112
|
+
const isDaemon = commandLine === '' || /[\\/]daemon[\\/]daemon\.js/.test(commandLine);
|
|
113
|
+
verifiedDaemonPids.set(pid, isDaemon);
|
|
114
|
+
return isDaemon;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
verifiedDaemonPids.set(pid, true);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
89
121
|
export function isDaemonRunning(sessionId) {
|
|
90
122
|
assertValidSessionId(sessionId);
|
|
91
123
|
const pid = getDaemonPid(sessionId);
|
|
92
124
|
if (pid) {
|
|
93
125
|
try {
|
|
94
126
|
process.kill(pid, 0); // Throws if process doesn't exist
|
|
95
|
-
return
|
|
127
|
+
return isDaemonProcess(pid);
|
|
96
128
|
}
|
|
97
129
|
catch {
|
|
98
130
|
// Process is dead, stale PID file. Proceed with startup.
|
|
131
|
+
verifiedDaemonPids.delete(pid);
|
|
99
132
|
}
|
|
100
133
|
}
|
|
101
134
|
return false;
|
|
@@ -21,31 +21,68 @@ export class DevToolsCommentBridge {
|
|
|
21
21
|
isAttached(devtoolsPage) {
|
|
22
22
|
return this.#attachedPages.has(devtoolsPage);
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Attaches to the DevTools page if it exposes the CD4A bridge.
|
|
26
|
+
*
|
|
27
|
+
* Returns false if the bridge does not exist, e.g., because the connected
|
|
28
|
+
* Chrome version does not support DevTools comments or the feature is
|
|
29
|
+
* disabled, or if the comment event binding could not be installed.
|
|
30
|
+
* Negative results are not cached so that a DevTools window that has not
|
|
31
|
+
* finished loading yet is checked again on the next call.
|
|
32
|
+
*/
|
|
24
33
|
async attach(devtoolsPage) {
|
|
25
34
|
if (this.#attachedPages.has(devtoolsPage)) {
|
|
26
|
-
return;
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const hasBridge = await devtoolsPage.evaluate(() => {
|
|
39
|
+
return Boolean(window.universe?.cd4aBridge);
|
|
40
|
+
});
|
|
41
|
+
if (!hasBridge) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
logger?.('DevToolsCommentBridge: evaluate failed', e);
|
|
47
|
+
return false;
|
|
27
48
|
}
|
|
28
|
-
this.#attachedPages.add(devtoolsPage);
|
|
29
49
|
try {
|
|
30
50
|
await devtoolsPage.exposeFunction('__onDevToolsCommentEvent', () => {
|
|
31
51
|
this.#handleCommentEvent();
|
|
32
52
|
});
|
|
33
53
|
}
|
|
34
54
|
catch (e) {
|
|
55
|
+
// The binding may already exist, e.g., exposed by a previous bridge.
|
|
56
|
+
// Whether it is usable is checked in the page below.
|
|
35
57
|
logger?.('DevToolsCommentBridge: exposeFunction already bound or failed', e);
|
|
36
58
|
}
|
|
59
|
+
let attached = false;
|
|
37
60
|
try {
|
|
38
|
-
await devtoolsPage.evaluate(() => {
|
|
61
|
+
attached = await devtoolsPage.evaluate(() => {
|
|
62
|
+
const cd4aBridge = window.universe?.cd4aBridge;
|
|
63
|
+
if (!cd4aBridge ||
|
|
64
|
+
typeof window.__onDevToolsCommentEvent !== 'function') {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (window.__onDevToolsCommentListener) {
|
|
68
|
+
cd4aBridge.removeEventListener('CommentThreadsChanged', window.__onDevToolsCommentListener);
|
|
69
|
+
}
|
|
39
70
|
window.__onDevToolsCommentListener = () => {
|
|
40
71
|
window.__onDevToolsCommentEvent?.();
|
|
41
72
|
};
|
|
42
|
-
|
|
43
|
-
|
|
73
|
+
cd4aBridge.addEventListener('CommentThreadsChanged', window.__onDevToolsCommentListener);
|
|
74
|
+
cd4aBridge.setAgentAttached(true);
|
|
75
|
+
return true;
|
|
44
76
|
});
|
|
45
77
|
}
|
|
46
78
|
catch (e) {
|
|
47
79
|
logger?.('DevToolsCommentBridge: evaluate failed', e);
|
|
48
80
|
}
|
|
81
|
+
if (!attached) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
this.#attachedPages.add(devtoolsPage);
|
|
85
|
+
return true;
|
|
49
86
|
}
|
|
50
87
|
async getComments(devtoolsPage) {
|
|
51
88
|
return await devtoolsPage.evaluate(() => {
|
|
@@ -70,7 +107,7 @@ export class DevToolsCommentBridge {
|
|
|
70
107
|
try {
|
|
71
108
|
const result = page.evaluate(() => {
|
|
72
109
|
if (window.__onDevToolsCommentListener) {
|
|
73
|
-
window.universe?.cd4aBridge?.removeEventListener
|
|
110
|
+
window.universe?.cd4aBridge?.removeEventListener('CommentThreadsChanged', window.__onDevToolsCommentListener);
|
|
74
111
|
delete window.__onDevToolsCommentListener;
|
|
75
112
|
}
|
|
76
113
|
window.universe?.cd4aBridge?.setAgentAttached(false);
|
|
@@ -194,7 +194,7 @@ export class SymbolizedError {
|
|
|
194
194
|
static async fromError(opts) {
|
|
195
195
|
const details = await SymbolizedError.#getExceptionDetails(opts.devTools, opts.error, opts.targetId);
|
|
196
196
|
if (details) {
|
|
197
|
-
return SymbolizedError.fromDetails({
|
|
197
|
+
return await SymbolizedError.fromDetails({
|
|
198
198
|
details,
|
|
199
199
|
devTools: opts.devTools,
|
|
200
200
|
targetId: opts.targetId,
|
|
@@ -252,7 +252,7 @@ export async function createStackTraceForConsoleMessage(devTools, consoleMessage
|
|
|
252
252
|
const message = consoleMessage;
|
|
253
253
|
const rawStackTrace = message._rawStackTrace();
|
|
254
254
|
if (rawStackTrace) {
|
|
255
|
-
return createStackTrace(devTools, rawStackTrace, message._targetId());
|
|
255
|
+
return await createStackTrace(devTools, rawStackTrace, message._targetId());
|
|
256
256
|
}
|
|
257
257
|
return undefined;
|
|
258
258
|
}
|
|
@@ -282,7 +282,7 @@ export async function createStackTrace(devTools, rawStackTrace, targetId) {
|
|
|
282
282
|
.catch()));
|
|
283
283
|
const binding = devTools.universe.context.get(DevTools.DebuggerWorkspaceBinding);
|
|
284
284
|
// DevTools uses branded types for ScriptId and others. Casting the puppeteer protocol type to the DevTools protocol type is safe.
|
|
285
|
-
return binding.createStackTraceFromProtocolRuntime(rawStackTrace, target);
|
|
285
|
+
return await binding.createStackTraceFromProtocolRuntime(rawStackTrace, target);
|
|
286
286
|
}
|
|
287
287
|
// Waits indefinitely for the script so pair it with Promise.race.
|
|
288
288
|
async function waitForScript(model, scriptId, signal) {
|
|
@@ -9,16 +9,10 @@ function formatCommentThread(thread) {
|
|
|
9
9
|
`- Comment: ${thread.text}`,
|
|
10
10
|
];
|
|
11
11
|
if (thread.elementUid) {
|
|
12
|
-
lines.push(`-
|
|
12
|
+
lines.push(`- Related element uid: ${thread.elementUid}`);
|
|
13
13
|
}
|
|
14
14
|
if (thread.reqid !== undefined) {
|
|
15
|
-
lines.push(`-
|
|
16
|
-
}
|
|
17
|
-
if (thread.editor) {
|
|
18
|
-
const location = thread.editor.filePath
|
|
19
|
-
? `${thread.editor.filePath}:${thread.editor.lineNumber}`
|
|
20
|
-
: `line ${thread.editor.lineNumber}`;
|
|
21
|
-
lines.push(`- Editor location: ${location}`);
|
|
15
|
+
lines.push(`- Related network reqid: ${thread.reqid}`);
|
|
22
16
|
}
|
|
23
17
|
return lines.join('\n');
|
|
24
18
|
}
|
|
@@ -44,8 +38,8 @@ export class CommentFormatter {
|
|
|
44
38
|
for (const thread of threads) {
|
|
45
39
|
let elementUid;
|
|
46
40
|
const resolveBackendNodeId = options?.resolveBackendNodeId;
|
|
47
|
-
if (thread.backendNodeId !== undefined && resolveBackendNodeId) {
|
|
48
|
-
elementUid = await resolveBackendNodeId(thread.backendNodeId);
|
|
41
|
+
if (thread.node?.backendNodeId !== undefined && resolveBackendNodeId) {
|
|
42
|
+
elementUid = await resolveBackendNodeId(thread.node.backendNodeId);
|
|
49
43
|
}
|
|
50
44
|
let reqid;
|
|
51
45
|
const resolveCdpRequestId = options?.resolveCdpRequestId;
|
|
@@ -62,9 +56,6 @@ export class CommentFormatter {
|
|
|
62
56
|
if (reqid !== undefined) {
|
|
63
57
|
item.reqid = reqid;
|
|
64
58
|
}
|
|
65
|
-
if (thread.editor) {
|
|
66
|
-
item.editor = thread.editor;
|
|
67
|
-
}
|
|
68
59
|
structuredThreads.push(item);
|
|
69
60
|
}
|
|
70
61
|
return new CommentFormatter(structuredThreads);
|