Skip to main content
Glama

Execute JavaScript in a page

javascript_tool

Evaluate a JavaScript expression in a Chrome tab's main or isolated world and return the last JSON-serializable value, even when the page's CSP blocks eval.

Instructions

Evaluate JavaScript in the main frame and return the value of the last expression. Do NOT use 'return' — just write the expression. The result must be JSON-serializable (undefined comes back as null); a returned promise is awaited for up to 25s. By default it runs in the page's MAIN world and sees the page's own JavaScript. world:'isolated' runs it in a dedicated isolated world of the main frame instead (reused within a document, so its own globals persist between calls): same DOM, but none of the page's globals or patched prototypes, so a page cannot change the DOM functions the script uses. It still reads the page's own content; if that world cannot be created the call fails and nothing runs (no fallback to MAIN). Runs through the debugger tier, so it works on pages whose Content-Security-Policy forbids eval — the page's own eval stays blocked, and the page's policy is never edited. Close DevTools on that tab first (only one debugger can attach). If the page navigates while the script is evaluating, the call fails and the script is NOT run again.

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": {
      "text": "<text>",
      "tabId": "<tabId>"
    },
    "name": "javascript_tool"
  }
}

Input Schema

NameRequiredDescriptionDefault
textYesJavaScript expression to evaluate
tabIdYesTarget tab ID (must be in the MCP group)
worldNo'main' (default): the page's own JavaScript world. 'isolated': a dedicated isolated world (reused within a document) sharing only the DOM

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.24.1

TDQS

A4.6/5.0
Behavior5/5

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

With no annotations supplied, the description carries the full disclosure burden and does so thoroughly: JSON-serializability with undefined mapped to null, a 25s await for returned promises, no-fallback failure when the isolated world cannot be created, and an explicit failure (not a retry) if the page navigates mid-evaluation. It also explains the debugger tier's CSP interaction — the page's own eval stays blocked and its policy is never edited.

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 with the core contract, then prerequisites and edge cases, so an agent can stop reading early and still call it correctly. It is dense — heavy parentheticals and a long isolated-world sentence — but essentially every sentence adds operational information rather than restating the schema.

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?

For a three-parameter, no-annotation, no-output-schema execution tool, the description covers the return contract, serialization rules, async behavior, world isolation semantics, debugger attachment conflicts, CSP behavior, and navigation failure modes. Nothing an agent needs to invoke it correctly appears to be 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 genuinely adds meaning beyond the schema: the 'text' parameter is constrained to expression-only (no 'return'), its value must be JSON-serializable, and promises are awaited. The 'world' parameter is expanded from a one-line schema note into a full account of globals persistence and prototype isolation.

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?

Opens with a specific verb and resource — 'Evaluate JavaScript in the main frame and return the value of the last expression' — which is unambiguous about what the call does and what comes back. It is clearly distinguishable from read-only extraction siblings such as read_page or get_page_text because it returns an expression value, not page-derived text.

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

Usage Guidelines4/5

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

Gives concrete usage rules: don't write 'return', write a bare expression, pick world:'isolated' when the page's globals or patched prototypes must not be trusted, and close DevTools on the tab first. It does not explicitly say when to prefer a sibling tool (e.g. read_page) over running JavaScript, so guidance is strong but not a full alternative routing.

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