evaluateWithReturn
Execute JavaScript code in a browser automation context and retrieve the returned result for web scraping, testing, or interaction tasks.
Instructions
Execute JavaScript code and return the result
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/controllers/playwright.ts:816-829 (handler)The core handler function that executes the provided JavaScript script on the current page using page.evaluate() and returns the result.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'); } }
- src/server.ts:428-441 (schema)Tool schema definition including input schema that requires a '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-553 (registration)Registration of the tool in the tools object passed to MCP server capabilities.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)MCP callTool request handler case that validates input and delegates to the playwright controller's evaluateWithReturn method.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" }] }; }