Skip to main content
Glama
seabassgonzalez

MCP Browser Screenshot Server

browser_execute_script

Execute JavaScript code within browser sessions to manipulate web content, extract data, or modify page behavior for testing and automation purposes.

Instructions

Execute JavaScript in the browser context

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • Executes the provided JavaScript script in the browser page context using Puppeteer's page.evaluate method, which evaluates the script and returns the result serialized as JSON.
    case 'browser_execute_script': {
      const { page } = await ensureBrowser();
      const script = args?.script as string;
    
      const result = await page.evaluate((scriptToRun) => {
        return eval(scriptToRun);
      }, script);
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(result, null, 2),
          },
        ],
      };
    }
  • Defines the input schema for the browser_execute_script tool, specifying a required 'script' string parameter.
    {
      name: 'browser_execute_script',
      description: 'Execute JavaScript in the browser context',
      inputSchema: {
        type: 'object',
        properties: {
          script: {
            type: 'string',
            description: 'JavaScript code to execute',
          },
        },
        required: ['script'],
      },
    },
  • src/index.ts:90-207 (registration)
    Registers the browser_execute_script tool by including it in the list of tools returned by the ListToolsRequest handler.
    server.setRequestHandler(ListToolsRequestSchema, async () => {
      return {
        tools: [
          {
            name: 'browser_launch',
            description: 'Launch a new browser instance',
            inputSchema: {
              type: 'object',
              properties: {
                headless: {
                  type: 'boolean',
                  description: 'Run browser in headless mode',
                  default: true,
                },
              },
            },
          },
          {
            name: 'browser_navigate',
            description: 'Navigate to a URL',
            inputSchema: {
              type: 'object',
              properties: {
                url: {
                  type: 'string',
                  description: 'URL to navigate to',
                },
                waitUntil: {
                  type: 'string',
                  enum: [
                    'load',
                    'domcontentloaded',
                    'networkidle0',
                    'networkidle2',
                  ],
                  description: 'When to consider navigation complete',
                  default: 'networkidle2',
                },
              },
              required: ['url'],
            },
          },
          {
            name: 'browser_close',
            description: 'Close the browser instance',
            inputSchema: {
              type: 'object',
              properties: {},
            },
          },
          {
            name: 'screenshot_capture',
            description: 'Take a screenshot of the current page',
            inputSchema: {
              type: 'object',
              properties: {
                fullPage: {
                  type: 'boolean',
                  description: 'Capture full page screenshot',
                  default: false,
                },
                selector: {
                  type: 'string',
                  description: 'CSS selector of element to screenshot',
                },
                format: {
                  type: 'string',
                  enum: ['base64', 'binary'],
                  description: 'Output format for the screenshot',
                  default: 'base64',
                },
              },
            },
          },
          {
            name: 'screenshot_viewport',
            description: 'Take a screenshot with specific viewport settings',
            inputSchema: {
              type: 'object',
              properties: {
                preset: {
                  type: 'string',
                  enum: ['mobile', 'tablet', 'desktop', 'laptop'],
                  description: 'Viewport preset to use',
                },
                width: {
                  type: 'number',
                  description: 'Custom viewport width',
                },
                height: {
                  type: 'number',
                  description: 'Custom viewport height',
                },
                fullPage: {
                  type: 'boolean',
                  description: 'Capture full page screenshot',
                  default: false,
                },
              },
            },
          },
          {
            name: 'browser_execute_script',
            description: 'Execute JavaScript in the browser context',
            inputSchema: {
              type: 'object',
              properties: {
                script: {
                  type: 'string',
                  description: 'JavaScript code to execute',
                },
              },
              required: ['script'],
            },
          },
        ],
      };
    });
  • Helper function to ensure a browser instance and page are available, launching if necessary, used by the handler.
    async function ensureBrowser(): Promise<{ browser: Browser; page: Page }> {
      if (!browserState.browser || !browserState.browser.isConnected()) {
        const headless = process.env.HEADLESS !== 'false';
        browserState.browser = await puppeteer.launch({
          headless,
          args: ['--no-sandbox', '--disable-setuid-sandbox'],
        });
        browserState.page = await browserState.browser.newPage();
      }
    
      if (!browserState.page || browserState.page.isClosed()) {
        browserState.page = await browserState.browser.newPage();
      }
    
      return {
        browser: browserState.browser,
        page: browserState.page,
      };
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the action ('Execute JavaScript') but doesn't describe safety implications (e.g., potential for destructive effects, permissions needed), execution context (e.g., runs in page context vs. isolated), or response handling (e.g., returns script results). This leaves significant gaps for a mutation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's function with zero wasted words. It is appropriately sized and front-loaded, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations, no output schema, and a mutation tool that executes arbitrary JavaScript, the description is incomplete. It lacks critical context like safety warnings, return value expectations, or dependencies on other tools (e.g., browser_launch). This makes it inadequate for safe and effective use by an AI agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with the single parameter 'script' clearly documented in the schema as 'JavaScript code to execute'. The description adds no additional meaning beyond this, such as syntax examples or constraints. Baseline 3 is appropriate when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('Execute') and resource ('JavaScript in the browser context'), making the purpose immediately understandable. However, it doesn't distinguish this tool from potential alternatives like browser automation tools that might also execute scripts, though no direct siblings with overlapping functionality are listed.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., requiring an active browser session from browser_launch), exclusions, or comparisons to sibling tools like browser_navigate for navigation tasks. Usage is implied but not explicitly stated.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/seabassgonzalez/mcp-browser-screenshot'

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