proxyBackend.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. });