playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[MCP]: browser_run_code_unsafe with filename echoes the whole file in "Ran Playwright code"

Open igorsaevets opened this issue 1 week ago • 2 comments

What's going on?

browser_run_code_unsafe can load its code from a file (filename). The tool still returns the whole file: handle() reads it and passes the content to response.addCode() (runCode.ts#L51-L56), so the "Ran Playwright code" section repeats every line of it. A client that moves a long script into a file to keep it out of the conversation gets it back in the tool result.

The same call with a 13,997-byte script that returns a number:

browser_run_code_unsafe { "code": "async (page) => await page.title()" }
  tool result: 101 chars, "Ran Playwright code" section: 59 chars
browser_run_code_unsafe { "filename": "big-script.js" }
  tool result: 14,061 chars, "Ran Playwright code" section: 14,022 chars (the whole file)

In my own sessions a 14 KB script with two long text blocks cost about 10K tokens of context in one call. --codegen none removes the echo, but it also removes the generated code of every other tool.

Steps

  1. Save a long function as big-script.js in the workspace, e.g. async (page) => { /* ~14 KB */ return 1; }.
  2. Call browser_run_code_unsafe with { "filename": "big-script.js" }.
  3. The result's "Ran Playwright code" section holds the full file.

Expected: when the code comes from filename, the result names the file instead of repeating it (for example a single line that points to big-script.js), so it stays about as small as the { "code" } case.

Version

@playwright/mcp 0.0.86 (playwright-core 1.66.0-alpha-2026-11-18); runCode.ts on main (049226728d) has the same code. Windows 11, Node 24.21.0.

igorsaevets avatar Nov 22 '26 06:11 igorsaevets

Hi, I'm the Playwright bot. I started triaging this issue on Windows: agent run

Hi, I'm the Playwright bot. I reproduced this and checked #44296.

Reproduced on 1.66.0-next, commit aa3c4453c15602f20ccf60c52fde74497e1ac268, on Windows with Chrome, Chromium, Firefox and WebKit. File contents are passed to response.addCode() without checking their source. #44296 fixes this by reporting // Run code from test-code.js for file input and keeping the inline code response unchanged.

I verified #44296 at 3af2953ff0e244f54361fada104fa214e8648b62: its regression test fails without the fix, and its code and nearby CLI tests pass on all four browser projects. No second proposal is needed.

Reproduction and checks

The existing filename test writes this function to test-code.js:

async (page) => {
  await page.getByRole("button", { name: "Click" }).click();
}

Calling browser_run_code_unsafe with { filename: 'test-code.js' } on main returns the function in “Ran Playwright code”, instead of a file reference.

On Windows, using the browser revisions pinned by each checkout:

  • Main: the PR's filename assertion failed with the full script response on Chrome, Chromium, Firefox and WebKit.
  • #44296: an independent assertion that the response names the file and omits its contents passed on all four projects.
  • #44296, unchanged: run-code.spec.ts and cli-run-code.spec.ts passed all 56 tests across those projects.
  • Full build and npm run flint passed. A local merge check found no conflicts with main.

Initial checks needed dependency and browser installation. Switching between the two checkouts also required restoring their different pinned WebKit revisions. No Linux or macOS run, or published-package run, was performed. The PR's CLA check passed; no CI test jobs have run.

Triaged on Windows by the Playwright bot - agent run