feat(mcp): run JavaScript in run_code
Merged
OK
2d ago+2001+2001+0123456789012345678901234567890123456789-1598-1598-0123456789012345678901234567890123456789
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 hitINVALID_ARGUMENT, mostly from writing loops, variables, orpage.*. - 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 andtest_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 mcpcan already write a test and run it withnpx e2e run, which has full Node authority. The sandbox cost a 440-line guest prelude, a secondexpect, 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), thetype_secretchecks on a secretfillinrun_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 forrun_codestatements, 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 stillPOLICY_DENIEDin the engine. The single-action tools (type_secret,upload,locate) are unchanged. - On by default, by design: every
e2e mcpserver offersrun_code. There is no--allow-codeflag or config switch, and a client that approvescallapprovesrun_codewith it. The docs say so. - Docs:
SECURITY.md,AGENTS.md, and the security reference namerun_codeas 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
thenon 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.softfailure lets the code go on and fails the call at the end. - A function the code hands to the engine (a
browser.routehandler) runs in the call's context. Its line is drafted once, with a comment that the server called it. - An explicit
timeoutsets 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 nonavigateisINVALID_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 checkpasses.- 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, abrowser.routehandler, 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 calls | Time | Cost | Errors | |
|---|---|---|---|---|
| e2e MCP | 33 | 158 s | $0.50 | 2.0 |
| Playwright MCP 0.0.86 | 75 | 276 s | $0.48 | 6.7 |