Skip to main content
Glama

execute_js

Run JavaScript in the active page to click, scroll, read, or inspect DOM elements when overlays, viewport limits, or native interaction timeouts block standard actions.

Instructions

Run JavaScript directly in the active page. PRIMARY INTERACTION TOOL: try execute_js FIRST for clicks, scrolls, reads, attribute lookups, and most other DOM work -- it bypasses overlay/obscured-element issues, viewport constraints, and CDP timeouts that block native click/scroll. Typical patterns: return document.querySelector('#add-to-cart-button').click(), true; for clicks; return Array.from(document.querySelectorAll('a')).map(a=>a.href); for extraction; window.scrollTo(0, document.body.scrollHeight); return true; for scroll. After a JS click, verify with read_page or get_page_snapshot (a true click should produce observable DOM change). FALLBACK TO NATIVE TOOLS WHEN: (1) JS click reports success but the page state did not change (framework swallowed the synthetic event -- use native click which fires real CDP events that React/Angular/Vue listen to); (2) typing into controlled text inputs (use native type so React onChange fires correctly -- element.value = ... will NOT update component state); (3) real drag operations on react-beautiful-dnd / Sortable.js / Trello-style widgets (use drag_drop / drag for real pointer events); (4) form submission that depends on validated input state (use native click on submit). Code runs as a function body -- use return to send values back (results are stringified). Async work: Promises are not awaited, so fire-and-forget then poll on window.__yourKey. Related: read_page (verify result), get_dom_snapshot (debug after JS click), click / type / drag_drop (fallbacks when JS doesn't take effect). Multi-agent: agent-scoped tabs; cross-agent reject with TAB_NOT_OWNED; cap configurable (default 8, 1-64). Pass tab_id only when this agent owns multiple tabs; auto-resolves otherwise. RETURNS change_report: when this tool runs, the response includes a change_report field with a compact diff of what the action mutated (URL, dialogs_opened, nodes_added, nodes_removed, attrs_changed, inputs_changed, focus_shift). Use this to learn the consequence without calling read_page next. If the report exceeds the size cap, change_report.truncated:true and change_report_hint are set; call read_page for the full state.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesJavaScript code to execute in the page (use `return` to send a value back, e.g., "return document.title;" or "document.querySelector('button').click()")
clientYesAllowlisted client label. Validated against the shared v0.9.36 server/extension allowlist (see mcp/src/tools/visual-session.ts -- MCP_VISUAL_CLIENT_LABELS). Required.
tab_idNoOptional. Tab id this action targets. Omit when the calling agent owns exactly one tab; pass to disambiguate when the agent owns multiple. Single-tab agents and legacy popup/sidepanel/autopilot do not need to pass this.
is_finalNoWhen true, the visual session clears immediately after this tool resolves. Optional (default false).
visual_reasonYesShort human-readable reason shown in the overlay (for example, "Logging in to GitHub"). Required.

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.1.0

TDQS

A4.8/5.0
Behavior5/5

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

With no annotations, the description carries the full burden and does so thoroughly: code runs as a function body, `return` is required, results are stringified, Promises are NOT awaited (poll on window.__yourKey), multi-agent tab scoping with TAB_NOT_OWNED rejection, a configurable cap of 8 (1-64), and a returned change_report with truncation behavior. This is behavior an agent could not infer from the schema.

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?

Front-loaded and clearly sectioned ('Typical patterns:', 'FALLBACK TO NATIVE TOOLS WHEN:', 'RETURNS change_report:'), with concrete code snippets the agent can adapt. Dense but nearly every sentence earns its place; the repeated advise-to-verify-with-read_page/get_page_snapshot is slightly redundant but purposeful.

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

Completeness5/5

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

Although there is no output schema and no annotations, the description documents the return payload (change_report fields: URL, dialogs_opened, nodes_added/removed, attrs_changed, inputs_changed, focus_shift) and its truncation flag, plus execution semantics and fallback policy. Nothing needed to invoke or interpret the call is missing.

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 coverage is 100%, so the baseline is 3, but the description adds real meaning beyond the schema: tab_id should only be passed when the agent owns multiple tabs (auto-resolves otherwise), is_final clears the visual session on resolution, and `code` semantics (no awaited Promises, stringified returns) extend the schema's terse 'use return to send a value back'.

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?

Specific verb+resource ('Run JavaScript directly in the active page') with an explicit statement that it is the PRIMARY INTERACTION TOOL for DOM work. It is immediately distinguishable from siblings like click, type_text, and drag_drop, which it deliberately enumerates as fallbacks.

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?

Exceptional routing guidance: 'try execute_js FIRST' plus a numbered FALLBACK TO NATIVE TOOLS WHEN list specifying exactly when native click/type/drag must be used (swallowed synthetic events, React controlled inputs, drag libraries, validated form submission). Both when-to-use and when-not-to-use are explicit.

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