@mcp-use/cli 3.2.0-canary.6 → 3.2.0-canary.7

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/dist/index.js CHANGED
@@ -2945,52 +2945,67 @@ function waitForDevToolsUrl(child, timeoutMs = 5e3) {
2945
2945
  });
2946
2946
  }
2947
2947
  async function captureScreenshot(opts) {
2948
- const userDataDir = mkdtempSync(path4.join(os4.tmpdir(), "mcp-use-chrome-"));
2949
- const chromeArgs = [
2950
- "--headless=new",
2951
- "--remote-debugging-port=0",
2952
- `--user-data-dir=${userDataDir}`,
2953
- "--no-first-run",
2954
- "--no-default-browser-check",
2955
- "--disable-extensions",
2956
- "--disable-gpu",
2957
- "--hide-scrollbars",
2958
- "--mute-audio",
2959
- `--window-size=${opts.width},${opts.height}`,
2960
- "about:blank"
2961
- ];
2962
- const child = spawn(opts.chromePath, chromeArgs, {
2963
- stdio: ["ignore", "pipe", "pipe"]
2964
- });
2965
- child.stdout?.resume();
2948
+ let userDataDir;
2949
+ let child;
2966
2950
  let cdp;
2967
2951
  let cleanedUp = false;
2968
2952
  const cleanup = () => {
2969
2953
  if (cleanedUp) return;
2970
2954
  cleanedUp = true;
2971
2955
  cdp?.close();
2972
- if (!child.killed) {
2956
+ if (child && !child.killed) {
2973
2957
  try {
2974
2958
  child.kill("SIGTERM");
2975
2959
  } catch {
2976
2960
  }
2961
+ const localChild = child;
2977
2962
  const killTimer = setTimeout(() => {
2978
- if (!child.killed) {
2963
+ if (!localChild.killed) {
2979
2964
  try {
2980
- child.kill("SIGKILL");
2965
+ localChild.kill("SIGKILL");
2981
2966
  } catch {
2982
2967
  }
2983
2968
  }
2984
2969
  }, 2e3);
2985
2970
  killTimer.unref();
2986
2971
  }
2987
- try {
2988
- rmSync(userDataDir, { recursive: true, force: true });
2989
- } catch {
2972
+ if (userDataDir) {
2973
+ try {
2974
+ rmSync(userDataDir, { recursive: true, force: true });
2975
+ } catch {
2976
+ }
2990
2977
  }
2991
2978
  };
2992
2979
  try {
2993
- const wsUrl = await waitForDevToolsUrl(child);
2980
+ let wsUrl;
2981
+ if (opts.cdpUrl) {
2982
+ wsUrl = opts.cdpUrl;
2983
+ } else {
2984
+ if (!opts.chromePath) {
2985
+ throw new Error(
2986
+ "captureScreenshot requires either `cdpUrl` or `chromePath`"
2987
+ );
2988
+ }
2989
+ userDataDir = mkdtempSync(path4.join(os4.tmpdir(), "mcp-use-chrome-"));
2990
+ const chromeArgs = [
2991
+ "--headless=new",
2992
+ "--remote-debugging-port=0",
2993
+ `--user-data-dir=${userDataDir}`,
2994
+ "--no-first-run",
2995
+ "--no-default-browser-check",
2996
+ "--disable-extensions",
2997
+ "--disable-gpu",
2998
+ "--hide-scrollbars",
2999
+ "--mute-audio",
3000
+ `--window-size=${opts.width},${opts.height}`,
3001
+ "about:blank"
3002
+ ];
3003
+ child = spawn(opts.chromePath, chromeArgs, {
3004
+ stdio: ["ignore", "pipe", "pipe"]
3005
+ });
3006
+ child.stdout?.resume();
3007
+ wsUrl = await waitForDevToolsUrl(child);
3008
+ }
2994
3009
  const ws = new WebSocket(wsUrl);
2995
3010
  await new Promise((resolve2, reject) => {
2996
3011
  const onOpen = () => {
@@ -3005,14 +3020,47 @@ async function captureScreenshot(opts) {
3005
3020
  ws.once("error", onError);
3006
3021
  });
3007
3022
  cdp = new CdpClient(ws);
3008
- const { targetId } = await cdp.send(
3009
- "Target.createTarget",
3010
- { url: "about:blank" }
3011
- );
3012
- const { sessionId } = await cdp.send(
3013
- "Target.attachToTarget",
3014
- { targetId, flatten: true }
3015
- );
3023
+ let sessionId;
3024
+ if (opts.cdpUrl) {
3025
+ const attachPromise = new Promise((resolve2, reject) => {
3026
+ const timer = setTimeout(
3027
+ () => reject(
3028
+ new Error(
3029
+ "Timed out waiting for Target.attachedToTarget event from remote CDP"
3030
+ )
3031
+ ),
3032
+ 1e4
3033
+ );
3034
+ const onMessage = (data) => {
3035
+ try {
3036
+ const msg = JSON.parse(data.toString());
3037
+ if (msg.method === "Target.attachedToTarget" && msg.params?.targetInfo?.type === "page" && typeof msg.params.sessionId === "string") {
3038
+ clearTimeout(timer);
3039
+ ws.off("message", onMessage);
3040
+ resolve2(msg.params.sessionId);
3041
+ }
3042
+ } catch {
3043
+ }
3044
+ };
3045
+ ws.on("message", onMessage);
3046
+ });
3047
+ await cdp.send("Target.setAutoAttach", {
3048
+ autoAttach: true,
3049
+ waitForDebuggerOnStart: false,
3050
+ flatten: true
3051
+ });
3052
+ sessionId = await attachPromise;
3053
+ } else {
3054
+ const { targetId } = await cdp.send(
3055
+ "Target.createTarget",
3056
+ { url: "about:blank" }
3057
+ );
3058
+ const attach = await cdp.send(
3059
+ "Target.attachToTarget",
3060
+ { targetId, flatten: true }
3061
+ );
3062
+ sessionId = attach.sessionId;
3063
+ }
3016
3064
  await cdp.send("Page.enable", {}, sessionId);
3017
3065
  await cdp.send(
3018
3066
  "Emulation.setDeviceMetricsOverride",
@@ -3192,7 +3240,7 @@ async function captureToolScreenshot(inputs, options = {}) {
3192
3240
  const theme = options.theme ?? "light";
3193
3241
  const timeoutMs = options.timeoutMs ?? 3e4;
3194
3242
  const delayMs = options.delayMs ?? 0;
3195
- const chromePath = resolveChromePath();
3243
+ const chromePath = options.cdpUrl ? void 0 : resolveChromePath();
3196
3244
  const view = extractViewName(inputs.resourceUri);
3197
3245
  const devOptions = {
3198
3246
  width: String(width),
@@ -3228,6 +3276,7 @@ async function captureToolScreenshot(inputs, options = {}) {
3228
3276
  timeoutMs,
3229
3277
  outputPath,
3230
3278
  chromePath,
3279
+ cdpUrl: options.cdpUrl,
3231
3280
  delayMs: Number.isFinite(delayMs) && delayMs > 0 ? delayMs : 0,
3232
3281
  bundle
3233
3282
  });
@@ -3488,7 +3537,8 @@ async function screenshotCommand(options, argsList) {
3488
3537
  delayMs,
3489
3538
  timeoutMs: navTimeout,
3490
3539
  inspector: options.inspector,
3491
- quiet: options.quiet
3540
+ quiet: options.quiet,
3541
+ cdpUrl: options.cdpUrl
3492
3542
  }
3493
3543
  );
3494
3544
  console.log(
@@ -3534,7 +3584,10 @@ function createScreenshotCommand() {
3534
3584
  "--delay <ms>",
3535
3585
  "Extra wait after readiness, to let chart animations / async layouts settle.",
3536
3586
  "0"
3537
- ).option("--timeout <ms>", "Navigation + readiness timeout in ms.", "30000").option("--quiet", "Suppress dev-server output.").action(async (args, opts) => {
3587
+ ).option("--timeout <ms>", "Navigation + readiness timeout in ms.", "30000").option(
3588
+ "--cdp-url <url>",
3589
+ "Connect to an existing CDP WebSocket (ws:// or wss://) instead of spawning local Chrome. Useful for hosted browsers like Notte."
3590
+ ).option("--quiet", "Suppress dev-server output.").action(async (args, opts) => {
3538
3591
  await screenshotCommand(opts, args);
3539
3592
  });
3540
3593
  }