@cotestdev/mcp_playwright 0.0.8 → 0.0.10

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/lib/index.d.ts CHANGED
@@ -1,13 +1,23 @@
1
1
  /**
2
- * @cotestdev/mcp_playwright - Playwright MCP Tools
2
+ * Copyright (c) Microsoft Corporation.
3
3
  *
4
- * This package exports Playwright browser automation tools for use with
5
- * Model Context Protocol (MCP) servers.
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
6
7
  *
7
- * Reference implementation: https://github.com/microsoft/playwright-mcp
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
+ * @cotestdev/mcp_playwright
18
+ * Standalone MCP package for Playwright browser automation
19
+ * The actual CLI entry point is at lib/mcp/cli.js
8
20
  */
9
- export * from 'playwright/lib/mcp/browser/tools';
10
- export { default } from 'playwright/lib/mcp/browser/tools';
11
- export * from 'playwright/lib/mcp/sdk/exports';
12
- export * from 'playwright/lib/mcp/sdk/server';
21
+ declare const _default: {};
22
+ export default _default;
13
23
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,cAAc,kCAAkC,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,kCAAkC,CAAC;AAG3D,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH;;;;GAIG;;AAEH,wBAAkB"}
package/lib/index.js CHANGED
@@ -1,36 +1,24 @@
1
1
  "use strict";
2
2
  /**
3
- * @cotestdev/mcp_playwright - Playwright MCP Tools
3
+ * Copyright (c) Microsoft Corporation.
4
4
  *
5
- * This package exports Playwright browser automation tools for use with
6
- * Model Context Protocol (MCP) servers.
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
7
8
  *
8
- * Reference implementation: https://github.com/microsoft/playwright-mcp
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
+ * @cotestdev/mcp_playwright
19
+ * Standalone MCP package for Playwright browser automation
20
+ * The actual CLI entry point is at lib/mcp/cli.js
9
21
  */
10
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- var desc = Object.getOwnPropertyDescriptor(m, k);
13
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
14
- desc = { enumerable: true, get: function() { return m[k]; } };
15
- }
16
- Object.defineProperty(o, k2, desc);
17
- }) : (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- o[k2] = m[k];
20
- }));
21
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
22
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23
- };
24
- var __importDefault = (this && this.__importDefault) || function (mod) {
25
- return (mod && mod.__esModule) ? mod : { "default": mod };
26
- };
27
22
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.default = void 0;
29
- // Export MCP browser tools
30
- __exportStar(require("playwright/lib/mcp/browser/tools"), exports);
31
- var tools_1 = require("playwright/lib/mcp/browser/tools");
32
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(tools_1).default; } });
33
- // Export MCP SDK components for server setup
34
- __exportStar(require("playwright/lib/mcp/sdk/exports"), exports);
35
- __exportStar(require("playwright/lib/mcp/sdk/server"), exports);
23
+ exports.default = {};
36
24
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;;;;;;AAEH,2BAA2B;AAC3B,mEAAiD;AACjD,0DAA2D;AAAlD,iHAAA,OAAO,OAAA;AAEhB,6CAA6C;AAC7C,iEAA+C;AAC/C,gEAA8C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;AACH;;;;GAIG;;AAEH,kBAAe,EAAE,CAAC"}
package/lib/mcp/cli.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 { program } = require('./program');
19
+ program.parse(process.argv);
@@ -0,0 +1,351 @@
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 cdpRelay_exports = {};
30
+ __export(cdpRelay_exports, {
31
+ CDPRelayServer: () => CDPRelayServer
32
+ });
33
+ module.exports = __toCommonJS(cdpRelay_exports);
34
+ var import_child_process = require("child_process");
35
+ var import_utilsBundle = require("playwright-core/lib/utilsBundle");
36
+ var import_registry = require("playwright-core/lib/server/registry/index");
37
+ var import_utils = require("playwright-core/lib/utils");
38
+ var import_http2 = require("../sdk/http");
39
+ var import_log = require("../log");
40
+ var protocol = __toESM(require("./protocol"));
41
+ const debugLogger = (0, import_utilsBundle.debug)("pw:mcp:relay");
42
+ class CDPRelayServer {
43
+ constructor(server, browserChannel, userDataDir, executablePath) {
44
+ this._playwrightConnection = null;
45
+ this._extensionConnection = null;
46
+ this._nextSessionId = 1;
47
+ this._wsHost = (0, import_http2.httpAddressToString)(server.address()).replace(/^http/, "ws");
48
+ this._browserChannel = browserChannel;
49
+ this._userDataDir = userDataDir;
50
+ this._executablePath = executablePath;
51
+ const uuid = crypto.randomUUID();
52
+ this._cdpPath = `/cdp/${uuid}`;
53
+ this._extensionPath = `/extension/${uuid}`;
54
+ this._resetExtensionConnection();
55
+ this._wss = new import_utilsBundle.wsServer({ server });
56
+ this._wss.on("connection", this._onConnection.bind(this));
57
+ }
58
+ cdpEndpoint() {
59
+ return `${this._wsHost}${this._cdpPath}`;
60
+ }
61
+ extensionEndpoint() {
62
+ return `${this._wsHost}${this._extensionPath}`;
63
+ }
64
+ async ensureExtensionConnectionForMCPContext(clientInfo, abortSignal, toolName) {
65
+ debugLogger("Ensuring extension connection for MCP context");
66
+ if (this._extensionConnection)
67
+ return;
68
+ this._connectBrowser(clientInfo, toolName);
69
+ debugLogger("Waiting for incoming extension connection");
70
+ await Promise.race([
71
+ this._extensionConnectionPromise,
72
+ new Promise((_, reject) => setTimeout(() => {
73
+ reject(new Error(`Extension connection timeout. Make sure the "Playwright MCP Bridge" extension is installed. See https://github.com/microsoft/playwright-mcp/blob/main/extension/README.md for installation instructions.`));
74
+ }, process.env.PWMCP_TEST_CONNECTION_TIMEOUT ? parseInt(process.env.PWMCP_TEST_CONNECTION_TIMEOUT, 10) : 5e3)),
75
+ new Promise((_, reject) => abortSignal.addEventListener("abort", reject))
76
+ ]);
77
+ debugLogger("Extension connection established");
78
+ }
79
+ _connectBrowser(clientInfo, toolName) {
80
+ const mcpRelayEndpoint = `${this._wsHost}${this._extensionPath}`;
81
+ const url = new URL("chrome-extension://jakfalbnbhgkpmoaakfflhflbfpkailf/connect.html");
82
+ url.searchParams.set("mcpRelayUrl", mcpRelayEndpoint);
83
+ const client = {
84
+ name: clientInfo.name,
85
+ version: clientInfo.version
86
+ };
87
+ url.searchParams.set("client", JSON.stringify(client));
88
+ url.searchParams.set("protocolVersion", process.env.PWMCP_TEST_PROTOCOL_VERSION ?? protocol.VERSION.toString());
89
+ if (toolName)
90
+ url.searchParams.set("newTab", String(toolName === "browser_navigate"));
91
+ const token = process.env.PLAYWRIGHT_MCP_EXTENSION_TOKEN;
92
+ if (token)
93
+ url.searchParams.set("token", token);
94
+ const href = url.toString();
95
+ let executablePath = this._executablePath;
96
+ if (!executablePath) {
97
+ const executableInfo = import_registry.registry.findExecutable(this._browserChannel);
98
+ if (!executableInfo)
99
+ throw new Error(`Unsupported channel: "${this._browserChannel}"`);
100
+ executablePath = executableInfo.executablePath("javascript");
101
+ if (!executablePath)
102
+ throw new Error(`"${this._browserChannel}" executable not found. Make sure it is installed at a standard location.`);
103
+ }
104
+ const args = [];
105
+ if (this._userDataDir)
106
+ args.push(`--user-data-dir=${this._userDataDir}`);
107
+ args.push(href);
108
+ (0, import_child_process.spawn)(executablePath, args, {
109
+ windowsHide: true,
110
+ detached: true,
111
+ shell: false,
112
+ stdio: "ignore"
113
+ });
114
+ }
115
+ stop() {
116
+ this.closeConnections("Server stopped");
117
+ this._wss.close();
118
+ }
119
+ closeConnections(reason) {
120
+ this._closePlaywrightConnection(reason);
121
+ this._closeExtensionConnection(reason);
122
+ }
123
+ _onConnection(ws2, request) {
124
+ const url = new URL(`http://localhost${request.url}`);
125
+ debugLogger(`New connection to ${url.pathname}`);
126
+ if (url.pathname === this._cdpPath) {
127
+ this._handlePlaywrightConnection(ws2);
128
+ } else if (url.pathname === this._extensionPath) {
129
+ this._handleExtensionConnection(ws2);
130
+ } else {
131
+ debugLogger(`Invalid path: ${url.pathname}`);
132
+ ws2.close(4004, "Invalid path");
133
+ }
134
+ }
135
+ _handlePlaywrightConnection(ws2) {
136
+ if (this._playwrightConnection) {
137
+ debugLogger("Rejecting second Playwright connection");
138
+ ws2.close(1e3, "Another CDP client already connected");
139
+ return;
140
+ }
141
+ this._playwrightConnection = ws2;
142
+ ws2.on("message", async (data) => {
143
+ try {
144
+ const message = JSON.parse(data.toString());
145
+ await this._handlePlaywrightMessage(message);
146
+ } catch (error) {
147
+ debugLogger(`Error while handling Playwright message
148
+ ${data.toString()}
149
+ `, error);
150
+ }
151
+ });
152
+ ws2.on("close", () => {
153
+ if (this._playwrightConnection !== ws2)
154
+ return;
155
+ this._playwrightConnection = null;
156
+ this._closeExtensionConnection("Playwright client disconnected");
157
+ debugLogger("Playwright WebSocket closed");
158
+ });
159
+ ws2.on("error", (error) => {
160
+ debugLogger("Playwright WebSocket error:", error);
161
+ });
162
+ debugLogger("Playwright MCP connected");
163
+ }
164
+ _closeExtensionConnection(reason) {
165
+ this._extensionConnection?.close(reason);
166
+ this._extensionConnectionPromise.reject(new Error(reason));
167
+ this._resetExtensionConnection();
168
+ }
169
+ _resetExtensionConnection() {
170
+ this._connectedTabInfo = void 0;
171
+ this._extensionConnection = null;
172
+ this._extensionConnectionPromise = new import_utils.ManualPromise();
173
+ void this._extensionConnectionPromise.catch(import_log.logUnhandledError);
174
+ }
175
+ _closePlaywrightConnection(reason) {
176
+ if (this._playwrightConnection?.readyState === import_utilsBundle.ws.OPEN)
177
+ this._playwrightConnection.close(1e3, reason);
178
+ this._playwrightConnection = null;
179
+ }
180
+ _handleExtensionConnection(ws2) {
181
+ if (this._extensionConnection) {
182
+ ws2.close(1e3, "Another extension connection already established");
183
+ return;
184
+ }
185
+ this._extensionConnection = new ExtensionConnection(ws2);
186
+ this._extensionConnection.onclose = (c, reason) => {
187
+ debugLogger("Extension WebSocket closed:", reason, c === this._extensionConnection);
188
+ if (this._extensionConnection !== c)
189
+ return;
190
+ this._resetExtensionConnection();
191
+ this._closePlaywrightConnection(`Extension disconnected: ${reason}`);
192
+ };
193
+ this._extensionConnection.onmessage = this._handleExtensionMessage.bind(this);
194
+ this._extensionConnectionPromise.resolve();
195
+ }
196
+ _handleExtensionMessage(method, params) {
197
+ switch (method) {
198
+ case "forwardCDPEvent":
199
+ const sessionId = params.sessionId || this._connectedTabInfo?.sessionId;
200
+ this._sendToPlaywright({
201
+ sessionId,
202
+ method: params.method,
203
+ params: params.params
204
+ });
205
+ break;
206
+ }
207
+ }
208
+ async _handlePlaywrightMessage(message) {
209
+ debugLogger("\u2190 Playwright:", `${message.method} (id=${message.id})`);
210
+ const { id, sessionId, method, params } = message;
211
+ try {
212
+ const result = await this._handleCDPCommand(method, params, sessionId);
213
+ this._sendToPlaywright({ id, sessionId, result });
214
+ } catch (e) {
215
+ debugLogger("Error in the extension:", e);
216
+ this._sendToPlaywright({
217
+ id,
218
+ sessionId,
219
+ error: { message: e.message }
220
+ });
221
+ }
222
+ }
223
+ async _handleCDPCommand(method, params, sessionId) {
224
+ switch (method) {
225
+ case "Browser.getVersion": {
226
+ return {
227
+ protocolVersion: "1.3",
228
+ product: "Chrome/Extension-Bridge",
229
+ userAgent: "CDP-Bridge-Server/1.0.0"
230
+ };
231
+ }
232
+ case "Browser.setDownloadBehavior": {
233
+ return {};
234
+ }
235
+ case "Target.setAutoAttach": {
236
+ if (sessionId)
237
+ break;
238
+ const { targetInfo } = await this._extensionConnection.send("attachToTab", {});
239
+ this._connectedTabInfo = {
240
+ targetInfo,
241
+ sessionId: `pw-tab-${this._nextSessionId++}`
242
+ };
243
+ debugLogger("Simulating auto-attach");
244
+ this._sendToPlaywright({
245
+ method: "Target.attachedToTarget",
246
+ params: {
247
+ sessionId: this._connectedTabInfo.sessionId,
248
+ targetInfo: {
249
+ ...this._connectedTabInfo.targetInfo,
250
+ attached: true
251
+ },
252
+ waitingForDebugger: false
253
+ }
254
+ });
255
+ return {};
256
+ }
257
+ case "Target.getTargetInfo": {
258
+ return this._connectedTabInfo?.targetInfo;
259
+ }
260
+ }
261
+ return await this._forwardToExtension(method, params, sessionId);
262
+ }
263
+ async _forwardToExtension(method, params, sessionId) {
264
+ if (!this._extensionConnection)
265
+ throw new Error("Extension not connected");
266
+ if (this._connectedTabInfo?.sessionId === sessionId)
267
+ sessionId = void 0;
268
+ return await this._extensionConnection.send("forwardCDPCommand", { sessionId, method, params });
269
+ }
270
+ _sendToPlaywright(message) {
271
+ debugLogger("\u2192 Playwright:", `${message.method ?? `response(id=${message.id})`}`);
272
+ this._playwrightConnection?.send(JSON.stringify(message));
273
+ }
274
+ }
275
+ class ExtensionConnection {
276
+ constructor(ws2) {
277
+ this._callbacks = /* @__PURE__ */ new Map();
278
+ this._lastId = 0;
279
+ this._ws = ws2;
280
+ this._ws.on("message", this._onMessage.bind(this));
281
+ this._ws.on("close", this._onClose.bind(this));
282
+ this._ws.on("error", this._onError.bind(this));
283
+ }
284
+ async send(method, params) {
285
+ if (this._ws.readyState !== import_utilsBundle.ws.OPEN)
286
+ throw new Error(`Unexpected WebSocket state: ${this._ws.readyState}`);
287
+ const id = ++this._lastId;
288
+ this._ws.send(JSON.stringify({ id, method, params }));
289
+ const error = new Error(`Protocol error: ${method}`);
290
+ return new Promise((resolve, reject) => {
291
+ this._callbacks.set(id, { resolve, reject, error });
292
+ });
293
+ }
294
+ close(message) {
295
+ debugLogger("closing extension connection:", message);
296
+ if (this._ws.readyState === import_utilsBundle.ws.OPEN)
297
+ this._ws.close(1e3, message);
298
+ }
299
+ _onMessage(event) {
300
+ const eventData = event.toString();
301
+ let parsedJson;
302
+ try {
303
+ parsedJson = JSON.parse(eventData);
304
+ } catch (e) {
305
+ debugLogger(`<closing ws> Closing websocket due to malformed JSON. eventData=${eventData} e=${e?.message}`);
306
+ this._ws.close();
307
+ return;
308
+ }
309
+ try {
310
+ this._handleParsedMessage(parsedJson);
311
+ } catch (e) {
312
+ debugLogger(`<closing ws> Closing websocket due to failed onmessage callback. eventData=${eventData} e=${e?.message}`);
313
+ this._ws.close();
314
+ }
315
+ }
316
+ _handleParsedMessage(object) {
317
+ if (object.id && this._callbacks.has(object.id)) {
318
+ const callback = this._callbacks.get(object.id);
319
+ this._callbacks.delete(object.id);
320
+ if (object.error) {
321
+ const error = callback.error;
322
+ error.message = object.error;
323
+ callback.reject(error);
324
+ } else {
325
+ callback.resolve(object.result);
326
+ }
327
+ } else if (object.id) {
328
+ debugLogger("\u2190 Extension: unexpected response", object);
329
+ } else {
330
+ this.onmessage?.(object.method, object.params);
331
+ }
332
+ }
333
+ _onClose(event) {
334
+ debugLogger(`<ws closed> code=${event.code} reason=${event.reason}`);
335
+ this._dispose();
336
+ this.onclose?.(this, event.reason);
337
+ }
338
+ _onError(event) {
339
+ debugLogger(`<ws error> message=${event.message} type=${event.type} target=${event.target}`);
340
+ this._dispose();
341
+ }
342
+ _dispose() {
343
+ for (const callback of this._callbacks.values())
344
+ callback.reject(new Error("WebSocket closed"));
345
+ this._callbacks.clear();
346
+ }
347
+ }
348
+ // Annotate the CommonJS export names for ESM import in node:
349
+ 0 && (module.exports = {
350
+ CDPRelayServer
351
+ });
@@ -0,0 +1,75 @@
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 extensionContextFactory_exports = {};
30
+ __export(extensionContextFactory_exports, {
31
+ ExtensionContextFactory: () => ExtensionContextFactory
32
+ });
33
+ module.exports = __toCommonJS(extensionContextFactory_exports);
34
+ var playwright = __toESM(require("playwright-core"));
35
+ var import_utilsBundle = require("playwright-core/lib/utilsBundle");
36
+ var import_http = require("../sdk/http");
37
+ var import_cdpRelay = require("./cdpRelay");
38
+ const debugLogger = (0, import_utilsBundle.debug)("pw:mcp:relay");
39
+ class ExtensionContextFactory {
40
+ constructor(browserChannel, userDataDir, executablePath) {
41
+ this._browserChannel = browserChannel;
42
+ this._userDataDir = userDataDir;
43
+ this._executablePath = executablePath;
44
+ }
45
+ async createContext(clientInfo, abortSignal, toolName) {
46
+ const browser = await this._obtainBrowser(clientInfo, abortSignal, toolName);
47
+ return {
48
+ browserContext: browser.contexts()[0],
49
+ close: async () => {
50
+ debugLogger("close() called for browser context");
51
+ await browser.close();
52
+ }
53
+ };
54
+ }
55
+ async _obtainBrowser(clientInfo, abortSignal, toolName) {
56
+ const relay = await this._startRelay(abortSignal);
57
+ await relay.ensureExtensionConnectionForMCPContext(clientInfo, abortSignal, toolName);
58
+ return await playwright.chromium.connectOverCDP(relay.cdpEndpoint());
59
+ }
60
+ async _startRelay(abortSignal) {
61
+ const httpServer = await (0, import_http.startHttpServer)({});
62
+ if (abortSignal.aborted) {
63
+ httpServer.close();
64
+ throw new Error(abortSignal.reason);
65
+ }
66
+ const cdpRelayServer = new import_cdpRelay.CDPRelayServer(httpServer, this._browserChannel, this._userDataDir, this._executablePath);
67
+ abortSignal.addEventListener("abort", () => cdpRelayServer.stop());
68
+ debugLogger(`CDP relay server started, extension endpoint: ${cdpRelayServer.extensionEndpoint()}.`);
69
+ return cdpRelayServer;
70
+ }
71
+ }
72
+ // Annotate the CommonJS export names for ESM import in node:
73
+ 0 && (module.exports = {
74
+ ExtensionContextFactory
75
+ });
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var protocol_exports = {};
20
+ __export(protocol_exports, {
21
+ VERSION: () => VERSION
22
+ });
23
+ module.exports = __toCommonJS(protocol_exports);
24
+ const VERSION = 1;
25
+ // Annotate the CommonJS export names for ESM import in node:
26
+ 0 && (module.exports = {
27
+ VERSION
28
+ });