How to run custom JavaScript (evaluate) in Playwright MCP?
Is there a way to run arbitrary JavaScript in the page context with the Playwright MCP server, similar to Playwright's page.evaluate()? I need it for form-filling and injecting CSS, and the default tools are slow because they act on one field at a time. A feature request for this was created in microsoft/playwright-mcp — has it been implemented?
Originally asked on GitHub: https://github.com/microsoft/playwright-mcp/issues/424
2 answers
Fixed. The issue was closed as completed, and a later comment confirmed the feature shipped ("Thank you for implementing this feature!"). The thread does not show the final tool name or schema, so after updating the MCP server, check the tool list exposed to your client for the exact name and parameters — they may differ from the original proposal.
The proposal (not necessarily the final implementation) was a tool named
evaluatewith ascriptstring property, whose handler calledawait page.evaluate(params.script). That is the pattern to replicate if you write your own wrapper.While the feature was pending, a commenter reported a workaround: they used agent mode to add an
evaluatetool to the Playwright MCP setup. The generated script filled a whole form in a few seconds, whereas the stock MCP tools filled one field per call, which they described as slow.Motivating use cases from the thread: injecting a CSS border to highlight page elements, calling in-product test hooks (for example, creating a fake call), and memory-leak checks.
General advice: executing arbitrary page JavaScript from an AI agent runs with the page's privileges, so use it only in sessions and on pages you trust.
Did this work for you?What @vhreal1302 tried on GitHub (Worked)
Used agent mode to add an evaluate tool to the MCP setup. The generated script filled a whole form in a few seconds, much faster than the default behavior of filling one field at a time.
Source: https://github.com/microsoft/playwright-mcp/issues/424#issuecomment-2906372785 (opens external site)
Did this work for you?
Related questions
- Playwright MCP browser_take_screenshot shows no saved file or unreadable image in Cursor or VS Code
- Windows: playwright-mcp via Claude Desktop fails: Chromium distribution 'chrome' is not found
- playwright-mcp fails to start in Cursor after 0.0.19/0.0.20 (Client closed / Connection closed)
- Can I generate codegen-style Playwright test code from a prompt-driven playwright-mcp flow?
- Does Playwright MCP have a quiet mode to reduce page snapshot tokens?