Skip to main content
Glama

Run JavaScript

execute_javascript

Run JavaScript in the page's own context to read globals, call page functions, or extract structured data in one pass, returning the result as JSON.

Instructions

Execute JavaScript in the page's own context and return its value as JSON.

The escape hatch for anything the dedicated tools do not cover: reading page globals, calling page functions, extracting structured data in one pass. Takes an expression, or a statement block that uses return (run as the body of an async function, so await works too). It runs in the page's main world, so it sees the page's variables and the page could in principle observe it; prefer the dedicated tools where they apply. Nothing is rolled back: whatever the script changed before it threw stays, and a script that times out may still be running in the page.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "script": "<script>"
    },
    "name": "execute_javascript"
  }
}

Input Schema

NameRequiredDescriptionDefault
tabNoTab id from list_tabs or new_tab to act on, instead of the active tab. The active tab does not change. Omit to use the active tab. Example: 'tab_2'
scriptYesJavaScript to run in the page: an expression ('document.title'), or statements that end in a return ('const n = document.links.length; return n'). Promises are awaited. Example: 'return [...document.querySelectorAll("h2")].map(h => h.textContent)'
timeoutNoSeconds to wait for the result, promises included, between 1 and 115. Raise it for scripts that poll or retry. Example: 45

Output Schema

NameRequiredDescriptionDefault
valueNoThe expression's value, as JSON (null if none).
summaryYesOne line saying what happened.
has_valueYesFalse when the expression produced undefined/null.

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.5.0

TDQS

A4.7/5.0
Behavior4/5

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

Goes well beyond the annotations: it discloses execution in the page's main world, that the page can observe the call, that nothing is rolled back on error, and that a timed-out script may still be running. These are real hazards an agent cannot infer from readOnlyHint=false / destructiveHint=false. Minor tension with destructiveHint=false (persisted mutations on throw) is mitigated because the description explicitly warns about it rather than hiding it.

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

Conciseness5/5

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

Front-loads the core action, then the escape-hatch routing, then execution mechanics, then safety caveats — a logical progression with no filler. Each clause (async body, main world, no rollback, timeout persistence) carries distinct information the agent needs.

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?

An output schema exists, all three parameters are documented at 100% coverage, and annotations cover the safety profile; the description fills the remaining gap — execution environment and irreversibility — so an agent has everything needed to call it correctly and cautiously.

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 baseline would be 3, but the description adds genuine meaning about the `script` parameter: it accepts an expression or a statement block that uses `return`, and runs as an async function body so `await` works. `tab` and `timeout` are left to their already-thorough schema descriptions.

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?

States a specific verb and resource ("Execute JavaScript in the page's own context and return its value as JSON") and immediately frames its role as "the escape hatch for anything the dedicated tools do not cover." That framing lets an agent distinguish it from the click/type_text/get_text_content siblings without opening any schema.

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?

Gives explicit when-to-use cases (reading page globals, calling page functions, extracting structured data in one pass) and an explicit when-not rule ("prefer the dedicated tools where they apply"). The tradeoff that the page could observe the script reinforces the same routing decision rather than restating the purpose.

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