Skip to main content
Glama
jomon003

PlayMCP Browser Automation Server

by jomon003

executeJavaScript

Run custom JavaScript code on web pages and retrieve results using PlayMCP Browser Automation Server. Automate interactions, extract data, or execute scripts directly in the browser environment.

Instructions

Execute arbitrary JavaScript code on the current page and return the result

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesThe JavaScript code to execute on the page. Can be expressions or statements.

Implementation Reference

  • The main handler function that executes arbitrary JavaScript code on the current browser page using Playwright's page.evaluate. Includes initialization check, logging, eval wrapping for expressions/statements, and comprehensive error handling with BrowserError.
    async executeJavaScript(script: string): Promise<any> { try { if (!this.isInitialized()) { throw new Error('Browser not initialized'); } this.log('Executing JavaScript:', script); const result = await this.state.page?.evaluate((scriptToExecute) => { // Create a function wrapper to handle different types of JavaScript code try { // If the script is an expression, return its value // If the script is statements, execute them and return undefined const wrappedScript = ` (function() { ${scriptToExecute} })() `; return eval(wrappedScript); } catch (error) { // If wrapping fails, try executing directly return eval(scriptToExecute); } }, script); this.log('JavaScript execution completed:', result); return result; } catch (error: any) { console.error('Execute JavaScript error:', error); throw new BrowserError('Failed to execute JavaScript', 'Check if the JavaScript syntax is valid'); } }
  • Defines the Tool interface for executeJavaScript, including name, description, and input schema requiring a 'script' string parameter.
    const EXECUTE_JAVASCRIPT_TOOL: Tool = { name: "executeJavaScript", description: "Execute arbitrary JavaScript code on the current page and return the result", inputSchema: { type: "object", properties: { script: { type: "string", description: "The JavaScript code to execute on the page. Can be expressions or statements." } }, required: ["script"] } };
  • src/server.ts:772-786 (registration)
    Registers and dispatches the executeJavaScript tool call within the MCP server's callTool request handler switch statement, validating input and invoking the playwrightController handler.
    case 'executeJavaScript': { if (!args.script || typeof args.script !== 'string') { return { content: [{ type: "text", text: "JavaScript script is required" }], isError: true }; } const result = await playwrightController.executeJavaScript(args.script); return { content: [{ type: "text", text: result !== undefined ? JSON.stringify(result, null, 2) : "Script executed successfully (no return value)" }] }; }
  • src/server.ts:534-535 (registration)
    Registers the EXECUTE_JAVASCRIPT_TOOL in the tools object used for MCP server capabilities.
    executeJavaScript: EXECUTE_JAVASCRIPT_TOOL, goForward: GO_FORWARD_TOOL,
  • src/server.ts:561-564 (registration)
    Passes the tools object (including executeJavaScript) to the MCP Server constructor for tool registration.
    capabilities: { 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/jomon003/PlayMCP'

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