playwright 1.55.0-alpha-2025-08-15 → 1.55.0-alpha-2025-08-16

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/index.js CHANGED
@@ -251,7 +251,7 @@ const playwrightFixtures = {
251
251
  title: renderTitle(channel.type, channel.method, channel.params, data.title),
252
252
  apiName: data.apiName,
253
253
  params: channel.params,
254
- visibility: (0, import_utils.getActionGroup)({ type: channel.type, method: channel.method }) ? "hidden" : void 0
254
+ group: (0, import_utils.getActionGroup)({ type: channel.type, method: channel.method })
255
255
  }, tracingGroupSteps[tracingGroupSteps.length - 1]);
256
256
  data.userData = step;
257
257
  data.stepId = step.stepId;
@@ -0,0 +1,59 @@
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 bundle_exports = {};
20
+ __export(bundle_exports, {
21
+ CallToolRequestSchema: () => CallToolRequestSchema,
22
+ Client: () => Client,
23
+ ListRootsRequestSchema: () => ListRootsRequestSchema,
24
+ ListToolsRequestSchema: () => ListToolsRequestSchema,
25
+ PingRequestSchema: () => PingRequestSchema,
26
+ SSEServerTransport: () => SSEServerTransport,
27
+ Server: () => Server,
28
+ StdioServerTransport: () => StdioServerTransport,
29
+ StreamableHTTPServerTransport: () => StreamableHTTPServerTransport,
30
+ z: () => z,
31
+ zodToJsonSchema: () => zodToJsonSchema
32
+ });
33
+ module.exports = __toCommonJS(bundle_exports);
34
+ const bundle = require("./mcpBundleImpl");
35
+ const zodToJsonSchema = require("./mcpBundleImpl").zodToJsonSchema;
36
+ const Client = bundle.Client;
37
+ const Server = bundle.Server;
38
+ const SSEServerTransport = bundle.SSEServerTransport;
39
+ const StdioServerTransport = bundle.StdioServerTransport;
40
+ const StreamableHTTPServerTransport = bundle.StreamableHTTPServerTransport;
41
+ const CallToolRequestSchema = bundle.CallToolRequestSchema;
42
+ const ListRootsRequestSchema = bundle.ListRootsRequestSchema;
43
+ const ListToolsRequestSchema = bundle.ListToolsRequestSchema;
44
+ const PingRequestSchema = bundle.PingRequestSchema;
45
+ const z = bundle.z;
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ CallToolRequestSchema,
49
+ Client,
50
+ ListRootsRequestSchema,
51
+ ListToolsRequestSchema,
52
+ PingRequestSchema,
53
+ SSEServerTransport,
54
+ Server,
55
+ StdioServerTransport,
56
+ StreamableHTTPServerTransport,
57
+ z,
58
+ zodToJsonSchema
59
+ });
@@ -0,0 +1,30 @@
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 __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+ var exports_exports = {};
17
+ module.exports = __toCommonJS(exports_exports);
18
+ __reExport(exports_exports, require("./inProcessTransport.js"), module.exports);
19
+ __reExport(exports_exports, require("./proxyBackend.js"), module.exports);
20
+ __reExport(exports_exports, require("./server.js"), module.exports);
21
+ __reExport(exports_exports, require("./tool.js"), module.exports);
22
+ __reExport(exports_exports, require("./transport.js"), module.exports);
23
+ // Annotate the CommonJS export names for ESM import in node:
24
+ 0 && (module.exports = {
25
+ ...require("./inProcessTransport.js"),
26
+ ...require("./proxyBackend.js"),
27
+ ...require("./server.js"),
28
+ ...require("./tool.js"),
29
+ ...require("./transport.js")
30
+ });
@@ -0,0 +1,71 @@
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 inProcessTransport_exports = {};
20
+ __export(inProcessTransport_exports, {
21
+ InProcessTransport: () => InProcessTransport
22
+ });
23
+ module.exports = __toCommonJS(inProcessTransport_exports);
24
+ class InProcessTransport {
25
+ constructor(server) {
26
+ this._connected = false;
27
+ this._server = server;
28
+ this._serverTransport = new InProcessServerTransport(this);
29
+ }
30
+ async start() {
31
+ if (this._connected)
32
+ throw new Error("InprocessTransport already started!");
33
+ await this._server.connect(this._serverTransport);
34
+ this._connected = true;
35
+ }
36
+ async send(message, options) {
37
+ if (!this._connected)
38
+ throw new Error("Transport not connected");
39
+ this._serverTransport._receiveFromClient(message);
40
+ }
41
+ async close() {
42
+ if (this._connected) {
43
+ this._connected = false;
44
+ this.onclose?.();
45
+ this._serverTransport.onclose?.();
46
+ }
47
+ }
48
+ _receiveFromServer(message, extra) {
49
+ this.onmessage?.(message, extra);
50
+ }
51
+ }
52
+ class InProcessServerTransport {
53
+ constructor(clientTransport) {
54
+ this._clientTransport = clientTransport;
55
+ }
56
+ async start() {
57
+ }
58
+ async send(message, options) {
59
+ this._clientTransport._receiveFromServer(message);
60
+ }
61
+ async close() {
62
+ this.onclose?.();
63
+ }
64
+ _receiveFromClient(message) {
65
+ this.onmessage?.(message);
66
+ }
67
+ }
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ InProcessTransport
71
+ });
@@ -0,0 +1,123 @@
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
+ class ProxyBackend {
38
+ constructor(name, version, mcpProviders) {
39
+ this._roots = [];
40
+ this.name = name;
41
+ this.version = version;
42
+ this._mcpProviders = mcpProviders;
43
+ this._contextSwitchTool = this._defineContextSwitchTool();
44
+ }
45
+ async initialize(clientVersion, roots) {
46
+ this._roots = roots;
47
+ await this._setCurrentClient(this._mcpProviders[0]);
48
+ }
49
+ async listTools() {
50
+ const response = await this._currentClient.listTools();
51
+ if (this._mcpProviders.length === 1)
52
+ return response.tools;
53
+ return [
54
+ ...response.tools,
55
+ this._contextSwitchTool
56
+ ];
57
+ }
58
+ async callTool(name, args) {
59
+ if (name === this._contextSwitchTool.name)
60
+ return this._callContextSwitchTool(args);
61
+ return await this._currentClient.callTool({
62
+ name,
63
+ arguments: args
64
+ });
65
+ }
66
+ serverClosed() {
67
+ void this._currentClient?.close().catch(errorsDebug);
68
+ }
69
+ async _callContextSwitchTool(params) {
70
+ try {
71
+ const factory = this._mcpProviders.find((factory2) => factory2.name === params.name);
72
+ if (!factory)
73
+ throw new Error("Unknown connection method: " + params.name);
74
+ await this._setCurrentClient(factory);
75
+ return {
76
+ content: [{ type: "text", text: "### Result\nSuccessfully changed connection method.\n" }]
77
+ };
78
+ } catch (error) {
79
+ return {
80
+ content: [{ type: "text", text: `### Result
81
+ Error: ${error}
82
+ ` }],
83
+ isError: true
84
+ };
85
+ }
86
+ }
87
+ _defineContextSwitchTool() {
88
+ return {
89
+ name: "browser_connect",
90
+ description: [
91
+ "Connect to a browser using one of the available methods:",
92
+ ...this._mcpProviders.map((factory) => `- "${factory.name}": ${factory.description}`)
93
+ ].join("\n"),
94
+ inputSchema: mcpBundle.zodToJsonSchema(mcpBundle.z.object({
95
+ name: mcpBundle.z.enum(this._mcpProviders.map((factory) => factory.name)).default(this._mcpProviders[0].name).describe("The method to use to connect to the browser")
96
+ }), { strictUnions: true }),
97
+ annotations: {
98
+ title: "Connect to a browser context",
99
+ readOnlyHint: true,
100
+ openWorldHint: false
101
+ }
102
+ };
103
+ }
104
+ async _setCurrentClient(factory) {
105
+ await this._currentClient?.close();
106
+ this._currentClient = void 0;
107
+ const client = new mcpBundle.Client({ name: this.name, version: this.version });
108
+ client.registerCapabilities({
109
+ roots: {
110
+ listRoots: true
111
+ }
112
+ });
113
+ client.setRequestHandler(mcpBundle.ListRootsRequestSchema, () => ({ roots: this._roots }));
114
+ client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));
115
+ const transport = await factory.connect();
116
+ await client.connect(transport);
117
+ this._currentClient = client;
118
+ }
119
+ }
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ ProxyBackend
123
+ });
@@ -0,0 +1,118 @@
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
+ });
34
+ module.exports = __toCommonJS(server_exports);
35
+ var import_utilsBundle = require("playwright-core/lib/utilsBundle");
36
+ var mcpBundle = __toESM(require("./bundle"));
37
+ const serverDebug = (0, import_utilsBundle.debug)("pw:mcp:server");
38
+ const errorsDebug = (0, import_utilsBundle.debug)("pw:mcp:errors");
39
+ async function connect(serverBackendFactory, transport, runHeartbeat) {
40
+ const backend = serverBackendFactory();
41
+ const server = createServer(backend, runHeartbeat);
42
+ await server.connect(transport);
43
+ }
44
+ function createServer(backend, runHeartbeat) {
45
+ let initializedCallback = () => {
46
+ };
47
+ const initializedPromise = new Promise((resolve) => initializedCallback = resolve);
48
+ const server = new mcpBundle.Server({ name: backend.name, version: backend.version }, {
49
+ capabilities: {
50
+ tools: {}
51
+ }
52
+ });
53
+ server.setRequestHandler(mcpBundle.ListToolsRequestSchema, async () => {
54
+ serverDebug("listTools");
55
+ await initializedPromise;
56
+ const tools = await backend.listTools();
57
+ return { tools };
58
+ });
59
+ let heartbeatRunning = false;
60
+ server.setRequestHandler(mcpBundle.CallToolRequestSchema, async (request) => {
61
+ serverDebug("callTool", request);
62
+ await initializedPromise;
63
+ if (runHeartbeat && !heartbeatRunning) {
64
+ heartbeatRunning = true;
65
+ startHeartbeat(server);
66
+ }
67
+ try {
68
+ return await backend.callTool(request.params.name, request.params.arguments || {});
69
+ } catch (error) {
70
+ return {
71
+ content: [{ type: "text", text: "### Result\n" + String(error) }],
72
+ isError: true
73
+ };
74
+ }
75
+ });
76
+ addServerListener(server, "initialized", async () => {
77
+ try {
78
+ const capabilities = server.getClientCapabilities();
79
+ let clientRoots = [];
80
+ if (capabilities?.roots) {
81
+ const { roots } = await server.listRoots(void 0, { timeout: 2e3 }).catch(() => ({ roots: [] }));
82
+ clientRoots = roots;
83
+ }
84
+ const clientVersion = server.getClientVersion() ?? { name: "unknown", version: "unknown" };
85
+ await backend.initialize?.(clientVersion, clientRoots);
86
+ initializedCallback();
87
+ } catch (e) {
88
+ errorsDebug(e);
89
+ }
90
+ });
91
+ addServerListener(server, "close", () => backend.serverClosed?.());
92
+ return server;
93
+ }
94
+ const startHeartbeat = (server) => {
95
+ const beat = () => {
96
+ Promise.race([
97
+ server.ping(),
98
+ new Promise((_, reject) => setTimeout(() => reject(new Error("ping timeout")), 5e3))
99
+ ]).then(() => {
100
+ setTimeout(beat, 3e3);
101
+ }).catch(() => {
102
+ void server.close();
103
+ });
104
+ };
105
+ beat();
106
+ };
107
+ function addServerListener(server, event, listener) {
108
+ const oldListener = server[`on${event}`];
109
+ server[`on${event}`] = () => {
110
+ oldListener?.();
111
+ listener();
112
+ };
113
+ }
114
+ // Annotate the CommonJS export names for ESM import in node:
115
+ 0 && (module.exports = {
116
+ connect,
117
+ createServer
118
+ });
@@ -0,0 +1,41 @@
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
+ toMcpTool: () => toMcpTool
22
+ });
23
+ module.exports = __toCommonJS(tool_exports);
24
+ var import_bundle = require("./bundle");
25
+ function toMcpTool(tool) {
26
+ return {
27
+ name: tool.name,
28
+ description: tool.description,
29
+ inputSchema: (0, import_bundle.zodToJsonSchema)(tool.inputSchema, { strictUnions: true }),
30
+ annotations: {
31
+ title: tool.title,
32
+ readOnlyHint: tool.type === "readOnly",
33
+ destructiveHint: tool.type === "destructive",
34
+ openWorldHint: true
35
+ }
36
+ };
37
+ }
38
+ // Annotate the CommonJS export names for ESM import in node:
39
+ 0 && (module.exports = {
40
+ toMcpTool
41
+ });
@@ -0,0 +1,161 @@
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 transport_exports = {};
30
+ __export(transport_exports, {
31
+ start: () => start
32
+ });
33
+ module.exports = __toCommonJS(transport_exports);
34
+ var import_assert = __toESM(require("assert"));
35
+ var import_http = __toESM(require("http"));
36
+ var import_crypto = __toESM(require("crypto"));
37
+ var import_utilsBundle = require("playwright-core/lib/utilsBundle");
38
+ var mcpBundle = __toESM(require("./bundle"));
39
+ var mcpServer = __toESM(require("./server"));
40
+ async function start(serverBackendFactory, options) {
41
+ if (options.port !== void 0) {
42
+ const httpServer = await startHttpServer(options);
43
+ startHttpTransport(httpServer, serverBackendFactory);
44
+ } else {
45
+ await startStdioTransport(serverBackendFactory);
46
+ }
47
+ }
48
+ async function startStdioTransport(serverBackendFactory) {
49
+ await mcpServer.connect(serverBackendFactory, new mcpBundle.StdioServerTransport(), false);
50
+ }
51
+ const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test");
52
+ async function handleSSE(serverBackendFactory, req, res, url, sessions) {
53
+ if (req.method === "POST") {
54
+ const sessionId = url.searchParams.get("sessionId");
55
+ if (!sessionId) {
56
+ res.statusCode = 400;
57
+ return res.end("Missing sessionId");
58
+ }
59
+ const transport = sessions.get(sessionId);
60
+ if (!transport) {
61
+ res.statusCode = 404;
62
+ return res.end("Session not found");
63
+ }
64
+ return await transport.handlePostMessage(req, res);
65
+ } else if (req.method === "GET") {
66
+ const transport = new mcpBundle.SSEServerTransport("/sse", res);
67
+ sessions.set(transport.sessionId, transport);
68
+ testDebug(`create SSE session: ${transport.sessionId}`);
69
+ await mcpServer.connect(serverBackendFactory, transport, false);
70
+ res.on("close", () => {
71
+ testDebug(`delete SSE session: ${transport.sessionId}`);
72
+ sessions.delete(transport.sessionId);
73
+ });
74
+ return;
75
+ }
76
+ res.statusCode = 405;
77
+ res.end("Method not allowed");
78
+ }
79
+ async function handleStreamable(serverBackendFactory, req, res, sessions) {
80
+ const sessionId = req.headers["mcp-session-id"];
81
+ if (sessionId) {
82
+ const transport = sessions.get(sessionId);
83
+ if (!transport) {
84
+ res.statusCode = 404;
85
+ res.end("Session not found");
86
+ return;
87
+ }
88
+ return await transport.handleRequest(req, res);
89
+ }
90
+ if (req.method === "POST") {
91
+ const transport = new mcpBundle.StreamableHTTPServerTransport({
92
+ sessionIdGenerator: () => import_crypto.default.randomUUID(),
93
+ onsessioninitialized: async (sessionId2) => {
94
+ testDebug(`create http session: ${transport.sessionId}`);
95
+ await mcpServer.connect(serverBackendFactory, transport, true);
96
+ sessions.set(sessionId2, transport);
97
+ }
98
+ });
99
+ transport.onclose = () => {
100
+ if (!transport.sessionId)
101
+ return;
102
+ sessions.delete(transport.sessionId);
103
+ testDebug(`delete http session: ${transport.sessionId}`);
104
+ };
105
+ await transport.handleRequest(req, res);
106
+ return;
107
+ }
108
+ res.statusCode = 400;
109
+ res.end("Invalid request");
110
+ }
111
+ function startHttpTransport(httpServer, serverBackendFactory) {
112
+ const sseSessions = /* @__PURE__ */ new Map();
113
+ const streamableSessions = /* @__PURE__ */ new Map();
114
+ httpServer.on("request", async (req, res) => {
115
+ const url2 = new URL(`http://localhost${req.url}`);
116
+ if (url2.pathname.startsWith("/sse"))
117
+ await handleSSE(serverBackendFactory, req, res, url2, sseSessions);
118
+ else
119
+ await handleStreamable(serverBackendFactory, req, res, streamableSessions);
120
+ });
121
+ const url = httpAddressToString(httpServer.address());
122
+ const message = [
123
+ `Listening on ${url}`,
124
+ "Put this in your client config:",
125
+ JSON.stringify({
126
+ "mcpServers": {
127
+ "playwright": {
128
+ "url": `${url}/mcp`
129
+ }
130
+ }
131
+ }, void 0, 2),
132
+ "For legacy SSE transport support, you can use the /sse endpoint instead."
133
+ ].join("\n");
134
+ console.error(message);
135
+ }
136
+ async function startHttpServer(config) {
137
+ const { host, port } = config;
138
+ const httpServer = import_http.default.createServer();
139
+ await new Promise((resolve, reject) => {
140
+ httpServer.on("error", reject);
141
+ httpServer.listen(port, host, () => {
142
+ resolve();
143
+ httpServer.removeListener("error", reject);
144
+ });
145
+ });
146
+ return httpServer;
147
+ }
148
+ function httpAddressToString(address) {
149
+ (0, import_assert.default)(address, "Could not bind server socket");
150
+ if (typeof address === "string")
151
+ return address;
152
+ const resolvedPort = address.port;
153
+ let resolvedHost = address.family === "IPv4" ? address.address : `[${address.address}]`;
154
+ if (resolvedHost === "0.0.0.0" || resolvedHost === "[::]")
155
+ resolvedHost = "localhost";
156
+ return `http://${resolvedHost}:${resolvedPort}`;
157
+ }
158
+ // Annotate the CommonJS export names for ESM import in node:
159
+ 0 && (module.exports = {
160
+ start
161
+ });