@darbotlabs/darbot-browser-mcp 0.1.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/cli.js ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import './lib/program.js';
package/config.d.ts ADDED
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import type * as playwright from 'playwright';
18
+
19
+ export type ToolCapability = 'core' | 'tabs' | 'pdf' | 'history' | 'wait' | 'files' | 'install' | 'testing';
20
+
21
+ export type Config = {
22
+ /**
23
+ * The browser to use.
24
+ */
25
+ browser?: {
26
+ /**
27
+ * Use browser agent (experimental).
28
+ */
29
+ browserAgent?: string;
30
+
31
+ /**
32
+ * The type of browser to use.
33
+ */
34
+ browserName?: 'chromium' | 'firefox' | 'webkit';
35
+
36
+ /**
37
+ * Keep the browser profile in memory, do not save it to disk.
38
+ */
39
+ isolated?: boolean;
40
+
41
+ /**
42
+ * Path to a user data directory for browser profile persistence.
43
+ * Temporary directory is created by default.
44
+ */
45
+ userDataDir?: string;
46
+
47
+ /**
48
+ * Launch options passed to
49
+ * @see https://playwright.dev/docs/api/class-browsertype#browser-type-launch-persistent-context
50
+ *
51
+ * This is useful for settings options like `channel`, `headless`, `executablePath`, etc.
52
+ */
53
+ launchOptions?: playwright.LaunchOptions;
54
+
55
+ /**
56
+ * Context options for the browser context.
57
+ *
58
+ * This is useful for settings options like `viewport`.
59
+ */
60
+ contextOptions?: playwright.BrowserContextOptions;
61
+
62
+ /**
63
+ * Chrome DevTools Protocol endpoint to connect to an existing browser instance in case of Chromium family browsers.
64
+ */
65
+ cdpEndpoint?: string;
66
+
67
+ /**
68
+ * Remote endpoint to connect to an existing Playwright server.
69
+ */
70
+ remoteEndpoint?: string;
71
+ },
72
+
73
+ server?: {
74
+ /**
75
+ * The port to listen on for SSE or MCP transport.
76
+ */
77
+ port?: number;
78
+
79
+ /**
80
+ * The host to bind the server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.
81
+ */
82
+ host?: string;
83
+ },
84
+
85
+ /**
86
+ * List of enabled tool capabilities. Possible values:
87
+ * - 'core': Core browser automation features.
88
+ * - 'tabs': Tab management features.
89
+ * - 'pdf': PDF generation and manipulation.
90
+ * - 'history': Browser history access.
91
+ * - 'wait': Wait and timing utilities.
92
+ * - 'files': File upload/download support.
93
+ * - 'install': Browser installation utilities.
94
+ */
95
+ capabilities?: ToolCapability[];
96
+
97
+ /**
98
+ * Run server that uses screenshots (Aria snapshots are used by default).
99
+ */
100
+ vision?: boolean;
101
+
102
+ /**
103
+ * Whether to save the Playwright trace of the session into the output directory.
104
+ */
105
+ saveTrace?: boolean;
106
+
107
+ /**
108
+ * The directory to save output files.
109
+ */
110
+ outputDir?: string;
111
+
112
+ network?: {
113
+ /**
114
+ * List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
115
+ */
116
+ allowedOrigins?: string[];
117
+
118
+ /**
119
+ * List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
120
+ */
121
+ blockedOrigins?: string[];
122
+ };
123
+
124
+ /**
125
+ * Whether to send image responses to the client. Can be "allow", "omit", or "auto". Defaults to "auto", which sends images if the client can display them.
126
+ */
127
+ imageResponses?: 'allow' | 'omit' | 'auto';
128
+ };
package/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
19
+ import type { Config } from './config.js';
20
+ import type { BrowserContext } from 'playwright';
21
+
22
+ export type Connection = {
23
+ server: Server;
24
+ close(): Promise<void>;
25
+ };
26
+
27
+ export declare function createConnection(config?: Config, contextGetter?: () => Promise<BrowserContext>): Promise<Connection>;
28
+ export {};
package/index.js ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { createConnection } from './lib/index.js';
19
+ export { createConnection };
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import fs from 'node:fs';
17
+ import net from 'node:net';
18
+ import path from 'node:path';
19
+ import os from 'node:os';
20
+ import debug from 'debug';
21
+ import * as playwright from 'playwright';
22
+ import { userDataDir } from './fileUtils.js';
23
+ const testDebug = debug('pw:mcp:test');
24
+ export function contextFactory(browserConfig) {
25
+ if (browserConfig.remoteEndpoint)
26
+ return new RemoteContextFactory(browserConfig);
27
+ if (browserConfig.cdpEndpoint)
28
+ return new CdpContextFactory(browserConfig);
29
+ if (browserConfig.isolated)
30
+ return new IsolatedContextFactory(browserConfig);
31
+ if (browserConfig.browserAgent)
32
+ return new BrowserServerContextFactory(browserConfig);
33
+ return new PersistentContextFactory(browserConfig);
34
+ }
35
+ class BaseContextFactory {
36
+ browserConfig;
37
+ _browserPromise;
38
+ name;
39
+ constructor(name, browserConfig) {
40
+ this.name = name;
41
+ this.browserConfig = browserConfig;
42
+ }
43
+ async _obtainBrowser() {
44
+ if (this._browserPromise)
45
+ return this._browserPromise;
46
+ testDebug(`obtain browser (${this.name})`);
47
+ this._browserPromise = this._doObtainBrowser();
48
+ void this._browserPromise.then(browser => {
49
+ browser.on('disconnected', () => {
50
+ this._browserPromise = undefined;
51
+ });
52
+ }).catch(() => {
53
+ this._browserPromise = undefined;
54
+ });
55
+ return this._browserPromise;
56
+ }
57
+ async _doObtainBrowser() {
58
+ throw new Error('Not implemented');
59
+ }
60
+ async createContext() {
61
+ testDebug(`create browser context (${this.name})`);
62
+ const browser = await this._obtainBrowser();
63
+ const browserContext = await this._doCreateContext(browser);
64
+ return { browserContext, close: () => this._closeBrowserContext(browserContext, browser) };
65
+ }
66
+ async _doCreateContext(browser) {
67
+ throw new Error('Not implemented');
68
+ }
69
+ async _closeBrowserContext(browserContext, browser) {
70
+ testDebug(`close browser context (${this.name})`);
71
+ if (browser.contexts().length === 1)
72
+ this._browserPromise = undefined;
73
+ await browserContext.close().catch(() => { });
74
+ if (browser.contexts().length === 0) {
75
+ testDebug(`close browser (${this.name})`);
76
+ await browser.close().catch(() => { });
77
+ }
78
+ }
79
+ }
80
+ class IsolatedContextFactory extends BaseContextFactory {
81
+ constructor(browserConfig) {
82
+ super('isolated', browserConfig);
83
+ }
84
+ async _doObtainBrowser() {
85
+ await injectCdpPort(this.browserConfig);
86
+ const browserType = playwright[this.browserConfig.browserName];
87
+ return browserType.launch({
88
+ ...this.browserConfig.launchOptions,
89
+ handleSIGINT: false,
90
+ handleSIGTERM: false,
91
+ }).catch(error => {
92
+ if (error.message.includes('Executable doesn\'t exist'))
93
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
94
+ throw error;
95
+ });
96
+ }
97
+ async _doCreateContext(browser) {
98
+ return browser.newContext(this.browserConfig.contextOptions);
99
+ }
100
+ }
101
+ class CdpContextFactory extends BaseContextFactory {
102
+ constructor(browserConfig) {
103
+ super('cdp', browserConfig);
104
+ }
105
+ async _doObtainBrowser() {
106
+ return playwright.chromium.connectOverCDP(this.browserConfig.cdpEndpoint);
107
+ }
108
+ async _doCreateContext(browser) {
109
+ return this.browserConfig.isolated ? await browser.newContext() : browser.contexts()[0];
110
+ }
111
+ }
112
+ class RemoteContextFactory extends BaseContextFactory {
113
+ constructor(browserConfig) {
114
+ super('remote', browserConfig);
115
+ }
116
+ async _doObtainBrowser() {
117
+ const url = new URL(this.browserConfig.remoteEndpoint);
118
+ url.searchParams.set('browser', this.browserConfig.browserName);
119
+ if (this.browserConfig.launchOptions)
120
+ url.searchParams.set('launch-options', JSON.stringify(this.browserConfig.launchOptions));
121
+ return playwright[this.browserConfig.browserName].connect(String(url));
122
+ }
123
+ async _doCreateContext(browser) {
124
+ return browser.newContext();
125
+ }
126
+ }
127
+ class PersistentContextFactory {
128
+ browserConfig;
129
+ _userDataDirs = new Set();
130
+ constructor(browserConfig) {
131
+ this.browserConfig = browserConfig;
132
+ }
133
+ async createContext() {
134
+ await injectCdpPort(this.browserConfig);
135
+ testDebug('create browser context (persistent)');
136
+ const userDataDir = this.browserConfig.userDataDir ?? await this._createUserDataDir();
137
+ this._userDataDirs.add(userDataDir);
138
+ testDebug('lock user data dir', userDataDir);
139
+ const browserType = playwright[this.browserConfig.browserName];
140
+ for (let i = 0; i < 5; i++) {
141
+ try {
142
+ const browserContext = await browserType.launchPersistentContext(userDataDir, {
143
+ ...this.browserConfig.launchOptions,
144
+ ...this.browserConfig.contextOptions,
145
+ handleSIGINT: false,
146
+ handleSIGTERM: false,
147
+ });
148
+ const close = () => this._closeBrowserContext(browserContext, userDataDir);
149
+ return { browserContext, close };
150
+ }
151
+ catch (error) {
152
+ if (error.message.includes('Executable doesn\'t exist'))
153
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
154
+ if (error.message.includes('ProcessSingleton') || error.message.includes('Invalid URL')) {
155
+ // User data directory is already in use, try again.
156
+ await new Promise(resolve => setTimeout(resolve, 1000));
157
+ continue;
158
+ }
159
+ throw error;
160
+ }
161
+ }
162
+ throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
163
+ }
164
+ async _closeBrowserContext(browserContext, userDataDir) {
165
+ testDebug('close browser context (persistent)');
166
+ testDebug('release user data dir', userDataDir);
167
+ await browserContext.close().catch(() => { });
168
+ this._userDataDirs.delete(userDataDir);
169
+ testDebug('close browser context complete (persistent)');
170
+ }
171
+ async _createUserDataDir() {
172
+ let cacheDirectory;
173
+ if (process.platform === 'linux')
174
+ cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
175
+ else if (process.platform === 'darwin')
176
+ cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
177
+ else if (process.platform === 'win32')
178
+ cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
179
+ else
180
+ throw new Error('Unsupported platform: ' + process.platform);
181
+ const result = path.join(cacheDirectory, 'ms-playwright', `mcp-${this.browserConfig.launchOptions?.channel ?? this.browserConfig?.browserName}-profile`);
182
+ await fs.promises.mkdir(result, { recursive: true });
183
+ return result;
184
+ }
185
+ }
186
+ export class BrowserServerContextFactory extends BaseContextFactory {
187
+ constructor(browserConfig) {
188
+ super('persistent', browserConfig);
189
+ }
190
+ async _doObtainBrowser() {
191
+ const response = await fetch(new URL(`/json/launch`, this.browserConfig.browserAgent), {
192
+ method: 'POST',
193
+ body: JSON.stringify({
194
+ browserType: this.browserConfig.browserName,
195
+ userDataDir: this.browserConfig.userDataDir ?? await this._createUserDataDir(),
196
+ launchOptions: this.browserConfig.launchOptions,
197
+ contextOptions: this.browserConfig.contextOptions,
198
+ }),
199
+ });
200
+ const info = await response.json();
201
+ if (info.error)
202
+ throw new Error(info.error);
203
+ return await playwright.chromium.connectOverCDP(`http://localhost:${info.cdpPort}/`);
204
+ }
205
+ async _doCreateContext(browser) {
206
+ return this.browserConfig.isolated ? await browser.newContext() : browser.contexts()[0];
207
+ }
208
+ async _createUserDataDir() {
209
+ const dir = await userDataDir(this.browserConfig);
210
+ await fs.promises.mkdir(dir, { recursive: true });
211
+ return dir;
212
+ }
213
+ }
214
+ async function injectCdpPort(browserConfig) {
215
+ if (browserConfig.browserName === 'chromium')
216
+ browserConfig.launchOptions.cdpPort = await findFreePort();
217
+ }
218
+ async function findFreePort() {
219
+ return new Promise((resolve, reject) => {
220
+ const server = net.createServer();
221
+ server.listen(0, () => {
222
+ const { port } = server.address();
223
+ server.close(() => resolve(port));
224
+ });
225
+ server.on('error', reject);
226
+ });
227
+ }
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /* eslint-disable no-console */
17
+ import net from 'net';
18
+ import { program } from 'commander';
19
+ import playwright from 'playwright';
20
+ import { HttpServer } from './httpServer.js';
21
+ import { packageJSON } from './package.js';
22
+ class BrowserServer {
23
+ _server = new HttpServer();
24
+ _entries = [];
25
+ constructor() {
26
+ this._setupExitHandler();
27
+ }
28
+ async start(port) {
29
+ await this._server.start({ port });
30
+ this._server.routePath('/json/list', (req, res) => {
31
+ this._handleJsonList(res);
32
+ });
33
+ this._server.routePath('/json/launch', async (req, res) => {
34
+ void this._handleLaunchBrowser(req, res).catch(e => console.error(e));
35
+ });
36
+ this._setEntries([]);
37
+ }
38
+ _handleJsonList(res) {
39
+ const list = this._entries.map(browser => browser.info);
40
+ res.end(JSON.stringify(list));
41
+ }
42
+ async _handleLaunchBrowser(req, res) {
43
+ const request = await readBody(req);
44
+ let info = this._entries.map(entry => entry.info).find(info => info.userDataDir === request.userDataDir);
45
+ if (!info || info.error)
46
+ info = await this._newBrowser(request);
47
+ res.end(JSON.stringify(info));
48
+ }
49
+ async _newBrowser(request) {
50
+ const cdpPort = await findFreePort();
51
+ request.launchOptions.cdpPort = cdpPort;
52
+ const info = {
53
+ browserType: request.browserType,
54
+ userDataDir: request.userDataDir,
55
+ cdpPort,
56
+ launchOptions: request.launchOptions,
57
+ contextOptions: request.contextOptions,
58
+ };
59
+ const browserType = playwright[request.browserType];
60
+ const { browser, error } = await browserType.launchPersistentContext(request.userDataDir, {
61
+ ...request.launchOptions,
62
+ ...request.contextOptions,
63
+ handleSIGINT: false,
64
+ handleSIGTERM: false,
65
+ }).then(context => {
66
+ return { browser: context.browser(), error: undefined };
67
+ }).catch(error => {
68
+ return { browser: undefined, error: error.message };
69
+ });
70
+ this._setEntries([...this._entries, {
71
+ browser,
72
+ info: {
73
+ browserType: request.browserType,
74
+ userDataDir: request.userDataDir,
75
+ cdpPort,
76
+ launchOptions: request.launchOptions,
77
+ contextOptions: request.contextOptions,
78
+ error,
79
+ },
80
+ }]);
81
+ browser?.on('disconnected', () => {
82
+ this._setEntries(this._entries.filter(entry => entry.browser !== browser));
83
+ });
84
+ return info;
85
+ }
86
+ _updateReport() {
87
+ // Clear the current line and move cursor to top of screen
88
+ process.stdout.write('\x1b[2J\x1b[H');
89
+ process.stdout.write(`Browser Server v${packageJSON.version}\n`);
90
+ process.stdout.write(`Listening on ${this._server.urlPrefix('human-readable')}\n\n`);
91
+ if (this._entries.length === 0) {
92
+ process.stdout.write('No browsers currently running\n');
93
+ return;
94
+ }
95
+ process.stdout.write('Running browsers:\n');
96
+ for (const entry of this._entries) {
97
+ const status = entry.browser ? 'running' : 'error';
98
+ const statusColor = entry.browser ? '\x1b[32m' : '\x1b[31m'; // green for running, red for error
99
+ process.stdout.write(`${statusColor}${entry.info.browserType}\x1b[0m (${entry.info.userDataDir}) - ${statusColor}${status}\x1b[0m\n`);
100
+ if (entry.info.error)
101
+ process.stdout.write(` Error: ${entry.info.error}\n`);
102
+ }
103
+ }
104
+ _setEntries(entries) {
105
+ this._entries = entries;
106
+ this._updateReport();
107
+ }
108
+ _setupExitHandler() {
109
+ let isExiting = false;
110
+ const handleExit = async () => {
111
+ if (isExiting)
112
+ return;
113
+ isExiting = true;
114
+ setTimeout(() => process.exit(0), 15000);
115
+ for (const entry of this._entries)
116
+ await entry.browser?.close().catch(() => { });
117
+ process.exit(0);
118
+ };
119
+ process.stdin.on('close', handleExit);
120
+ process.on('SIGINT', handleExit);
121
+ process.on('SIGTERM', handleExit);
122
+ }
123
+ }
124
+ program
125
+ .name('browser-agent')
126
+ .option('-p, --port <port>', 'Port to listen on', '9224')
127
+ .action(async (options) => {
128
+ await main(options);
129
+ });
130
+ void program.parseAsync(process.argv);
131
+ async function main(options) {
132
+ const server = new BrowserServer();
133
+ await server.start(+options.port);
134
+ }
135
+ function readBody(req) {
136
+ return new Promise((resolve, reject) => {
137
+ const chunks = [];
138
+ req.on('data', (chunk) => chunks.push(chunk));
139
+ req.on('end', () => resolve(JSON.parse(Buffer.concat(chunks).toString())));
140
+ });
141
+ }
142
+ async function findFreePort() {
143
+ return new Promise((resolve, reject) => {
144
+ const server = net.createServer();
145
+ server.listen(0, () => {
146
+ const { port } = server.address();
147
+ server.close(() => resolve(port));
148
+ });
149
+ server.on('error', reject);
150
+ });
151
+ }