Skip to main content
Glama

interceptor_browser_evaluate

Execute a JavaScript file in a browser page and retrieve its return value. Supports optional arguments and isolated or main execution worlds.

Instructions

Execute a JS file in the page and return its result. Source is loaded from script_path (absolute path). The file body is wrapped in an arrow function receiving __args (so the file may return value; directly and access the optional args object). Worlds: isolated (default) or main (camoufox-only, requires main_world_eval: true at launch). Cloakbrowser (Chromium): isolated runs in Playwright's utility world (different window, same DOM). main is rejected — use interceptor_browser_inject_init_script for main-world patching there. Camoufox (cloverlabs/FF150): there is no separate isolated world — both permitted modes run in the page's main world. Reads are invisible to the page; mutations (window.x = …, Object.defineProperty, prototype patches) are observable by page scripts. Earlier daijro/FF135 had a Juggler scope that made isolated invisible to the page; that scope was removed in cloverlabs. Verify on your installed build with scripts/camoufox-world-probe.ts. Rate-limit on cloakbrowser before reCAPTCHA: each call emits CDP traffic that behavioural scorers count.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
target_idYesTarget ID from interceptor_browser_launch or interceptor_camoufox_launch
script_pathYesAbsolute path to a .js file. File body is the function body; use `return` to send a value back.
argsNoOptional JSON-serialisable args object, available inside the script as `__args`.
worldNo`isolated` (default) or `main`. On current camoufox build (cloverlabs/FF150) both run in the page's main world — arg is accepted but has no observable effect.isolated
value_max_charsNoMax characters of the JSON-stringified return value (default: 20000).

Implementation Reference

  • Main handler function for the interceptor_browser_evaluate tool. Reads a JS file, wraps it in an arrow function, evaluates it in the target page (isolated or main world), and returns the capped result.
    server.tool(
      "interceptor_browser_evaluate",
      "Execute a JS file in the page and return its result. " +
      "Source is loaded from `script_path` (absolute path). The file body is wrapped in an arrow " +
      "function receiving `__args` (so the file may `return value;` directly and access the optional " +
      "args object). " +
      "Worlds: `isolated` (default) or `main` (camoufox-only, requires `main_world_eval: true` at launch). " +
      "Cloakbrowser (Chromium): `isolated` runs in Playwright's utility world (different `window`, same DOM). " +
      "`main` is rejected — use `interceptor_browser_inject_init_script` for main-world patching there. " +
      "Camoufox (cloverlabs/FF150): there is no separate isolated world — both permitted modes run in the page's main world. " +
      "Reads are invisible to the page; mutations (`window.x = …`, `Object.defineProperty`, prototype patches) are observable by page scripts. " +
      "Earlier daijro/FF135 had a Juggler scope that made `isolated` invisible to the page; that scope was removed in cloverlabs. " +
      "Verify on your installed build with `scripts/camoufox-world-probe.ts`. " +
      "Rate-limit on cloakbrowser before reCAPTCHA: each call emits CDP traffic that behavioural scorers count.",
      {
        target_id: z.string().describe("Target ID from interceptor_browser_launch or interceptor_camoufox_launch"),
        script_path: z.string().describe("Absolute path to a .js file. File body is the function body; use `return` to send a value back."),
        args: z.record(z.unknown()).optional().describe("Optional JSON-serialisable args object, available inside the script as `__args`."),
        world: z.enum(["isolated", "main"]).optional().default("isolated")
          .describe("`isolated` (default) or `main`. On current camoufox build (cloverlabs/FF150) both run in the page's main world — arg is accepted but has no observable effect."),
        value_max_chars: z.number().optional().default(HARD_VALUE_CAP_CHARS)
          .describe(`Max characters of the JSON-stringified return value (default: ${HARD_VALUE_CAP_CHARS}).`),
      },
      async ({ target_id, script_path, args, world, value_max_chars }) => {
        try {
          if (!isAbsolute(script_path)) {
            return { content: [{ type: "text", text: JSON.stringify({ status: "error", error: `script_path must be absolute: '${script_path}'` }) }] };
          }
          const source = await readFile(script_path, "utf-8");
          const page = await getPageForTarget(target_id);
          const isCamoufox = isCamoufoxTarget(target_id);
    
          if (world === "main") {
            if (!isCamoufox) {
              return { content: [{ type: "text", text: JSON.stringify({
                status: "error",
                error: "world: 'main' is only supported on camoufox targets. On cloakbrowser, use interceptor_browser_inject_init_script for main-world patching.",
              }) }] };
            }
            const entry = getEntry(target_id);
            const mwEnabled = Boolean((entry.target.details as { main_world_eval?: boolean } | undefined)?.main_world_eval);
            if (!mwEnabled) {
              return { content: [{ type: "text", text: JSON.stringify({
                status: "error",
                error: "Camoufox target launched without main_world_eval=true; main-world evaluate is disabled. Relaunch with `main_world_eval: true`.",
              }) }] };
            }
          }
    
          const argsLiteral = JSON.stringify(args ?? {});
          const fnExpr = `((__args) => { ${source}\n })(${argsLiteral})`;
          const pageFunction = world === "main" && isCamoufox ? `mw:${fnExpr}` : fnExpr;
    
          const result = await page.evaluate(pageFunction);
          const serialised = result === undefined ? "" : JSON.stringify(result);
          const capped = capValue(serialised, Math.max(0, Math.min(HARD_VALUE_CAP_CHARS, Math.trunc(value_max_chars ?? HARD_VALUE_CAP_CHARS))));
    
          return {
            content: [{
              type: "text",
              text: truncateResult({
                status: "success",
                target_id,
                world,
                backend: isCamoufox ? "camoufox" : "cloakbrowser",
                value: capped.value,
                value_length: capped.valueLength,
                value_truncated: capped.truncated,
                value_max_chars: capped.maxChars,
              }),
            }],
          };
        } catch (e) {
          return { content: [{ type: "text", text: JSON.stringify({ status: "error", error: errorToString(e) }) }] };
        }
      },
    );
  • Zod schema defining the input parameters for the evaluate tool: target_id, script_path, args (optional), world (enum isolated/main), value_max_chars.
      target_id: z.string().describe("Target ID from interceptor_browser_launch or interceptor_camoufox_launch"),
      script_path: z.string().describe("Absolute path to a .js file. File body is the function body; use `return` to send a value back."),
      args: z.record(z.unknown()).optional().describe("Optional JSON-serialisable args object, available inside the script as `__args`."),
      world: z.enum(["isolated", "main"]).optional().default("isolated")
        .describe("`isolated` (default) or `main`. On current camoufox build (cloverlabs/FF150) both run in the page's main world — arg is accepted but has no observable effect."),
      value_max_chars: z.number().optional().default(HARD_VALUE_CAP_CHARS)
        .describe(`Max characters of the JSON-stringified return value (default: ${HARD_VALUE_CAP_CHARS}).`),
    },
  • Helper function that caps a string value to a maximum number of characters, appending '...' if truncated.
    function capValue(value: string, maxChars: number): { value: string; valueLength: number; truncated: boolean; maxChars: number } {
      const valueLength = value.length;
      const effectiveMax = Math.max(0, Math.min(HARD_VALUE_CAP_CHARS, Math.trunc(maxChars)));
      if (effectiveMax === 0) {
        return { value, valueLength, truncated: false, maxChars: 0 };
      }
      if (valueLength <= effectiveMax) {
        return { value, valueLength, truncated: false, maxChars: effectiveMax };
      }
      return { value: value.slice(0, effectiveMax) + "...", valueLength, truncated: true, maxChars: effectiveMax };
    }
  • Helper that resolves a target_id to the corresponding Playwright Page object, supporting both Camoufox and Cloakbrowser targets.
      return isCamoufoxTargetId(targetId);
    }
    
    async function ensureCamoufoxPage(entry: CamoufoxEntryWithDriver): Promise<Page> {
      if (entry.page && !entry.page.isClosed()) return entry.page;
      if (!entry.browser) {
        entry.browser = await firefox.connect(entry.wsUrl);
      }
      let ctx = entry.browser.contexts()[0];
      if (!ctx) {
        // BrowserServer + persistent_context: the persistent context lives
        // server-side and `Browser.contexts()` from a fresh `firefox.connect()`
        // returns empty. New contexts created here do NOT inherit the
        // launch-level proxy, so we have to wire the MITM proxy explicitly
        // or the firefox process reaches the internet directly and bypasses
        // capture. Pull the port back out of the entry details (set at
        // activate() time).
        const proxyPort = (entry.target.details as { proxyPort?: number } | undefined)?.proxyPort;
        ctx = await entry.browser.newContext({
          ignoreHTTPSErrors: true,
          ...(proxyPort ? { proxy: { server: `http://127.0.0.1:${proxyPort}` } } : {}),
        });
      }
      let page = ctx.pages()[0];
      if (!page) {
        page = await ctx.newPage();
      }
      entry.context = ctx;
      entry.page = page;
      ensureConsoleBuffer(entry, page);
      return page;
    }
    
    function ensureConsoleBuffer(entry: CamoufoxEntryWithDriver, page: Page): ConsoleEntry[] {
      if (!entry.consoleBuffer) entry.consoleBuffer = [];
      if (entry.consolePage === page) return entry.consoleBuffer;
    
      entry.consolePage = page;
      page.on("console", (msg: ConsoleMessage) => {
        const loc = msg.location();
        entry.consoleBuffer?.push({
          type: msg.type(),
          text: msg.text(),
          location: loc.url ? `${loc.url}:${loc.lineNumber ?? 0}:${loc.columnNumber ?? 0}` : "",
          timestamp: Date.now(),
        });
        if (entry.consoleBuffer && entry.consoleBuffer.length > CONSOLE_BUFFER_MAX) {
          entry.consoleBuffer.splice(0, entry.consoleBuffer.length - CONSOLE_BUFFER_MAX);
        }
      });
    
      return entry.consoleBuffer;
    }
    
    export function getEntry(targetId: string): BrowserTargetEntry | CamoufoxEntryWithDriver {
      if (isCamoufoxTargetId(targetId)) {
        const cam = getCamoufoxInterceptor();
        const entry = cam?.getEntry(targetId) as CamoufoxEntryWithDriver | undefined;
        if (!entry) throw new Error(`Browser target '${targetId}' not found. Is it still running?`);
        return entry;
      }
      const entry = getBrowserInterceptor().getEntry(targetId);
      if (!entry) throw new Error(`Browser target '${targetId}' not found. Is it still running?`);
      return entry;
    }
    
    /**
     * Cloakbrowser-only entry getter. Use when the caller needs cloakbrowser
     * internals rather than the shared page/context helpers. Camoufox targets get
     * a clear error instead of a deep type-mismatch.
     */
    export function getBrowserEntry(targetId: string): BrowserTargetEntry {
      if (isCamoufoxTargetId(targetId)) {
        throw new Error(
          `Internal cloakbrowser-only entry requested for camoufox target ('${targetId}'). ` +
          "Use the shared browser tools for camoufox targets.",
        );
      }
      const entry = getBrowserInterceptor().getEntry(targetId);
      if (!entry) throw new Error(`Browser target '${targetId}' not found. Is it still running?`);
      return entry;
    }
    
    export async function getPageForTarget(targetId: string): Promise<Page> {
  • Helper that checks whether a given target ID belongs to a Camoufox browser (prefixed with 'camoufox_').
    function isCamoufoxTargetId(targetId: string): boolean {
      return typeof targetId === "string" && targetId.startsWith("camoufox_");
    }
    
    export function isCamoufoxTarget(targetId: string): boolean {
      return isCamoufoxTargetId(targetId);
    }
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Discloses multiple behavioral traits: script wrapping in arrow function, world isolation differences, mutation visibility, rate-limiting implications, and provides a verification command. No annotations present, so description fully covers the burden.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Description is detailed and front-loaded with the main purpose, but slightly verbose. Could be trimmed while retaining key information, but still well-structured and informative.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Covers environment differences, safety, limitations, and return value truncation. Lacks explicit return value format but infers JSON-stringified result. Adequate for a complex tool with no output schema.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema has 100% coverage, so baseline is 3. Description adds value by explaining that script_path is absolute, args accessible as __args, world mode details, and default for value_max_chars.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states 'Execute a JS file in the page and return its result' with a specific verb and resource. It distinguishes from sibling tools like interceptor_browser_inject_init_script by explaining when to use each.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Provides explicit guidance on when to use isolated vs main world, including platform-specific behavior (Cloakbrowser vs Camoufox) and suggests alternative tool for main-world patching on Chromium.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yfe404/proxy-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server