@cotestdev/mcp_playwright 0.0.2 → 0.0.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.
- package/lib/cli.d.ts +8 -0
- package/lib/cli.d.ts.map +1 -0
- package/lib/cli.js +11 -0
- package/lib/cli.js.map +1 -0
- package/lib/index.d.ts +4 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +27 -4
- package/lib/index.js.map +1 -1
- package/lib/mcp/browser/actions.d.js +16 -0
- package/lib/mcp/browser/browserContextFactory.js +285 -0
- package/lib/mcp/browser/browserServerBackend.js +74 -0
- package/lib/mcp/browser/codegen.js +66 -0
- package/lib/mcp/browser/config.js +346 -0
- package/lib/mcp/browser/context.js +260 -0
- package/lib/mcp/browser/processUtils.js +102 -0
- package/lib/mcp/browser/response.js +169 -0
- package/lib/mcp/browser/sessionLog.js +160 -0
- package/lib/mcp/browser/tab.js +301 -0
- package/lib/mcp/browser/tools/common.js +71 -0
- package/lib/mcp/browser/tools/console.js +41 -0
- package/lib/mcp/browser/tools/dialogs.js +61 -0
- package/lib/mcp/browser/tools/evaluate.js +71 -0
- package/lib/mcp/browser/tools/files.js +54 -0
- package/lib/mcp/browser/tools/form.js +75 -0
- package/lib/mcp/browser/tools/install.js +69 -0
- package/lib/mcp/browser/tools/keyboard.js +85 -0
- package/lib/mcp/browser/tools/mouse.js +107 -0
- package/lib/mcp/browser/tools/navigate.js +63 -0
- package/lib/mcp/browser/tools/network.js +49 -0
- package/lib/mcp/browser/tools/pdf.js +59 -0
- package/lib/mcp/browser/tools/screenshot.js +95 -0
- package/lib/mcp/browser/tools/script.js +60 -0
- package/lib/mcp/browser/tools/snapshot.js +183 -0
- package/lib/mcp/browser/tools/tabs.js +71 -0
- package/lib/mcp/browser/tools/tool.js +49 -0
- package/lib/mcp/browser/tools/tracing.js +74 -0
- package/lib/mcp/browser/tools/utils.js +96 -0
- package/lib/mcp/browser/tools/verify.js +155 -0
- package/lib/mcp/browser/tools/wait.js +64 -0
- package/lib/mcp/browser/tools.js +79 -0
- package/lib/mcp/browser/watchdog.js +44 -0
- package/lib/mcp/sdk/bundle.js +75 -0
- package/lib/mcp/sdk/exports.js +32 -0
- package/lib/mcp/sdk/http.js +165 -0
- package/lib/mcp/sdk/inProcessTransport.js +71 -0
- package/lib/mcp/sdk/mdb.js +208 -0
- package/lib/mcp/sdk/proxyBackend.js +128 -0
- package/lib/mcp/sdk/server.js +155 -0
- package/lib/mcp/sdk/tool.js +46 -0
- package/lib/server.d.ts +8 -0
- package/lib/server.d.ts.map +1 -0
- package/lib/server.js +27 -0
- package/lib/server.js.map +1 -0
- package/package.json +10 -4
|
@@ -0,0 +1,208 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
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 server_exports = {};
|
|
30
|
+
__export(server_exports, {
|
|
31
|
+
connect: () => connect,
|
|
32
|
+
createServer: () => createServer,
|
|
33
|
+
firstRootPath: () => firstRootPath,
|
|
34
|
+
start: () => start,
|
|
35
|
+
wrapInProcess: () => wrapInProcess
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(server_exports);
|
|
38
|
+
var import_url = require("url");
|
|
39
|
+
var import_utilsBundle = require("playwright-core/lib/utilsBundle");
|
|
40
|
+
var mcpBundle = __toESM(require("./bundle"));
|
|
41
|
+
var import_http = require("./http");
|
|
42
|
+
var import_inProcessTransport = require("./inProcessTransport");
|
|
43
|
+
const serverDebug = (0, import_utilsBundle.debug)("pw:mcp:server");
|
|
44
|
+
async function connect(factory, transport, runHeartbeat) {
|
|
45
|
+
const server = createServer(factory.name, factory.version, factory.create(), runHeartbeat);
|
|
46
|
+
await server.connect(transport);
|
|
47
|
+
}
|
|
48
|
+
async function wrapInProcess(backend) {
|
|
49
|
+
const server = createServer("Internal", "0.0.0", backend, false);
|
|
50
|
+
return new import_inProcessTransport.InProcessTransport(server);
|
|
51
|
+
}
|
|
52
|
+
function createServer(name, version, backend, runHeartbeat) {
|
|
53
|
+
const server = new mcpBundle.Server({ name, version }, {
|
|
54
|
+
capabilities: {
|
|
55
|
+
tools: {}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
server.setRequestHandler(mcpBundle.ListToolsRequestSchema, async () => {
|
|
59
|
+
serverDebug("listTools");
|
|
60
|
+
const tools = await backend.listTools();
|
|
61
|
+
return { tools };
|
|
62
|
+
});
|
|
63
|
+
let initializePromise;
|
|
64
|
+
server.setRequestHandler(mcpBundle.CallToolRequestSchema, async (request) => {
|
|
65
|
+
serverDebug("callTool", request);
|
|
66
|
+
try {
|
|
67
|
+
if (!initializePromise)
|
|
68
|
+
initializePromise = initializeServer(server, backend, runHeartbeat);
|
|
69
|
+
await initializePromise;
|
|
70
|
+
return await backend.callTool(request.params.name, request.params.arguments || {});
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return {
|
|
73
|
+
content: [{ type: "text", text: "### Result\n" + String(error) }],
|
|
74
|
+
isError: true
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
addServerListener(server, "close", () => backend.serverClosed?.(server));
|
|
79
|
+
return server;
|
|
80
|
+
}
|
|
81
|
+
const initializeServer = async (server, backend, runHeartbeat) => {
|
|
82
|
+
const capabilities = server.getClientCapabilities();
|
|
83
|
+
let clientRoots = [];
|
|
84
|
+
if (capabilities?.roots) {
|
|
85
|
+
const { roots } = await server.listRoots().catch((e) => {
|
|
86
|
+
serverDebug(e);
|
|
87
|
+
return { roots: [] };
|
|
88
|
+
});
|
|
89
|
+
clientRoots = roots;
|
|
90
|
+
}
|
|
91
|
+
const clientInfo = {
|
|
92
|
+
name: server.getClientVersion()?.name ?? "unknown",
|
|
93
|
+
version: server.getClientVersion()?.version ?? "unknown",
|
|
94
|
+
roots: clientRoots,
|
|
95
|
+
timestamp: Date.now()
|
|
96
|
+
};
|
|
97
|
+
await backend.initialize?.(server, clientInfo);
|
|
98
|
+
if (runHeartbeat)
|
|
99
|
+
startHeartbeat(server);
|
|
100
|
+
};
|
|
101
|
+
const startHeartbeat = (server) => {
|
|
102
|
+
const beat = () => {
|
|
103
|
+
Promise.race([
|
|
104
|
+
server.ping(),
|
|
105
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error("ping timeout")), 5e3))
|
|
106
|
+
]).then(() => {
|
|
107
|
+
setTimeout(beat, 3e3);
|
|
108
|
+
}).catch(() => {
|
|
109
|
+
void server.close();
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
beat();
|
|
113
|
+
};
|
|
114
|
+
function addServerListener(server, event, listener) {
|
|
115
|
+
const oldListener = server[`on${event}`];
|
|
116
|
+
server[`on${event}`] = () => {
|
|
117
|
+
oldListener?.();
|
|
118
|
+
listener();
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
async function start(serverBackendFactory, options) {
|
|
122
|
+
if (options.port === void 0) {
|
|
123
|
+
await connect(serverBackendFactory, new mcpBundle.StdioServerTransport(), false);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const httpServer = await (0, import_http.startHttpServer)(options);
|
|
127
|
+
await (0, import_http.installHttpTransport)(httpServer, serverBackendFactory);
|
|
128
|
+
const url = (0, import_http.httpAddressToString)(httpServer.address());
|
|
129
|
+
const mcpConfig = { mcpServers: {} };
|
|
130
|
+
mcpConfig.mcpServers[serverBackendFactory.nameInConfig] = {
|
|
131
|
+
url: `${url}/mcp`
|
|
132
|
+
};
|
|
133
|
+
const message = [
|
|
134
|
+
`Listening on ${url}`,
|
|
135
|
+
"Put this in your client config:",
|
|
136
|
+
JSON.stringify(mcpConfig, void 0, 2),
|
|
137
|
+
"For legacy SSE transport support, you can use the /sse endpoint instead."
|
|
138
|
+
].join("\n");
|
|
139
|
+
console.error(message);
|
|
140
|
+
}
|
|
141
|
+
function firstRootPath(clientInfo) {
|
|
142
|
+
if (clientInfo.roots.length === 0)
|
|
143
|
+
return void 0;
|
|
144
|
+
const firstRootUri = clientInfo.roots[0]?.uri;
|
|
145
|
+
const url = firstRootUri ? new URL(firstRootUri) : void 0;
|
|
146
|
+
return url ? (0, import_url.fileURLToPath)(url) : void 0;
|
|
147
|
+
}
|
|
148
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
149
|
+
0 && (module.exports = {
|
|
150
|
+
connect,
|
|
151
|
+
createServer,
|
|
152
|
+
firstRootPath,
|
|
153
|
+
start,
|
|
154
|
+
wrapInProcess
|
|
155
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
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 tool_exports = {};
|
|
20
|
+
__export(tool_exports, {
|
|
21
|
+
defineToolSchema: () => defineToolSchema,
|
|
22
|
+
toMcpTool: () => toMcpTool
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(tool_exports);
|
|
25
|
+
var import_bundle = require("../sdk/bundle");
|
|
26
|
+
function toMcpTool(tool) {
|
|
27
|
+
return {
|
|
28
|
+
name: tool.name,
|
|
29
|
+
description: tool.description,
|
|
30
|
+
inputSchema: (0, import_bundle.zodToJsonSchema)(tool.inputSchema, { strictUnions: true }),
|
|
31
|
+
annotations: {
|
|
32
|
+
title: tool.title,
|
|
33
|
+
readOnlyHint: tool.type === "readOnly",
|
|
34
|
+
destructiveHint: tool.type === "destructive",
|
|
35
|
+
openWorldHint: true
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function defineToolSchema(tool) {
|
|
40
|
+
return tool;
|
|
41
|
+
}
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
defineToolSchema,
|
|
45
|
+
toMcpTool
|
|
46
|
+
});
|
package/lib/server.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";AAEA;;;;GAIG"}
|
package/lib/server.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
/**
|
|
4
|
+
* @cotestdev/mcp_playwright Server
|
|
5
|
+
*
|
|
6
|
+
* Standalone MCP server for Playwright browser automation
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
// Start the MCP server
|
|
10
|
+
async function main() {
|
|
11
|
+
try {
|
|
12
|
+
console.log('Starting Playwright MCP Server...');
|
|
13
|
+
console.log('Version: 0.0.2');
|
|
14
|
+
console.log('MCP Tools: execute_playwright_script, dialogs, tabs, and 15+ more');
|
|
15
|
+
console.log('');
|
|
16
|
+
console.log('For integration with MCP clients, use:');
|
|
17
|
+
console.log(' npx @cotestdev/mcp_playwright');
|
|
18
|
+
console.log('');
|
|
19
|
+
console.log('Server is ready to accept connections.');
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
console.error('Failed to start MCP server:', error);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
main().catch(console.error);
|
|
27
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;AAEA;;;;GAIG;;AAIH,uBAAuB;AACvB,KAAK,UAAU,IAAI;IACjB,IAAI,CAAC;QACH,OAAO,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;QACjD,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;QAC9B,OAAO,CAAC,GAAG,CAAC,mEAAmE,CAAC,CAAC;QACjF,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChB,OAAO,CAAC,GAAG,CAAC,wCAAwC,CAAC,CAAC;QACtD,OAAO,CAAC,GAAG,CAAC,iCAAiC,CAAC,CAAC;QAC/C,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChB,OAAO,CAAC,GAAG,CAAC,wCAAwC,CAAC,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAC;QACpD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cotestdev/mcp_playwright",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Playwright MCP (Model Context Protocol) tools for browser automation",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"mcp-playwright": "lib/server.js"
|
|
9
|
+
},
|
|
7
10
|
"files": [
|
|
8
|
-
"lib
|
|
11
|
+
"lib/**/*.js",
|
|
12
|
+
"lib/**/*.d.ts",
|
|
13
|
+
"lib/**/*.map"
|
|
9
14
|
],
|
|
10
15
|
"scripts": {
|
|
11
|
-
"build": "tsc",
|
|
12
|
-
"publish": "npm publish --access public"
|
|
16
|
+
"build": "tsc && node scripts/copy-mcp-tools.js",
|
|
17
|
+
"publish": "npm publish --access public",
|
|
18
|
+
"start": "node lib/server.js"
|
|
13
19
|
},
|
|
14
20
|
"keywords": [
|
|
15
21
|
"playwright",
|