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
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | ||
| tabId | No |
Implementation Reference
- index-multi-debug.js:1070-1083 (handler)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)}` }] }; }
- index-multi-debug.js:164-175 (registration)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'] } },
- index-multi-debug.js:167-173 (schema)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']
- index-multi-debug.js:1021-1033 (helper)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); } }
- index-multi-debug.js:1400-1404 (helper)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.'); } }