From f2666dbae2bedb7752a9d8d17a06ba258e9a7296 Mon Sep 17 00:00:00 2001 From: Coff0xc <184964491+Coff0xc@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:48:01 +0000 Subject: [PATCH 1/3] fix(mcp): launch the browser MCP from playwright-mcp, not npx npx @playwright/mcp queries the npm registry on every start even with the package installed globally, so the image's pin did not decide what ran and offline starts failed. Seed playwright-mcp --headless --browser chromium, rewrite leftover stock npx rows once, and install the Chromium build the MCP's own playwright-core expects. Fixes #329 --- CHANGELOG.md | 1 + Dockerfile | 14 ++- db/browser_mcp.go | 89 +++++++++++++++++ db/browser_mcp_test.go | 216 +++++++++++++++++++++++++++++++++++++++++ db/db.go | 12 ++- server/mcpdiscover.go | 2 +- 6 files changed, 327 insertions(+), 7 deletions(-) create mode 100644 db/browser_mcp.go create mode 100644 db/browser_mcp_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 736c242..7ad90aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ All notable changes to this project are documented in this file. The format is b - **Chat session directories**: `ChatAgent.Chat` accepts only `[A-Za-z0-9_-]` session ids (at most 64 bytes) before joining them under `/sessions`, so `..` and path separators cannot escape that directory. - **Change-password rate limit**: `POST /api/auth/change-password` uses the same limiter as login, keyed by client IP and by the session token, so a stolen session cannot guess the current password without backoff. - **Ingested traffic headers**: `RecordExternal` rejects a method that is not an HTTP token and header names or values that contain CR, LF, or NUL, matching Replay, so ingested exchanges cannot inject extra header lines into stored evidence. +- **Browser MCP starts offline** (#329): the seeded `browser` MCP ran `npx @playwright/mcp`, which contacts the npm registry on every start even with the package installed globally, and Playwright MCP defaulted to a Chrome channel the image does not ship. It now runs the image's `playwright-mcp` binary with `--headless --browser chromium`. Existing databases that still have the stock `npx` row are rewritten once; a row the operator has edited is left alone. The image also installs the Chromium build `@playwright/mcp` and `@playwright/cli` expect, which is not the same build `playwright install` puts in the cache. - **ask_operator image_path**: the screenshot path must resolve inside the task working directory (`/tasks/`). Absolute paths, `..` segments, and symlinks that leave that directory are refused, so the tool cannot copy an arbitrary server image into the operator transcript. - **insert_assets fail-closed**: when intercept or task allow/block rules cannot be loaded, `insert_assets` refuses the whole call instead of evaluating an empty rule set (which allowed every asset). The tool result does not include the underlying database error. - **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`. diff --git a/Dockerfile b/Dockerfile index 4e4a539..80a83b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -167,18 +167,24 @@ RUN set -eux; \ ! grep -qE "Installing|ERRR|CRITICAL" /tmp/bbot-dry.log || { cat /tmp/bbot-dry.log; exit 1; } -# Preinstall Playwright MCP and CLI (globally) so no npx network download is needed at runtime. -# @playwright/mcp: browser MCP runs directly via `npx @playwright/mcp` (installed globally, no -y/@latest needed). +# Preinstall Playwright MCP and CLI (globally) so no network download is needed at runtime. +# @playwright/mcp: the seeded browser MCP runs the playwright-mcp binary directly. +# `npx @playwright/mcp` is not used: it queries the npm registry on every start even +# with the package installed globally. # @playwright/cli: provides playwright-cli, verified with --help right after install. # Then install playwright (browser management) and preinstall chromium plus its system -# dependencies via --with-deps, so MCP/CLI work on first start inside the container -# without downloading browsers over the network. All three are pinned; upgrade deliberately. +# dependencies via --with-deps. @playwright/mcp and @playwright/cli bundle their own +# playwright-core, which expects a different Chromium build than +# playwright@PLAYWRIGHT_VERSION installs, so that build is installed as well (second +# install line); without it neither can launch a browser. All three packages are +# pinned; upgrade deliberately. ARG PLAYWRIGHT_MCP_VERSION=0.0.83 ARG PLAYWRIGHT_CLI_VERSION=0.1.22 ARG PLAYWRIGHT_VERSION=1.64.1 RUN npm install -g "@playwright/mcp@${PLAYWRIGHT_MCP_VERSION}" "@playwright/cli@${PLAYWRIGHT_CLI_VERSION}" "playwright@${PLAYWRIGHT_VERSION}" \ && playwright-cli --help \ && playwright install --with-deps chromium \ + && node /usr/local/lib/node_modules/@playwright/mcp/node_modules/playwright-core/cli.js install chromium \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Binary compiled from source in the build stage above. diff --git a/db/browser_mcp.go b/db/browser_mcp.go new file mode 100644 index 0000000..0f1afcd --- /dev/null +++ b/db/browser_mcp.go @@ -0,0 +1,89 @@ +package db + +import ( + "database/sql" + "encoding/json" + "errors" + "fmt" + "strings" +) + +const ( + browserMCPName = "browser" + browserMCPCommand = "playwright-mcp" + browserMCPDirectBinSetting = "browser_mcp_direct_bin_v1" +) + +// browserMCPSeedArgs is the JSON args array for a fresh browser MCP row. +// --headless: the image has no display. +// --browser chromium: Playwright MCP defaults to the "chrome" channel at +// /opt/google/chrome, which the image does not ship. +const browserMCPSeedArgs = `["--headless","--browser","chromium"]` + +// rewriteStockNpxBrowserMCP converts the original seed +// `npx @playwright/mcp [--headless ...]` to the image's global playwright-mcp +// binary. ok is false when the row is not that stock invocation (the operator +// changed the command or the package), so those rows are left alone. +func rewriteStockNpxBrowserMCP(command string, args []string) (string, []string, bool) { + if command != "npx" || len(args) == 0 { + return command, args, false + } + pkg := args[0] + if pkg != "@playwright/mcp" && !strings.HasPrefix(pkg, "@playwright/mcp@") { + return command, args, false + } + rest := append([]string{}, args[1:]...) + if !hasCLIFlag(rest, "--headless") { + rest = append(rest, "--headless") + } + if !hasCLIFlag(rest, "--browser") { + rest = append(rest, "--browser", "chromium") + } + return browserMCPCommand, rest, true +} + +func hasCLIFlag(args []string, flag string) bool { + eq := flag + "=" + for _, a := range args { + if a == flag || strings.HasPrefix(a, eq) { + return true + } + } + return false +} + +// migrateStockBrowserMCP rewrites a leftover stock `npx @playwright/mcp` browser +// MCP row once. Gated by a settings flag so an operator who later puts npx back +// is not rewritten on the next start. A row whose command or first arg is no +// longer the stock seed is left as-is. +func (d *DB) migrateStockBrowserMCP() error { + if v, _, _ := d.GetSetting(browserMCPDirectBinSetting); v == "done" { + return nil + } + var command string + var argsRaw []byte + err := d.QueryRow(`SELECT COALESCE(command,''), args FROM mcp_servers WHERE name=$1`, browserMCPName).Scan(&command, &argsRaw) + if errors.Is(err, sql.ErrNoRows) { + return d.SetSetting(browserMCPDirectBinSetting, "done") + } + if err != nil { + return fmt.Errorf("read browser mcp: %w", err) + } + var args []string + if len(argsRaw) > 0 { + if err := json.Unmarshal(argsRaw, &args); err != nil { + return d.SetSetting(browserMCPDirectBinSetting, "done") + } + } + newCommand, newArgs, ok := rewriteStockNpxBrowserMCP(command, args) + if ok { + raw, err := json.Marshal(newArgs) + if err != nil { + return fmt.Errorf("encode browser mcp args: %w", err) + } + if _, err := d.Exec(`UPDATE mcp_servers SET command=$1, args=$2 WHERE name=$3`, newCommand, string(raw), browserMCPName); err != nil { + return fmt.Errorf("update browser mcp: %w", err) + } + } + return d.SetSetting(browserMCPDirectBinSetting, "done") +} diff --git a/db/browser_mcp_test.go b/db/browser_mcp_test.go new file mode 100644 index 0000000..ea68742 --- /dev/null +++ b/db/browser_mcp_test.go @@ -0,0 +1,216 @@ +package db + +import ( + "encoding/json" + "slices" + "testing" +) + +func TestRewriteStockNpxBrowserMCP(t *testing.T) { + cases := []struct { + name string + command string + args []string + wantOK bool + wantCommand string + wantArgs []string + }{ + { + name: "stock seed", + command: "npx", + args: []string{"@playwright/mcp", "--headless"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "chromium"}, + }, + { + name: "package only", + command: "npx", + args: []string{"@playwright/mcp"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "chromium"}, + }, + { + name: "pinned package version", + command: "npx", + args: []string{"@playwright/mcp@0.0.83", "--headless"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "chromium"}, + }, + { + name: "capture proxy args kept", + command: "npx", + args: []string{"@playwright/mcp", "--headless", "--proxy-server", "http://127.0.0.1:8788"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--proxy-server", "http://127.0.0.1:8788", "--browser", "chromium"}, + }, + { + name: "existing --browser kept", + command: "npx", + args: []string{"@playwright/mcp", "--headless", "--browser", "firefox"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "firefox"}, + }, + { + name: "equals-form --browser kept", + command: "npx", + args: []string{"@playwright/mcp", "--browser=chrome"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--browser=chrome", "--headless"}, + }, + { + name: "already playwright-mcp", + command: "playwright-mcp", + args: []string{"--headless"}, + }, + { + name: "npx -y is a user edit", + command: "npx", + args: []string{"-y", "@playwright/mcp", "--headless"}, + }, + { + name: "different package", + command: "npx", + args: []string{"@modelcontextprotocol/server-github"}, + }, + { + name: "empty args", + command: "npx", + }, + { + name: "empty command", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + gotCommand, gotArgs, ok := rewriteStockNpxBrowserMCP(tc.command, tc.args) + if ok != tc.wantOK { + t.Fatalf("ok=%v, want %v", ok, tc.wantOK) + } + if !tc.wantOK { + if gotCommand != tc.command || !slices.Equal(gotArgs, tc.args) { + t.Fatalf("unchanged: got %q %q", gotCommand, gotArgs) + } + return + } + if gotCommand != tc.wantCommand { + t.Fatalf("command=%q, want %q", gotCommand, tc.wantCommand) + } + if !slices.Equal(gotArgs, tc.wantArgs) { + t.Fatalf("args=%q, want %q", gotArgs, tc.wantArgs) + } + }) + } +} + +func TestRewriteStockNpxBrowserMCPDoesNotAliasInput(t *testing.T) { + in := []string{"@playwright/mcp", "--headless"} + orig := slices.Clone(in) + _, out, ok := rewriteStockNpxBrowserMCP("npx", in) + if !ok { + t.Fatal("expected rewrite") + } + out[0] = "mutated" + if !slices.Equal(in, orig) { + t.Fatalf("input mutated: %q", in) + } +} + +func TestSeedAndMigrateBrowserMCP(t *testing.T) { + d, err := Open(testDSN(t)) + if err != nil { + t.Skipf("postgres unavailable (%v) — skipping", err) + } + + origCmd, _, origRaw := readBrowserMCP(t, d) + origSetting, origHad, err := d.GetSetting(browserMCPDirectBinSetting) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + _, err := d.Exec(`UPDATE mcp_servers SET command=$1, args=$2 WHERE name=$3`, origCmd, origRaw, browserMCPName) + cleanupErr(t, "restore browser mcp", err) + if origHad { + cleanupErr(t, "restore setting", d.SetSetting(browserMCPDirectBinSetting, origSetting)) + } else { + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + } + d.Close() + }) + + t.Run("stock npx rewritten once", func(t *testing.T) { + putBrowserMCP(t, d, "npx", []string{"@playwright/mcp", "--headless"}) + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ := readBrowserMCP(t, d) + if cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("got %q %q", cmd, args) + } + putBrowserMCP(t, d, "npx", []string{"@playwright/mcp", "--headless"}) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ = readBrowserMCP(t, d) + if cmd != "npx" || !slices.Equal(args, []string{"@playwright/mcp", "--headless"}) { + t.Fatalf("second pass clobbered: %q %q", cmd, args) + } + }) + + t.Run("custom npx -y left alone", func(t *testing.T) { + custom := []string{"-y", "@playwright/mcp", "--headless"} + putBrowserMCP(t, d, "npx", custom) + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ := readBrowserMCP(t, d) + if cmd != "npx" || !slices.Equal(args, custom) { + t.Fatalf("custom row rewritten: %q %q", cmd, args) + } + }) + + t.Run("proxy args preserved", func(t *testing.T) { + putBrowserMCP(t, d, "npx", []string{"@playwright/mcp", "--headless", "--proxy-server", "http://127.0.0.1:8788"}) + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ := readBrowserMCP(t, d) + want := []string{"--headless", "--proxy-server", "http://127.0.0.1:8788", "--browser", "chromium"} + if cmd != browserMCPCommand || !slices.Equal(args, want) { + t.Fatalf("got %q %q", cmd, args) + } + }) +} + +func putBrowserMCP(t *testing.T, d *DB, command string, args []string) { + t.Helper() + raw, err := json.Marshal(args) + if err != nil { + t.Fatal(err) + } + if _, err := d.Exec(`UPDATE mcp_servers SET command=$1, args=$2 WHERE name=$3`, command, string(raw), browserMCPName); err != nil { + t.Fatal(err) + } +} + +func readBrowserMCP(t *testing.T, d *DB) (command string, args []string, raw []byte) { + t.Helper() + err := d.QueryRow(`SELECT COALESCE(command,''), args FROM mcp_servers WHERE name=$1`, browserMCPName).Scan(&command, &raw) + if err != nil { + t.Fatal(err) + } + if len(raw) > 0 { + if err := json.Unmarshal(raw, &args); err != nil { + t.Fatal(err) + } + } + return +} diff --git a/db/db.go b/db/db.go index 2c07dac..ef9ec28 100644 --- a/db/db.go +++ b/db/db.go @@ -317,13 +317,21 @@ ON CONFLICT (agent_id, var_name) DO UPDATE // the recording proxy + CA at runtime (server.Manager.syncBrowserMCPProxy). // Insert only if absent so we never clobber user edits (args/env/enabled/ // visibility) on restart. + // Launched through the installed playwright-mcp binary, not `npx + // @playwright/mcp`: npx asks the npm registry on every start, even with the + // package installed globally, so the version that runs is the registry's. + // --browser chromium: the default is the "chrome" channel (Google Chrome), + // which the image does not have; it ships Playwright's Chromium. if _, err := d.Exec(` INSERT INTO mcp_servers(name, transport, command, args, env, enabled) -VALUES ('browser', 'stdio', 'npx', $1, '{}', false) +VALUES ('browser', 'stdio', $1, $2, '{}', false) ON CONFLICT (name) DO NOTHING`, - `["@playwright/mcp","--headless"]`); err != nil { + browserMCPCommand, browserMCPSeedArgs); err != nil { return fmt.Errorf("seed browser mcp: %w", err) } + if err := d.migrateStockBrowserMCP(); err != nil { + return fmt.Errorf("migrate browser mcp: %w", err) + } // NOTE: the placeholder ScopeSentry data-source MCP (empty URL + empty X-API-Key, // disabled) is seeded directly in schema.sql §F so a raw `psql < schema.sql` init // also gets it. schema.sql is Exec'd on every startup, so it stays idempotent. diff --git a/server/mcpdiscover.go b/server/mcpdiscover.go index fbb5818..a517035 100644 --- a/server/mcpdiscover.go +++ b/server/mcpdiscover.go @@ -72,7 +72,7 @@ func (s *Server) discoverAndCacheMCP(ctx context.Context, m *db.MCPServer) error // discoverEmptyMCPsOnStartup fills the tool cache for any enabled MCP that has none // yet (notably the seeded browser MCP on first run). Runs sequentially in one -// goroutine so we never spawn many stdio servers (npx) at once, and never blocks +// goroutine so we never spawn many stdio servers at once, and never blocks // startup. Best-effort: a failure leaves the cache empty to retry next start. func (s *Server) discoverEmptyMCPsOnStartup() { servers, err := s.m.pg.ListMCP() From 3546ea0fff44852f3aa12a2cbe1d06ab03145e16 Mon Sep 17 00:00:00 2001 From: Coff0xc <184964491+Coff0xc@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:56:10 +0000 Subject: [PATCH 2/3] fix(mcp): rewrite npx -y and playwright-mcp rows missing --browser The stock seed was npx @playwright/mcp, but the UI mock and common invocations use npx -y, which the first pass left untouched. Also add --browser chromium to a leftover playwright-mcp --headless row, resolve the MCP's playwright-core CLI via require.resolve, and cover proxy-arg stripping so capture sync does not depend on args[0]. --- CHANGELOG.md | 2 +- Dockerfile | 2 +- db/browser_mcp.go | 66 ++++++++++++++++++----- db/browser_mcp_test.go | 86 ++++++++++++++++++++++++++++-- server/manager_browser_mcp_test.go | 24 +++++++++ 5 files changed, 161 insertions(+), 19 deletions(-) create mode 100644 server/manager_browser_mcp_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ad90aa..c5a2543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,7 +54,7 @@ All notable changes to this project are documented in this file. The format is b - **Chat session directories**: `ChatAgent.Chat` accepts only `[A-Za-z0-9_-]` session ids (at most 64 bytes) before joining them under `/sessions`, so `..` and path separators cannot escape that directory. - **Change-password rate limit**: `POST /api/auth/change-password` uses the same limiter as login, keyed by client IP and by the session token, so a stolen session cannot guess the current password without backoff. - **Ingested traffic headers**: `RecordExternal` rejects a method that is not an HTTP token and header names or values that contain CR, LF, or NUL, matching Replay, so ingested exchanges cannot inject extra header lines into stored evidence. -- **Browser MCP starts offline** (#329): the seeded `browser` MCP ran `npx @playwright/mcp`, which contacts the npm registry on every start even with the package installed globally, and Playwright MCP defaulted to a Chrome channel the image does not ship. It now runs the image's `playwright-mcp` binary with `--headless --browser chromium`. Existing databases that still have the stock `npx` row are rewritten once; a row the operator has edited is left alone. The image also installs the Chromium build `@playwright/mcp` and `@playwright/cli` expect, which is not the same build `playwright install` puts in the cache. +- **Browser MCP starts offline** (#329): the seeded `browser` MCP ran `npx @playwright/mcp`, which contacts the npm registry on every start even with the package installed globally, and Playwright MCP defaulted to a Chrome channel the image does not ship. It now runs the image's `playwright-mcp` binary with `--headless --browser chromium`. Existing databases that still have a stock `npx @playwright/mcp` row (including the common `-y` form) or a `playwright-mcp` row with no `--browser` are rewritten once; any other command or package is left alone. The image also installs the Chromium build `@playwright/mcp` and `@playwright/cli` expect, which is not the same build `playwright install` puts in the cache. - **ask_operator image_path**: the screenshot path must resolve inside the task working directory (`/tasks/`). Absolute paths, `..` segments, and symlinks that leave that directory are refused, so the tool cannot copy an arbitrary server image into the operator transcript. - **insert_assets fail-closed**: when intercept or task allow/block rules cannot be loaded, `insert_assets` refuses the whole call instead of evaluating an empty rule set (which allowed every asset). The tool result does not include the underlying database error. - **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`. diff --git a/Dockerfile b/Dockerfile index 80a83b2..48cbf37 100644 --- a/Dockerfile +++ b/Dockerfile @@ -184,7 +184,7 @@ ARG PLAYWRIGHT_VERSION=1.64.1 RUN npm install -g "@playwright/mcp@${PLAYWRIGHT_MCP_VERSION}" "@playwright/cli@${PLAYWRIGHT_CLI_VERSION}" "playwright@${PLAYWRIGHT_VERSION}" \ && playwright-cli --help \ && playwright install --with-deps chromium \ - && node /usr/local/lib/node_modules/@playwright/mcp/node_modules/playwright-core/cli.js install chromium \ + && node "$(node -p 'require.resolve("playwright-core/cli.js",{paths:["/usr/local/lib/node_modules/@playwright/mcp"]})')" install chromium \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Binary compiled from source in the build stage above. diff --git a/db/browser_mcp.go b/db/browser_mcp.go index 0f1afcd..3cee0d5 100644 --- a/db/browser_mcp.go +++ b/db/browser_mcp.go @@ -20,19 +20,44 @@ const ( // /opt/google/chrome, which the image does not ship. const browserMCPSeedArgs = `["--headless","--browser","chromium"]` -// rewriteStockNpxBrowserMCP converts the original seed -// `npx @playwright/mcp [--headless ...]` to the image's global playwright-mcp -// binary. ok is false when the row is not that stock invocation (the operator -// changed the command or the package), so those rows are left alone. +// npxMetaFlags are npx switches that do not take a value and that still leave +// the following argument as the package spec. `-y` is the form the UI mock +// used; `--` is a common separator. Flags that take a value (`-p`, `--package`) +// are treated as a custom invocation and left alone. +var npxMetaFlags = map[string]bool{ + "-y": true, + "--yes": true, + "--": true, + "-q": true, + "--quiet": true, +} + +func playwrightMCPPackageIndex(args []string) (int, bool) { + for i := 0; i < len(args); i++ { + a := args[i] + if npxMetaFlags[a] { + continue + } + if a == "@playwright/mcp" || strings.HasPrefix(a, "@playwright/mcp@") { + return i, true + } + return 0, false + } + return 0, false +} + +// rewriteStockNpxBrowserMCP converts `npx [@meta...] @playwright/mcp [...]` to +// the image's global playwright-mcp binary. ok is false when the row is not +// that invocation (the operator changed the command or the package). func rewriteStockNpxBrowserMCP(command string, args []string) (string, []string, bool) { - if command != "npx" || len(args) == 0 { + if command != "npx" { return command, args, false } - pkg := args[0] - if pkg != "@playwright/mcp" && !strings.HasPrefix(pkg, "@playwright/mcp@") { + idx, ok := playwrightMCPPackageIndex(args) + if !ok { return command, args, false } - rest := append([]string{}, args[1:]...) + rest := append([]string{}, args[idx+1:]...) if !hasCLIFlag(rest, "--headless") { rest = append(rest, "--headless") } @@ -42,6 +67,22 @@ func rewriteStockNpxBrowserMCP(command string, args []string) (string, []string, return browserMCPCommand, rest, true } +// rewriteBrowserMCPRow is the one-time row rewrite: stock npx @playwright/mcp +// becomes playwright-mcp, and a playwright-mcp row that still has no --browser +// (the MCP defaults to a Chrome channel the image does not ship) gets +// --browser chromium. Any other command or package is left alone. +func rewriteBrowserMCPRow(command string, args []string) (string, []string, bool) { + cmd, out, ok := rewriteStockNpxBrowserMCP(command, args) + if !ok { + cmd, out = command, append([]string{}, args...) + } + if cmd == browserMCPCommand && !hasCLIFlag(out, "--browser") { + out = append(out, "--browser", "chromium") + ok = true + } + return cmd, out, ok +} + func hasCLIFlag(args []string, flag string) bool { eq := flag + "=" for _, a := range args { @@ -53,9 +94,10 @@ func hasCLIFlag(args []string, flag string) bool { } // migrateStockBrowserMCP rewrites a leftover stock `npx @playwright/mcp` browser -// MCP row once. Gated by a settings flag so an operator who later puts npx back -// is not rewritten on the next start. A row whose command or first arg is no -// longer the stock seed is left as-is. +// MCP row once, and adds `--browser chromium` to a playwright-mcp row that +// still has none. Gated by a settings flag so an operator who later puts npx +// back is not rewritten on the next start. Any other command or package is +// left as-is. func (d *DB) migrateStockBrowserMCP() error { if v, _, _ := d.GetSetting(browserMCPDirectBinSetting); v == "done" { return nil @@ -75,7 +117,7 @@ func (d *DB) migrateStockBrowserMCP() error { return d.SetSetting(browserMCPDirectBinSetting, "done") } } - newCommand, newArgs, ok := rewriteStockNpxBrowserMCP(command, args) + newCommand, newArgs, ok := rewriteBrowserMCPRow(command, args) if ok { raw, err := json.Marshal(newArgs) if err != nil { diff --git a/db/browser_mcp_test.go b/db/browser_mcp_test.go index ea68742..5786c49 100644 --- a/db/browser_mcp_test.go +++ b/db/browser_mcp_test.go @@ -63,15 +63,31 @@ func TestRewriteStockNpxBrowserMCP(t *testing.T) { wantCommand: "playwright-mcp", wantArgs: []string{"--browser=chrome", "--headless"}, }, + { + name: "npx -y", + command: "npx", + args: []string{"-y", "@playwright/mcp", "--headless"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "chromium"}, + }, + { + name: "npx --yes -- separator", + command: "npx", + args: []string{"--yes", "--", "@playwright/mcp"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "chromium"}, + }, { name: "already playwright-mcp", command: "playwright-mcp", args: []string{"--headless"}, }, { - name: "npx -y is a user edit", + name: "npx -p is a custom invocation", command: "npx", - args: []string{"-y", "@playwright/mcp", "--headless"}, + args: []string{"-p", "@playwright/mcp", "playwright-mcp", "--headless"}, }, { name: "different package", @@ -108,6 +124,34 @@ func TestRewriteStockNpxBrowserMCP(t *testing.T) { } } +func TestRewriteBrowserMCPRow(t *testing.T) { + cmd, args, ok := rewriteBrowserMCPRow("playwright-mcp", []string{"--headless"}) + if !ok || cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("missing --browser: ok=%v %q %q", ok, cmd, args) + } + + cmd, args, ok = rewriteBrowserMCPRow("playwright-mcp", []string{"--headless", "--browser", "firefox"}) + if ok { + t.Fatalf("existing --browser rewritten: %q %q", cmd, args) + } + + cmd, args, ok = rewriteBrowserMCPRow("npx", []string{"-y", "@playwright/mcp", "--headless"}) + if !ok || cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("npx -y: ok=%v %q %q", ok, cmd, args) + } + + in := []string{"--headless"} + orig := slices.Clone(in) + _, out, ok := rewriteBrowserMCPRow("playwright-mcp", in) + if !ok { + t.Fatal("expected rewrite") + } + out[0] = "mutated" + if !slices.Equal(in, orig) { + t.Fatalf("input mutated: %q", in) + } +} + func TestRewriteStockNpxBrowserMCPDoesNotAliasInput(t *testing.T) { in := []string{"@playwright/mcp", "--headless"} orig := slices.Clone(in) @@ -163,8 +207,32 @@ func TestSeedAndMigrateBrowserMCP(t *testing.T) { } }) - t.Run("custom npx -y left alone", func(t *testing.T) { - custom := []string{"-y", "@playwright/mcp", "--headless"} + t.Run("npx -y rewritten", func(t *testing.T) { + putBrowserMCP(t, d, "npx", []string{"-y", "@playwright/mcp", "--headless"}) + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ := readBrowserMCP(t, d) + if cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("got %q %q", cmd, args) + } + }) + + t.Run("playwright-mcp missing --browser", func(t *testing.T) { + putBrowserMCP(t, d, "playwright-mcp", []string{"--headless"}) + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ := readBrowserMCP(t, d) + if cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("got %q %q", cmd, args) + } + }) + + t.Run("custom package left alone", func(t *testing.T) { + custom := []string{"@modelcontextprotocol/server-github"} putBrowserMCP(t, d, "npx", custom) cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) if err := d.seedBuiltins(); err != nil { @@ -196,9 +264,17 @@ func putBrowserMCP(t *testing.T, d *DB, command string, args []string) { if err != nil { t.Fatal(err) } - if _, err := d.Exec(`UPDATE mcp_servers SET command=$1, args=$2 WHERE name=$3`, command, string(raw), browserMCPName); err != nil { + res, err := d.Exec(`UPDATE mcp_servers SET command=$1, args=$2 WHERE name=$3`, command, string(raw), browserMCPName) + if err != nil { t.Fatal(err) } + n, err := res.RowsAffected() + if err != nil { + t.Fatal(err) + } + if n != 1 { + t.Fatalf("update browser mcp: %d rows", n) + } } func readBrowserMCP(t *testing.T, d *DB) (command string, args []string, raw []byte) { diff --git a/server/manager_browser_mcp_test.go b/server/manager_browser_mcp_test.go new file mode 100644 index 0000000..fa5eb27 --- /dev/null +++ b/server/manager_browser_mcp_test.go @@ -0,0 +1,24 @@ +package server + +import ( + "slices" + "testing" +) + +func TestStripProxyArgsLeavesBrowserMCPFlags(t *testing.T) { + in := []string{"--headless", "--browser", "chromium", "--proxy-server", "http://127.0.0.1:8788"} + got := stripProxyArgs(in) + want := []string{"--headless", "--browser", "chromium"} + if !slices.Equal(got, want) { + t.Fatalf("got %q, want %q", got, want) + } + if in[3] != "--proxy-server" { + t.Fatal("input mutated") + } + + equals := []string{"--headless", "--proxy-server=http://127.0.0.1:8788", "--browser", "chromium"} + got = stripProxyArgs(equals) + if !slices.Equal(got, want) { + t.Fatalf("equals form: got %q, want %q", got, want) + } +} From 5a4af1b4daf19887292ea36b5bda690e8057138d Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:25:45 +0100 Subject: [PATCH 3/3] fix(docker): install MCP chromium through its linked playwright-core playwright-core's exports map does not expose ./cli.js, so require.resolve threw inside the command substitution, the outer node got an empty script path, and the build still succeeded without installing chromium v1247. Run the binary npm links for @playwright/mcp instead. Verified with docker build: both v1247 and v1248 are in the cache, and @playwright/mcp's playwright-core launches 155.0.8059.12. --- Dockerfile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 48cbf37..1a623e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -181,10 +181,13 @@ RUN set -eux; \ ARG PLAYWRIGHT_MCP_VERSION=0.0.83 ARG PLAYWRIGHT_CLI_VERSION=0.1.22 ARG PLAYWRIGHT_VERSION=1.64.1 +# playwright-core's exports map hides cli.js from require.resolve, so run the +# binary npm links for @playwright/mcp instead: it is the copy MCP actually +# loads, and it installs the Chromium revision that copy expects. RUN npm install -g "@playwright/mcp@${PLAYWRIGHT_MCP_VERSION}" "@playwright/cli@${PLAYWRIGHT_CLI_VERSION}" "playwright@${PLAYWRIGHT_VERSION}" \ && playwright-cli --help \ && playwright install --with-deps chromium \ - && node "$(node -p 'require.resolve("playwright-core/cli.js",{paths:["/usr/local/lib/node_modules/@playwright/mcp"]})')" install chromium \ + && "$(npm root -g)/@playwright/mcp/node_modules/.bin/playwright-core" install chromium \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Binary compiled from source in the build stage above.