Skip to main content
Glama

evaluate

Execute JavaScript code in a browser to automate tasks, test web functionality, or extract data during browser automation sessions.

Instructions

Execute JavaScript in the browser

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • The core handler function for the 'evaluate' tool. It ensures the Chromium browser is running, sends a Chrome DevTools Protocol (CDP) 'Runtime.evaluate' command with the provided script, and returns the result as a formatted text response.
    async evaluate(script) { await this.ensureChromium(); const result = await this.sendCDPCommand('Runtime.evaluate', { expression: script, returnByValue: true, }); return { content: [{ type: 'text', text: `Result: ${JSON.stringify(result.result.value)}` }], }; }
  • The input schema definition for the 'evaluate' tool, specifying that it requires a 'script' string parameter.
    name: 'evaluate', description: 'Execute JavaScript in the browser (read-only operations)', inputSchema: { type: 'object', properties: { script: { type: 'string', description: 'JavaScript code to execute (for reading page info)', }, }, required: ['script'], },
  • Registration of the 'evaluate' tool in the ListTools response, including name, description, and schema.
    { name: 'evaluate', description: 'Execute JavaScript in the browser (read-only operations)', inputSchema: { type: 'object', properties: { script: { type: 'string', description: 'JavaScript code to execute (for reading page info)', }, }, required: ['script'], }, }, { name: 'close_browser', description: 'Close the browser instance', inputSchema: { type: 'object', properties: {}, }, }, ], }));
  • Dispatch/registration of the 'evaluate' tool handler in the CallToolRequestSchema switch statement.
    case 'evaluate': return await this.evaluate(args.script);
  • Helper function used by the evaluate handler to send CDP commands over WebSocket to the browser.
    async sendCDPCommand(method, params = {}) { return new Promise((resolve, reject) => { if (!wsConnection || wsConnection.readyState !== WebSocket.OPEN) { reject(new Error('No browser connection available')); return; } const commandId = Date.now(); const command = { id: commandId, method, params }; const timeout = setTimeout(() => { reject(new Error(`CDP command timeout: ${method}`)); }, 10000); const messageHandler = (data) => { try { const response = JSON.parse(data.toString()); if (response.id === commandId) { clearTimeout(timeout); wsConnection.off('message', messageHandler); if (response.error) { reject(new Error(`CDP error: ${response.error.message}`)); } else { resolve(response.result); } } } catch (error) { // Ignore parsing errors for non-matching messages } }; wsConnection.on('message', messageHandler); wsConnection.send(JSON.stringify(command)); });

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/nfodor/claude-arm64-browser'

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