|
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 http_exports = {}; |
|
f7eb47b…
|
lmata
|
30 |
__export(http_exports, { |
|
f7eb47b…
|
lmata
|
31 |
addressToString: () => addressToString, |
|
f7eb47b…
|
lmata
|
32 |
startMcpHttpServer: () => startMcpHttpServer |
|
f7eb47b…
|
lmata
|
33 |
}); |
|
f7eb47b…
|
lmata
|
34 |
module.exports = __toCommonJS(http_exports); |
|
f7eb47b…
|
lmata
|
35 |
var import_assert = __toESM(require("assert")); |
|
f7eb47b…
|
lmata
|
36 |
var import_crypto = __toESM(require("crypto")); |
|
f7eb47b…
|
lmata
|
37 |
var import_utilsBundle = require("playwright-core/lib/utilsBundle"); |
|
f7eb47b…
|
lmata
|
38 |
var mcpBundle = __toESM(require("playwright-core/lib/mcpBundle")); |
|
f7eb47b…
|
lmata
|
39 |
var import_utils = require("playwright-core/lib/utils"); |
|
f7eb47b…
|
lmata
|
40 |
var mcpServer = __toESM(require("./server")); |
|
f7eb47b…
|
lmata
|
41 |
const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test"); |
|
f7eb47b…
|
lmata
|
42 |
async function startMcpHttpServer(config, serverBackendFactory, allowedHosts) { |
|
f7eb47b…
|
lmata
|
43 |
const httpServer = (0, import_utils.createHttpServer)(); |
|
f7eb47b…
|
lmata
|
44 |
await (0, import_utils.startHttpServer)(httpServer, config); |
|
f7eb47b…
|
lmata
|
45 |
return await installHttpTransport(httpServer, serverBackendFactory, allowedHosts); |
|
f7eb47b…
|
lmata
|
46 |
} |
|
f7eb47b…
|
lmata
|
47 |
function addressToString(address, options) { |
|
f7eb47b…
|
lmata
|
48 |
(0, import_assert.default)(address, "Could not bind server socket"); |
|
f7eb47b…
|
lmata
|
49 |
if (typeof address === "string") |
|
f7eb47b…
|
lmata
|
50 |
throw new Error("Unexpected address type: " + address); |
|
f7eb47b…
|
lmata
|
51 |
let host = address.family === "IPv4" ? address.address : `[${address.address}]`; |
|
f7eb47b…
|
lmata
|
52 |
if (options.normalizeLoopback && (host === "0.0.0.0" || host === "[::]" || host === "[::1]" || host === "127.0.0.1")) |
|
f7eb47b…
|
lmata
|
53 |
host = "localhost"; |
|
f7eb47b…
|
lmata
|
54 |
return `${options.protocol}://${host}:${address.port}`; |
|
f7eb47b…
|
lmata
|
55 |
} |
|
f7eb47b…
|
lmata
|
56 |
async function installHttpTransport(httpServer, serverBackendFactory, allowedHosts) { |
|
f7eb47b…
|
lmata
|
57 |
const url = addressToString(httpServer.address(), { protocol: "http", normalizeLoopback: true }); |
|
f7eb47b…
|
lmata
|
58 |
const host = new URL(url).host; |
|
f7eb47b…
|
lmata
|
59 |
allowedHosts = (allowedHosts || [host]).map((h) => h.toLowerCase()); |
|
f7eb47b…
|
lmata
|
60 |
const allowAnyHost = allowedHosts.includes("*"); |
|
f7eb47b…
|
lmata
|
61 |
const sseSessions = /* @__PURE__ */ new Map(); |
|
f7eb47b…
|
lmata
|
62 |
const streamableSessions = /* @__PURE__ */ new Map(); |
|
f7eb47b…
|
lmata
|
63 |
httpServer.on("request", async (req, res) => { |
|
f7eb47b…
|
lmata
|
64 |
if (!allowAnyHost) { |
|
f7eb47b…
|
lmata
|
65 |
const host2 = req.headers.host?.toLowerCase(); |
|
f7eb47b…
|
lmata
|
66 |
if (!host2) { |
|
f7eb47b…
|
lmata
|
67 |
res.statusCode = 400; |
|
f7eb47b…
|
lmata
|
68 |
return res.end("Missing host"); |
|
f7eb47b…
|
lmata
|
69 |
} |
|
f7eb47b…
|
lmata
|
70 |
if (!allowedHosts.includes(host2)) { |
|
f7eb47b…
|
lmata
|
71 |
res.statusCode = 403; |
|
f7eb47b…
|
lmata
|
72 |
return res.end("Access is only allowed at " + allowedHosts.join(", ")); |
|
f7eb47b…
|
lmata
|
73 |
} |
|
f7eb47b…
|
lmata
|
74 |
} |
|
f7eb47b…
|
lmata
|
75 |
const url2 = new URL(`http://localhost${req.url}`); |
|
f7eb47b…
|
lmata
|
76 |
if (url2.pathname === "/killkillkill" && req.method === "GET") { |
|
f7eb47b…
|
lmata
|
77 |
res.statusCode = 200; |
|
f7eb47b…
|
lmata
|
78 |
res.end("Killing process"); |
|
f7eb47b…
|
lmata
|
79 |
process.emit("SIGINT"); |
|
f7eb47b…
|
lmata
|
80 |
return; |
|
f7eb47b…
|
lmata
|
81 |
} |
|
f7eb47b…
|
lmata
|
82 |
if (url2.pathname.startsWith("/sse")) |
|
f7eb47b…
|
lmata
|
83 |
await handleSSE(serverBackendFactory, req, res, url2, sseSessions); |
|
f7eb47b…
|
lmata
|
84 |
else |
|
f7eb47b…
|
lmata
|
85 |
await handleStreamable(serverBackendFactory, req, res, streamableSessions); |
|
f7eb47b…
|
lmata
|
86 |
}); |
|
f7eb47b…
|
lmata
|
87 |
return url; |
|
f7eb47b…
|
lmata
|
88 |
} |
|
f7eb47b…
|
lmata
|
89 |
async function handleSSE(serverBackendFactory, req, res, url, sessions) { |
|
f7eb47b…
|
lmata
|
90 |
if (req.method === "POST") { |
|
f7eb47b…
|
lmata
|
91 |
const sessionId = url.searchParams.get("sessionId"); |
|
f7eb47b…
|
lmata
|
92 |
if (!sessionId) { |
|
f7eb47b…
|
lmata
|
93 |
res.statusCode = 400; |
|
f7eb47b…
|
lmata
|
94 |
return res.end("Missing sessionId"); |
|
f7eb47b…
|
lmata
|
95 |
} |
|
f7eb47b…
|
lmata
|
96 |
const transport = sessions.get(sessionId); |
|
f7eb47b…
|
lmata
|
97 |
if (!transport) { |
|
f7eb47b…
|
lmata
|
98 |
res.statusCode = 404; |
|
f7eb47b…
|
lmata
|
99 |
return res.end("Session not found"); |
|
f7eb47b…
|
lmata
|
100 |
} |
|
f7eb47b…
|
lmata
|
101 |
return await transport.handlePostMessage(req, res); |
|
f7eb47b…
|
lmata
|
102 |
} else if (req.method === "GET") { |
|
f7eb47b…
|
lmata
|
103 |
const transport = new mcpBundle.SSEServerTransport("/sse", res); |
|
f7eb47b…
|
lmata
|
104 |
sessions.set(transport.sessionId, transport); |
|
f7eb47b…
|
lmata
|
105 |
testDebug(`create SSE session: ${transport.sessionId}`); |
|
f7eb47b…
|
lmata
|
106 |
await mcpServer.connect(serverBackendFactory, transport, false); |
|
f7eb47b…
|
lmata
|
107 |
res.on("close", () => { |
|
f7eb47b…
|
lmata
|
108 |
testDebug(`delete SSE session: ${transport.sessionId}`); |
|
f7eb47b…
|
lmata
|
109 |
sessions.delete(transport.sessionId); |
|
f7eb47b…
|
lmata
|
110 |
}); |
|
f7eb47b…
|
lmata
|
111 |
return; |
|
f7eb47b…
|
lmata
|
112 |
} |
|
f7eb47b…
|
lmata
|
113 |
res.statusCode = 405; |
|
f7eb47b…
|
lmata
|
114 |
res.end("Method not allowed"); |
|
f7eb47b…
|
lmata
|
115 |
} |
|
f7eb47b…
|
lmata
|
116 |
async function handleStreamable(serverBackendFactory, req, res, sessions) { |
|
f7eb47b…
|
lmata
|
117 |
const sessionId = req.headers["mcp-session-id"]; |
|
f7eb47b…
|
lmata
|
118 |
if (sessionId) { |
|
f7eb47b…
|
lmata
|
119 |
const transport = sessions.get(sessionId); |
|
f7eb47b…
|
lmata
|
120 |
if (!transport) { |
|
f7eb47b…
|
lmata
|
121 |
res.statusCode = 404; |
|
f7eb47b…
|
lmata
|
122 |
res.end("Session not found"); |
|
f7eb47b…
|
lmata
|
123 |
return; |
|
f7eb47b…
|
lmata
|
124 |
} |
|
f7eb47b…
|
lmata
|
125 |
return await transport.handleRequest(req, res); |
|
f7eb47b…
|
lmata
|
126 |
} |
|
f7eb47b…
|
lmata
|
127 |
if (req.method === "POST") { |
|
f7eb47b…
|
lmata
|
128 |
const transport = new mcpBundle.StreamableHTTPServerTransport({ |
|
f7eb47b…
|
lmata
|
129 |
sessionIdGenerator: () => import_crypto.default.randomUUID(), |
|
f7eb47b…
|
lmata
|
130 |
onsessioninitialized: async (sessionId2) => { |
|
f7eb47b…
|
lmata
|
131 |
testDebug(`create http session: ${transport.sessionId}`); |
|
f7eb47b…
|
lmata
|
132 |
await mcpServer.connect(serverBackendFactory, transport, true); |
|
f7eb47b…
|
lmata
|
133 |
sessions.set(sessionId2, transport); |
|
f7eb47b…
|
lmata
|
134 |
} |
|
f7eb47b…
|
lmata
|
135 |
}); |
|
f7eb47b…
|
lmata
|
136 |
transport.onclose = () => { |
|
f7eb47b…
|
lmata
|
137 |
if (!transport.sessionId) |
|
f7eb47b…
|
lmata
|
138 |
return; |
|
f7eb47b…
|
lmata
|
139 |
sessions.delete(transport.sessionId); |
|
f7eb47b…
|
lmata
|
140 |
testDebug(`delete http session: ${transport.sessionId}`); |
|
f7eb47b…
|
lmata
|
141 |
}; |
|
f7eb47b…
|
lmata
|
142 |
await transport.handleRequest(req, res); |
|
f7eb47b…
|
lmata
|
143 |
return; |
|
f7eb47b…
|
lmata
|
144 |
} |
|
f7eb47b…
|
lmata
|
145 |
res.statusCode = 400; |
|
f7eb47b…
|
lmata
|
146 |
res.end("Invalid request"); |
|
f7eb47b…
|
lmata
|
147 |
} |
|
f7eb47b…
|
lmata
|
148 |
// Annotate the CommonJS export names for ESM import in node: |
|
f7eb47b…
|
lmata
|
149 |
0 && (module.exports = { |
|
f7eb47b…
|
lmata
|
150 |
addressToString, |
|
f7eb47b…
|
lmata
|
151 |
startMcpHttpServer |
|
f7eb47b…
|
lmata
|
152 |
}); |