Skip to main content
Glama

execute_script

Run custom JavaScript code within Firefox browser tabs using Playwright for automation, enabling multi-tab debugging and browser control via MCP-enabled applications.

Instructions

Execute JavaScript in the browser

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYes
tabIdNo

Implementation Reference

  • The core handler function for the 'execute_script' tool. It retrieves the target page, ensures the browser is running, evaluates the provided JavaScript script using Playwright's page.evaluate(), and returns the execution result.
    async executeScript(args) {
      this.ensureBrowserRunning();
      const { script, tabId } = args;
      const page = this.getPage(tabId);
      
      const result = await page.evaluate(script);
      
      return {
        content: [{
          type: 'text',
          text: `Script executed in tab '${tabId || this.activeTabId}'. Result: ${JSON.stringify(result)}`
        }]
      };
    }
  • Registers the 'execute_script' tool in the ListTools response, including its name, description, and input schema specifying required 'script' parameter and optional 'tabId'.
    {
      name: 'execute_script',
      description: 'Execute JavaScript in the browser',
      inputSchema: {
        type: 'object',
        properties: {
          script: { type: 'string' },
          tabId: { type: 'string' }
        },
        required: ['script']
      }
    },
  • Input schema for the 'execute_script' tool, defining properties 'script' (required string) and 'tabId' (optional string).
    inputSchema: {
      type: 'object',
      properties: {
        script: { type: 'string' },
        tabId: { type: 'string' }
      },
      required: ['script']
  • Helper method used by executeScript to retrieve the Playwright Page object for the specified or active tabId.
    getPage(tabId) {
      if (tabId) {
        if (!this.pages.has(tabId)) {
          throw new Error(`Tab '${tabId}' not found`);
        }
        return this.pages.get(tabId);
      } else {
        if (!this.activeTabId || !this.pages.has(this.activeTabId)) {
          throw new Error('No active tab. Use create_tab or set_active_tab first.');
        }
        return this.pages.get(this.activeTabId);
      }
    }
  • Helper method called by executeScript to ensure the Firefox browser instance is launched before executing scripts.
    ensureBrowserRunning() {
      if (!this.browser) {
        throw new Error('Firefox browser is not running. Please launch it first using the launch_firefox_multi tool.');
      }
    }

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/JediLuke/firefox-mcp-server'

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