Skip to main content
Glama

browser_evaluate

Execute JavaScript expressions on web pages or specific elements using structured accessibility snapshots for precise interactions through Playwright MCP server.

Instructions

Evaluate JavaScript expression on page or element

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
elementNoHuman-readable element description used to obtain permission to interact with the element
functionYes() => { /* code */ } or (element) => { /* code */ } when element is provided
refNoExact target element reference from the page snapshot

Implementation Reference

  • The handler function that performs the JavaScript evaluation on the browser page or element. It handles locators if provided, adds replay code to the response, waits for completion, and adds the result.
    handle: async (tab, params, response) => { response.setIncludeSnapshot(); let locator: playwright.Locator | undefined; if (params.ref && params.element) { locator = await tab.refLocator({ ref: params.ref, element: params.element }); response.addCode(`await page.${await generateLocator(locator)}.evaluate(${javascript.quote(params.function)});`); } else { response.addCode(`await page.evaluate(${javascript.quote(params.function)});`); } await tab.waitForCompletion(async () => { const receiver = locator ?? tab.page as any; const result = await receiver._evaluateFunction(params.function); response.addResult(JSON.stringify(result, null, 2) || 'undefined'); }); },
  • Zod schema for input validation (function, optional element, ref) and the tool schema definition including name 'browser_evaluate'.
    const evaluateSchema = z.object({ function: z.string().describe('() => { /* code */ } or (element) => { /* code */ } when element is provided'), element: z.string().optional().describe('Human-readable element description used to obtain permission to interact with the element'), ref: z.string().optional().describe('Exact target element reference from the page snapshot'), }); const evaluate = defineTabTool({ capability: 'core', schema: { name: 'browser_evaluate', title: 'Evaluate JavaScript', description: 'Evaluate JavaScript expression on page or element', inputSchema: evaluateSchema, type: 'destructive', },
  • Exports the defined tool for import and collection in src/tools.ts
    export default [ evaluate, ];
  • src/tools.ts:36-52 (registration)
    Collects and exports all tools, spreading the evaluate tool (browser_evaluate) into the allTools array used by the browser server backend.
    export const allTools: Tool<any>[] = [ ...common, ...console, ...dialogs, ...evaluate, ...files, ...install, ...keyboard, ...navigate, ...network, ...mouse, ...pdf, ...screenshot, ...snapshot, ...tabs, ...wait, ];

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/nzjami/mcpPlaywright'

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