program.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 program_exports = {};
  30. __export(program_exports, {
  31. decorateCommand: () => decorateCommand
  32. });
  33. module.exports = __toCommonJS(program_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_utilsBundle = require("playwright-core/lib/utilsBundle");
  36. var import_server = require("playwright-core/lib/server");
  37. var mcpServer = __toESM(require("./sdk/server"));
  38. var import_config = require("./browser/config");
  39. var import_watchdog = require("./browser/watchdog");
  40. var import_browserContextFactory = require("./browser/browserContextFactory");
  41. var import_proxyBackend = require("./sdk/proxyBackend");
  42. var import_browserServerBackend = require("./browser/browserServerBackend");
  43. var import_extensionContextFactory = require("./extension/extensionContextFactory");
  44. function decorateCommand(command, version) {
  45. command.option("--allowed-hosts <hosts...>", "comma-separated list of hosts this server is allowed to serve from. Defaults to the host the server is bound to. Pass '*' to disable the host check.", import_config.commaSeparatedList).option("--block-service-workers", "block service workers").option("--browser <browser>", "browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.").option("--caps <caps>", "comma-separated list of additional capabilities to enable, possible values: vision, pdf.", import_config.commaSeparatedList).option("--cdp-endpoint <endpoint>", "CDP endpoint to connect to.").option("--cdp-header <headers...>", "CDP headers to send with the connect request, multiple can be specified.", import_config.headerParser).option("--config <path>", "path to the configuration file.").option("--device <device>", 'device to emulate, for example: "iPhone 15"').option("--executable-path <path>", "path to the browser executable.").option("--extension", 'Connect to a running browser instance (Edge/Chrome only). Requires the "Playwright MCP Bridge" browser extension to be installed.').option("--grant-permissions <permissions...>", 'List of permissions to grant to the browser context, for example "geolocation", "clipboard-read", "clipboard-write".', import_config.commaSeparatedList).option("--headless", "run browser in headless mode, headed by default").option("--host <host>", "host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.").option("--ignore-https-errors", "ignore https errors").option("--init-page <path...>", "path to TypeScript file to evaluate on Playwright page object").option("--init-script <path...>", "path to JavaScript file to add as an initialization script. The script will be evaluated in every page before any of the page's scripts. Can be specified multiple times.").option("--isolated", "keep the browser profile in memory, do not save it to disk.").option("--image-responses <mode>", 'whether to send image responses to the client. Can be "allow" or "omit", Defaults to "allow".').option("--no-sandbox", "disable the sandbox for all process types that are normally sandboxed.").option("--output-dir <path>", "path to the directory for output files.").option("--port <port>", "port to listen on for SSE transport.").option("--proxy-bypass <bypass>", 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"').option("--proxy-server <proxy>", 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"').option("--save-session", "Whether to save the Playwright MCP session into the output directory.").option("--save-trace", "Whether to save the Playwright Trace of the session into the output directory.").option("--save-video <size>", 'Whether to save the video of the session into the output directory. For example "--save-video=800x600"', import_config.resolutionParser.bind(null, "--save-video")).option("--secrets <path>", "path to a file containing secrets in the dotenv format", import_config.dotenvFileLoader).option("--shared-browser-context", "reuse the same browser context between all connected HTTP clients.").option("--storage-state <path>", "path to the storage state file for isolated sessions.").option("--test-id-attribute <attribute>", 'specify the attribute to use for test ids, defaults to "data-testid"').option("--timeout-action <timeout>", "specify action timeout in milliseconds, defaults to 5000ms", import_config.numberParser).option("--timeout-navigation <timeout>", "specify navigation timeout in milliseconds, defaults to 60000ms", import_config.numberParser).option("--user-agent <ua string>", "specify user agent string").option("--user-data-dir <path>", "path to the user data directory. If not specified, a temporary directory will be created.").option("--viewport-size <size>", 'specify browser viewport size in pixels, for example "1280x720"', import_config.resolutionParser.bind(null, "--viewport-size")).addOption(new import_utilsBundle.ProgramOption("--connect-tool", "Allow to switch between different browser connection methods.").hideHelp()).addOption(new import_utilsBundle.ProgramOption("--vision", "Legacy option, use --caps=vision instead").hideHelp()).action(async (options) => {
  46. (0, import_watchdog.setupExitWatchdog)();
  47. if (options.vision) {
  48. console.error("The --vision option is deprecated, use --caps=vision instead");
  49. options.caps = "vision";
  50. }
  51. const config = await (0, import_config.resolveCLIConfig)(options);
  52. if (config.saveVideo && !checkFfmpeg()) {
  53. console.error(import_utilsBundle.colors.red(`
  54. Error: ffmpeg required to save the video is not installed.`));
  55. console.error(`
  56. Please run the command below. It will install a local copy of ffmpeg and will not change any system-wide settings.`);
  57. console.error(`
  58. npx playwright install ffmpeg
  59. `);
  60. process.exit(1);
  61. }
  62. const browserContextFactory = (0, import_browserContextFactory.contextFactory)(config);
  63. const extensionContextFactory = new import_extensionContextFactory.ExtensionContextFactory(config.browser.launchOptions.channel || "chrome", config.browser.userDataDir, config.browser.launchOptions.executablePath);
  64. if (options.extension) {
  65. const serverBackendFactory = {
  66. name: "Playwright w/ extension",
  67. nameInConfig: "playwright-extension",
  68. version,
  69. create: () => new import_browserServerBackend.BrowserServerBackend(config, extensionContextFactory)
  70. };
  71. await mcpServer.start(serverBackendFactory, config.server);
  72. return;
  73. }
  74. if (options.connectTool) {
  75. const providers = [
  76. {
  77. name: "default",
  78. description: "Starts standalone browser",
  79. connect: () => mcpServer.wrapInProcess(new import_browserServerBackend.BrowserServerBackend(config, browserContextFactory))
  80. },
  81. {
  82. name: "extension",
  83. description: "Connect to a browser using the Playwright MCP extension",
  84. connect: () => mcpServer.wrapInProcess(new import_browserServerBackend.BrowserServerBackend(config, extensionContextFactory))
  85. }
  86. ];
  87. const factory2 = {
  88. name: "Playwright w/ switch",
  89. nameInConfig: "playwright-switch",
  90. version,
  91. create: () => new import_proxyBackend.ProxyBackend(providers)
  92. };
  93. await mcpServer.start(factory2, config.server);
  94. return;
  95. }
  96. const factory = {
  97. name: "Playwright",
  98. nameInConfig: "playwright",
  99. version,
  100. create: () => new import_browserServerBackend.BrowserServerBackend(config, browserContextFactory)
  101. };
  102. await mcpServer.start(factory, config.server);
  103. });
  104. }
  105. function checkFfmpeg() {
  106. try {
  107. const executable = import_server.registry.findExecutable("ffmpeg");
  108. return import_fs.default.existsSync(executable.executablePath("javascript"));
  109. } catch (error) {
  110. return false;
  111. }
  112. }
  113. // Annotate the CommonJS export names for ESM import in node:
  114. 0 && (module.exports = {
  115. decorateCommand
  116. });