feat(mcp): run JavaScript in run_code

Merged
OK
2d ago
+2001-1598

e2e mcp's run_code now runs JavaScript, so an agent reads, computes, and acts in one call. Before, it took flat test statements only: no variables, loops, or return values.

  • 59% of MCP sessions in the last 30 days call run_code, and 27% of those hit INVALID_ARGUMENT, mostly from writing loops, variables, or page.*.
  • The code runs in the server's Node process, on the objects a test body gets: screen, app, expect, credentials, secrets, and the engine's fixture (browser, device). A call waits, matches, and fails as it does in a test.
  • A recorder (session-tools/code-recorder.ts) wraps those objects and writes down the test line each call spells, for the result and test_lines.
  • open_session {path} opens on one page of the app, and the opening text is smaller.
  • A single action on a control nothing names now prints a test line by its role (getByRole("spinbutton")), alone or narrowed like twins. Closes #1825.
  • agent.act() is untouched. Only the MCP server changes.
const items = screen.getByTestId('feed-item');
let seen = 0;
for (let i = 0; i < 60; i++) {
  const count = await items.count();
  if (count === seen) break;
  seen = count;
  await items.last().scrollIntoView();
}
const texts = await items.allTextContents();
return { total: texts.length, title: await browser.evaluate(() => document.title) };

Trust model change

run_code has the authority of a test file. An earlier revision of this PR ran the code in a QuickJS sandbox and kept the closed grammar; that is gone (maintainer decision).

  • Why: a coding agent on e2e mcp can already write a test and run it with npx e2e run, which has full Node authority. The sandbox cost a 440-line guest prelude, a second expect, no callbacks, and a name list of refused members. Playwright MCP's equivalent tool also runs in the server process.
  • Removed checks: the grammar (code-parse.ts, code-surface.ts), the type_secret checks on a secret fill in run_code, the refusal of a locator found by part of a text in a project with secrets, and the refusal of host paths. These were on main for run_code statements, so this is a behavior change there too.
  • Kept: the result passes the secret ledger, so a configured secret's exact value shows as <secret:name>. A read of a secure field is still POLICY_DENIED in the engine. The single-action tools (type_secret, upload, locate) are unchanged.
  • On by default, by design: every e2e mcp server offers run_code. There is no --allow-code flag or config switch, and a client that approves call approves run_code with it. The docs say so.
  • Docs: SECURITY.md, AGENTS.md, and the security reference name run_code as the one place agent code runs, as code-trust.

This answers the two P0 review findings (host files, secrets in changed form) by changing the model, not by enforcement. It needs a reviewer's agreement.

Behavior

  • A line that fails throws the error a test gets. Uncaught, it stops the code.
  • Breaking: lines the code does not await start together, as in any JavaScript. A flat script from main that relied on statement order needs await. The result carries a note with the count of calls not awaited.
  • A failure nothing awaited fails the call: the line's promise records whether anything called then on it. So does a promise the code left to reject, and an error a timer callback throws; that one ends the call at once. None of them reach the process.
  • An error from code the call left running after it ended goes to stderr as a warning. The session, or the held test's worker, keeps running.
  • An expect.soft failure lets the code go on and fails the call at the end.
  • A function the code hands to the engine (a browser.route handler) runs in the call's context. Its line is drafted once, with a comment that the server called it.
  • An explicit timeout sets how long a line waits. Without one, a line waits up to 5 s.
  • An object a call resolves with, such as a new tab, gets a const newTab1 = await browser.newTab(); line in the draft.
  • import() takes a Node built-in or an absolute path. A relative one resolves from e2e's install, not the project.
  • open_session {path} on an engine with no navigate is INVALID_ARGUMENT.
  • The code compiles strict, so it leaves no name on the server's globals.
  • Code that never yields (while (true) {}) blocks the server. There is no CPU limit.

Verification

  • pnpm check passes.
  • Unit and MCP integration tests pass locally: 252 files, 4493 tests.
  • Driven live through the built CLI over MCP stdio: the web benchmark app (reads, expect, browser.evaluate, a browser.route handler, a new tab, syntax and runtime errors with line numbers, unawaited failures), and the mobile benchmark app on an iOS 26.5 simulator and an Android API 36 emulator (device.* reads and actions, a credential fill, await import('node:fs')).
  • Cost benchmark rerun after the rewrite: 8 QA tasks, 3 runs each, Claude Code on Sonnet. Totals for the 8 tasks, averaged over the runs:
Tool callsTimeCostErrors
e2e MCP33158 s$0.502.0
Playwright MCP 0.0.8675276 s$0.486.7