playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[MCP]: browser_evaluate with filename saves a string result JSON-encoded on one line

Open igorsaevets opened this issue 1 week ago • 2 comments

What's going on?

browser_evaluate can save its result to a file (filename). For a string result the file gets the string's JSON encoding, not the string: handle() runs every JSON value through JSON.stringify(result, null, 2) and passes that text to response.addResult() (evaluate.ts#L72-L80). A multi-line string is saved as one line, in quotes, with \n escapes, whatever the file name.

browser_evaluate { "function": "() => 'first line\\nsecond line\\n'", "filename": "eval-out.txt" }
  eval-out.txt: "first line\nsecond line\n"   (one line, no line breaks)

eval-out.md and eval-out.json get the same content.

In my own sessions I used filename to keep a multi-line value out of the conversation; the .txt file came out as one quoted line and needed a decoding step before the next command could use it. The other tools write plain text to their filename (console messages and network requests as lines), so a string from browser_evaluate is the odd one out.

Steps

  1. browser_navigate to any page.
  2. Call browser_evaluate with { "function": "() => 'first line\\nsecond line\\n'", "filename": "eval-out.txt" }.
  3. Open eval-out.txt in the workspace: it holds "first line\nsecond line\n" on one line.

Expected: when the result is a string and filename is given, the file holds the string itself (two lines here), at least when the name does not end in .json. Other values can stay JSON.

Version

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

igorsaevets avatar Nov 22 '26 07: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 on Windows.

Reproduced on 1.66.0-next, commit aa3c4453c15602f20ccf60c52fde74497e1ac268, in Chromium, Firefox and WebKit. The file writer receives the JSON encoding of the string, so it writes quotes and escaped line breaks. The proposal writes strings directly to explicitly named files, regardless of extension, and keeps inline responses and non-string results unchanged.

Reproduction and checks

After navigating to the fixture server's page:

await client.callTool({
  name: 'browser_evaluate',
  arguments: {
    function: "() => ['first line', 'second line', ''].join('\\n')",
    filename: 'eval-out.txt',
  },
});

The file contains "first line\nsecond line\n" instead of two lines with a final line break. Without filename, the JSON-encoded text remains the expected response.

All runs were on Windows, headless:

Code Checks Result
Main aa3c4453c15602f20ccf60c52fde74497e1ac268 Independent reproduction, Chromium / Firefox / WebKit File-content assertion failed in all three; inline JSON response passed
Main, with only #44298's test PR regression, Chromium / Firefox / WebKit Same three failures
#44298 86af5672f29f7fcce1e3ece4c55da5009e34f4e0 Independent reproduction, Chromium / Firefox / WebKit 3 passed
Same PR head Evaluation, console, network, run-code and CLI run-code tests, Chromium / Firefox / WebKit 138 passed; build and flint passed
Proposed fix on main Same neighbouring test files, Chromium / Firefox / WebKit 135 passed
Final proposal Entire evaluation test file, Chrome / Chromium / Firefox / WebKit / Edge 25 passed; build and flint passed

Immediately before handoff, removing the production fix again caused the same three failures; restoring it passed the final 25 evaluation checks. The regression extends the existing browser_evaluate test.

The test commands used npm run test-mcp -- evaluate.spec.ts:21 for the reproduction, npm run test-mcp -- evaluate.spec.ts console.spec.ts network.spec.ts run-code.spec.ts for the neighbouring files, and npm run test-mcp -- evaluate.spec.ts for the final check, with the projects listed above. The reported published version was not needed: the issue reproduced on current main.

Draft PR: https://github.com/microsoft/playwright/pull/44318

Triaged on Windows by the Playwright bot - agent run