Skip to main content
Glama

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, }; }

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