Skip to main content
Glama

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
NameRequiredDescriptionDefault
instanceIdYesInstance ID
scriptYesJavaScript code to execute

Implementation Reference

  • 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 }; }
  • 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, }; });
  • 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}` ); } });

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/sailaoda/concurrent-browser-mcp'

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