-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathbrowser.ts
More file actions
156 lines (141 loc) · 3.51 KB
/
browser.ts
File metadata and controls
156 lines (141 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import puppeteer, {
Browser,
ChromeReleaseChannel,
ConnectOptions,
LaunchOptions,
Target,
} from 'puppeteer-core';
import os from 'node:os';
import path from 'node:path';
import fs from 'fs';
let browser: Browser | undefined;
const ignoredPrefixes = new Set([
'chrome://',
'chrome-extension://',
'chrome-untrusted://',
'devtools://',
]);
function targetFilter(target: Target): boolean {
if (target.url() === 'chrome://newtab/') {
return true;
}
for (const prefix of ignoredPrefixes) {
if (target.url().startsWith(prefix)) {
return false;
}
}
return true;
}
const connectOptions: ConnectOptions = {
targetFilter,
// We do not expect any single CDP command to take more than 10sec.
protocolTimeout: 10_000,
};
async function ensureBrowserConnected(browserURL: string) {
if (browser?.connected) {
return browser;
}
browser = await puppeteer.connect({
...connectOptions,
browserURL,
defaultViewport: null,
});
return browser;
}
type McpLaunchOptions = {
executablePath?: string;
customDevTools?: string;
channel?: Channel;
userDataDir?: string;
headless: boolean;
isolated: boolean;
};
export async function launch(options: McpLaunchOptions): Promise<Browser> {
const {channel, executablePath, customDevTools, headless, isolated} = options;
const profileDirName =
channel && channel !== 'stable' ? `mcp-profile-${channel}` : 'mcp-profile';
let userDataDir = options.userDataDir;
if (!isolated && !userDataDir) {
userDataDir = path.join(
os.homedir(),
'.cache',
'chrome-devtools-mcp',
profileDirName,
);
await fs.promises.mkdir(userDataDir, {
recursive: true,
});
}
const args: LaunchOptions['args'] = [
'--remote-debugging-pipe',
'--no-first-run',
'--hide-crash-restore-bubble',
];
if (customDevTools) {
args.push(`--custom-devtools-frontend=file://${customDevTools}`);
}
let puppeterChannel: ChromeReleaseChannel | undefined;
if (!executablePath) {
puppeterChannel =
channel && channel !== 'stable'
? (`chrome-${channel}` as ChromeReleaseChannel)
: 'chrome';
}
try {
return await puppeteer.launch({
...connectOptions,
channel: puppeterChannel,
executablePath,
defaultViewport: null,
userDataDir,
pipe: true,
headless,
args,
});
} catch (error) {
// TODO: check browser logs for `Failed to create a ProcessSingleton for
// your profile directory` instead.
if (
userDataDir &&
(error as Error).message.includes(
'(Target.setDiscoverTargets): Target closed',
)
) {
throw new Error(
`The browser is already running for ${userDataDir}. Use --isolated to run multiple browser instances.`,
{
cause: error,
},
);
}
throw error;
}
}
async function ensureBrowserLaunched(
options: McpLaunchOptions,
): Promise<Browser> {
if (browser?.connected) {
return browser;
}
browser = await launch(options);
return browser;
}
export async function resolveBrowser(options: {
browserUrl?: string;
executablePath?: string;
customDevTools?: string;
channel?: Channel;
headless: boolean;
isolated: boolean;
}) {
const browser = options.browserUrl
? await ensureBrowserConnected(options.browserUrl)
: await ensureBrowserLaunched(options);
return browser;
}
export type Channel = 'stable' | 'canary' | 'beta' | 'dev';