Skip to main content
Glama

execute-code

Run custom Playwright JavaScript code on web pages to retrieve titles, log outputs, and handle errors. Returns execution results, logs, and error details for clarity and debugging.

Instructions

Execute custom Playwright JS code against the current page

Input Schema

NameRequiredDescriptionDefault
codeYesThe Playwright code to execute. Must be an async function declaration that takes a page parameter. Example: async function run(page) { console.log(await page.title()); return await page.title(); } Returns an object with: - result: The return value from your function - logs: Array of console logs from execution - errors: Array of any errors encountered Example response: {"result": "Google", "logs": ["[log] Google"], "errors": []}

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "code": { "description": "The Playwright code to execute. Must be an async function declaration that takes a page parameter.\n\nExample:\nasync function run(page) {\n console.log(await page.title());\n return await page.title();\n}\n\nReturns an object with:\n- result: The return value from your function\n- logs: Array of console logs from execution\n- errors: Array of any errors encountered\n\nExample response:\n{\"result\": \"Google\", \"logs\": [\"[log] Google\"], \"errors\": []}", "type": "string" } }, "required": [ "code" ], "type": "object" }

Implementation Reference

  • Handler for the 'execute-code' tool: logs analytics event, executes code via secureEvalAsync, and returns the result as formatted JSON text.
    async ({ code }) => { posthogServer.capture({ distinctId: getUserId(), event: 'execute_code', properties: { codeLength: code.length, }, }); const result = await secureEvalAsync(page, code); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) // Pretty print the JSON } ] }; } )
  • Zod schema defining the input 'code' parameter for the 'execute-code' tool, including detailed usage description.
    { code: z.string().describe(`The Playwright code to execute. Must be an async function declaration that takes a page parameter. Example: async function run(page) { console.log(await page.title()); return await page.title(); } Returns an object with: - result: The return value from your function - logs: Array of console logs from execution - errors: Array of any errors encountered Example response: {"result": "Google", "logs": ["[log] Google"], "errors": []}`) },
  • Registration of the 'execute-code' tool with name and description.
    server.tool( 'execute-code', 'Execute custom Playwright JS code against the current page',
  • Core helper function secureEvalAsync that sandboxes and executes the provided Playwright code using Node's vm module, capturing logs and errors, with the page object injected.
    export const secureEvalAsync = async (page: Page, code: string, context = {}) => { // Set default options const timeout = 20000; const filename = 'eval.js'; let logs: string[] = []; let errors: string[] = []; // Code should already be a function declaration // Just need to execute it with page argument const wrappedCode = ` ${code} run(page); `; // Create restricted sandbox with provided context const sandbox = { // Core async essentials Promise, setTimeout, clearTimeout, setImmediate, clearImmediate, // Pass page object to sandbox page, // Capture all console methods console: { log: (...args: any[]) => { const msg = args.map(arg => String(arg)).join(' '); logs.push(`[log] ${msg}`); }, error: (...args: any[]) => { const msg = args.map(arg => String(arg)).join(' '); errors.push(`[error] ${msg}`); }, warn: (...args: any[]) => { const msg = args.map(arg => String(arg)).join(' '); logs.push(`[warn] ${msg}`); }, info: (...args: any[]) => { const msg = args.map(arg => String(arg)).join(' '); logs.push(`[info] ${msg}`); }, debug: (...args: any[]) => { const msg = args.map(arg => String(arg)).join(' '); logs.push(`[debug] ${msg}`); }, trace: (...args: any[]) => { const msg = args.map(arg => String(arg)).join(' '); logs.push(`[trace] ${msg}`); } }, // User-provided context ...context, // Explicitly block access to sensitive globals process: undefined, global: undefined, require: undefined, __dirname: undefined, __filename: undefined, Buffer: undefined }; try { // Create context and script const vmContext = vm.createContext(sandbox); const script = new vm.Script(wrappedCode, { filename }); // Execute and await result const result = script.runInContext(vmContext); const awaitedResult = await result; return { result: awaitedResult, logs, errors }; } catch (error: any) { return { error: true, message: error.message, stack: error.stack, logs, errors }; } }

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/Ashish-Bansal/playwright-mcp'

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