Skip to main content
Glama

browser.eval_js

Execute JavaScript in browser pages to query DOM elements, extract data, or perform lightweight scripting tasks directly within the current page context.

Instructions

Execute a JavaScript expression in the current page context and return the result. Use for DOM queries, value extraction, or lightweight scripting that has no dedicated tool.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
session_idYes
expressionYes

Implementation Reference

  • The handler function `_eval_js` in `McpToolGateway` executes a JavaScript expression in the current page context using Playwright's `evaluate` method.
    async def _eval_js(self, payload: EvalJsInput) -> dict[str, Any]:
        session = await self.manager.get_session(payload.session_id)
        result = await session.page.evaluate(payload.expression)
        return {"session_id": payload.session_id, "result": result}
  • Input model for `browser.eval_js` tool.
    class EvalJsInput(SessionIdInput):
        expression: str = Field(min_length=1, max_length=50000)
  • Registration of `browser.eval_js` tool in `McpToolGateway`'s `_tools` map.
        name="browser.eval_js",
        description=(
            "Execute a JavaScript expression in the current page context "
            "and return the result. Use for DOM queries, value extraction, "
            "or lightweight scripting that has no dedicated tool."
        ),
        input_model=EvalJsInput,
        handler=self._eval_js,
    ),
Behavior2/5

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

No annotations provided, so description carries full burden. While it mentions returning a result, it fails to disclose critical execution semantics: error handling (what happens if the JS throws?), timeout behavior, side effects (does it modify page state?), async/Promise resolution, or return value serialization format.

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?

Two sentences, zero waste. First sentence defines the operation; second provides usage context. Perfectly front-loaded with no redundant information.

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

Completeness3/5

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

Adequate for basic tool selection but insufficient for a code-execution tool given the lack of annotations and output schema. Missing critical safety and behavioral context (execution environment, error handling) that would be necessary for an agent to use this tool effectively without trial and error.

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

Parameters3/5

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

With 0% schema description coverage, the description partially compensates by clarifying that 'expression' should be a JavaScript expression. However, it fails to document 'session_id' (though implied by 'current page context') or validation constraints (maxLength 50000, minLength 1).

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 specific action ('Execute a JavaScript expression'), context ('current page context'), and outcome ('return the result'). The phrase 'lightweight scripting that has no dedicated tool' effectively distinguishes it from siblings like find_elements or get_html.

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?

Provides explicit when-to-use guidance ('Use for DOM queries, value extraction, or lightweight scripting'), implicitly directing users away from this tool when dedicated alternatives exist. Lacks explicit exclusions or prerequisites (e.g., session must be active).

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/LvcidPsyche/auto-browser'

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