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