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