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,
    ),

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