DEV Community

Trung Thanh Tran
Trung Thanh Tran

Posted on

Building a Faster Playwright MCP Server: Compact DOM Deltas, 1-Turn Form Filling, and Submit Guards

While using a browser automation agent over Model Context Protocol (MCP) to prefill job applications and publishing forms, four bottlenecks showed up in the execution logs before any site rate limit did:

  1. Full accessibility snapshots kept piling up in the conversation context until single turns carried 90,000+ tokens.
  2. Filling a form step-by-step (click, fill, click, fill) spent 10 to 15 MCP turns per page, and typing keystrokes into <select> elements caused multi-second stalls.
  3. Passing file uploads as inline Base64 strings through LLM tool arguments forced the model to emit ~20,000 output tokens and stalled tool calls for over three minutes.
  4. One wrong click on a button labeled Submit or Publish sent half-checked data live before a human could inspect the page.

I built turbo-browser-mcp (v0.4.0, MIT, playwright-core with no other runtime dependencies) to group those patterns into 10 consolidated MCP tools:

https://github.com/Tthanh2k6/turbo-browser-mcp

Here are the four design choices in the codebase, along with where the approach still has limits.

1. Bounded data-tbm-ref snapshots and onlyChanged deltas

inspect_compact walks the main document, nested accessible frames, and open Shadow DOM roots (el.shadowRoot), stamps deterministic f<frameIdx>:e<N> IDs onto data-tbm-ref, and caps output at maxChars (4000 by default).

Instead of dumping a full accessibility tree after every action, the server hashes each line's signature per tab (frameIdx, tag, label, value/checked state) and supports onlyChanged: true to return only added or modified controls. Calling act_fast, batch_actions, or smart_fill_form with returnSnapshot: true folds the updated view into the same tool response so the agent does not spend a separate read turn.

2. One-turn smart_fill_form for React inputs, rich text, and disk uploads

Standard .value = "..." writes often vanish on React forms on blur or submit because React tracks internal input state via _valueTracker. In src/dom-engine.mjs, smart_fill_form updates inputs through the native HTMLInputElement.prototype or HTMLTextAreaElement.prototype value setter after calling _valueTracker.setValue(''), then dispatches input, change, and blur.

In the same single MCP call, fields can target:

  • React/plain <input> and <textarea> elements, single/multi <select> (values), <input type="range"> sliders, radios, checkboxes, and ARIA [role="combobox"] widgets
  • contenteditable editors (ProseMirror, Quill, compose boxes) via execCommand('insertText') with InputEvent fallback
  • Nested iframes (frameIdx) and open Shadow DOM fields
  • Local files via file or files, which Playwright attaches straight from disk through setInputFiles or filechooser (never through inline Base64 strings)

3. A default allowSubmit=false guard that checks coordinates too

By default, resolveAllowSubmit evaluates to false unless allowSubmit: true is passed per call or TURBO_BROWSER_ALLOW_SUBMIT=true / --allow-submit is enabled on launch.

In src/dom-engine.mjs, SUBMIT_LABEL_RE blocks clicks, Enter key presses, and type submit: true actions matching submit, publish, post, send, apply, confirm and pay, pay, checkout, delete, and Vietnamese equivalents (gửi, đăng bài, nộp đơn, thanh toán), plus <input type="submit"> elements outside an explicit safe list (next, continue, search, filter, save draft). Raw viewport { x, y } mouse clicks resolve the element at that screen coordinate first so a coordinate click cannot bypass the label guard.

4. Local benchmark numbers (npm run benchmark)

Measured locally on Windows (Node v24.15.0, Chromium headless) against the self-contained HTTP fixture in benchmarks/benchmark.mjs:

Scenario Baseline turbo-browser-mcp Measured change (o200k_base)
Dense 140-button SaaS DOM snapshot 18,654 tok raw HTML / 3,568 tok full ARIA 1,135 tok compact (28 ms) / 56 tok delta (18 ms) 93.9% vs HTML, 68.2% vs ARIA, 98.4% on delta
10-field form fill + snapshot 11 MCP turns (574 ms, 12,197 dir / 72,614 cum tok) 1 MCP turn (82 ms, 811 tok, filled=10) 11 -> 1 turns, 7x faster, 93.4% dir / 98.9% cum saved
48 KiB PDF file upload (49,152 bytes) 44,728 tok inline Base64 tool arg 85 tok disk path (68 ms) 99.8% tokens saved, no Base64 encode
Open 6 tabs (60 ms simulated TTFB) 6 turns (998 ms sequential) 1 turn (420 ms parallel open_many) 6 -> 1 turns, 2.38x faster
Heavy media load (turbo vs clean) 252 ms (304 KiB, 9 asset hits) 165 ms (0 KiB, 0 asset hits) 1.53x faster, 100% media bandwidth cut
Article Markdown + Diagnostics errors 3,595 tok HTML / 3,460 tok all logs 156 tok Markdown / 601 tok errors-only 95.7% and 82.6% tokens saved

The 405 tests across 85 suites (25 test files under test/*.test.mjs: core integration, DOM/form and session/stdio stress suites, multi-round adversarial simulations, and 10 independent audit suites) run via npm test and pass on GitHub Actions CI for Node 20 and Node 22.

Trade-offs and open gaps

  1. Pre-existing closed Shadow DOM and sandboxed frames: An init script hooks Element.prototype.attachShadow so closed shadow roots created after page initialization stay inspectable and guarded, while closed roots that already existed on an attached CDP tab or live inside strict sandboxed frames fall back to Playwright frame handles, coordinate clicks, and keyboard input.
  2. Real network latency still dominates navigation: batching collapses DOM round-trips, while remote page load times still depend on the target server (blocking images, fonts, and media via network_session preset "turbo" trims asset overhead on heavy sites).
  3. Long-lived MCP child processes: if your client already has an older MCP stdio process running when you pull updates, you need to reload the MCP client so added schemas (like open_many, onlyChanged, capture_page, or network_session) register.

Looking for community feedback

If you use Playwright MCP or browser agents in daily workflows, what breaks most often in practice for you? I am especially curious about custom virtualized dropdowns, tricky iframe embed patterns, diagnostics you miss from Chrome DevTools, or places where SUBMIT_LABEL_RE would misclassify a safe navigation button.

Issues, PRs, and edge-case reports are welcome on GitHub:
https://github.com/Tthanh2k6/turbo-browser-mcp

Top comments (9)

Collapse
 
amintacccp profile image
tamina •

The allowSubmit=false default is the part I'd steal first — one wrong click sending half-checked data live is exactly the failure mode that makes people distrust browser agents. And the React _valueTracker detail is the kind of thing you only learn by watching a fill vanish on blur. Curious about your open question: did SUBMIT_LABEL_RE ever misclassify a safe button in your testing, like a 'Save draft' or 'Continue' that also triggers a submit? Nice write-up, the local benchmark numbers make the trade-offs easy to follow.

Collapse
 
tthanh2k6 profile image
Trung Thanh Tran •

Yes, that exact case bit me early on. Multi-step wizards (like Workable and Greenhouse forms) often render "Continue", "Next", or "Save draft" as <button type="submit"> inside a <form>, so blocking every submit-type button broke pagination and draft saving.

I ended up splitting the check into three regexes in src/dom-engine.mjs: SUBMIT_LABEL_RE (publish, submit, send, pay, delete), SAFE_SUBMIT_TYPE_RE (next, continue, back, search, filter, save draft, preview, cancel), and COMPOUND_DANGER_RE. If a <button type="submit"> matches SAFE_SUBMIT_TYPE_RE, the form guard temporarily marks form.__tbmAllowedSubmit = true for that click unless the label also contains a final-action phrase like "Continue to payment" or "Save and submit".

Collapse
 
amintacccp profile image
tamina •

That three-way split is a neat fix — I hadn't thought about the wizard case where 'Continue' is technically a submit button but semantically just pagination. The COMPOUND_DANGER_RE escape hatch for 'Continue to payment' is the part I'd steal back, honestly. Thanks for walking through the actual regexes, that makes the trade-off a lot clearer than the usual 'just block submits' advice.

Collapse
 
alexcodebytes profile image
Oleksandr •

the react _valuetracker hack is so painfully real. i've spent hours trying to figure out why .value = '...' wasn't sticking on react forms until i realized i had to bypass the prototype. but to answer your question: custom virtualized dropdowns (like react-select) are the absolute worst for agents. they literally don't exist in the dom until you click them, so standard snapshots miss them completely. how are you handling those?

Collapse
 
tthanh2k6 profile image
Trung Thanh Tran •

react-select and Radix/Headless UI portals are definitely the trickiest ones because the <div role="option"> nodes only mount after focus or pointerdown.

Right now smart_fill_form handles the standard [role="combobox"] / [aria-haspopup="listbox"] pattern in two steps inside the same call: it clicks the combobox trigger first, waits briefly for the portal/listbox to mount in the document, and then matches the visible [role="option"] text. Where that still breaks is heavy windowing (like react-window inside a 5,000-row select) where the target option isn't mounted until you type into the inner search input or scroll the list container. For those, I currently fall back to batch_actions (click trigger -> type filter text -> click option).

Collapse
 
alexcodebytes profile image
Oleksandr •

oh man, react-window inside a select is the absolute final boss of browser automation. if it's not in the dom, you literally can't click it, so the batch_actions fallback (click -> type -> click) is actually the smartest way to handle it. it mimics what a real human would do anyway. trying to manually scroll a virtualized list via script is a special kind of hell lmao.

Collapse
 
johnnylemonny profile image
𝗝𝗼𝗵𝗻 •

Resolving raw { x, y } coordinates against SUBMIT_LABEL_RE before firing a mouse click is such a smart detail. A label-based submit guard that an agent can accidentally bypass with a coordinate click is just security theater, so keeping allowSubmit=false by default and checking the underlying element at the target coordinates gives real human-in-the-loop safety before anything irreversible goes live.

Resetting React's _valueTracker alongside native prototype setters in smart_fill_form also fixes one of the most annoying silent failure modes in browser automation, where .value = "..." looks completely fine in the DOM until blur or submit wipes the controlled state clean.

To answer your question on what breaks most often in daily workflows: portaled dropdowns and virtualized comboboxes (like Radix or Headless UI menus that mount their listbox options at document.body instead of inside the form container). When the trigger is inside a nested frame or card and the options render at the root, or only mount while focused—scoped snapshots and onlyChanged deltas can easily miss the newly mounted option nodes.

Collapse
 
paullovelace profile image
Paul Lovelace •

The 48 KiB PDF number is the one that made me wince — 44,728 tokens just to hand a file to the model, versus 85 for a disk path. That's not an optimisation, that's a whole failure mode hiding in plain sight. Haven't run turbo-browser-mcp myself, but the upload fix alone would've saved me a few very long afternoons.

Collapse
 
eye_java_420f1faa10ae8b86 profile image
Nan •

On the open question about SUBMIT_LABEL_RE misclassifying buttons: I moved that check to the request layer a while ago and never looked back. Continue in a Workable wizard and Submit on the last step both sit on a submit button, but only one ends in a URL change or a mutating fetch. If you record the URL and in-flight requests for about a second after the click, draft and save buttons give themselves away: they fire an XHR and stay on the same page. Labels lie in every language, so classifying by aftermath sidesteps the naming problem entirely.

For the portal dropdowns, keyboard events saved me more than pointer ones. Focus the combobox and send ArrowDown then Enter. react-select and Radix open their menu on focus anyway, so you never have to wait for the option list to mount, and react-window stays somebody else's problem.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.