Skip to main content
Glama
jomon003

PlayMCP Browser Automation Server

by jomon003

evaluateWithReturn

Execute JavaScript code within a browser context using Playwright and retrieve the result, enabling dynamic data extraction and interaction in web automation tasks with PlayMCP Browser Automation Server.

Instructions

Execute JavaScript code and return the result

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • The core handler function in PlaywrightController that executes the provided JavaScript script on the current page using page.evaluate() and returns the result directly.
    async evaluateWithReturn(script: string): Promise<any> { try { if (!this.isInitialized() || !this.state.page) { throw new Error('Browser not initialized'); } this.log('Evaluating JavaScript with return', { script }); const result = await this.state.page.evaluate(script); this.log('JavaScript evaluation complete'); return result; } catch (error: any) { console.error('JavaScript evaluation error:', error); throw new BrowserError('Failed to evaluate JavaScript', 'Check if the script is valid JavaScript'); } }
  • Defines the tool's input schema, name, and description for MCP protocol compliance, specifying a required 'script' string parameter.
    const EVALUATE_WITH_RETURN_TOOL: Tool = { name: "evaluateWithReturn", description: "Execute JavaScript code and return the result", inputSchema: { type: "object", properties: { script: { type: "string", description: "JavaScript code to execute" } }, required: ["script"] } };
  • src/server.ts:514-552 (registration)
    Registers the evaluateWithReturn tool in the tools object map, which is provided to the MCP Server's capabilities for listTools and callTool handling.
    const tools = { openBrowser: OPEN_BROWSER_TOOL, navigate: NAVIGATE_TOOL, type: TYPE_TOOL, click: CLICK_TOOL, moveMouse: MOVE_MOUSE_TOOL, scroll: SCROLL_TOOL, screenshot: SCREENSHOT_TOOL, getPageSource: GET_PAGE_SOURCE_TOOL, getPageText: GET_PAGE_TEXT_TOOL, getPageTitle: GET_PAGE_TITLE_TOOL, getPageUrl: GET_PAGE_URL_TOOL, getScripts: GET_SCRIPTS_TOOL, getStylesheets: GET_STYLESHEETS_TOOL, getMetaTags: GET_META_TAGS_TOOL, getLinks: GET_LINKS_TOOL, getImages: GET_IMAGES_TOOL, getForms: GET_FORMS_TOOL, getElementContent: GET_ELEMENT_CONTENT_TOOL, getElementHierarchy: GET_ELEMENT_HIERARCHY_TOOL, executeJavaScript: EXECUTE_JAVASCRIPT_TOOL, goForward: GO_FORWARD_TOOL, hover: HOVER_TOOL, dragAndDrop: DRAG_AND_DROP_TOOL, selectOption: SELECT_OPTION_TOOL, pressKey: PRESS_KEY_TOOL, waitForText: WAIT_FOR_TEXT_TOOL, waitForSelector: WAIT_FOR_SELECTOR_TOOL, resize: RESIZE_TOOL, handleDialog: HANDLE_DIALOG_TOOL, getConsoleMessages: GET_CONSOLE_MESSAGES_TOOL, getNetworkRequests: GET_NETWORK_REQUESTS_TOOL, uploadFiles: UPLOAD_FILES_TOOL, evaluateWithReturn: EVALUATE_WITH_RETURN_TOOL, takeScreenshot: TAKE_SCREENSHOT_TOOL, mouseMove: MOUSE_MOVE_TOOL, mouseClick: MOUSE_CLICK_TOOL, mouseDrag: MOUSE_DRAG_TOOL, closeBrowser: CLOSE_BROWSER_TOOL
  • src/server.ts:926-940 (registration)
    Registers the tool dispatch logic in the MCP server's callTool request handler switch statement, validating input and delegating to the controller's handler.
    case 'evaluateWithReturn': { if (!args.script || typeof args.script !== 'string') { return { content: [{ type: "text", text: "JavaScript script is required" }], isError: true }; } const result = await playwrightController.evaluateWithReturn(args.script); return { content: [{ type: "text", text: result !== undefined ? JSON.stringify(result, null, 2) : "null" }] }; }

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