@cotestdev/mcp_playwright 0.0.51 → 0.0.53

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 (71) hide show
  1. package/lib/mcp/browser/browserContextFactory.js +11 -6
  2. package/lib/mcp/browser/browserServerBackend.js +2 -4
  3. package/lib/mcp/browser/config.js +71 -47
  4. package/lib/mcp/browser/context.js +65 -4
  5. package/lib/mcp/browser/logFile.js +96 -0
  6. package/lib/mcp/browser/response.js +107 -104
  7. package/lib/mcp/browser/sessionLog.js +1 -1
  8. package/lib/mcp/browser/tab.js +73 -18
  9. package/lib/mcp/browser/tools/config.js +41 -0
  10. package/lib/mcp/browser/tools/console.js +6 -2
  11. package/lib/mcp/browser/tools/cookies.js +152 -0
  12. package/lib/mcp/browser/tools/install.js +1 -0
  13. package/lib/mcp/browser/tools/network.js +25 -11
  14. package/lib/mcp/browser/tools/pdf.js +3 -4
  15. package/lib/mcp/browser/tools/route.js +140 -0
  16. package/lib/mcp/browser/tools/runCode.js +0 -2
  17. package/lib/mcp/browser/tools/screenshot.js +6 -7
  18. package/lib/mcp/browser/tools/storage.js +3 -4
  19. package/lib/mcp/browser/tools/tracing.js +10 -9
  20. package/lib/mcp/browser/tools/utils.js +0 -6
  21. package/lib/mcp/browser/tools/video.js +31 -13
  22. package/lib/mcp/browser/tools/webstorage.js +223 -0
  23. package/lib/mcp/browser/tools.js +11 -3
  24. package/lib/mcp/extension/cdpRelay.js +7 -7
  25. package/lib/mcp/extension/extensionContextFactory.js +4 -2
  26. package/lib/mcp/program.js +20 -12
  27. package/lib/mcp/terminal/cli.js +23 -2
  28. package/lib/mcp/terminal/command.js +34 -30
  29. package/lib/mcp/terminal/commands.js +329 -46
  30. package/lib/mcp/terminal/daemon.js +25 -39
  31. package/lib/mcp/terminal/helpGenerator.js +27 -7
  32. package/lib/mcp/terminal/program.js +495 -200
  33. package/lib/mcp/terminal/socketConnection.js +17 -2
  34. package/lib/mcpBundleImpl/index.js +27 -27
  35. package/package.json +2 -2
  36. package/lib/common/config.js +0 -281
  37. package/lib/common/configLoader.js +0 -344
  38. package/lib/common/esmLoaderHost.js +0 -104
  39. package/lib/common/expectBundle.js +0 -43
  40. package/lib/common/expectBundleImpl.js +0 -407
  41. package/lib/common/fixtures.js +0 -302
  42. package/lib/common/globals.js +0 -58
  43. package/lib/common/ipc.js +0 -60
  44. package/lib/common/poolBuilder.js +0 -85
  45. package/lib/common/process.js +0 -132
  46. package/lib/common/suiteUtils.js +0 -140
  47. package/lib/common/test.js +0 -322
  48. package/lib/common/testLoader.js +0 -101
  49. package/lib/common/testType.js +0 -298
  50. package/lib/common/validators.js +0 -68
  51. package/lib/mcp/browser/actions.d.js +0 -16
  52. package/lib/mcp/browser/codegen.js +0 -66
  53. package/lib/mcp/browser/processUtils.js +0 -102
  54. package/lib/mcp/browser/tools/script.js +0 -60
  55. package/lib/mcp/sdk/bundle.js +0 -75
  56. package/lib/mcp/sdk/mdb.js +0 -208
  57. package/lib/mcp/sdk/proxyBackend.js +0 -128
  58. package/lib/mcp/vscode/host.js +0 -187
  59. package/lib/mcp/vscode/main.js +0 -77
  60. package/lib/mcpBundleImpl.js +0 -41
  61. package/lib/third_party/pirates.js +0 -62
  62. package/lib/third_party/tsconfig-loader.js +0 -103
  63. package/lib/transform/babelBundle.js +0 -43
  64. package/lib/transform/babelBundleImpl.js +0 -461
  65. package/lib/transform/babelHighlightUtils.js +0 -63
  66. package/lib/transform/compilationCache.js +0 -272
  67. package/lib/transform/esmLoader.js +0 -103
  68. package/lib/transform/portTransport.js +0 -67
  69. package/lib/transform/transform.js +0 -296
  70. package/lib/utilsBundleImpl/index.js +0 -218
  71. package/lib/utilsBundleImpl.js +0 -103
@@ -1,75 +0,0 @@
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 bundle_exports = {};
30
- __export(bundle_exports, {
31
- CallToolRequestSchema: () => CallToolRequestSchema,
32
- Client: () => Client,
33
- ListRootsRequestSchema: () => ListRootsRequestSchema,
34
- ListToolsRequestSchema: () => ListToolsRequestSchema,
35
- PingRequestSchema: () => PingRequestSchema,
36
- SSEServerTransport: () => SSEServerTransport,
37
- Server: () => Server,
38
- StdioClientTransport: () => StdioClientTransport,
39
- StdioServerTransport: () => StdioServerTransport,
40
- StreamableHTTPClientTransport: () => StreamableHTTPClientTransport,
41
- StreamableHTTPServerTransport: () => StreamableHTTPServerTransport,
42
- z: () => z,
43
- zodToJsonSchema: () => zodToJsonSchema
44
- });
45
- module.exports = __toCommonJS(bundle_exports);
46
- var bundle = __toESM(require("../../mcpBundle"));
47
- const zodToJsonSchema = bundle.zodToJsonSchema;
48
- const Client = bundle.Client;
49
- const Server = bundle.Server;
50
- const SSEServerTransport = bundle.SSEServerTransport;
51
- const StdioClientTransport = bundle.StdioClientTransport;
52
- const StdioServerTransport = bundle.StdioServerTransport;
53
- const StreamableHTTPServerTransport = bundle.StreamableHTTPServerTransport;
54
- const StreamableHTTPClientTransport = bundle.StreamableHTTPClientTransport;
55
- const CallToolRequestSchema = bundle.CallToolRequestSchema;
56
- const ListRootsRequestSchema = bundle.ListRootsRequestSchema;
57
- const ListToolsRequestSchema = bundle.ListToolsRequestSchema;
58
- const PingRequestSchema = bundle.PingRequestSchema;
59
- const z = bundle.z;
60
- // Annotate the CommonJS export names for ESM import in node:
61
- 0 && (module.exports = {
62
- CallToolRequestSchema,
63
- Client,
64
- ListRootsRequestSchema,
65
- ListToolsRequestSchema,
66
- PingRequestSchema,
67
- SSEServerTransport,
68
- Server,
69
- StdioClientTransport,
70
- StdioServerTransport,
71
- StreamableHTTPClientTransport,
72
- StreamableHTTPServerTransport,
73
- z,
74
- zodToJsonSchema
75
- });
@@ -1,208 +0,0 @@
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 mdb_exports = {};
30
- __export(mdb_exports, {
31
- MDBBackend: () => MDBBackend,
32
- runMainBackend: () => runMainBackend,
33
- runOnPauseBackendLoop: () => runOnPauseBackendLoop
34
- });
35
- module.exports = __toCommonJS(mdb_exports);
36
- var import_utilsBundle = require("playwright-core/lib/utilsBundle");
37
- var import_utils = require("playwright-core/lib/utils");
38
- var import_tool = require("./tool");
39
- var mcpBundle = __toESM(require("./bundle"));
40
- var mcpServer = __toESM(require("./server"));
41
- var mcpHttp = __toESM(require("./http"));
42
- var import_server = require("./server");
43
- const mdbDebug = (0, import_utilsBundle.debug)("pw:mcp:mdb");
44
- const errorsDebug = (0, import_utilsBundle.debug)("pw:mcp:errors");
45
- const z = mcpBundle.z;
46
- class MDBBackend {
47
- constructor(topLevelBackend) {
48
- this._stack = [];
49
- this._topLevelBackend = topLevelBackend;
50
- }
51
- async initialize(server, clientInfo) {
52
- if (!this._clientInfo)
53
- this._clientInfo = clientInfo;
54
- }
55
- async listTools() {
56
- const client = await this._client();
57
- const response = await client.listTools();
58
- return response.tools;
59
- }
60
- async callTool(name, args) {
61
- await this._client();
62
- if (name === pushToolsSchema.name)
63
- return await this._pushTools(pushToolsSchema.inputSchema.parse(args || {}));
64
- const interruptPromise = new import_utils.ManualPromise();
65
- this._interruptPromise = interruptPromise;
66
- let [entry] = this._stack;
67
- while (entry && !entry.toolNames.includes(name)) {
68
- mdbDebug("popping client from stack for ", name);
69
- this._stack.shift();
70
- await entry.client.close().catch(errorsDebug);
71
- entry = this._stack[0];
72
- }
73
- if (!entry)
74
- throw new Error(`Tool ${name} not found in the tool stack`);
75
- const client = await this._client();
76
- const resultPromise = new import_utils.ManualPromise();
77
- entry.resultPromise = resultPromise;
78
- client.callTool({
79
- name,
80
- arguments: args
81
- }).then((result2) => {
82
- resultPromise.resolve(result2);
83
- }).catch((e) => resultPromise.reject(e));
84
- const result = await Promise.race([interruptPromise, resultPromise]);
85
- if (interruptPromise.isDone())
86
- mdbDebug("client call intercepted", result);
87
- else
88
- mdbDebug("client call result", result);
89
- return result;
90
- }
91
- async _client() {
92
- if (!this._stack.length) {
93
- const transport = await (0, import_server.wrapInProcess)(this._topLevelBackend);
94
- await this._pushClient(transport);
95
- }
96
- return this._stack[0].client;
97
- }
98
- async _pushTools(params) {
99
- mdbDebug("pushing tools to the stack", params.mcpUrl);
100
- const transport = new mcpBundle.StreamableHTTPClientTransport(new URL(params.mcpUrl));
101
- await this._pushClient(transport, params.introMessage);
102
- return { content: [{ type: "text", text: "Tools pushed" }] };
103
- }
104
- async _pushClient(transport, introMessage) {
105
- mdbDebug("pushing client to the stack");
106
- const client = new mcpBundle.Client({ name: "Pushing client", version: "0.0.0" }, { capabilities: { roots: {} } });
107
- client.setRequestHandler(mcpBundle.ListRootsRequestSchema, () => ({ roots: this._clientInfo?.roots || [] }));
108
- client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));
109
- await client.connect(transport);
110
- mdbDebug("connected to the new client");
111
- const { tools } = await client.listTools();
112
- this._stack.unshift({ client, toolNames: tools.map((tool) => tool.name), resultPromise: void 0 });
113
- mdbDebug("new tools added to the stack:", tools.map((tool) => tool.name));
114
- mdbDebug("interrupting current call:", !!this._interruptPromise);
115
- this._interruptPromise?.resolve({
116
- content: [{
117
- type: "text",
118
- text: introMessage || ""
119
- }]
120
- });
121
- this._interruptPromise = void 0;
122
- return { content: [{ type: "text", text: "Tools pushed" }] };
123
- }
124
- }
125
- const pushToolsSchema = (0, import_tool.defineToolSchema)({
126
- name: "mdb_push_tools",
127
- title: "Push MCP tools to the tools stack",
128
- description: "Push MCP tools to the tools stack",
129
- inputSchema: z.object({
130
- mcpUrl: z.string(),
131
- introMessage: z.string().optional()
132
- }),
133
- type: "readOnly"
134
- });
135
- async function runMainBackend(backendFactory, options) {
136
- const mdbBackend = new MDBBackend(backendFactory.create());
137
- const factory = {
138
- ...backendFactory,
139
- create: () => mdbBackend
140
- };
141
- const url = await startAsHttp(factory, { port: options?.port || 0 });
142
- process.env.PLAYWRIGHT_DEBUGGER_MCP = url;
143
- if (options?.port !== void 0)
144
- return url;
145
- await mcpServer.connect(factory, new mcpBundle.StdioServerTransport(), false);
146
- }
147
- async function runOnPauseBackendLoop(backend, introMessage) {
148
- const wrappedBackend = new ServerBackendWithCloseListener(backend);
149
- const factory = {
150
- name: "on-pause-backend",
151
- nameInConfig: "on-pause-backend",
152
- version: "0.0.0",
153
- create: () => wrappedBackend
154
- };
155
- const httpServer = await mcpHttp.startHttpServer({ port: 0 });
156
- await mcpHttp.installHttpTransport(httpServer, factory);
157
- const url = mcpHttp.httpAddressToString(httpServer.address());
158
- const client = new mcpBundle.Client({ name: "On-pause client", version: "0.0.0" });
159
- client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));
160
- const transport = new mcpBundle.StreamableHTTPClientTransport(new URL(process.env.PLAYWRIGHT_DEBUGGER_MCP));
161
- await client.connect(transport);
162
- const pushToolsResult = await client.callTool({
163
- name: pushToolsSchema.name,
164
- arguments: {
165
- mcpUrl: url,
166
- introMessage
167
- }
168
- });
169
- if (pushToolsResult.isError)
170
- errorsDebug("Failed to push tools", pushToolsResult.content);
171
- await transport.terminateSession();
172
- await client.close();
173
- await wrappedBackend.waitForClosed();
174
- httpServer.close();
175
- }
176
- async function startAsHttp(backendFactory, options) {
177
- const httpServer = await mcpHttp.startHttpServer(options);
178
- await mcpHttp.installHttpTransport(httpServer, backendFactory);
179
- return mcpHttp.httpAddressToString(httpServer.address());
180
- }
181
- class ServerBackendWithCloseListener {
182
- constructor(backend) {
183
- this._serverClosedPromise = new import_utils.ManualPromise();
184
- this._backend = backend;
185
- }
186
- async initialize(server, clientInfo) {
187
- await this._backend.initialize?.(server, clientInfo);
188
- }
189
- async listTools() {
190
- return this._backend.listTools();
191
- }
192
- async callTool(name, args) {
193
- return this._backend.callTool(name, args);
194
- }
195
- serverClosed(server) {
196
- this._backend.serverClosed?.(server);
197
- this._serverClosedPromise.resolve();
198
- }
199
- async waitForClosed() {
200
- await this._serverClosedPromise;
201
- }
202
- }
203
- // Annotate the CommonJS export names for ESM import in node:
204
- 0 && (module.exports = {
205
- MDBBackend,
206
- runMainBackend,
207
- runOnPauseBackendLoop
208
- });
@@ -1,128 +0,0 @@
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 proxyBackend_exports = {};
30
- __export(proxyBackend_exports, {
31
- ProxyBackend: () => ProxyBackend
32
- });
33
- module.exports = __toCommonJS(proxyBackend_exports);
34
- var import_utilsBundle = require("playwright-core/lib/utilsBundle");
35
- var mcpBundle = __toESM(require("./bundle"));
36
- const errorsDebug = (0, import_utilsBundle.debug)("pw:mcp:errors");
37
- const { z, zodToJsonSchema } = mcpBundle;
38
- class ProxyBackend {
39
- constructor(mcpProviders) {
40
- this._mcpProviders = mcpProviders;
41
- this._contextSwitchTool = this._defineContextSwitchTool();
42
- }
43
- async initialize(server, clientInfo) {
44
- this._clientInfo = clientInfo;
45
- }
46
- async listTools() {
47
- const currentClient = await this._ensureCurrentClient();
48
- const response = await currentClient.listTools();
49
- if (this._mcpProviders.length === 1)
50
- return response.tools;
51
- return [
52
- ...response.tools,
53
- this._contextSwitchTool
54
- ];
55
- }
56
- async callTool(name, args) {
57
- if (name === this._contextSwitchTool.name)
58
- return this._callContextSwitchTool(args);
59
- const currentClient = await this._ensureCurrentClient();
60
- return await currentClient.callTool({
61
- name,
62
- arguments: args
63
- });
64
- }
65
- serverClosed() {
66
- void this._currentClient?.close().catch(errorsDebug);
67
- }
68
- async _callContextSwitchTool(params) {
69
- try {
70
- const factory = this._mcpProviders.find((factory2) => factory2.name === params.name);
71
- if (!factory)
72
- throw new Error("Unknown connection method: " + params.name);
73
- await this._setCurrentClient(factory);
74
- return {
75
- content: [{ type: "text", text: "### Result\nSuccessfully changed connection method.\n" }]
76
- };
77
- } catch (error) {
78
- return {
79
- content: [{ type: "text", text: `### Result
80
- Error: ${error}
81
- ` }],
82
- isError: true
83
- };
84
- }
85
- }
86
- _defineContextSwitchTool() {
87
- return {
88
- name: "browser_connect",
89
- description: [
90
- "Connect to a browser using one of the available methods:",
91
- ...this._mcpProviders.map((factory) => `- "${factory.name}": ${factory.description}`)
92
- ].join("\n"),
93
- inputSchema: zodToJsonSchema(z.object({
94
- name: z.enum(this._mcpProviders.map((factory) => factory.name)).default(this._mcpProviders[0].name).describe("The method to use to connect to the browser")
95
- }), { strictUnions: true }),
96
- annotations: {
97
- title: "Connect to a browser context",
98
- readOnlyHint: true,
99
- openWorldHint: false
100
- }
101
- };
102
- }
103
- async _ensureCurrentClient() {
104
- if (this._currentClient)
105
- return this._currentClient;
106
- return await this._setCurrentClient(this._mcpProviders[0]);
107
- }
108
- async _setCurrentClient(factory) {
109
- await this._currentClient?.close();
110
- this._currentClient = void 0;
111
- const client = new mcpBundle.Client({ name: "Playwright MCP Proxy", version: "0.0.0" });
112
- client.registerCapabilities({
113
- roots: {
114
- listRoots: true
115
- }
116
- });
117
- client.setRequestHandler(mcpBundle.ListRootsRequestSchema, () => ({ roots: this._clientInfo?.roots || [] }));
118
- client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));
119
- const transport = await factory.connect();
120
- await client.connect(transport);
121
- this._currentClient = client;
122
- return client;
123
- }
124
- }
125
- // Annotate the CommonJS export names for ESM import in node:
126
- 0 && (module.exports = {
127
- ProxyBackend
128
- });
@@ -1,187 +0,0 @@
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 host_exports = {};
30
- __export(host_exports, {
31
- runVSCodeTools: () => runVSCodeTools
32
- });
33
- module.exports = __toCommonJS(host_exports);
34
- var import_path = __toESM(require("path"));
35
- var mcpBundle = __toESM(require("../sdk/bundle"));
36
- var mcpServer = __toESM(require("../sdk/server"));
37
- var import_log = require("../log");
38
- var import_browserServerBackend = require("../browser/browserServerBackend");
39
- var import_browserContextFactory = require("../browser/browserContextFactory");
40
- const packageJSON = require("../../../package.json");
41
- const { z, zodToJsonSchema } = mcpBundle;
42
- const contextSwitchOptions = z.object({
43
- connectionString: z.string().optional().describe("The connection string to use to connect to the browser"),
44
- lib: z.string().optional().describe("The library to use for the connection"),
45
- debugController: z.boolean().optional().describe("Enable the debug controller")
46
- });
47
- class VSCodeProxyBackend {
48
- constructor(_config, _defaultTransportFactory) {
49
- this._config = _config;
50
- this._defaultTransportFactory = _defaultTransportFactory;
51
- this.name = "Playwright MCP Client Switcher";
52
- this.version = packageJSON.version;
53
- this._contextSwitchTool = this._defineContextSwitchTool();
54
- }
55
- async initialize(server, clientInfo) {
56
- this._clientInfo = clientInfo;
57
- const transport = await this._defaultTransportFactory(this);
58
- await this._setCurrentClient(transport);
59
- }
60
- async listTools() {
61
- const response = await this._currentClient.listTools();
62
- return [
63
- ...response.tools,
64
- this._contextSwitchTool
65
- ];
66
- }
67
- async callTool(name, args) {
68
- if (name === this._contextSwitchTool.name)
69
- return this._callContextSwitchTool(args);
70
- return await this._currentClient.callTool({
71
- name,
72
- arguments: args
73
- });
74
- }
75
- serverClosed(server) {
76
- void this._currentClient?.close().catch(import_log.logUnhandledError);
77
- }
78
- onContext(context) {
79
- this._context = context;
80
- context.on("close", () => {
81
- this._context = void 0;
82
- });
83
- }
84
- async _getDebugControllerURL() {
85
- if (!this._context)
86
- return;
87
- const browser = this._context.browser();
88
- if (!browser || !browser._launchServer)
89
- return;
90
- if (this._browser !== browser)
91
- this._browserServer = void 0;
92
- if (!this._browserServer)
93
- this._browserServer = await browser._launchServer({ _debugController: true });
94
- const url = new URL(this._browserServer.wsEndpoint());
95
- url.searchParams.set("debug-controller", "1");
96
- return url.toString();
97
- }
98
- async _callContextSwitchTool(params) {
99
- if (params.debugController) {
100
- const url = await this._getDebugControllerURL();
101
- const lines = [`### Result`];
102
- if (url) {
103
- lines.push(`URL: ${url}`);
104
- lines.push(`Version: ${packageJSON.version}`);
105
- } else {
106
- lines.push(`No open browsers.`);
107
- }
108
- return { content: [{ type: "text", text: lines.join("\n") }] };
109
- }
110
- if (!params.connectionString || !params.lib) {
111
- const transport = await this._defaultTransportFactory(this);
112
- await this._setCurrentClient(transport);
113
- return {
114
- content: [{ type: "text", text: "### Result\nSuccessfully disconnected.\n" }]
115
- };
116
- }
117
- await this._setCurrentClient(
118
- new mcpBundle.StdioClientTransport({
119
- command: process.execPath,
120
- cwd: process.cwd(),
121
- args: [
122
- import_path.default.join(__dirname, "main.js"),
123
- JSON.stringify(this._config),
124
- params.connectionString,
125
- params.lib
126
- ]
127
- })
128
- );
129
- return {
130
- content: [{ type: "text", text: "### Result\nSuccessfully connected.\n" }]
131
- };
132
- }
133
- _defineContextSwitchTool() {
134
- return {
135
- name: "browser_connect",
136
- description: "Do not call, this tool is used in the integration with the Playwright VS Code Extension and meant for programmatic usage only.",
137
- inputSchema: zodToJsonSchema(contextSwitchOptions, { strictUnions: true }),
138
- annotations: {
139
- title: "Connect to a browser running in VS Code.",
140
- readOnlyHint: true,
141
- openWorldHint: false
142
- }
143
- };
144
- }
145
- async _setCurrentClient(transport) {
146
- await this._currentClient?.close();
147
- this._currentClient = void 0;
148
- const client = new mcpBundle.Client({ name: this._clientInfo.name, version: this._clientInfo.version });
149
- client.registerCapabilities({
150
- roots: {
151
- listRoots: true
152
- }
153
- });
154
- client.setRequestHandler(mcpBundle.ListRootsRequestSchema, () => ({ roots: this._clientInfo.roots }));
155
- client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));
156
- await client.connect(transport);
157
- this._currentClient = client;
158
- }
159
- }
160
- async function runVSCodeTools(config) {
161
- const serverBackendFactory = {
162
- name: "Playwright w/ vscode",
163
- nameInConfig: "playwright-vscode",
164
- version: packageJSON.version,
165
- create: () => new VSCodeProxyBackend(
166
- config,
167
- (delegate) => mcpServer.wrapInProcess(
168
- new import_browserServerBackend.BrowserServerBackend(
169
- config,
170
- {
171
- async createContext(clientInfo, abortSignal, toolName) {
172
- const context = await (0, import_browserContextFactory.contextFactory)(config).createContext(clientInfo, abortSignal, toolName);
173
- delegate.onContext(context.browserContext);
174
- return context;
175
- }
176
- }
177
- )
178
- )
179
- )
180
- };
181
- await mcpServer.start(serverBackendFactory, config.server);
182
- return;
183
- }
184
- // Annotate the CommonJS export names for ESM import in node:
185
- 0 && (module.exports = {
186
- runVSCodeTools
187
- });
@@ -1,77 +0,0 @@
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 __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- var mcpBundle = __toESM(require("../sdk/bundle"));
25
- var mcpServer = __toESM(require("../sdk/server"));
26
- var import_browserServerBackend = require("../browser/browserServerBackend");
27
- class VSCodeBrowserContextFactory {
28
- constructor(_config, _playwright, _connectionString) {
29
- this._config = _config;
30
- this._playwright = _playwright;
31
- this._connectionString = _connectionString;
32
- this.name = "vscode";
33
- this.description = "Connect to a browser running in the Playwright VS Code extension";
34
- }
35
- async createContext(clientInfo, abortSignal) {
36
- let launchOptions = this._config.browser.launchOptions;
37
- if (this._config.browser.userDataDir) {
38
- launchOptions = {
39
- ...launchOptions,
40
- ...this._config.browser.contextOptions,
41
- userDataDir: this._config.browser.userDataDir
42
- };
43
- }
44
- const connectionString = new URL(this._connectionString);
45
- connectionString.searchParams.set("launch-options", JSON.stringify(launchOptions));
46
- const browserType = this._playwright.chromium;
47
- const browser = await browserType.connect(connectionString.toString());
48
- const context = browser.contexts()[0] ?? await browser.newContext(this._config.browser.contextOptions);
49
- return {
50
- browserContext: context,
51
- close: async () => {
52
- await browser.close();
53
- }
54
- };
55
- }
56
- }
57
- async function main(config, connectionString, lib) {
58
- const playwright = await import(lib).then((mod) => mod.default ?? mod);
59
- const factory = new VSCodeBrowserContextFactory(config, playwright, connectionString);
60
- await mcpServer.connect(
61
- {
62
- name: "Playwright MCP",
63
- nameInConfig: "playwright-vscode",
64
- create: () => new import_browserServerBackend.BrowserServerBackend(config, factory),
65
- version: "unused"
66
- },
67
- new mcpBundle.StdioServerTransport(),
68
- false
69
- );
70
- }
71
- void (async () => {
72
- await main(
73
- JSON.parse(process.argv[2]),
74
- process.argv[3],
75
- process.argv[4]
76
- );
77
- })();