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] 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()