|
f7eb47b…
|
lmata
|
1 |
"use strict"; |
|
f7eb47b…
|
lmata
|
2 |
var __create = Object.create; |
|
f7eb47b…
|
lmata
|
3 |
var __defProp = Object.defineProperty; |
|
f7eb47b…
|
lmata
|
4 |
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
|
f7eb47b…
|
lmata
|
5 |
var __getOwnPropNames = Object.getOwnPropertyNames; |
|
f7eb47b…
|
lmata
|
6 |
var __getProtoOf = Object.getPrototypeOf; |
|
f7eb47b…
|
lmata
|
7 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
|
f7eb47b…
|
lmata
|
8 |
var __export = (target, all) => { |
|
f7eb47b…
|
lmata
|
9 |
for (var name in all) |
|
f7eb47b…
|
lmata
|
10 |
__defProp(target, name, { get: all[name], enumerable: true }); |
|
f7eb47b…
|
lmata
|
11 |
}; |
|
f7eb47b…
|
lmata
|
12 |
var __copyProps = (to, from, except, desc) => { |
|
f7eb47b…
|
lmata
|
13 |
if (from && typeof from === "object" || typeof from === "function") { |
|
f7eb47b…
|
lmata
|
14 |
for (let key of __getOwnPropNames(from)) |
|
f7eb47b…
|
lmata
|
15 |
if (!__hasOwnProp.call(to, key) && key !== except) |
|
f7eb47b…
|
lmata
|
16 |
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
|
f7eb47b…
|
lmata
|
17 |
} |
|
f7eb47b…
|
lmata
|
18 |
return to; |
|
f7eb47b…
|
lmata
|
19 |
}; |
|
f7eb47b…
|
lmata
|
20 |
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
|
f7eb47b…
|
lmata
|
21 |
// If the importer is in node compatibility mode or this is not an ESM |
|
f7eb47b…
|
lmata
|
22 |
// file that has been converted to a CommonJS file using a Babel- |
|
f7eb47b…
|
lmata
|
23 |
// compatible transform (i.e. "__esModule" has not been set), then set |
|
f7eb47b…
|
lmata
|
24 |
// "default" to the CommonJS "module.exports" for node compatibility. |
|
f7eb47b…
|
lmata
|
25 |
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
|
f7eb47b…
|
lmata
|
26 |
mod |
|
f7eb47b…
|
lmata
|
27 |
)); |
|
f7eb47b…
|
lmata
|
28 |
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
|
f7eb47b…
|
lmata
|
29 |
var server_exports = {}; |
|
f7eb47b…
|
lmata
|
30 |
__export(server_exports, { |
|
f7eb47b…
|
lmata
|
31 |
allRootPaths: () => allRootPaths, |
|
f7eb47b…
|
lmata
|
32 |
connect: () => connect, |
|
f7eb47b…
|
lmata
|
33 |
createServer: () => createServer, |
|
f7eb47b…
|
lmata
|
34 |
firstRootPath: () => firstRootPath, |
|
f7eb47b…
|
lmata
|
35 |
start: () => start, |
|
f7eb47b…
|
lmata
|
36 |
wrapInClient: () => wrapInClient, |
|
f7eb47b…
|
lmata
|
37 |
wrapInProcess: () => wrapInProcess |
|
f7eb47b…
|
lmata
|
38 |
}); |
|
f7eb47b…
|
lmata
|
39 |
module.exports = __toCommonJS(server_exports); |
|
f7eb47b…
|
lmata
|
40 |
var import_url = require("url"); |
|
f7eb47b…
|
lmata
|
41 |
var import_utilsBundle = require("playwright-core/lib/utilsBundle"); |
|
f7eb47b…
|
lmata
|
42 |
var mcpBundle = __toESM(require("playwright-core/lib/mcpBundle")); |
|
f7eb47b…
|
lmata
|
43 |
var import_http = require("./http"); |
|
f7eb47b…
|
lmata
|
44 |
var import_inProcessTransport = require("./inProcessTransport"); |
|
f7eb47b…
|
lmata
|
45 |
const serverDebug = (0, import_utilsBundle.debug)("pw:mcp:server"); |
|
f7eb47b…
|
lmata
|
46 |
const serverDebugResponse = (0, import_utilsBundle.debug)("pw:mcp:server:response"); |
|
f7eb47b…
|
lmata
|
47 |
async function connect(factory, transport, runHeartbeat) { |
|
f7eb47b…
|
lmata
|
48 |
const server = createServer(factory.name, factory.version, factory.create(), runHeartbeat); |
|
f7eb47b…
|
lmata
|
49 |
await server.connect(transport); |
|
f7eb47b…
|
lmata
|
50 |
} |
|
f7eb47b…
|
lmata
|
51 |
function wrapInProcess(backend) { |
|
f7eb47b…
|
lmata
|
52 |
const server = createServer("Internal", "0.0.0", backend, false); |
|
f7eb47b…
|
lmata
|
53 |
return new import_inProcessTransport.InProcessTransport(server); |
|
f7eb47b…
|
lmata
|
54 |
} |
|
f7eb47b…
|
lmata
|
55 |
async function wrapInClient(backend, options) { |
|
f7eb47b…
|
lmata
|
56 |
const server = createServer("Internal", "0.0.0", backend, false); |
|
f7eb47b…
|
lmata
|
57 |
const transport = new import_inProcessTransport.InProcessTransport(server); |
|
f7eb47b…
|
lmata
|
58 |
const client = new mcpBundle.Client({ name: options.name, version: options.version }); |
|
f7eb47b…
|
lmata
|
59 |
await client.connect(transport); |
|
f7eb47b…
|
lmata
|
60 |
await client.ping(); |
|
f7eb47b…
|
lmata
|
61 |
return client; |
|
f7eb47b…
|
lmata
|
62 |
} |
|
f7eb47b…
|
lmata
|
63 |
function createServer(name, version, backend, runHeartbeat) { |
|
f7eb47b…
|
lmata
|
64 |
const server = new mcpBundle.Server({ name, version }, { |
|
f7eb47b…
|
lmata
|
65 |
capabilities: { |
|
f7eb47b…
|
lmata
|
66 |
tools: {} |
|
f7eb47b…
|
lmata
|
67 |
} |
|
f7eb47b…
|
lmata
|
68 |
}); |
|
f7eb47b…
|
lmata
|
69 |
server.setRequestHandler(mcpBundle.ListToolsRequestSchema, async () => { |
|
f7eb47b…
|
lmata
|
70 |
serverDebug("listTools"); |
|
f7eb47b…
|
lmata
|
71 |
const tools = await backend.listTools(); |
|
f7eb47b…
|
lmata
|
72 |
return { tools }; |
|
f7eb47b…
|
lmata
|
73 |
}); |
|
f7eb47b…
|
lmata
|
74 |
let initializePromise; |
|
f7eb47b…
|
lmata
|
75 |
server.setRequestHandler(mcpBundle.CallToolRequestSchema, async (request, extra) => { |
|
f7eb47b…
|
lmata
|
76 |
serverDebug("callTool", request); |
|
f7eb47b…
|
lmata
|
77 |
const progressToken = request.params._meta?.progressToken; |
|
f7eb47b…
|
lmata
|
78 |
let progressCounter = 0; |
|
f7eb47b…
|
lmata
|
79 |
const progress = progressToken ? (params) => { |
|
f7eb47b…
|
lmata
|
80 |
extra.sendNotification({ |
|
f7eb47b…
|
lmata
|
81 |
method: "notifications/progress", |
|
f7eb47b…
|
lmata
|
82 |
params: { |
|
f7eb47b…
|
lmata
|
83 |
progressToken, |
|
f7eb47b…
|
lmata
|
84 |
progress: params.progress ?? ++progressCounter, |
|
f7eb47b…
|
lmata
|
85 |
total: params.total, |
|
f7eb47b…
|
lmata
|
86 |
message: params.message |
|
f7eb47b…
|
lmata
|
87 |
} |
|
f7eb47b…
|
lmata
|
88 |
}).catch(serverDebug); |
|
f7eb47b…
|
lmata
|
89 |
} : () => { |
|
f7eb47b…
|
lmata
|
90 |
}; |
|
f7eb47b…
|
lmata
|
91 |
try { |
|
f7eb47b…
|
lmata
|
92 |
if (!initializePromise) |
|
f7eb47b…
|
lmata
|
93 |
initializePromise = initializeServer(server, backend, runHeartbeat); |
|
f7eb47b…
|
lmata
|
94 |
await initializePromise; |
|
f7eb47b…
|
lmata
|
95 |
const toolResult = await backend.callTool(request.params.name, request.params.arguments || {}, progress); |
|
f7eb47b…
|
lmata
|
96 |
const mergedResult = mergeTextParts(toolResult); |
|
f7eb47b…
|
lmata
|
97 |
serverDebugResponse("callResult", mergedResult); |
|
f7eb47b…
|
lmata
|
98 |
return mergedResult; |
|
f7eb47b…
|
lmata
|
99 |
} catch (error) { |
|
f7eb47b…
|
lmata
|
100 |
return { |
|
f7eb47b…
|
lmata
|
101 |
content: [{ type: "text", text: "### Result\n" + String(error) }], |
|
f7eb47b…
|
lmata
|
102 |
isError: true |
|
f7eb47b…
|
lmata
|
103 |
}; |
|
f7eb47b…
|
lmata
|
104 |
} |
|
f7eb47b…
|
lmata
|
105 |
}); |
|
f7eb47b…
|
lmata
|
106 |
addServerListener(server, "close", () => backend.serverClosed?.(server)); |
|
f7eb47b…
|
lmata
|
107 |
return server; |
|
f7eb47b…
|
lmata
|
108 |
} |
|
f7eb47b…
|
lmata
|
109 |
const initializeServer = async (server, backend, runHeartbeat) => { |
|
f7eb47b…
|
lmata
|
110 |
const capabilities = server.getClientCapabilities(); |
|
f7eb47b…
|
lmata
|
111 |
let clientRoots = []; |
|
f7eb47b…
|
lmata
|
112 |
if (capabilities?.roots) { |
|
f7eb47b…
|
lmata
|
113 |
const { roots } = await server.listRoots().catch((e) => { |
|
f7eb47b…
|
lmata
|
114 |
serverDebug(e); |
|
f7eb47b…
|
lmata
|
115 |
return { roots: [] }; |
|
f7eb47b…
|
lmata
|
116 |
}); |
|
f7eb47b…
|
lmata
|
117 |
clientRoots = roots; |
|
f7eb47b…
|
lmata
|
118 |
} |
|
f7eb47b…
|
lmata
|
119 |
const clientInfo = { |
|
f7eb47b…
|
lmata
|
120 |
name: server.getClientVersion()?.name ?? "unknown", |
|
f7eb47b…
|
lmata
|
121 |
version: server.getClientVersion()?.version ?? "unknown", |
|
f7eb47b…
|
lmata
|
122 |
roots: clientRoots, |
|
f7eb47b…
|
lmata
|
123 |
timestamp: Date.now() |
|
f7eb47b…
|
lmata
|
124 |
}; |
|
f7eb47b…
|
lmata
|
125 |
await backend.initialize?.(clientInfo); |
|
f7eb47b…
|
lmata
|
126 |
if (runHeartbeat) |
|
f7eb47b…
|
lmata
|
127 |
startHeartbeat(server); |
|
f7eb47b…
|
lmata
|
128 |
}; |
|
f7eb47b…
|
lmata
|
129 |
const startHeartbeat = (server) => { |
|
f7eb47b…
|
lmata
|
130 |
const beat = () => { |
|
f7eb47b…
|
lmata
|
131 |
Promise.race([ |
|
f7eb47b…
|
lmata
|
132 |
server.ping(), |
|
f7eb47b…
|
lmata
|
133 |
new Promise((_, reject) => setTimeout(() => reject(new Error("ping timeout")), 5e3)) |
|
f7eb47b…
|
lmata
|
134 |
]).then(() => { |
|
f7eb47b…
|
lmata
|
135 |
setTimeout(beat, 3e3); |
|
f7eb47b…
|
lmata
|
136 |
}).catch(() => { |
|
f7eb47b…
|
lmata
|
137 |
void server.close(); |
|
f7eb47b…
|
lmata
|
138 |
}); |
|
f7eb47b…
|
lmata
|
139 |
}; |
|
f7eb47b…
|
lmata
|
140 |
beat(); |
|
f7eb47b…
|
lmata
|
141 |
}; |
|
f7eb47b…
|
lmata
|
142 |
function addServerListener(server, event, listener) { |
|
f7eb47b…
|
lmata
|
143 |
const oldListener = server[`on${event}`]; |
|
f7eb47b…
|
lmata
|
144 |
server[`on${event}`] = () => { |
|
f7eb47b…
|
lmata
|
145 |
oldListener?.(); |
|
f7eb47b…
|
lmata
|
146 |
listener(); |
|
f7eb47b…
|
lmata
|
147 |
}; |
|
f7eb47b…
|
lmata
|
148 |
} |
|
f7eb47b…
|
lmata
|
149 |
async function start(serverBackendFactory, options) { |
|
f7eb47b…
|
lmata
|
150 |
if (options.port === void 0) { |
|
f7eb47b…
|
lmata
|
151 |
await connect(serverBackendFactory, new mcpBundle.StdioServerTransport(), false); |
|
f7eb47b…
|
lmata
|
152 |
return; |
|
f7eb47b…
|
lmata
|
153 |
} |
|
f7eb47b…
|
lmata
|
154 |
const url = await (0, import_http.startMcpHttpServer)(options, serverBackendFactory, options.allowedHosts); |
|
f7eb47b…
|
lmata
|
155 |
const mcpConfig = { mcpServers: {} }; |
|
f7eb47b…
|
lmata
|
156 |
mcpConfig.mcpServers[serverBackendFactory.nameInConfig] = { |
|
f7eb47b…
|
lmata
|
157 |
url: `${url}/mcp` |
|
f7eb47b…
|
lmata
|
158 |
}; |
|
f7eb47b…
|
lmata
|
159 |
const message = [ |
|
f7eb47b…
|
lmata
|
160 |
`Listening on ${url}`, |
|
f7eb47b…
|
lmata
|
161 |
"Put this in your client config:", |
|
f7eb47b…
|
lmata
|
162 |
JSON.stringify(mcpConfig, void 0, 2), |
|
f7eb47b…
|
lmata
|
163 |
"For legacy SSE transport support, you can use the /sse endpoint instead." |
|
f7eb47b…
|
lmata
|
164 |
].join("\n"); |
|
f7eb47b…
|
lmata
|
165 |
console.error(message); |
|
f7eb47b…
|
lmata
|
166 |
} |
|
f7eb47b…
|
lmata
|
167 |
function firstRootPath(clientInfo) { |
|
f7eb47b…
|
lmata
|
168 |
if (clientInfo.roots.length === 0) |
|
f7eb47b…
|
lmata
|
169 |
return void 0; |
|
f7eb47b…
|
lmata
|
170 |
const firstRootUri = clientInfo.roots[0]?.uri; |
|
f7eb47b…
|
lmata
|
171 |
const url = firstRootUri ? new URL(firstRootUri) : void 0; |
|
f7eb47b…
|
lmata
|
172 |
try { |
|
f7eb47b…
|
lmata
|
173 |
return url ? (0, import_url.fileURLToPath)(url) : void 0; |
|
f7eb47b…
|
lmata
|
174 |
} catch (error) { |
|
f7eb47b…
|
lmata
|
175 |
serverDebug(error); |
|
f7eb47b…
|
lmata
|
176 |
return void 0; |
|
f7eb47b…
|
lmata
|
177 |
} |
|
f7eb47b…
|
lmata
|
178 |
} |
|
f7eb47b…
|
lmata
|
179 |
function allRootPaths(clientInfo) { |
|
f7eb47b…
|
lmata
|
180 |
const paths = []; |
|
f7eb47b…
|
lmata
|
181 |
for (const root of clientInfo.roots) { |
|
f7eb47b…
|
lmata
|
182 |
try { |
|
f7eb47b…
|
lmata
|
183 |
const url = new URL(root.uri); |
|
f7eb47b…
|
lmata
|
184 |
const path = (0, import_url.fileURLToPath)(url); |
|
f7eb47b…
|
lmata
|
185 |
if (path) |
|
f7eb47b…
|
lmata
|
186 |
paths.push(path); |
|
f7eb47b…
|
lmata
|
187 |
} catch (error) { |
|
f7eb47b…
|
lmata
|
188 |
serverDebug(error); |
|
f7eb47b…
|
lmata
|
189 |
} |
|
f7eb47b…
|
lmata
|
190 |
} |
|
f7eb47b…
|
lmata
|
191 |
return paths; |
|
f7eb47b…
|
lmata
|
192 |
} |
|
f7eb47b…
|
lmata
|
193 |
function mergeTextParts(result) { |
|
f7eb47b…
|
lmata
|
194 |
const content = []; |
|
f7eb47b…
|
lmata
|
195 |
const testParts = []; |
|
f7eb47b…
|
lmata
|
196 |
for (const part of result.content) { |
|
f7eb47b…
|
lmata
|
197 |
if (part.type === "text") { |
|
f7eb47b…
|
lmata
|
198 |
testParts.push(part.text); |
|
f7eb47b…
|
lmata
|
199 |
continue; |
|
f7eb47b…
|
lmata
|
200 |
} |
|
f7eb47b…
|
lmata
|
201 |
if (testParts.length > 0) { |
|
f7eb47b…
|
lmata
|
202 |
content.push({ type: "text", text: testParts.join("\n") }); |
|
f7eb47b…
|
lmata
|
203 |
testParts.length = 0; |
|
f7eb47b…
|
lmata
|
204 |
} |
|
f7eb47b…
|
lmata
|
205 |
content.push(part); |
|
f7eb47b…
|
lmata
|
206 |
} |
|
f7eb47b…
|
lmata
|
207 |
if (testParts.length > 0) |
|
f7eb47b…
|
lmata
|
208 |
content.push({ type: "text", text: testParts.join("\n") }); |
|
f7eb47b…
|
lmata
|
209 |
return { |
|
f7eb47b…
|
lmata
|
210 |
...result, |
|
f7eb47b…
|
lmata
|
211 |
content |
|
f7eb47b…
|
lmata
|
212 |
}; |
|
f7eb47b…
|
lmata
|
213 |
} |
|
f7eb47b…
|
lmata
|
214 |
// Annotate the CommonJS export names for ESM import in node: |
|
f7eb47b…
|
lmata
|
215 |
0 && (module.exports = { |
|
f7eb47b…
|
lmata
|
216 |
allRootPaths, |
|
f7eb47b…
|
lmata
|
217 |
connect, |
|
f7eb47b…
|
lmata
|
218 |
createServer, |
|
f7eb47b…
|
lmata
|
219 |
firstRootPath, |
|
f7eb47b…
|
lmata
|
220 |
start, |
|
f7eb47b…
|
lmata
|
221 |
wrapInClient, |
|
f7eb47b…
|
lmata
|
222 |
wrapInProcess |
|
f7eb47b…
|
lmata
|
223 |
}); |