proxyBackend.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. const { z, zodToJsonSchema } = mcpBundle;
  38. class ProxyBackend {
  39. constructor(mcpProviders) {
  40. this._mcpProviders = mcpProviders;
  41. this._contextSwitchTool = this._defineContextSwitchTool();
  42. }
  43. async initialize(clientInfo) {
  44. this._clientInfo = clientInfo;
  45. }
  46. async listTools() {
  47. const currentClient = await this._ensureCurrentClient();
  48. const response = await currentClient.listTools();
  49. if (this._mcpProviders.length === 1)
  50. return response.tools;
  51. return [
  52. ...response.tools,
  53. this._contextSwitchTool
  54. ];
  55. }
  56. async callTool(name, args) {
  57. if (name === this._contextSwitchTool.name)
  58. return this._callContextSwitchTool(args);
  59. const currentClient = await this._ensureCurrentClient();
  60. return await currentClient.callTool({
  61. name,
  62. arguments: args
  63. });
  64. }
  65. serverClosed() {
  66. void this._currentClient?.close().catch(errorsDebug);
  67. }
  68. async _callContextSwitchTool(params) {
  69. try {
  70. const factory = this._mcpProviders.find((factory2) => factory2.name === params.name);
  71. if (!factory)
  72. throw new Error("Unknown connection method: " + params.name);
  73. await this._setCurrentClient(factory);
  74. return {
  75. content: [{ type: "text", text: "### Result\nSuccessfully changed connection method.\n" }]
  76. };
  77. } catch (error) {
  78. return {
  79. content: [{ type: "text", text: `### Result
  80. Error: ${error}
  81. ` }],
  82. isError: true
  83. };
  84. }
  85. }
  86. _defineContextSwitchTool() {
  87. return {
  88. name: "browser_connect",
  89. description: [
  90. "Connect to a browser using one of the available methods:",
  91. ...this._mcpProviders.map((factory) => `- "${factory.name}": ${factory.description}`)
  92. ].join("\n"),
  93. inputSchema: zodToJsonSchema(z.object({
  94. name: z.enum(this._mcpProviders.map((factory) => factory.name)).default(this._mcpProviders[0].name).describe("The method to use to connect to the browser")
  95. }), { strictUnions: true }),
  96. annotations: {
  97. title: "Connect to a browser context",
  98. readOnlyHint: true,
  99. openWorldHint: false
  100. }
  101. };
  102. }
  103. async _ensureCurrentClient() {
  104. if (this._currentClient)
  105. return this._currentClient;
  106. return await this._setCurrentClient(this._mcpProviders[0]);
  107. }
  108. async _setCurrentClient(factory) {
  109. await this._currentClient?.close();
  110. this._currentClient = void 0;
  111. const client = new mcpBundle.Client({ name: "Playwright MCP Proxy", version: "0.0.0" });
  112. client.registerCapabilities({
  113. roots: {
  114. listRoots: true
  115. }
  116. });
  117. client.setRequestHandler(mcpBundle.ListRootsRequestSchema, () => ({ roots: this._clientInfo?.roots || [] }));
  118. client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));
  119. const transport = await factory.connect();
  120. await client.connect(transport);
  121. this._currentClient = client;
  122. return client;
  123. }
  124. }
  125. // Annotate the CommonJS export names for ESM import in node:
  126. 0 && (module.exports = {
  127. ProxyBackend
  128. });