@cotestdev/mcp_playwright 0.0.9 → 0.0.11

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.
@@ -0,0 +1,41 @@
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 streams_exports = {};
20
+ __export(streams_exports, {
21
+ StringWriteStream: () => StringWriteStream
22
+ });
23
+ module.exports = __toCommonJS(streams_exports);
24
+ var import_stream = require("stream");
25
+ class StringWriteStream extends import_stream.Writable {
26
+ constructor() {
27
+ super(...arguments);
28
+ this._chunks = [];
29
+ }
30
+ _write(chunk, encoding, callback) {
31
+ this._chunks.push(chunk.toString());
32
+ callback();
33
+ }
34
+ content() {
35
+ return this._chunks.join("");
36
+ }
37
+ }
38
+ // Annotate the CommonJS export names for ESM import in node:
39
+ 0 && (module.exports = {
40
+ StringWriteStream
41
+ });
@@ -0,0 +1,79 @@
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 testBackend_exports = {};
30
+ __export(testBackend_exports, {
31
+ TestServerBackend: () => TestServerBackend
32
+ });
33
+ module.exports = __toCommonJS(testBackend_exports);
34
+ var mcp = __toESM(require("../sdk/exports"));
35
+ var import_testContext = require("./testContext");
36
+ var import_testTools = require("./testTools.js");
37
+ var import_tools = require("../browser/tools");
38
+ var import_configLoader = require("../../common/configLoader");
39
+ class TestServerBackend {
40
+ constructor(configOption, options) {
41
+ this.name = "Playwright";
42
+ this.version = "0.0.1";
43
+ this._tools = [import_testTools.listTests, import_testTools.runTests, import_testTools.debugTest, import_testTools.setupPage];
44
+ this._context = new import_testContext.TestContext(options);
45
+ this._configOption = configOption;
46
+ }
47
+ async initialize(server, clientInfo) {
48
+ if (this._configOption) {
49
+ this._context.setConfigLocation((0, import_configLoader.resolveConfigLocation)(this._configOption));
50
+ return;
51
+ }
52
+ const rootPath = mcp.firstRootPath(clientInfo);
53
+ if (rootPath) {
54
+ this._context.setConfigLocation((0, import_configLoader.resolveConfigLocation)(rootPath));
55
+ return;
56
+ }
57
+ throw new Error("No config option or MCP root path provided");
58
+ }
59
+ async listTools() {
60
+ return [
61
+ ...this._tools.map((tool) => mcp.toMcpTool(tool.schema)),
62
+ ...import_tools.browserTools.map((tool) => mcp.toMcpTool(tool.schema))
63
+ ];
64
+ }
65
+ async callTool(name, args) {
66
+ const tool = this._tools.find((tool2) => tool2.schema.name === name);
67
+ if (!tool)
68
+ throw new Error(`Tool not found: ${name}. Available tools: ${this._tools.map((tool2) => tool2.schema.name).join(", ")}`);
69
+ const parsedArguments = tool.schema.inputSchema.parse(args || {});
70
+ return await tool.handle(this._context, parsedArguments);
71
+ }
72
+ serverClosed() {
73
+ void this._context.close();
74
+ }
75
+ }
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ TestServerBackend
79
+ });
@@ -0,0 +1,50 @@
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 testContext_exports = {};
20
+ __export(testContext_exports, {
21
+ TestContext: () => TestContext
22
+ });
23
+ module.exports = __toCommonJS(testContext_exports);
24
+ var import_testRunner = require("../../runner/testRunner");
25
+ class TestContext {
26
+ constructor(options) {
27
+ this.options = options;
28
+ }
29
+ setConfigLocation(configLocation) {
30
+ this.configLocation = configLocation;
31
+ }
32
+ async createTestRunner() {
33
+ if (this._testRunner)
34
+ await this._testRunner.stopTests();
35
+ const testRunner = new import_testRunner.TestRunner(this.configLocation, {});
36
+ await testRunner.initialize({});
37
+ this._testRunner = testRunner;
38
+ testRunner.on(import_testRunner.TestRunnerEvent.TestFilesChanged, (testFiles) => {
39
+ this._testRunner?.emit(import_testRunner.TestRunnerEvent.TestFilesChanged, testFiles);
40
+ });
41
+ this._testRunner = testRunner;
42
+ return testRunner;
43
+ }
44
+ async close() {
45
+ }
46
+ }
47
+ // Annotate the CommonJS export names for ESM import in node:
48
+ 0 && (module.exports = {
49
+ TestContext
50
+ });
@@ -0,0 +1,30 @@
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 testTool_exports = {};
20
+ __export(testTool_exports, {
21
+ defineTestTool: () => defineTestTool
22
+ });
23
+ module.exports = __toCommonJS(testTool_exports);
24
+ function defineTestTool(tool) {
25
+ return tool;
26
+ }
27
+ // Annotate the CommonJS export names for ESM import in node:
28
+ 0 && (module.exports = {
29
+ defineTestTool
30
+ });
@@ -0,0 +1,194 @@
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 testTools_exports = {};
30
+ __export(testTools_exports, {
31
+ debugTest: () => debugTest,
32
+ listTests: () => listTests,
33
+ runTests: () => runTests,
34
+ setupPage: () => setupPage
35
+ });
36
+ module.exports = __toCommonJS(testTools_exports);
37
+ var import_fs = __toESM(require("fs"));
38
+ var import_path = __toESM(require("path"));
39
+ var import_utils = require("playwright-core/lib/utils");
40
+ var import_bundle = require("../sdk/bundle");
41
+ var import_base = require("../../reporters/base");
42
+ var import_list = __toESM(require("../../reporters/list"));
43
+ var import_listModeReporter = __toESM(require("../../reporters/listModeReporter"));
44
+ var import_projectUtils = require("../../runner/projectUtils");
45
+ var import_testTool = require("./testTool");
46
+ var import_streams = require("./streams");
47
+ const listTests = (0, import_testTool.defineTestTool)({
48
+ schema: {
49
+ name: "test_list",
50
+ title: "List tests",
51
+ description: "List tests",
52
+ inputSchema: import_bundle.z.object({}),
53
+ type: "readOnly"
54
+ },
55
+ handle: async (context) => {
56
+ const { screen, stream } = createScreen();
57
+ const reporter = new import_listModeReporter.default({ screen, includeTestId: true });
58
+ const testRunner = await context.createTestRunner();
59
+ await testRunner.listTests(reporter, {});
60
+ return {
61
+ content: [{ type: "text", text: stream.content() }]
62
+ };
63
+ }
64
+ });
65
+ const runTests = (0, import_testTool.defineTestTool)({
66
+ schema: {
67
+ name: "test_run",
68
+ title: "Run tests",
69
+ description: "Run tests",
70
+ inputSchema: import_bundle.z.object({
71
+ locations: import_bundle.z.array(import_bundle.z.string()).optional().describe('Folder, file or location to run: "test/e2e" or "test/e2e/file.spec.ts" or "test/e2e/file.spec.ts:20"'),
72
+ projects: import_bundle.z.array(import_bundle.z.string()).optional().describe('Projects to run, projects from playwright.config.ts, by default runs all projects. Running with "chromium" is a good start')
73
+ }),
74
+ type: "readOnly"
75
+ },
76
+ handle: async (context, params) => {
77
+ const { screen, stream } = createScreen();
78
+ const configDir = context.configLocation.configDir;
79
+ const reporter = new import_list.default({ configDir, screen, includeTestId: true });
80
+ const testRunner = await context.createTestRunner();
81
+ await testRunner.runTests(reporter, {
82
+ locations: params.locations,
83
+ projects: params.projects,
84
+ disableConfigReporters: true
85
+ });
86
+ const text = stream.content();
87
+ return {
88
+ content: [
89
+ { type: "text", text }
90
+ ]
91
+ };
92
+ }
93
+ });
94
+ const debugTest = (0, import_testTool.defineTestTool)({
95
+ schema: {
96
+ name: "test_debug",
97
+ title: "Debug single test",
98
+ description: "Debug single test",
99
+ inputSchema: import_bundle.z.object({
100
+ test: import_bundle.z.object({
101
+ id: import_bundle.z.string().describe("Test ID to debug."),
102
+ title: import_bundle.z.string().describe("Human readable test title for granting permission to debug the test.")
103
+ })
104
+ }),
105
+ type: "readOnly"
106
+ },
107
+ handle: async (context, params) => {
108
+ const { screen, stream } = createScreen();
109
+ const configDir = context.configLocation.configDir;
110
+ const reporter = new import_list.default({ configDir, screen });
111
+ const testRunner = await context.createTestRunner();
112
+ const result = await testRunner.runTests(reporter, {
113
+ headed: !context.options?.headless,
114
+ testIds: [params.test.id],
115
+ // For automatic recovery
116
+ timeout: 0,
117
+ workers: 1,
118
+ pauseOnError: true,
119
+ disableConfigReporters: true
120
+ });
121
+ const text = stream.content();
122
+ return {
123
+ content: [
124
+ { type: "text", text }
125
+ ],
126
+ isError: result.status !== "passed"
127
+ };
128
+ }
129
+ });
130
+ const setupPage = (0, import_testTool.defineTestTool)({
131
+ schema: {
132
+ name: "test_setup_page",
133
+ title: "Setup page",
134
+ description: "Setup the page for test",
135
+ inputSchema: import_bundle.z.object({
136
+ project: import_bundle.z.string().optional().describe('Project to use for setup. For example: "chromium", if no project is provided uses the first project in the config.'),
137
+ testLocation: import_bundle.z.string().optional().describe('Location of the seed test to use for setup. For example: "test/seed/default.spec.ts:20".')
138
+ }),
139
+ type: "readOnly"
140
+ },
141
+ handle: async (context, params) => {
142
+ const { screen, stream } = createScreen();
143
+ const configDir = context.configLocation.configDir;
144
+ const reporter = new import_list.default({ configDir, screen });
145
+ const testRunner = await context.createTestRunner();
146
+ let testLocation = params.testLocation;
147
+ if (!testLocation) {
148
+ testLocation = "default.seed.spec.ts";
149
+ const config = await testRunner.loadConfig();
150
+ const project = params.project ? config.projects.find((p) => p.project.name === params.project) : (0, import_projectUtils.findTopLevelProjects)(config)[0];
151
+ const testDir = project?.project.testDir || configDir;
152
+ const seedFile = import_path.default.join(testDir, testLocation);
153
+ if (!import_fs.default.existsSync(seedFile)) {
154
+ await import_fs.default.promises.mkdir(import_path.default.dirname(seedFile), { recursive: true });
155
+ await import_fs.default.promises.writeFile(seedFile, `import { test, expect } from '@playwright/test';
156
+
157
+ test('seed', async ({ page }) => {});
158
+ `);
159
+ }
160
+ }
161
+ const result = await testRunner.runTests(reporter, {
162
+ headed: !context.options?.headless,
163
+ locations: [testLocation],
164
+ projects: params.project ? [params.project] : void 0,
165
+ timeout: 0,
166
+ workers: 1,
167
+ pauseAtEnd: true,
168
+ disableConfigReporters: true
169
+ });
170
+ const text = stream.content();
171
+ return {
172
+ content: [{ type: "text", text }],
173
+ isError: result.status !== "passed"
174
+ };
175
+ }
176
+ });
177
+ function createScreen() {
178
+ const stream = new import_streams.StringWriteStream();
179
+ const screen = {
180
+ ...import_base.terminalScreen,
181
+ isTTY: false,
182
+ colors: import_utils.noColors,
183
+ stdout: stream,
184
+ stderr: stream
185
+ };
186
+ return { screen, stream };
187
+ }
188
+ // Annotate the CommonJS export names for ESM import in node:
189
+ 0 && (module.exports = {
190
+ debugTest,
191
+ listTests,
192
+ runTests,
193
+ setupPage
194
+ });
@@ -0,0 +1,187 @@
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
+ });
@@ -0,0 +1,77 @@
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
+ })();