#!/usr/bin/env bash # # Starts the Chrome the Chrome DevTools MCP server connects to. # # The MCP server is configured with --browser-url=http://127.0.0.1:9222, so it # attaches to the browser this script starts instead of launching one itself. # That is the whole point: the profile here is a real one you can sign into, so # the agent traces the app against your actual Navidrome library — with the # warmer, the cache and the stream requests that only exist once there is a # server behind it — rather than a logged-out shell that has nothing to measure. # # Log in once in this window and the session is kept: the profile directory is # persistent, and it is only ever this debugging profile, never your browsing # one. Chrome requires a non-default --user-data-dir to open a debug port at # all, so the two are separate by the browser's own rule, not by preference. # # Usage (from the repo, inside `nix develop .#perf`): # scripts/dev-chrome # headed, opens http://localhost:3000 # scripts/dev-chrome https://dev.hack.computa # HEADLESS=1 scripts/dev-chrome # for CI-like one-shot traces # # Any extra arguments are passed straight through to Chrome. set -euo pipefail port="${CHROME_DEBUG_PORT:-9222}" profile="${CHROME_PROFILE_DIR:-$HOME/.cache/chrome-devtools-mcp/hackermusic-profile}" url="${1:-http://localhost:3000}" [[ $# -gt 0 ]] && shift # A second Chrome on the same profile does not open a second window, it fails # with a confusing "profile appears to be in use" — or, worse, attaches to the # first instance and then the debug port belongs to a process nobody started # this way. Say so instead. if curl -s --max-time 2 "http://127.0.0.1:${port}/json/version" > /dev/null; then echo "dev-chrome: something already owns port ${port}." >&2 echo " If it is this script's earlier run, that browser is still open — close it" >&2 echo " or kill it (pkill -f 'remote-debugging-port=${port}'). Otherwise pick another" >&2 echo " port: CHROME_DEBUG_PORT=9223 scripts/dev-chrome (and match --browser-url)." >&2 exit 1 fi # Resolution order: an explicit override, then a browser already on PATH, then # nixpkgs' Chromium fetched on demand. The last branch is why this works outside # the dev shell too — `nix run` needs nothing installed. launch=() if [[ -n "${CHROME_BIN:-}" ]]; then launch=("$CHROME_BIN") elif command -v chromium > /dev/null; then launch=("$(command -v chromium)") elif command -v google-chrome-stable > /dev/null; then launch=("$(command -v google-chrome-stable)") elif command -v google-chrome > /dev/null; then launch=("$(command -v google-chrome)") elif command -v nix > /dev/null; then launch=(nix run --accept-flake-config nixpkgs#chromium --) else echo "dev-chrome: no Chrome found. Set CHROME_BIN, or use nixpkgs#chromium via 'nix develop .#perf'." >&2 exit 1 fi # --no-first-run only suppresses the first-run wizard; it does not skip anything # the app cares about. The debug port is the reason this script exists. exec "${launch[@]}" \ --remote-debugging-port="${port}" \ --user-data-dir="${profile}" \ --no-first-run \ ${HEADLESS:+--headless=new} \ "$@" \ "${url}"