browser_evaluate
Execute client-side JavaScript within a webpage context using a concurrent MCP server. Supports multiple browser instances for efficient script evaluation.
Instructions
Execute JavaScript code in the page context
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instanceId | Yes | Instance ID | |
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/tools.ts:979-998 (handler)Executes the provided JavaScript code in the browser instance's page context using Playwright's page.evaluate() method, handling errors and returning structured results.private async evaluate(instanceId: string, script: string): Promise<ToolResult> { const instance = this.browserManager.getInstance(instanceId); if (!instance) { return { success: false, error: `Instance ${instanceId} not found` }; } try { const result = await instance.page.evaluate(script); return { success: true, data: { script, result }, instanceId }; } catch (error) { return { success: false, error: `Evaluate failed: ${error instanceof Error ? error.message : error}`, instanceId }; }
- src/tools.ts:441-458 (schema)Defines the tool schema, including name, description, and input parameters (instanceId and script).{ name: 'browser_evaluate', description: 'Execute JavaScript code in the page context', inputSchema: { type: 'object', properties: { instanceId: { type: 'string', description: 'Instance ID' }, script: { type: 'string', description: 'JavaScript code to execute', } }, required: ['instanceId', 'script'] } },
- src/server.ts:40-45 (registration)Registers the MCP listTools handler, which returns all tool definitions from BrowserTools.getTools(), including browser_evaluate.this.server.setRequestHandler(ListToolsRequestSchema, async () => { const tools = this.browserTools.getTools(); return { tools: tools, }; });
- src/tools.ts:575-576 (handler)Dispatch case in the central executeTools switch statement that routes browser_evaluate calls to the specific evaluate handler.case 'browser_evaluate': return await this.evaluate(args.instanceId, args.script);
- src/server.ts:47-75 (registration)Registers the MCP CallTool handler, which invokes BrowserTools.executeTools for the requested tool name, handling browser_evaluate among others.// Handle tool call requests this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { const result = await this.browserTools.executeTools(name, args || {}); if (result.success) { return { content: [ { type: 'text', text: JSON.stringify(result.data, null, 2), }, ], }; } else { throw new McpError(ErrorCode.InternalError, result.error || 'Tool execution failed'); } } catch (error) { if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Tool execution failed: ${error instanceof Error ? error.message : error}` ); } });