[MCP]: browser_file_upload emits invalid code and does not cancel file chooser when paths is omitted
What's going on?
The schema description for browser_file_upload states:
If omitted, file chooser is cancelled.
However, when browser_file_upload is invoked without paths (or with paths: undefined):
- The tool emits invalid generated code
await fileChooser.setFiles(undefined)instead ofawait fileChooser.setFiles([]). - The file chooser is not cancelled on the page because the invocation of
modalState.fileChooser.setFilesis skipped whenpathsis undefined.
Steps to reproduce
- Open a page with
<input type="file" />. - Trigger the file chooser via
browser_click. - Call
browser_file_uploadwith{}(omittingpaths).
Actual behavior
- Tool emits
await fileChooser.setFiles(undefined). modalState.fileChooser.setFiles([])is never called, leaving the browser file chooser unhandled on the page.
Expected behavior
- Tool emits
await fileChooser.setFiles([]). modalState.fileChooser.setFiles([])is called to cancel the file chooser.
I have investigated the root cause and prepared a local fix with regression tests. Please assign this issue to me so I can submit the pull request.
Version
1.51.0 (main)
I have submitted a pull request with the fix and regression coverage in #44092.
Hi, I'd like to work on this issue. Could you please assign it to me? I'm interested in contributing and will work on implementing a solution. Thanks!
Hi, I'm the Playwright bot. I started triaging this issue on Linux: agent run
Hi, I'm the Playwright bot. I reproduced this and checked #44092.
Reproduced on main; fixed by #44092 at fd8a561f6c4bbe49ff65754ce39ffc69691607ff. Chromium, Firefox and WebKit behave the same on Linux. Omitting paths generates setFiles(undefined) and skips the actual setFiles call. The PR supplies [] and always calls setFiles, fixing both parts.
Reproduction and results
On main, version 1.66.0-next, commit 8534271f0413bae6a455b7cf43b2637391bc614b:
server.setContent('/', '<input type="file" />', 'text/html');
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});
await client.callTool({
name: 'browser_click',
arguments: { element: 'File input', target: 'e2' },
});
expect(await client.callTool({
name: 'browser_file_upload',
arguments: {},
})).toHaveResponse({
code: 'await fileChooser.setFiles([])',
modalState: undefined,
});
The generated code is instead await fileChooser.setFiles(undefined).
I also extended the existing upload test independently: upload a file, reopen the chooser, call the tool with {}, then check input.files.length and the generated code. On main, the file count stays at 1; on the PR, it becomes 0 and the code uses setFiles([]).
| Check | Chromium | Firefox | WebKit |
|---|---|---|---|
| Independent regression on main | Fails as reported | Fails as reported | Fails as reported |
| PR regression with main's implementation | Fails as reported | Fails as reported | Fails as reported |
| Independent regression on the PR | Passes | Passes | Passes |
| PR's upload regression | Passes | Passes | Passes |
Checks run
All browser runs were headless on Linux:
npm run test-mcp -- files.spec.ts --project=chromium --project=firefox --project=webkit --grep 'browser_file_upload$'
npm run test-mcp -- files.spec.ts file-paths.spec.ts dialogs.spec.ts --project=chromium --project=firefox --project=webkit --workers=2
npm run build
npm run flint
The broader test run passed 65 tests, with 4 existing skips. Build and all lint checks passed. WebKit initially needed system libraries; after installing them, its tests ran successfully. The PR remains open, is not a draft, and has no merge conflicts. Its CLA check passed; no browser CI jobs have run on the fork PR. No second fix is proposed.