Skip to content

perf(sandbox): start the Playwright MCP server only when a turn uses it - #4117

Merged
yannickmonney merged 4 commits into
mainfrom
perf/sandbox-lazy-playwright-mcp
Oct 3, 2026
Merged

yannickmonney merged 4 commits into
mainfrom
perf/sandbox-lazy-playwright-mcp

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Why

Every managed agent turn — every harness — lists the Playwright MCP server among its tools, so every turn started it: a Node process that loads Playwright as it starts. Measured (Node, require('playwright-core')): about 105 MB RSS and ~0.3 s of CPU, held for the whole turn. Most turns never open a browser. Earlier measurements put a whole Claude turn at +184 MiB, so this one idle server was a large share of what an agent session costs — and admission is by memory.

What changes

  • tale-playwright-mcp answers the start of a turn itself. It is now a small Python proxy (~20 MB, first answer in ~50 ms; python3 -Es, so a session's own Python packages cannot shadow its modules). It answers initialize, tools/list and ping from manifests the image build records from the real server, and starts the server only on the turn's first other request — a tool call. It then replays the client's initialize (and notifications/initialized, and any notification that came before) under an id of its own, and passes every message through both ways, server requests such as roots/list and the client's answers included.
  • The same answers the server would give. The manifest holds the server's answer to initialize for every protocol version MCP has named — the server's latest for one it does not speak — so the later start negotiates the same version (the launcher logs a mismatch). The server only advertises tools and already defers its own setup to the first tool call, so nothing else happens at start that the proxy skips.
  • Safe fallbacks. Arguments with no manifest, a protocol version outside the named ones, or TALE_PLAYWRIGHT_MCP_EAGER=1 start the server at once, exactly as before. The proxy flags (--proxy-server/--proxy-bypass from HTTPS_PROXY/NO_PROXY) and PLAYWRIGHT_DISABLE_FORCED_CHROMIUM_PROXIED_LOOPBACK are unchanged.
  • The record follows the platform. The image records manifests for the sets in services/sandbox-runtime/playwright-mcp-args.json; exec-builder.ts exports its sets and its test holds the two equal (a new set without a record would start the server every turn again). The platform's test task hashes the JSON.
  • The daemon's tests rerun when a wrapper changes. Its suite tests the wrappers beside it but its cache key held only the daemon workspace; a turbo.json now hashes services/sandbox-runtime/tale-*, entrypoint.sh and the args file.

Verification

  • tale-playwright-mcp.test.ts (a stand-in server on PATH that logs what reaches it): the build's manifest (answers per protocol version), the start answered without the server (known and not-spoken versions), the start replayed on the first tool call with the client's params and only the client's answers reaching the client, a roots/list round trip, an unknown version and the eager switch starting the server at once, the server's exit status, the proxy flags, and the interpreter line.
  • The image conformance test now runs the launcher under the session contract with the platform's arguments: no mcp-server-playwright process after initialize and tools/list, the same tool list as the server's own, and a browser_navigate through the server started on that call. (Docker on the authoring machine is down for want of disk, so CI's image build is where this runs first.)
  • Platform harness tests 235/235; daemon lint and typecheck clean.

Follows #4097, #4098, #4099 and #4116.

Every managed agent turn lists the Playwright MCP server among its tools,
so every turn started it: a Node process that loads Playwright as it
starts, about 100 MB of memory and a third of a second of CPU, held for the
whole turn, although most turns never open a browser.

tale-playwright-mcp, now a small Python proxy (about 20 MB, first answer in
about 50 ms), answers the start of a turn itself — initialize, tools/list
and ping — from manifests the image build records from the real server for
the argument sets the platform passes (playwright-mcp-args.json, held
equal to exec-builder's sets by its test). The answer to initialize is the
server's own for every protocol version MCP has named, its latest for one
it does not speak, so a later start negotiates the same version. On the
turn's first other request it starts the server, replays the client's
initialize under an id of its own, and passes every message through both
ways. Unknown arguments or protocol versions, and
TALE_PLAYWRIGHT_MCP_EAGER=1, start the server at once, as before. The
proxy flags it adds to the server are unchanged.
…ool call

Under the session container contract, the launcher with the platform's first argument set answers initialize and tools/list while no mcp-server-playwright process runs, its tool list equals the server's own, and a browser_navigate reaches the server it starts then.
The daemon's suite tests the wrappers beside it (tale-*-run, tale-playwright-mcp, tale-connectors-mcp, the hooks, entrypoint.sh), but its cache key held only the daemon workspace, so a change to a wrapper alone could replay a stale pass. Its test task now hashes them.
A ledger row for the launcher: the start answered from the build's manifests, the server started and the start replayed on the first other request, pass-through both ways, the eager fallbacks, and where each is automated.
@yannickmonney
yannickmonney merged commit df5be33 into main Oct 3, 2026
@yannickmonney
yannickmonney deleted the perf/sandbox-lazy-playwright-mcp branch October 3, 2026 08:43
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Post-merge independent review of #4117 at df5be3397 (merged by root at 08:43:27Z from head 597865f92, while this review was running). Tale agent #2 3f9fdcee, TALE-359 run 6b60fb49-b479-4eeb-850a-951812ec31f3. This is a source review by one read-only review agent, cross-checked by me; nothing was executed for it.

MEDIUM: Qwen Code turns still start the real Playwright MCP server on every turn. For Qwen, the PR saves nothing and adds the proxy on top.

  • The mechanism: the pinned Qwen Code 0.23.3 sends raw prompts/list and resources/list during discovery at the start of every turn, even to a server that doesn't advertise them.
  • The proxy's answer: its answered() returns False for any request it doesn't know (tale-playwright-mcp:247-250,305-307), so discovery itself starts mcp-server-playwright.
  • Reach: all five Qwen golden configs mount Playwright, and the register row automation.md:192 ("a managed agent turn that never opens a browser never starts the Playwright MCP server") is false for Qwen.
  • Status: from the pinned bundle's source. The unit test and the container check use a hand-written client that sends only initialize and tools/list.
  • Fix: answer prompts/list, resources/list and resources/templates/list with the real server's -32601 Method not found when the manifest's capabilities lack them.
  • Test: a stand-in sending Qwen's discovery sequence, asserting no started line.

LOW

  • The mismatch log compares the wrong values. It compares the version the client asked for, not the one the manifest answered (:199,229,266-269). Claude Code, Qwen and OpenClaw ask for 2025-11-25 and are answered 2025-06-18, so every lazy start logs an untrue mismatch, while a real mismatch would go unlogged.
  • The manifest is keyed by arguments only. The server's tool list also depends on PLAYWRIGHT_MCP_CAPS and PLAYWRIGHT_MCP_CONFIG. A harness environment that sets them sees the image's core-only list for the whole turn. Start the server at once when any PLAYWRIGHT_MCP_* variable is set.
  • A session-installed server is started, but the image's manifest is still served. A session that ran npm i -g @playwright/mcp@latest gets that server, since /agent/.runtime/deps/node/bin comes first on PATH, while the old tool list is served. Use the absolute path, or record it in the manifest.
  • A server whose start hangs stalls the first tool call indefinitely. No deadline bounds the replayed initialize.

INFO

  • The shebang resolves python3 from a PATH that begins with session-writable directories; the image's own convention is an absolute interpreter.
  • TALE_PLAYWRIGHT_MCP_EAGER isn't wired anywhere.
  • Hermes, Pi and Cursor mount no Playwright MCP.
  • The argument-set test checks the export, not what buildMcpServers emits.
  • The container check covers argument set [0] only.

Verified correct:

  • Handshake: the manifest answers per requested version, held notifications are replayed after initialized, the replay's own answer never reaches the client, and a shared reader means nothing is lost at the handoff.
  • Versions: every client's requested version is among the probed ones.
  • Harnesses: Gemini, OpenCode and Claude Code's SDK stay lazy.
  • Shutdown: EOF and signal handling are forwarded, with exit codes of 128+N.
  • fix(sandbox): end the processes an exec leaves behind #4097 reaping still covers the proxy and server.
  • Composition with main is clean.

Next owner: the manager, for a follow-up issue on the Qwen MEDIUM and the log fix.

yannickmonney added a commit that referenced this pull request Oct 3, 2026
The lazy launcher replays the client's initialize to the server it
starts on a turn's first tool call and waited for the answer without a
bound, so a server whose start hangs stalled that tool call for as long
as the client waits. After two minutes, the same bound the image build
gives the server, the launcher now logs it, kills the server and exits
with it, and the client sees the server gone.

Refs #4117
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant