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
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes | ||
| expression | Yes |
Implementation Reference
- controller/app/tool_gateway.py:1151-1155 (handler)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) - controller/app/tool_gateway.py:662-670 (registration)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, ),