Skip to main content
Glama

browser_evaluate

Execute JavaScript code directly in the browser context using Playwright MCP Server, enabling web automation tasks like navigation, DOM manipulation, and data extraction.

Instructions

Execute JavaScript code in the browser context

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYes

Implementation Reference

  • The handler function for the 'browser_evaluate' tool. It validates the input script using Zod, performs security checks to block dangerous JavaScript patterns (like require, import, eval, etc.), connects to Playwright if needed, executes the script using page.evaluate(), and returns the result as JSON or an error message.
    async (params: any) => { try { const input = z.object({ script: z.string() }).parse(params); await this.playwright.ensureConnected(); // Basic security check - prevent dangerous operations const script = input.script.trim(); const dangerousPatterns = [ /require\s*\(/, /import\s+/, /eval\s*\(/, /Function\s*\(/, /process\./, /global\./, /window\.location/, /document\.cookie/ ]; if (dangerousPatterns.some(pattern => pattern.test(script))) { throw new Error('Script contains potentially dangerous operations'); } const page = this.playwright.getPage(); const result = await page.evaluate(input.script); return { content: [{ type: 'text', text: `Script executed. Result: ${JSON.stringify(result)}` }] }; } catch (error) { return { content: [{ type: 'text', text: `Script evaluation failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
  • src/server.ts:273-325 (registration)
    Registration of the 'browser_evaluate' tool with the MCP server. Specifies the tool name, title 'Execute JavaScript', description, and inline input schema requiring a 'script' string.
    this.server.registerTool( 'browser_evaluate', { title: 'Execute JavaScript', description: 'Execute JavaScript code in the browser context', inputSchema: { script: z.string() } }, async (params: any) => { try { const input = z.object({ script: z.string() }).parse(params); await this.playwright.ensureConnected(); // Basic security check - prevent dangerous operations const script = input.script.trim(); const dangerousPatterns = [ /require\s*\(/, /import\s+/, /eval\s*\(/, /Function\s*\(/, /process\./, /global\./, /window\.location/, /document\.cookie/ ]; if (dangerousPatterns.some(pattern => pattern.test(script))) { throw new Error('Script contains potentially dangerous operations'); } const page = this.playwright.getPage(); const result = await page.evaluate(input.script); return { content: [{ type: 'text', text: `Script executed. Result: ${JSON.stringify(result)}` }] }; } catch (error) { return { content: [{ type: 'text', text: `Script evaluation failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
  • Zod input schema definition for the browser_evaluate tool: BrowserEvaluateInputSchema = z.object({ script: z.string() })
    export const BrowserEvaluateInputSchema = z.object({ script: z.string() });
  • TypeScript type definition: BrowserEvaluateInput = z.infer<typeof BrowserEvaluateInputSchema>
    export type BrowserEvaluateInput = z.infer<typeof BrowserEvaluateInputSchema>;

Other Tools

Related 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/b3nw/playwright-mcp-server'

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