@treegress.com/treegress-browser-mcp 0.0.56-treegress.3

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.
Files changed (67) hide show
  1. package/README.md +68 -0
  2. package/cli.js +25 -0
  3. package/config.d.ts +223 -0
  4. package/index.d.ts +23 -0
  5. package/index.js +19 -0
  6. package/mcp/browser/browserContextFactory.js +332 -0
  7. package/mcp/browser/browserServerBackend.js +105 -0
  8. package/mcp/browser/config.js +489 -0
  9. package/mcp/browser/configIni.js +194 -0
  10. package/mcp/browser/context.js +302 -0
  11. package/mcp/browser/domSnapshot.js +307 -0
  12. package/mcp/browser/logFile.js +96 -0
  13. package/mcp/browser/response.js +299 -0
  14. package/mcp/browser/sessionLog.js +75 -0
  15. package/mcp/browser/tab.js +1193 -0
  16. package/mcp/browser/tools/common.js +63 -0
  17. package/mcp/browser/tools/config.js +41 -0
  18. package/mcp/browser/tools/console.js +65 -0
  19. package/mcp/browser/tools/cookies.js +152 -0
  20. package/mcp/browser/tools/devtools.js +42 -0
  21. package/mcp/browser/tools/dialogs.js +59 -0
  22. package/mcp/browser/tools/evaluate.js +61 -0
  23. package/mcp/browser/tools/files.js +58 -0
  24. package/mcp/browser/tools/form.js +63 -0
  25. package/mcp/browser/tools/install.js +73 -0
  26. package/mcp/browser/tools/keyboard.js +151 -0
  27. package/mcp/browser/tools/mouse.js +159 -0
  28. package/mcp/browser/tools/navigate.js +105 -0
  29. package/mcp/browser/tools/network.js +92 -0
  30. package/mcp/browser/tools/pdf.js +48 -0
  31. package/mcp/browser/tools/route.js +140 -0
  32. package/mcp/browser/tools/runCode.js +76 -0
  33. package/mcp/browser/tools/screenshot.js +86 -0
  34. package/mcp/browser/tools/snapshot.js +207 -0
  35. package/mcp/browser/tools/storage.js +67 -0
  36. package/mcp/browser/tools/tabs.js +67 -0
  37. package/mcp/browser/tools/tool.js +47 -0
  38. package/mcp/browser/tools/tracing.js +75 -0
  39. package/mcp/browser/tools/utils.js +88 -0
  40. package/mcp/browser/tools/verify.js +143 -0
  41. package/mcp/browser/tools/video.js +89 -0
  42. package/mcp/browser/tools/wait.js +63 -0
  43. package/mcp/browser/tools/webstorage.js +223 -0
  44. package/mcp/browser/tools.js +96 -0
  45. package/mcp/browser/watchdog.js +44 -0
  46. package/mcp/config.d.js +16 -0
  47. package/mcp/extension/cdpRelay.js +354 -0
  48. package/mcp/extension/extensionContextFactory.js +77 -0
  49. package/mcp/extension/protocol.js +28 -0
  50. package/mcp/index.js +61 -0
  51. package/mcp/log.js +35 -0
  52. package/mcp/program.js +126 -0
  53. package/mcp/sdk/exports.js +28 -0
  54. package/mcp/sdk/http.js +172 -0
  55. package/mcp/sdk/inProcessTransport.js +71 -0
  56. package/mcp/sdk/server.js +223 -0
  57. package/mcp/sdk/tool.js +54 -0
  58. package/mcp/test/browserBackend.js +98 -0
  59. package/mcp/test/generatorTools.js +122 -0
  60. package/mcp/test/plannerTools.js +145 -0
  61. package/mcp/test/seed.js +82 -0
  62. package/mcp/test/streams.js +44 -0
  63. package/mcp/test/testBackend.js +99 -0
  64. package/mcp/test/testContext.js +285 -0
  65. package/mcp/test/testTool.js +30 -0
  66. package/mcp/test/testTools.js +108 -0
  67. package/package.json +46 -0
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # @treegress.com/treegress-browser-mcp
2
+
3
+ MCP server package for the Treegress browser stack.
4
+
5
+ This package currently wraps the Playwright MCP runtime and resolves `playwright-core`
6
+ from the published `@treegress.com/treegress-browser-core` package.
7
+
8
+ `@treegress.com/treegress-browser-mcp` and `@treegress.com/treegress-browser-core` are intended to be released together.
9
+
10
+ - `@treegress.com/treegress-browser-mcp` uses a pinned `@treegress.com/treegress-browser-core` version
11
+ - If you change runtime behavior in `@treegress.com/treegress-browser-core`, update the pinned core dependency in this package and publish a new MCP version as well
12
+ - If you change MCP-only behavior or packaging, you may only need a new `@treegress.com/treegress-browser-mcp` release
13
+
14
+ ## Cursor setup
15
+
16
+ You do not need a global install.
17
+
18
+ Create `.cursor/mcp.json` in the root of the project where you want to use the MCP server:
19
+
20
+ ```json
21
+ {
22
+ "mcpServers": {
23
+ "treegress-browser": {
24
+ "type": "stdio",
25
+ "command": "npx",
26
+ "args": [
27
+ "-y",
28
+ "@treegress.com/treegress-browser-mcp@latest",
29
+ "--snapshot-engine",
30
+ "dom"
31
+ ]
32
+ }
33
+ }
34
+ }
35
+ ```
36
+
37
+ Notes:
38
+
39
+ - Requires `Node.js 18+`
40
+ - `@treegress.com/treegress-browser-core` is installed automatically as a dependency
41
+ - Global `npm i -g` is not required
42
+ - `--snapshot-engine dom` enables the Treegress custom DOM snapshot path
43
+
44
+ ## Build and publish
45
+
46
+ Publish `@treegress.com/treegress-browser-core` first, then update the pinned core dependency in
47
+ `packages/playwright-mcp/package.json` before publishing this package.
48
+
49
+ Whenever the two packages need to stay aligned, treat the release as a coordinated pair: new core version first, then a new MCP version that points to it.
50
+
51
+ Prepare the package tarball:
52
+
53
+ ```bash
54
+ mkdir -p /tmp/treegress-release-artifacts
55
+ mkdir -p /tmp/treegress-npm-cache
56
+
57
+ cd <treegress-browser-mcp-repo>/packages/playwright-mcp
58
+ npm_config_cache=/tmp/treegress-npm-cache npm pack --dry-run
59
+ npm_config_cache=/tmp/treegress-npm-cache npm pack --pack-destination /tmp/treegress-release-artifacts
60
+ ```
61
+
62
+ Publish to npmjs:
63
+
64
+ ```bash
65
+ cd <treegress-browser-mcp-repo>/packages/playwright-mcp
66
+ npm_config_cache=/tmp/treegress-npm-cache npm publish --access public
67
+ npm view @treegress.com/treegress-browser-mcp version
68
+ ```
package/cli.js ADDED
@@ -0,0 +1,25 @@
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
+ const { program } = require('playwright-core/lib/utilsBundle');
19
+ const { decorateMCPCommand, decorateCommand } = require('./mcp/program');
20
+
21
+ const packageJSON = require('./package.json');
22
+ const p = program.version('Version ' + packageJSON.version).name('Playwright MCP');
23
+ const decorate = decorateMCPCommand || decorateCommand;
24
+ decorate(p, packageJSON.version);
25
+ void program.parseAsync(process.argv);
package/config.d.ts ADDED
@@ -0,0 +1,223 @@
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' | 'core-tabs' | 'core-install' | 'vision' | 'pdf' | 'testing' | 'tracing';
20
+
21
+ export type Config = {
22
+ /**
23
+ * The browser to use.
24
+ */
25
+ browser?: {
26
+ /**
27
+ * The type of browser to use.
28
+ */
29
+ browserName?: 'chromium' | 'firefox' | 'webkit';
30
+
31
+ /**
32
+ * Keep the browser profile in memory, do not save it to disk.
33
+ */
34
+ isolated?: boolean;
35
+
36
+ /**
37
+ * Path to a user data directory for browser profile persistence.
38
+ * Temporary directory is created by default.
39
+ */
40
+ userDataDir?: string;
41
+
42
+ /**
43
+ * Launch options passed to
44
+ * @see https://playwright.dev/docs/api/class-browsertype#browser-type-launch-persistent-context
45
+ *
46
+ * This is useful for settings options like `channel`, `headless`, `executablePath`, etc.
47
+ */
48
+ launchOptions?: playwright.LaunchOptions;
49
+
50
+ /**
51
+ * Context options for the browser context.
52
+ *
53
+ * This is useful for settings options like `viewport`.
54
+ */
55
+ contextOptions?: playwright.BrowserContextOptions;
56
+
57
+ /**
58
+ * Chrome DevTools Protocol endpoint to connect to an existing browser instance in case of Chromium family browsers.
59
+ */
60
+ cdpEndpoint?: string;
61
+
62
+ /**
63
+ * CDP headers to send with the connect request.
64
+ */
65
+ cdpHeaders?: Record<string, string>;
66
+
67
+ /**
68
+ * Timeout in milliseconds for connecting to CDP endpoint. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.
69
+ */
70
+ cdpTimeout?: number;
71
+
72
+ /**
73
+ * Remote endpoint to connect to an existing Playwright server.
74
+ */
75
+ remoteEndpoint?: string;
76
+
77
+ /**
78
+ * Paths to TypeScript files to add as initialization scripts for Playwright page.
79
+ */
80
+ initPage?: string[];
81
+
82
+ /**
83
+ * Paths to JavaScript files to add as initialization scripts.
84
+ * The scripts will be evaluated in every page before any of the page's scripts.
85
+ */
86
+ initScript?: string[];
87
+ },
88
+
89
+ server?: {
90
+ /**
91
+ * The port to listen on for SSE or MCP transport.
92
+ */
93
+ port?: number;
94
+
95
+ /**
96
+ * The host to bind the server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.
97
+ */
98
+ host?: string;
99
+
100
+ /**
101
+ * The hosts this server is allowed to serve from. Defaults to the host server is bound to.
102
+ * This is not for CORS, but rather for the DNS rebinding protection.
103
+ */
104
+ allowedHosts?: string[];
105
+ },
106
+
107
+ /**
108
+ * List of enabled tool capabilities. Possible values:
109
+ * - 'core': Core browser automation features.
110
+ * - 'pdf': PDF generation and manipulation.
111
+ * - 'vision': Coordinate-based interactions.
112
+ */
113
+ capabilities?: ToolCapability[];
114
+
115
+ /**
116
+ * Whether to save the Playwright session into the output directory.
117
+ */
118
+ saveSession?: boolean;
119
+
120
+ /**
121
+ * Whether to save the Playwright trace of the session into the output directory.
122
+ */
123
+ saveTrace?: boolean;
124
+
125
+ /**
126
+ * If specified, saves the Playwright video of the session into the output directory.
127
+ */
128
+ saveVideo?: {
129
+ width: number;
130
+ height: number;
131
+ };
132
+
133
+ /**
134
+ * Reuse the same browser context between all connected HTTP clients.
135
+ */
136
+ sharedBrowserContext?: boolean;
137
+
138
+ /**
139
+ * Secrets are used to prevent LLM from getting sensitive data while
140
+ * automating scenarios such as authentication.
141
+ * Prefer the browser.contextOptions.storageState over secrets file as a more secure alternative.
142
+ */
143
+ secrets?: Record<string, string>;
144
+
145
+ /**
146
+ * The directory to save output files.
147
+ */
148
+ outputDir?: string;
149
+
150
+ console?: {
151
+ /**
152
+ * The level of console messages to return. Each level includes the messages of more severe levels. Defaults to "info".
153
+ */
154
+ level?: 'error' | 'warning' | 'info' | 'debug';
155
+ },
156
+
157
+ network?: {
158
+ /**
159
+ * List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
160
+ */
161
+ allowedOrigins?: string[];
162
+
163
+ /**
164
+ * List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
165
+ */
166
+ blockedOrigins?: string[];
167
+ };
168
+
169
+ /**
170
+ * Specify the attribute to use for test ids, defaults to "data-testid".
171
+ */
172
+ testIdAttribute?: string;
173
+
174
+ timeouts?: {
175
+ /*
176
+ * Configures default action timeout: https://playwright.dev/docs/api/class-page#page-set-default-timeout. Defaults to 5000ms.
177
+ */
178
+ action?: number;
179
+
180
+ /*
181
+ * Configures default navigation timeout: https://playwright.dev/docs/api/class-page#page-set-default-navigation-timeout. Defaults to 60000ms.
182
+ */
183
+ navigation?: number;
184
+ };
185
+
186
+ /**
187
+ * 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.
188
+ */
189
+ imageResponses?: 'allow' | 'omit';
190
+
191
+ snapshot?: {
192
+ /**
193
+ * When taking snapshots for responses, specifies the mode to use.
194
+ */
195
+ mode?: 'incremental' | 'full' | 'none';
196
+ /**
197
+ * Snapshot engine used to resolve element references.
198
+ * "aria" uses the built-in aria-ref engine.
199
+ * "dom" uses the playwright-core custom-dom backend.
200
+ */
201
+ engine?: 'aria' | 'dom';
202
+ /**
203
+ * Path to a DOM serializer module used only by the historical/debug-only
204
+ * local DOM engine retained in this fork.
205
+ */
206
+ domSerializerPath?: string;
207
+ /**
208
+ * Historical/debug-only local DOM engine: allow fallback to aria-ref
209
+ * resolution.
210
+ */
211
+ domFallbackToAria?: boolean;
212
+ /**
213
+ * Historical/debug-only local DOM engine: disable strict uniqueness check.
214
+ */
215
+ domNonstrict?: boolean;
216
+ }
217
+
218
+ /**
219
+ * Whether to allow file uploads from anywhere on the file system.
220
+ * By default (false), file uploads are restricted to paths within the MCP roots only.
221
+ */
222
+ allowUnrestrictedFileAccess?: boolean;
223
+ };
package/index.d.ts ADDED
@@ -0,0 +1,23 @@
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';
20
+ import type { BrowserContext } from 'playwright';
21
+
22
+ export declare function createConnection(config?: Config, contextGetter?: () => Promise<BrowserContext>): Promise<Server>;
23
+ 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
+ const { createConnection } = require('./mcp/index');
19
+ module.exports = { createConnection };
@@ -0,0 +1,332 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var browserContextFactory_exports = {};
30
+ __export(browserContextFactory_exports, {
31
+ SharedContextFactory: () => SharedContextFactory,
32
+ contextFactory: () => contextFactory,
33
+ identityBrowserContextFactory: () => identityBrowserContextFactory
34
+ });
35
+ module.exports = __toCommonJS(browserContextFactory_exports);
36
+ var import_crypto = __toESM(require("crypto"));
37
+ var import_fs = __toESM(require("fs"));
38
+ var import_net = __toESM(require("net"));
39
+ var import_path = __toESM(require("path"));
40
+ var playwright = __toESM(require("playwright-core"));
41
+ var import_registry = require("playwright-core/lib/server/registry/index");
42
+ var import_server = require("playwright-core/lib/server");
43
+ var import_log = require("../log");
44
+ var import_config = require("./config");
45
+ var import_server2 = require("../sdk/server");
46
+ function contextFactory(config) {
47
+ if (config.sharedBrowserContext)
48
+ return SharedContextFactory.create(config);
49
+ if (config.browser.remoteEndpoint)
50
+ return new RemoteContextFactory(config);
51
+ if (config.browser.cdpEndpoint)
52
+ return new CdpContextFactory(config);
53
+ if (config.browser.isolated)
54
+ return new IsolatedContextFactory(config);
55
+ return new PersistentContextFactory(config);
56
+ }
57
+ function identityBrowserContextFactory(browserContext) {
58
+ return {
59
+ createContext: async (clientInfo, abortSignal, options) => {
60
+ return {
61
+ browserContext,
62
+ close: async () => {
63
+ }
64
+ };
65
+ }
66
+ };
67
+ }
68
+ class BaseContextFactory {
69
+ constructor(name, config) {
70
+ this._logName = name;
71
+ this.config = config;
72
+ }
73
+ async _obtainBrowser(clientInfo, options) {
74
+ if (this._browserPromise)
75
+ return this._browserPromise;
76
+ (0, import_log.testDebug)(`obtain browser (${this._logName})`);
77
+ this._browserPromise = this._doObtainBrowser(clientInfo, options);
78
+ void this._browserPromise.then((browser) => {
79
+ browser.on("disconnected", () => {
80
+ this._browserPromise = void 0;
81
+ });
82
+ }).catch(() => {
83
+ this._browserPromise = void 0;
84
+ });
85
+ return this._browserPromise;
86
+ }
87
+ async _doObtainBrowser(clientInfo, options) {
88
+ throw new Error("Not implemented");
89
+ }
90
+ async createContext(clientInfo, _, options) {
91
+ (0, import_log.testDebug)(`create browser context (${this._logName})`);
92
+ const browser = await this._obtainBrowser(clientInfo, options);
93
+ const browserContext = await this._doCreateContext(browser, clientInfo);
94
+ await addInitScript(browserContext, this.config.browser.initScript);
95
+ return {
96
+ browserContext,
97
+ close: () => this._closeBrowserContext(browserContext, browser)
98
+ };
99
+ }
100
+ async _doCreateContext(browser, clientInfo) {
101
+ throw new Error("Not implemented");
102
+ }
103
+ async _closeBrowserContext(browserContext, browser) {
104
+ (0, import_log.testDebug)(`close browser context (${this._logName})`);
105
+ if (browser.contexts().length === 1)
106
+ this._browserPromise = void 0;
107
+ await browserContext.close().catch(import_log.logUnhandledError);
108
+ if (browser.contexts().length === 0) {
109
+ (0, import_log.testDebug)(`close browser (${this._logName})`);
110
+ await browser.close().catch(import_log.logUnhandledError);
111
+ }
112
+ }
113
+ }
114
+ class IsolatedContextFactory extends BaseContextFactory {
115
+ constructor(config) {
116
+ super("isolated", config);
117
+ }
118
+ async _doObtainBrowser(clientInfo, options) {
119
+ await injectCdpPort(this.config.browser);
120
+ const browserType = playwright[this.config.browser.browserName];
121
+ const tracesDir = await computeTracesDir(this.config, clientInfo);
122
+ if (tracesDir && this.config.saveTrace)
123
+ await startTraceServer(this.config, tracesDir);
124
+ return browserType.launch({
125
+ tracesDir,
126
+ ...this.config.browser.launchOptions,
127
+ handleSIGINT: false,
128
+ handleSIGTERM: false
129
+ }).catch((error) => {
130
+ if (error.message.includes("Executable doesn't exist"))
131
+ throwBrowserIsNotInstalledError(this.config);
132
+ throw error;
133
+ });
134
+ }
135
+ async _doCreateContext(browser, clientInfo) {
136
+ return browser.newContext(await browserContextOptionsFromConfig(this.config, clientInfo));
137
+ }
138
+ }
139
+ class CdpContextFactory extends BaseContextFactory {
140
+ constructor(config) {
141
+ super("cdp", config);
142
+ }
143
+ async _doObtainBrowser() {
144
+ return playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint, {
145
+ headers: this.config.browser.cdpHeaders,
146
+ timeout: this.config.browser.cdpTimeout
147
+ });
148
+ }
149
+ async _doCreateContext(browser) {
150
+ return this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
151
+ }
152
+ }
153
+ class RemoteContextFactory extends BaseContextFactory {
154
+ constructor(config) {
155
+ super("remote", config);
156
+ }
157
+ async _doObtainBrowser() {
158
+ const url = new URL(this.config.browser.remoteEndpoint);
159
+ url.searchParams.set("browser", this.config.browser.browserName);
160
+ if (this.config.browser.launchOptions)
161
+ url.searchParams.set("launch-options", JSON.stringify(this.config.browser.launchOptions));
162
+ return playwright[this.config.browser.browserName].connect(String(url));
163
+ }
164
+ async _doCreateContext(browser) {
165
+ return browser.newContext();
166
+ }
167
+ }
168
+ class PersistentContextFactory {
169
+ constructor(config) {
170
+ this.name = "persistent";
171
+ this.description = "Create a new persistent browser context";
172
+ this._userDataDirs = /* @__PURE__ */ new Set();
173
+ this.config = config;
174
+ }
175
+ async createContext(clientInfo, abortSignal, options) {
176
+ await injectCdpPort(this.config.browser);
177
+ (0, import_log.testDebug)("create browser context (persistent)");
178
+ const userDataDir = this.config.browser.userDataDir ?? await this._createUserDataDir(clientInfo);
179
+ const tracesDir = await computeTracesDir(this.config, clientInfo);
180
+ if (tracesDir && this.config.saveTrace)
181
+ await startTraceServer(this.config, tracesDir);
182
+ this._userDataDirs.add(userDataDir);
183
+ (0, import_log.testDebug)("lock user data dir", userDataDir);
184
+ const browserType = playwright[this.config.browser.browserName];
185
+ for (let i = 0; i < 5; i++) {
186
+ const launchOptions = {
187
+ tracesDir,
188
+ ...this.config.browser.launchOptions,
189
+ ...await browserContextOptionsFromConfig(this.config, clientInfo),
190
+ handleSIGINT: false,
191
+ handleSIGTERM: false,
192
+ ignoreDefaultArgs: [
193
+ "--disable-extensions"
194
+ ],
195
+ assistantMode: true
196
+ };
197
+ try {
198
+ const browserContext = await browserType.launchPersistentContext(userDataDir, launchOptions);
199
+ await addInitScript(browserContext, this.config.browser.initScript);
200
+ const close = () => this._closeBrowserContext(browserContext, userDataDir);
201
+ return { browserContext, close };
202
+ } catch (error) {
203
+ if (error.message.includes("Executable doesn't exist"))
204
+ throwBrowserIsNotInstalledError(this.config);
205
+ if (error.message.includes("cannot open shared object file: No such file or directory")) {
206
+ const browserName = launchOptions.channel ?? this.config.browser.browserName;
207
+ throw new Error(`Missing system dependencies required to run browser ${browserName}. Install them with: sudo npx treegress-browser-core install-deps ${browserName}`);
208
+ }
209
+ if (error.message.includes("ProcessSingleton") || // On Windows the process exits silently with code 21 when the profile is in use.
210
+ error.message.includes("exitCode=21")) {
211
+ await new Promise((resolve) => setTimeout(resolve, 1e3));
212
+ continue;
213
+ }
214
+ throw error;
215
+ }
216
+ }
217
+ throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
218
+ }
219
+ async _closeBrowserContext(browserContext, userDataDir) {
220
+ (0, import_log.testDebug)("close browser context (persistent)");
221
+ (0, import_log.testDebug)("release user data dir", userDataDir);
222
+ await browserContext.close().catch(() => {
223
+ });
224
+ this._userDataDirs.delete(userDataDir);
225
+ if (process.env.PWMCP_PROFILES_DIR_FOR_TEST && userDataDir.startsWith(process.env.PWMCP_PROFILES_DIR_FOR_TEST))
226
+ await import_fs.default.promises.rm(userDataDir, { recursive: true }).catch(import_log.logUnhandledError);
227
+ (0, import_log.testDebug)("close browser context complete (persistent)");
228
+ }
229
+ async _createUserDataDir(clientInfo) {
230
+ const dir = process.env.PWMCP_PROFILES_DIR_FOR_TEST ?? import_registry.registryDirectory;
231
+ const browserToken = this.config.browser.launchOptions?.channel ?? this.config.browser?.browserName;
232
+ const rootPath = (0, import_server2.firstRootPath)(clientInfo);
233
+ const rootPathToken = rootPath ? `-${createHash(rootPath)}` : "";
234
+ const result = import_path.default.join(dir, `mcp-${browserToken}${rootPathToken}`);
235
+ await import_fs.default.promises.mkdir(result, { recursive: true });
236
+ return result;
237
+ }
238
+ }
239
+ async function injectCdpPort(browserConfig) {
240
+ if (browserConfig.browserName === "chromium")
241
+ browserConfig.launchOptions.cdpPort = await findFreePort();
242
+ }
243
+ async function findFreePort() {
244
+ return new Promise((resolve, reject) => {
245
+ const server = import_net.default.createServer();
246
+ server.listen(0, () => {
247
+ const { port } = server.address();
248
+ server.close(() => resolve(port));
249
+ });
250
+ server.on("error", reject);
251
+ });
252
+ }
253
+ async function startTraceServer(config, tracesDir) {
254
+ if (!config.saveTrace)
255
+ return;
256
+ const server = await (0, import_server.startTraceViewerServer)();
257
+ const urlPrefix = server.urlPrefix("human-readable");
258
+ const url = urlPrefix + "/trace/index.html?trace=" + tracesDir + "/trace.json";
259
+ console.error("\nTrace viewer listening on " + url);
260
+ }
261
+ function createHash(data) {
262
+ return import_crypto.default.createHash("sha256").update(data).digest("hex").slice(0, 7);
263
+ }
264
+ async function addInitScript(browserContext, initScript) {
265
+ for (const scriptPath of initScript ?? [])
266
+ await browserContext.addInitScript({ path: import_path.default.resolve(scriptPath) });
267
+ }
268
+ class SharedContextFactory {
269
+ static create(config) {
270
+ if (SharedContextFactory._instance)
271
+ throw new Error("SharedContextFactory already exists");
272
+ const baseConfig = { ...config, sharedBrowserContext: false };
273
+ const baseFactory = contextFactory(baseConfig);
274
+ SharedContextFactory._instance = new SharedContextFactory(baseFactory);
275
+ return SharedContextFactory._instance;
276
+ }
277
+ constructor(baseFactory) {
278
+ this._baseFactory = baseFactory;
279
+ }
280
+ async createContext(clientInfo, abortSignal, options) {
281
+ if (!this._contextPromise) {
282
+ (0, import_log.testDebug)("create shared browser context");
283
+ this._contextPromise = this._baseFactory.createContext(clientInfo, abortSignal, options);
284
+ }
285
+ const { browserContext } = await this._contextPromise;
286
+ (0, import_log.testDebug)(`shared context client connected`);
287
+ return {
288
+ browserContext,
289
+ close: async () => {
290
+ (0, import_log.testDebug)(`shared context client disconnected`);
291
+ }
292
+ };
293
+ }
294
+ static async dispose() {
295
+ await SharedContextFactory._instance?._dispose();
296
+ }
297
+ async _dispose() {
298
+ const contextPromise = this._contextPromise;
299
+ this._contextPromise = void 0;
300
+ if (!contextPromise)
301
+ return;
302
+ const { close } = await contextPromise;
303
+ await close();
304
+ }
305
+ }
306
+ async function computeTracesDir(config, clientInfo) {
307
+ return import_path.default.resolve((0, import_config.outputDir)(config, clientInfo), "traces");
308
+ }
309
+ async function browserContextOptionsFromConfig(config, clientInfo) {
310
+ const result = { ...config.browser.contextOptions };
311
+ if (config.saveVideo) {
312
+ const dir = await (0, import_config.outputFile)(config, clientInfo, `videos`, { origin: "code" });
313
+ result.recordVideo = {
314
+ dir,
315
+ size: config.saveVideo
316
+ };
317
+ }
318
+ return result;
319
+ }
320
+ function throwBrowserIsNotInstalledError(config) {
321
+ const channel = config.browser.launchOptions?.channel ?? config.browser.browserName;
322
+ if (config.skillMode)
323
+ throw new Error(`Browser "${channel}" is not installed. Run \`playwright-cli install-browser ${channel}\` to install`);
324
+ else
325
+ throw new Error(`Browser "${channel}" is not installed. Either install it (likely) or change the config.`);
326
+ }
327
+ // Annotate the CommonJS export names for ESM import in node:
328
+ 0 && (module.exports = {
329
+ SharedContextFactory,
330
+ contextFactory,
331
+ identityBrowserContextFactory
332
+ });