Skip to main content
Glama
pipethedev
by pipethedev

browsercat_evaluate

Execute JavaScript code directly in a browser console to automate web interactions, extract data, or test functionality through cloud-based browsing.

Instructions

Execute JavaScript in the browser console

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • Handler function for 'browsercat_evaluate' tool. Evaluates the provided JavaScript script in the browser page, temporarily overrides console methods to capture logs, restores console, and returns the result with captured console output.
    case "browsercat_evaluate":
      try {
        // Set up console log capture
        await page.evaluate(() => {
          window.mcpHelper = {
            logs: [],
            originalConsole: { ...console },
          };
    
          ['log', 'info', 'warn', 'error'].forEach(method => {
            (console as any)[method] = (...args: any[]) => {
              window.mcpHelper.logs.push(`[${method}] ${args.join(' ')}`);
              (window.mcpHelper.originalConsole as any)[method](...args);
            };
          });
        });
    
        const result = await page.evaluate(args.script);
    
        // Restore original console and get captured logs
        const logs = await page.evaluate(() => {
          Object.assign(console, window.mcpHelper.originalConsole);
          const logs = window.mcpHelper.logs;
          delete (window as any).mcpHelper;
          return logs;
        });
    
        return {
          content: [
            {
              type: "text",
              text: `Execution result:\n${JSON.stringify(result, null, 2)}\n\nConsole output:\n${logs.join('\n')}`,
            },
          ],
          isError: false,
        };
      } catch (error) {
        return {
          content: [{
            type: "text",
            text: `Script execution failed: ${(error as Error).message}`,
          }],
          isError: true,
        };
      }
  • Tool definition including name, description, and input schema for 'browsercat_evaluate'.
    {
      name: "browsercat_evaluate",
      description: "Execute JavaScript in the browser console",
      inputSchema: {
        type: "object",
        properties: {
          script: { type: "string", description: "JavaScript code to execute" },
        },
        required: ["script"],
      },
    },
  • index.ts:421-423 (registration)
    MCP server request handler for listing tools, which includes 'browsercat_evaluate' from the TOOLS array.
    server.setRequestHandler(ListToolsRequestSchema, async () => ({
      tools: TOOLS,
    }));
  • index.ts:425-427 (registration)
    MCP server request handler for calling tools, which dispatches to handleToolCall based on the tool name 'browsercat_evaluate'.
    server.setRequestHandler(CallToolRequestSchema, async (request) =>
      handleToolCall(request.params.name, request.params.arguments ?? {})
    );

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/pipethedev/browsercat-mcp-server'

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