Skip to content

[Bug]: MCP browser_run_code_unsafe throws unhandled TypeError when arguments are omitted #44080

Closed

Description

Version

1.66.0-next (main)

Steps to reproduce

Call the MCP tool browser_run_code_unsafe with empty arguments:

const response = await client.callTool({
  name: 'browser_run_code_unsafe',
  arguments: {},
});

Or execute via Node VM:

const vm = require('vm');
const context = { page: {} };
vm.createContext(context);
let code = undefined;
context.__fn__ = vm.runInContext('(' + code + ')', context); // returns undefined
await context.__fn__(context.page); // throws TypeError: context.__fn__ is not a function

Expected behavior

The tool should validate arguments and return a clean error:
Error: At least one of "code" or "filename" must be provided.

This matches the input validation pattern used in browser_drop and browser_find.

Actual behavior

The tool crashes with an internal VM TypeError:

### Error
TypeError: __fn__ is not a function

Additional context

In packages/playwright-core/src/tools/backend/runCode.ts:

  • Both code and filename are optional in the Zod schema (codeSchema).
  • When invoked with {}, params.code is undefined.
  • The handler executes:
    context.__fn__ = vm.runInContext('(' + code + ')', context);
    This evaluates (undefined) and sets context.__fn__ to undefined.
  • The IIFE then executes:
    const result = await __fn__(page);
    This throws TypeError: __fn__ is not a function.

Proposed fix:
Add validation in packages/playwright-core/src/tools/backend/runCode.ts:

if (!params.code && !params.filename)
  throw new Error('At least one of "code" or "filename" must be provided.');

I have verified the reproduction and the fix locally with tests in tests/mcp/run-code.spec.ts.
I would like to work on this issue and submit a pull request once approved. Please assign this issue to me.

Environment

  System:
    OS: macOS 26.5.2
    CPU: (8) arm64 Apple M3
    Memory: 161.59 MB / 8.00 GB
  Binaries:
    Node: 26.10.0 - /Users/aritrosaha/.nvm/versions/node/v26.10.0/bin/node
    npm: 11.19.1 - /Users/aritrosaha/.nvm/versions/node/v26.10.0/bin/npm
    pnpm: 10.33.4 - /Users/aritrosaha/.nvm/versions/node/v26.10.0/bin/pnpm
    bun: 1.4.4 - /opt/homebrew/bin/bun
    Deno: 2.9.8 - /opt/homebrew/bin/deno
  IDEs:
    VSCode: 1.142.0 - /usr/local/bin/code
  Languages:
    Bash: 3.2.57 - /bin/bash

Activity

  1. halcyon-past commented on Nov 12, 2026

    @halcyon-past
    ContributorAuthor

    I have reproduced this issue locally and have a fix with regression tests ready. Please assign this issue to me, and I will submit the pull request. Thank you!

  2. microsoft-playwright-automation commented on Nov 12, 2026

    @microsoft-playwright-automation
  3. microsoft-playwright-automation commented on Nov 12, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot and I reproduced this on Linux.

    Reproduced on Chromium, Firefox, and WebKit with 1.66.0-next, commit 8882d1c85ef808500d99a2e28778e544c8a77fef. Missing arguments reach the VM as (undefined), then the handler tries to call that value. The TypeError is returned as a tool error, rather than crashing the server.

    The proposed fix rejects calls with neither code nor filename with the requested message, following the existing browser_drop validation pattern.

    Reproduction and checks

    The existing browser_run_code_unsafe test now includes:

    expect(await client.callTool({
      name: 'browser_run_code_unsafe',
      arguments: {},
    })).toHaveResponse({
      error: 'Error: At least one of "code" or "filename" must be provided.',
      isError: true,
    });

    Before the fix, this assertion returned TypeError: __fn__ is not a function on all three browsers in headless mode. Node 24.22.2; Linux. The reported version is main, so no older release was required. macOS was not tested.

    Command: npm run test-mcp -- run-code.spec.ts --project=chromium --project=firefox --project=webkit --grep 'browser_run_code_unsafe$' --workers=1.

    With the fix, all 57 tests in run-code.spec.ts and evaluate.spec.ts passed across Chromium, Firefox, and WebKit: npm run test-mcp -- run-code.spec.ts evaluate.spec.ts --project=chromium --project=firefox --project=webkit --workers=2. This includes normal code execution, file loading, and calls after an error. npm run build and npm run flint also passed.

    Draft PR: #44087

    Triaged on Linux by the Playwright bot - agent run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions