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
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- index-browser-only.js:382-393 (handler)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)}` }], }; }
- index-browser-only.js:149-160 (schema)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'], },
- index-browser-only.js:148-171 (registration)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: {}, }, }, ], }));
- index-browser-only.js:184-185 (registration)Dispatch/registration of the 'evaluate' tool handler in the CallToolRequestSchema switch statement.case 'evaluate': return await this.evaluate(args.script);
- index-browser-only.js:209-242 (helper)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)); });