browser_evaluate
Execute JavaScript code within browser page contexts to automate interactions, extract data, or modify content across multiple parallel browser instances.
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)Core handler that executes JavaScript in the browser page using Playwright's page.evaluate(script). Handles instance lookup, execution, and error handling.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)Tool schema definition including name, description, and input schema requiring 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)MCP server registration for listing tools; returns all BrowserTools including browser_evaluate schema.this.server.setRequestHandler(ListToolsRequestSchema, async () => { const tools = this.browserTools.getTools(); return { tools: tools, }; });
- src/server.ts:48-53 (registration)MCP server handler for tool calls; delegates execution to BrowserTools.executeTools which handles browser_evaluate case.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { const result = await this.browserTools.executeTools(name, args || {});
- src/tools.ts:575-576 (registration)Dispatch case in executeTools method that routes browser_evaluate calls to the evaluate handler.case 'browser_evaluate': return await this.evaluate(args.instanceId, args.script);