From d23111d9e50584d3566debbbcc13447bf8da8bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hermann=20R=C3=B6sch?= Date: Sun, 15 Nov 2026 15:31:33 -0500 Subject: [PATCH 1/2] fix(setup): MCP servers launch with only their own key from fnox, not Doppler Setup now registers each MCP server through bin/mcp-fnox-inject when fnox can decrypt that server's key, and through mcp-env-inject with the env file otherwise; Doppler no longer launches any server. The helper asks fnox for one key, from HOME (a repository's fnox.toml is never read), exports it alone and execs the server: `fnox exec` would hand every server every secret fnox holds. A key fnox cannot decrypt stops the server with a message, and exec keeps one process, so Claude's signals reach the server. The backend is decided per server, by its own key, so a machine with Brave in fnox and Tavily only in the env file gets each right. opencode's entries follow. @brave/brave-search-mcp-server is pinned to 2.1.4, as tavily was. mcp-key-rotate and the PowerShell setup keep Doppler for now (#85). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014j4P2qfZbMTXBNFRaZdyiY --- bin/mcp-fnox-inject | 36 +++++++++ docs/architecture.md | 2 + docs/configuration.md | 2 +- docs/how-to-add-mcp-server.md | 13 +++- lib/setup-ps/mcp.ps1 | 2 +- lib/setup/mcp.sh | 97 +++++++++-------------- lib/setup/opencode.sh | 30 ++++--- setup.sh | 8 ++ tests/mcp-env-inject-sync.bats | 34 ++++---- tests/mcp-fnox-inject.bats | 66 ++++++++++++++++ tests/mcp-setup.bats | 138 ++++++++++----------------------- tests/opencode-setup.bats | 37 +++------ tests/setup-ps.Tests.ps1 | 2 +- 13 files changed, 246 insertions(+), 221 deletions(-) create mode 100755 bin/mcp-fnox-inject create mode 100644 tests/mcp-fnox-inject.bats diff --git a/bin/mcp-fnox-inject b/bin/mcp-fnox-inject new file mode 100755 index 0000000..0ec8c2d --- /dev/null +++ b/bin/mcp-fnox-inject @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# mcp-fnox-inject +# Path: claude-code-config/bin/mcp-fnox-inject +# +# Starts an MCP server with only its own API key, read from fnox. +# +# Usage (in ~/.claude.json mcpServers): +# "command": "mcp-fnox-inject", +# "args": ["BRAVE_API_KEY", "npx", "-y", "@brave/brave-search-mcp-server@2.1.4"] +# +# `fnox exec` would hand the server every secret fnox holds; this passes one. +# fnox runs from HOME, so a fnox.toml in the repository a session opened is +# never read. A key fnox cannot decrypt stops the server with a message rather +# than starting it without one. exec keeps one process, so Claude's signals +# reach the server and nothing is left behind. +set -euo pipefail + +readonly var="${1:?usage: mcp-fnox-inject VAR command [args...]}" +shift +if [[ ! "${var}" =~ ^[A-Z_][A-Z0-9_]*$ ]]; then + echo "mcp-fnox-inject: '${var}' is not a variable name" >&2 + exit 2 +fi +if (($# == 0)); then + echo "mcp-fnox-inject: no server command after ${var}" >&2 + exit 2 +fi + +# A shim path survives fnox upgrades, for launchers whose PATH lacks mise's shims. +fnox_bin="${MCP_FNOX_BIN:-$(command -v fnox || echo "${HOME}/.local/share/mise/shims/fnox")}" +if ! value="$(cd "${HOME}" && "${fnox_bin}" get "${var}" 2>/dev/null)" || [[ -z "${value}" ]]; then + echo "mcp-fnox-inject: fnox could not decrypt ${var}; not starting the server" >&2 + exit 1 +fi +export "${var}=${value}" +exec "$@" diff --git a/docs/architecture.md b/docs/architecture.md index 3890232..a6ec152 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -162,6 +162,8 @@ The system auto-detects the secrets backend: 1. **Doppler** (enterprise): if the Doppler CLI is installed and configured for the project, uses Doppler secrets management. 2. **Envfile** (default): reads/writes API keys from `~/.claude/mcp-keys.env`. After rotation, syncs the active key to the envfile. +Launching the servers is separate from rotating keys: setup registers each MCP server through `mcp-fnox-inject`, which gives it only its own key from fnox (read from HOME), when fnox can decrypt that key, and through `mcp-env-inject` with the envfile otherwise. Doppler no longer launches any server. + Key rotation is atomic: find the current key's index in the pool, advance to the next, and write the new active key. Quota checking calls the provider's API (Brave: response headers, Tavily: `/usage` endpoint) with a 5-minute cache to avoid redundant API calls. ## The setup system diff --git a/docs/configuration.md b/docs/configuration.md index d985e81..57fec68 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -180,7 +180,7 @@ files another tool owns, the second for that profile alone. `claude-as` shares | Server | Package | Environment Variable | |--------|---------|---------------------| -| `brave-search` | `@brave/brave-search-mcp-server` | `BRAVE_API_KEY` | +| `brave-search` | `@brave/brave-search-mcp-server@2.1.4` | `BRAVE_API_KEY` | | `tavily` | `tavily-mcp@0.2.17` | `TAVILY_API_KEY` | Both use `${VAR}` syntax for runtime environment variable expansion. diff --git a/docs/how-to-add-mcp-server.md b/docs/how-to-add-mcp-server.md index 401893b..c17c3d6 100644 --- a/docs/how-to-add-mcp-server.md +++ b/docs/how-to-add-mcp-server.md @@ -33,15 +33,20 @@ Add the key to `~/.claude/mcp-keys.env`: echo 'MY_API_KEY=your-key-here' >> ~/.claude/mcp-keys.env ``` -### Doppler backend (enterprise) +### fnox backend (preferred) -If using Doppler for secrets management: +If [fnox](https://github.com/jdx/fnox) can decrypt a server's key, setup registers that server as +`mcp-fnox-inject npx -y `. The helper reads only that key, from HOME (so a +repository's `fnox.toml` is never read), exports it alone and `exec`s the server. `fnox exec` +would hand the server every secret fnox holds. A key fnox cannot decrypt stops the server with +a message. Add the key from the directory that holds your fnox config: ```bash -doppler secrets set MY_API_KEY --project claude-code-config --config dev +printf %s "$MY_API_KEY" | fnox set MY_API_KEY --provider age ``` -The setup script auto-detects which backend to use. +Setup decides per server: fnox when it can decrypt that server's key, the env file otherwise. +The choice is made at setup time; if fnox later cannot decrypt the key, re-run setup. ## 3. Verify the server diff --git a/lib/setup-ps/mcp.ps1 b/lib/setup-ps/mcp.ps1 index 81dd5c7..e22faf7 100644 --- a/lib/setup-ps/mcp.ps1 +++ b/lib/setup-ps/mcp.ps1 @@ -12,7 +12,7 @@ $script:McpServers = @{ label = "brave-search" desc = "Web, image, video, news, local search (1,000/mo free)" env_var = "BRAVE_API_KEY" - package = "@brave/brave-search-mcp-server" + package = "@brave/brave-search-mcp-server@2.1.4" signup_url = "https://api-dashboard.search.brave.com/" free_limit = "1,000 searches/month (`$5 free credits)" } diff --git a/lib/setup/mcp.sh b/lib/setup/mcp.sh index 0d28950..1a0963b 100644 --- a/lib/setup/mcp.sh +++ b/lib/setup/mcp.sh @@ -20,7 +20,7 @@ mcp_get() { brave-search:label) echo "brave-search" ;; brave-search:desc) echo "Web, image, video, news, local search (1,000/mo free)" ;; brave-search:env_var) echo "BRAVE_API_KEY" ;; - brave-search:package) echo "@brave/brave-search-mcp-server" ;; + brave-search:package) echo "@brave/brave-search-mcp-server@2.1.4" ;; brave-search:signup_url) echo "https://api-dashboard.search.brave.com/" ;; brave-search:free_limit) echo "1,000 searches/month (\$5 free credits)" ;; tavily:label) echo "tavily" ;; @@ -34,27 +34,20 @@ mcp_get() { } # --- MCP Backend Detection --- -# Determines whether to use Doppler wrapper or mcp-env-inject wrapper. -# Returns: "doppler" or "envfile" +# Per server: fnox when it can decrypt that server's key, else the env file. +# Usage: detect_mcp_backend [key] (key defaults to brave-search) +# Returns: "fnox" or "envfile" -readonly DOPPLER_PROJECT="${MCP_DOPPLER_PROJECT:-claude-code-config}" -readonly DOPPLER_CONFIG="${MCP_DOPPLER_CONFIG:-dev}" readonly MCP_KEYS_ENV_FILE="${MCP_KEYS_ENV_FILE:-${CLAUDE_CONFIG_DIR:-${HOME}/.claude}/mcp-keys.env}" detect_mcp_backend() { - # Tier 1: Doppler CLI available and project accessible - if command -v doppler &>/dev/null; then - # Test that at least one MCP key is accessible via Doppler - local test_var - test_var="$(mcp_get "brave-search" env_var)" - if doppler secrets get "${test_var}" --plain \ - -p "${DOPPLER_PROJECT}" -c "${DOPPLER_CONFIG}" &>/dev/null; then - echo "doppler" - return - fi + local key="${1:-brave-search}" env_var + env_var="$(mcp_get "${key}" env_var)" + # From HOME, so a repository's fnox.toml is never read. + if command -v fnox &>/dev/null && (cd "${HOME}" && fnox get "${env_var}") &>/dev/null; then + echo "fnox" + return fi - - # Tier 2: Fall back to env file wrapper echo "envfile" } @@ -70,18 +63,15 @@ configure_mcp_servers() { return fi - local backend - backend="$(detect_mcp_backend)" - echo " MCP backend: ${backend}" - echo "" - - local key + local key backend any_envfile=0 for key in "${INSTALL_MCP_SERVERS[@]}"; do + backend="$(detect_mcp_backend "${key}")" + [[ "${backend}" == "envfile" ]] && any_envfile=1 _configure_single_mcp "${key}" "${backend}" done - # For envfile backend: create the keys env file - if [[ "${backend}" == "envfile" ]]; then + # For servers on the envfile backend: create the keys env file + if ((any_envfile)); then _create_mcp_keys_env fi } @@ -96,13 +86,14 @@ _build_mcp_json() { local package package="$(mcp_get "${key}" package)" - if [[ "${backend}" == "doppler" ]]; then - # Doppler wrapper: doppler run -p PROJECT -c CONFIG -- npx -y + if [[ "${backend}" == "fnox" ]]; then + # mcp-fnox-inject passes the server only its own key, read from fnox in HOME. + local env_var + env_var="$(mcp_get "${key}" env_var)" jq -n -c \ - --arg project "${DOPPLER_PROJECT}" \ - --arg config "${DOPPLER_CONFIG}" \ + --arg env_var "${env_var}" \ --arg package "${package}" \ - '{type:"stdio",command:"doppler",args:["run","-p",$project,"-c",$config,"--","npx","-y",$package]}' + '{type:"stdio",command:"mcp-fnox-inject",args:[$env_var,"npx","-y",$package]}' else # Env file wrapper: mcp-env-inject npx -y jq -n -c \ @@ -200,33 +191,21 @@ _create_mcp_keys_env() { } check_mcp_env_vars() { - local backend - backend="$(detect_mcp_backend)" - - if [[ "${backend}" == "doppler" ]]; then - echo " ✓ Doppler backend active — keys injected at MCP server launch" - return - fi - - # For envfile backend: check the env file exists and has keys - if [[ -f "${MCP_KEYS_ENV_FILE}" ]]; then - local key - for key in "${INSTALL_MCP_SERVERS[@]}"; do - local env_var - env_var="$(mcp_get "${key}" env_var)" - if grep -q "^${env_var}=" "${MCP_KEYS_ENV_FILE}" 2>/dev/null; then - echo " ✓ ${env_var} found in ${MCP_KEYS_ENV_FILE}" - else - local signup_url free_limit - signup_url="$(mcp_get "${key}" signup_url)" - free_limit="$(mcp_get "${key}" free_limit)" - echo " ⚠ ${env_var} missing from ${MCP_KEYS_ENV_FILE}" - echo " echo '${env_var}=YOUR_KEY' >> ${MCP_KEYS_ENV_FILE}" - echo " Get a free API key (${free_limit}): ${signup_url}" - fi - done - else - echo " ⚠ ${MCP_KEYS_ENV_FILE} not found." - echo " Re-run setup.sh or create it manually with your MCP API keys." - fi + local key env_var backend + for key in "${INSTALL_MCP_SERVERS[@]}"; do + env_var="$(mcp_get "${key}" env_var)" + backend="$(detect_mcp_backend "${key}")" + if [[ "${backend}" == "fnox" ]]; then + echo " ✓ ${env_var} in fnox — injected alone at ${key} launch" + elif [[ -f "${MCP_KEYS_ENV_FILE}" ]] && grep -q "^${env_var}=" "${MCP_KEYS_ENV_FILE}" 2>/dev/null; then + echo " ✓ ${env_var} found in ${MCP_KEYS_ENV_FILE}" + else + local signup_url free_limit + signup_url="$(mcp_get "${key}" signup_url)" + free_limit="$(mcp_get "${key}" free_limit)" + echo " ⚠ ${env_var} missing from fnox and ${MCP_KEYS_ENV_FILE}" + echo " echo '${env_var}=YOUR_KEY' >> ${MCP_KEYS_ENV_FILE}" + echo " Get a free API key (${free_limit}): ${signup_url}" + fi + done } diff --git a/lib/setup/opencode.sh b/lib/setup/opencode.sh index d064241..0b2c1b8 100644 --- a/lib/setup/opencode.sh +++ b/lib/setup/opencode.sh @@ -238,21 +238,17 @@ with open(dest_path, "w", encoding="utf-8") as fh: PYTHON_TRANSLATE } -# backend = "doppler" → `doppler run -p PROJ -c CONF -- npx -y PKG` +# backend = "fnox" → `mcp-fnox-inject VAR npx -y PKG` (only that server's key) # backend = "envfile" → `mcp-env-inject npx -y PKG` # Wrong wrapper = MCP -32000 "Connection closed" (no keys reach server). _opencode_build_mcp_entry() { - local pkg="$1" backend="$2" - if [[ "${backend}" == "doppler" ]]; then - jq -n -c \ - --arg project "${DOPPLER_PROJECT}" \ - --arg config "${DOPPLER_CONFIG}" \ - --arg pkg "${pkg}" \ - '{ - type: "local", - command: ["doppler", "run", "-p", $project, "-c", $config, "--", "npx", "-y", $pkg], - enabled: true - }' + local pkg="$1" backend="$2" env_var="${3:-}" + if [[ "${backend}" == "fnox" ]]; then + jq -n -c --arg pkg "${pkg}" --arg env_var "${env_var}" '{ + type: "local", + command: ["mcp-fnox-inject", $env_var, "npx", "-y", $pkg], + enabled: true + }' else jq -n -c --arg pkg "${pkg}" '{ type: "local", @@ -346,9 +342,7 @@ _opencode_generate_json() { OPENCODE_JSON="$(_opencode_resolve_config_path)" - local backend - backend="$(detect_mcp_backend)" - + local backend backends="" local mcp_section="{}" local key for key in "${INSTALL_MCP_SERVERS[@]}"; do @@ -357,8 +351,10 @@ _opencode_generate_json() { if [[ -z "${pkg}" ]]; then continue fi + backend="$(detect_mcp_backend "${key}")" + backends="${backends:+${backends}, }${key}: ${backend}" local entry - entry="$(_opencode_build_mcp_entry "${pkg}" "${backend}")" + entry="$(_opencode_build_mcp_entry "${pkg}" "${backend}" "$(mcp_get "${key}" env_var)")" mcp_section="$(printf '%s' "${mcp_section}" | jq -c \ --arg name "${key}" \ --argjson entry "${entry}" \ @@ -387,7 +383,7 @@ _opencode_generate_json() { '. + {"$schema": "https://opencode.ai/config.json"} + {mcp: ((.mcp // {}) + $mcp)}')" printf '%s\n' "${merged}" | jq '.' >"${OPENCODE_JSON}" - echo " ✓ ${OPENCODE_JSON} (${backend} backend)" + echo " ✓ ${OPENCODE_JSON} (${backends:-no servers})" } _opencode_link_agents_md() { diff --git a/setup.sh b/setup.sh index 6e3768b..d41cfb5 100755 --- a/setup.sh +++ b/setup.sh @@ -199,6 +199,14 @@ main() { echo " Run: chmod +x ${REPO_DIR}/bin/mcp-env-inject" fi + if [[ -x "${REPO_DIR}/bin/mcp-fnox-inject" ]]; then + ln -sf "${REPO_DIR}/bin/mcp-fnox-inject" "${bin_dir}/mcp-fnox-inject" + echo " ✓ ~/.local/bin/mcp-fnox-inject -> ${REPO_DIR}/bin/mcp-fnox-inject" + else + echo " ⚠ bin/mcp-fnox-inject not found or not executable (skipping)" + echo " Run: chmod +x ${REPO_DIR}/bin/mcp-fnox-inject" + fi + if [[ -x "${REPO_DIR}/bin/claude-permissions-audit" ]]; then ln -sf "${REPO_DIR}/bin/claude-permissions-audit" "${bin_dir}/claude-permissions-audit" echo " ✓ ~/.local/bin/claude-permissions-audit -> ${REPO_DIR}/bin/claude-permissions-audit" diff --git a/tests/mcp-env-inject-sync.bats b/tests/mcp-env-inject-sync.bats index 595b639..1eb3795 100644 --- a/tests/mcp-env-inject-sync.bats +++ b/tests/mcp-env-inject-sync.bats @@ -273,24 +273,21 @@ EOF # UNIT TESTS: detect_mcp_backend (from lib/setup/mcp.sh) # ========================================================================== -@test "detect_mcp_backend: returns doppler when doppler is available" { - # Mock doppler is already in PATH and BRAVE_API_KEY is seeded - doppler_seed "BRAVE_API_KEY" "${KEY_A}" - - run bash -c ' +@test "detect_mcp_backend: returns fnox when fnox can decrypt the key" { + local bin="${BATS_TEST_TMPDIR}/fnox-bin" + mkdir -p "${bin}" + printf '#!/bin/sh\n[ "$1" = get ] && [ "$2" = BRAVE_API_KEY ] && echo k\n' > "${bin}/fnox" + chmod +x "${bin}/fnox" + run env PATH="${bin}:/usr/bin:/bin" bash -c ' source "'"${MCP_SH}"'" detect_mcp_backend ' [ "$status" -eq 0 ] - [ "$output" = "doppler" ] + [ "$output" = "fnox" ] } -@test "detect_mcp_backend: returns envfile when doppler is not available" { - # Build a minimal PATH with only system dirs, excluding mock doppler AND real doppler - # We need basic commands (bash, grep, etc.) but no doppler anywhere - local minimal_path="/usr/bin:/bin:/usr/sbin:/sbin" - - run env PATH="${minimal_path}" bash -c ' +@test "detect_mcp_backend: returns envfile when fnox is not available" { + run env PATH="/usr/bin:/bin:/usr/sbin:/sbin" bash -c ' source "'"${MCP_SH}"'" detect_mcp_backend ' @@ -298,9 +295,12 @@ EOF [ "$output" = "envfile" ] } -@test "detect_mcp_backend: returns envfile when doppler project is inaccessible" { - # Mock doppler is in PATH but no secrets are seeded (secrets get will fail) - run bash -c ' +@test "detect_mcp_backend: returns envfile when fnox cannot decrypt the key" { + local bin="${BATS_TEST_TMPDIR}/fnox-bin" + mkdir -p "${bin}" + printf '#!/bin/sh\nexit 1\n' > "${bin}/fnox" + chmod +x "${bin}/fnox" + run env PATH="${bin}:/usr/bin:/bin" bash -c ' source "'"${MCP_SH}"'" detect_mcp_backend ' @@ -464,8 +464,8 @@ REPO_ROOT="$BATS_TEST_DIRNAME/.." grep -q "detect_mcp_backend" "${REPO_ROOT}/lib/setup/mcp.sh" } -@test "repo: lib/setup/mcp.sh references doppler run wrapper" { - grep -q "doppler run" "${REPO_ROOT}/lib/setup/mcp.sh" +@test "repo: lib/setup/mcp.sh references mcp-fnox-inject wrapper" { + grep -q "mcp-fnox-inject" "${REPO_ROOT}/lib/setup/mcp.sh" } @test "repo: lib/setup/mcp.sh references mcp-env-inject wrapper" { diff --git a/tests/mcp-fnox-inject.bats b/tests/mcp-fnox-inject.bats new file mode 100644 index 0000000..c6e4717 --- /dev/null +++ b/tests/mcp-fnox-inject.bats @@ -0,0 +1,66 @@ +#!/usr/bin/env bats +# mcp-fnox-inject.bats +# Path: tests/mcp-fnox-inject.bats +# +# bin/mcp-fnox-inject starts an MCP server with only its own key, read from +# fnox in HOME. A stub fnox records how it was called. + +INJECT="$BATS_TEST_DIRNAME/../bin/mcp-fnox-inject" + +setup() { + export STUB="${BATS_TEST_TMPDIR}/fnox" + export HOME="${BATS_TEST_TMPDIR}/home" + mkdir -p "${HOME}" "${BATS_TEST_TMPDIR}/repo" + cat >"${STUB}" <<'STUB' +#!/bin/sh +printf '%s|%s\n' "$(pwd)" "$*" >>"$(dirname "$0")/calls" +[ "$1" = get ] || exit 3 +case "$2" in + BRAVE_API_KEY) echo brave-secret ;; + EMPTY_KEY) echo "" ;; + *) exit 1 ;; +esac +STUB + chmod +x "${STUB}" + export MCP_FNOX_BIN="${STUB}" +} + +@test "the server gets its own key and no other fnox secret" { + export TAVILY_API_KEY=already-here + run bash -c 'cd "$1" && "$2" BRAVE_API_KEY sh -c "printf %s \"\$BRAVE_API_KEY\""' _ "${BATS_TEST_TMPDIR}/repo" "${INJECT}" + [ "$status" -eq 0 ] + [ "$output" = "brave-secret" ] + # Asked fnox for one key, from HOME, never `exec` (which exports every secret). + [ "$(cat "${BATS_TEST_TMPDIR}/calls")" = "${HOME}|get BRAVE_API_KEY" ] +} + +@test "a key fnox cannot decrypt stops the server with a message" { + run "${INJECT}" TAVILY_API_KEY sh -c 'echo started' + [ "$status" -eq 1 ] + [[ "$output" == *"could not decrypt TAVILY_API_KEY"* ]] + [[ "$output" != *started* ]] +} + +@test "an empty value counts as missing" { + run "${INJECT}" EMPTY_KEY sh -c 'echo started' + [ "$status" -eq 1 ] + [[ "$output" != *started* ]] +} + +@test "the server replaces the helper, so signals reach it and nothing is left behind" { + "${INJECT}" BRAVE_API_KEY sh -c 'echo $$ >"$0"; exec sleep 30' "${BATS_TEST_TMPDIR}/pid" & + local helper=$! + for _ in $(seq 50); do [ -s "${BATS_TEST_TMPDIR}/pid" ] && break; sleep 0.1; done + [ "$(cat "${BATS_TEST_TMPDIR}/pid")" = "${helper}" ] + kill "${helper}" + wait "${helper}" 2>/dev/null || true + run kill -0 "${helper}" + [ "$status" -ne 0 ] +} + +@test "a bad variable name or a missing command is refused" { + run "${INJECT}" 'bad-name' true + [ "$status" -eq 2 ] + run "${INJECT}" BRAVE_API_KEY + [ "$status" -eq 2 ] +} diff --git a/tests/mcp-setup.bats b/tests/mcp-setup.bats index 218433f..90554c9 100644 --- a/tests/mcp-setup.bats +++ b/tests/mcp-setup.bats @@ -39,60 +39,35 @@ teardown() { # bats file_tags=integration -@test "_build_mcp_json: doppler backend produces correct JSON for brave-search" { +@test "_build_mcp_json: fnox backend passes the server only its own key" { local result - result="$(_build_mcp_json "brave-search" "doppler")" - - # Validate it's valid JSON - echo "${result}" | python3 -m json.tool > /dev/null - - # Check structure - local cmd args_len - cmd="$(echo "${result}" | python3 -c "import sys,json; print(json.load(sys.stdin)['command'])")" - [ "$cmd" = "doppler" ] - - local type_val - type_val="$(echo "${result}" | python3 -c "import sys,json; print(json.load(sys.stdin)['type'])")" - [ "$type_val" = "stdio" ] + result="$(_build_mcp_json "brave-search" "fnox")" + echo "${result}" | python3 -c ' +import json, sys +d = json.load(sys.stdin) +assert d["type"] == "stdio" and d["command"] == "mcp-fnox-inject", d +assert d["args"] == ["BRAVE_API_KEY", "npx", "-y", "@brave/brave-search-mcp-server@2.1.4"], d["args"] +' } -@test "_build_mcp_json: doppler backend includes -p and -c flags in args" { +@test "_build_mcp_json: fnox backend names tavily's own key" { local result - result="$(_build_mcp_json "brave-search" "doppler")" - - # Extract args as a comma-separated string for easy matching - local args - args="$(echo "${result}" | python3 -c "import sys,json; print(','.join(json.load(sys.stdin)['args']))")" - - [[ "$args" == *"-p"* ]] - [[ "$args" == *"-c"* ]] - [[ "$args" == *"claude-code-config"* ]] - [[ "$args" == *"dev"* ]] - [[ "$args" == *"--"* ]] - [[ "$args" == *"npx"* ]] - [[ "$args" == *"-y"* ]] - [[ "$args" == *"@brave/brave-search-mcp-server"* ]] + result="$(_build_mcp_json "tavily" "fnox")" + echo "${result}" | python3 -c ' +import json, sys +a = json.load(sys.stdin)["args"] +assert a == ["TAVILY_API_KEY", "npx", "-y", "tavily-mcp@0.2.17"], a +' } -@test "_build_mcp_json: doppler backend args are in correct order" { - local result - result="$(_build_mcp_json "brave-search" "doppler")" - - # Verify exact args array - local args_json - args_json="$(echo "${result}" | python3 -c "import sys,json; print(json.dumps(json.load(sys.stdin)['args']))")" - [ "$args_json" = '["run", "-p", "claude-code-config", "-c", "dev", "--", "npx", "-y", "@brave/brave-search-mcp-server"]' ] -} - -@test "_build_mcp_json: doppler backend produces correct JSON for tavily" { - local result - result="$(_build_mcp_json "tavily" "doppler")" - - echo "${result}" | python3 -m json.tool > /dev/null - - local args_json - args_json="$(echo "${result}" | python3 -c "import sys,json; print(json.dumps(json.load(sys.stdin)['args']))")" - [ "$args_json" = '["run", "-p", "claude-code-config", "-c", "dev", "--", "npx", "-y", "tavily-mcp@0.2.17"]' ] +@test "detect_mcp_backend: decides per server, by that server's own key" { + mkdir -p "${TEST_TMPDIR}/bin" + printf '#!/bin/sh\n[ "$1" = get ] && [ "$2" = BRAVE_API_KEY ] && echo k\n' > "${TEST_TMPDIR}/bin/fnox" + chmod +x "${TEST_TMPDIR}/bin/fnox" + [ "$(PATH="${TEST_TMPDIR}/bin:${PATH}" detect_mcp_backend brave-search)" = "fnox" ] + [ "$(PATH="${TEST_TMPDIR}/bin:${PATH}" detect_mcp_backend tavily)" = "envfile" ] + printf '#!/bin/sh\nexit 1\n' > "${TEST_TMPDIR}/bin/fnox" + [ "$(PATH="${TEST_TMPDIR}/bin:${PATH}" detect_mcp_backend brave-search)" = "envfile" ] } @test "_build_mcp_json: envfile backend produces correct JSON for brave-search" { @@ -107,7 +82,7 @@ teardown() { local args_json args_json="$(echo "${result}" | python3 -c "import sys,json; print(json.dumps(json.load(sys.stdin)['args']))")" - [ "$args_json" = '["npx", "-y", "@brave/brave-search-mcp-server"]' ] + [ "$args_json" = '["npx", "-y", "@brave/brave-search-mcp-server@2.1.4"]' ] } @test "_build_mcp_json: envfile backend produces correct JSON for tavily" { @@ -130,32 +105,6 @@ teardown() { [ "$type_val" = "stdio" ] } -# ========================================================================== -# UNIT TESTS: _build_mcp_json — custom Doppler project/config -# ========================================================================== - -@test "_build_mcp_json: respects MCP_DOPPLER_PROJECT override" { - # DOPPLER_PROJECT is readonly in the sourced script, so we override - # by sourcing again with the env var set in a subshell - local result - result="$(MCP_DOPPLER_PROJECT=my-custom-proj bash -c ' - source "'"${MCP_SH}"'" - _build_mcp_json "brave-search" "doppler" - ')" - - [[ "$result" == *"my-custom-proj"* ]] -} - -@test "_build_mcp_json: respects MCP_DOPPLER_CONFIG override" { - local result - result="$(MCP_DOPPLER_CONFIG=staging bash -c ' - source "'"${MCP_SH}"'" - _build_mcp_json "brave-search" "doppler" - ')" - - [[ "$result" == *"staging"* ]] -} - # ========================================================================== # UNIT TESTS: _build_mcp_json — output is always valid JSON # ========================================================================== @@ -163,7 +112,7 @@ teardown() { @test "_build_mcp_json: all server+backend combos produce valid JSON" { local key backend for key in brave-search tavily; do - for backend in doppler envfile; do + for backend in fnox envfile; do local result result="$(_build_mcp_json "${key}" "${backend}")" echo "${result}" | python3 -m json.tool > /dev/null 2>&1 @@ -173,7 +122,7 @@ teardown() { @test "_build_mcp_json: JSON has exactly 3 top-level keys" { local result - result="$(_build_mcp_json "brave-search" "doppler")" + result="$(_build_mcp_json "brave-search" "fnox")" local key_count key_count="$(echo "${result}" | python3 -c "import sys,json; print(len(json.load(sys.stdin)))")" @@ -187,7 +136,7 @@ teardown() { @test "mcp_get: returns correct package for brave-search" { local pkg pkg="$(mcp_get "brave-search" "package")" - [ "$pkg" = "@brave/brave-search-mcp-server" ] + [ "$pkg" = "@brave/brave-search-mcp-server@2.1.4" ] } @test "mcp_get: returns correct package for tavily" { @@ -217,7 +166,7 @@ teardown() { # INTEGRATION TESTS: _configure_single_mcp with mocked claude CLI # ========================================================================== -@test "integration: _configure_single_mcp calls claude mcp add-json (doppler)" { +@test "integration: _configure_single_mcp calls claude mcp add-json (fnox)" { # Create a mock claude that captures its arguments local mock_log="${TEST_TMPDIR}/claude-calls.log" cat > "${TEST_TMPDIR}/claude" <<'MOCK' @@ -228,7 +177,7 @@ MOCK chmod +x "${TEST_TMPDIR}/claude" export PATH="${TEST_TMPDIR}:${PATH}" - _configure_single_mcp "brave-search" "doppler" + _configure_single_mcp "brave-search" "fnox" # Verify claude was called with add-json, not add local call @@ -268,7 +217,7 @@ MOCK chmod +x "${TEST_TMPDIR}/claude" export PATH="${TEST_TMPDIR}:${PATH}" - run _configure_single_mcp "brave-search" "doppler" + run _configure_single_mcp "brave-search" "fnox" [ "$status" -eq 0 ] [[ "$output" == *"✓"* ]] } @@ -282,7 +231,7 @@ MOCK chmod +x "${TEST_TMPDIR}/claude" export PATH="${TEST_TMPDIR}:${PATH}" - run _configure_single_mcp "brave-search" "doppler" + run _configure_single_mcp "brave-search" "fnox" [[ "$output" == *"⚠"* ]] [[ "$output" == *"Manual:"* ]] } @@ -302,7 +251,7 @@ MOCK chmod +x "${TEST_TMPDIR}/claude" export PATH="${TEST_TMPDIR}:${PATH}" - _configure_single_mcp "brave-search" "doppler" + _configure_single_mcp "brave-search" "fnox" # Should have two calls: remove then add-json local call_count @@ -333,7 +282,7 @@ MOCK chmod +x "${TEST_TMPDIR}/claude" export PATH="${TEST_TMPDIR}:${PATH}" - _configure_single_mcp "brave-search" "doppler" + _configure_single_mcp "brave-search" "fnox" # Should have only one call: add-json (no remove) local call_count @@ -360,7 +309,7 @@ MOCK chmod +x "${TEST_TMPDIR}/claude" export PATH="${TEST_TMPDIR}:${PATH}" - _configure_single_mcp "brave-search" "doppler" + _configure_single_mcp "brave-search" "fnox" # Should have only one call: add-json (no remove) local call_count @@ -422,21 +371,19 @@ MOCK # REGRESSION TEST: the original bug — short flags after -- are not consumed # ========================================================================== -@test "regression: JSON args contain -p and -c without CLI parser conflict" { +@test "regression: a short flag in the JSON args is not taken by the CLI parser" { # This is the core regression test. The old `claude mcp add` approach # failed because `-p` and `-c` after `--` were consumed by the CLI parser. # With `claude mcp add-json`, flags are inside a JSON string argument, # so the CLI parser never sees them. local result - result="$(_build_mcp_json "brave-search" "doppler")" + result="$(_build_mcp_json "brave-search" "fnox")" - # The JSON must contain -p and -c as args elements - local has_p has_c - has_p="$(echo "${result}" | python3 -c "import sys,json; print('-p' in json.load(sys.stdin)['args'])")" - has_c="$(echo "${result}" | python3 -c "import sys,json; print('-c' in json.load(sys.stdin)['args'])")" - [ "$has_p" = "True" ] - [ "$has_c" = "True" ] + # The JSON must contain npx's -y as an args element + local has_y + has_y="$(echo "${result}" | python3 -c "import sys,json; print('-y' in json.load(sys.stdin)['args'])")" + [ "$has_y" = "True" ] # But they're inside a JSON string, not as CLI flags # The mock claude should receive them as part of one JSON argument @@ -452,7 +399,7 @@ MOCK chmod +x "${TEST_TMPDIR}/claude" export PATH="${TEST_TMPDIR}:${PATH}" - _configure_single_mcp "brave-search" "doppler" + _configure_single_mcp "brave-search" "fnox" # -p and -c should NOT appear as standalone arguments # They should only appear inside the JSON string argument @@ -465,6 +412,5 @@ MOCK # The JSON argument should contain them local json_arg json_arg="$(grep 'ARG:{' "${mock_log}")" - [[ "$json_arg" == *'"-p"'* ]] - [[ "$json_arg" == *'"-c"'* ]] + [[ "$json_arg" == *'"-y"'* ]] } diff --git a/tests/opencode-setup.bats b/tests/opencode-setup.bats index 7e1f8d6..b8fae33 100644 --- a/tests/opencode-setup.bats +++ b/tests/opencode-setup.bats @@ -507,29 +507,15 @@ EOF [ "$first" = "$second" ] } -@test "_opencode_build_mcp_entry: doppler backend emits doppler run command" { +@test "_opencode_build_mcp_entry: fnox backend passes the server only its own key" { source "$OPENCODE_SH" local entry - entry="$(_opencode_build_mcp_entry "@brave/brave-search-mcp-server" "doppler")" - local cmd0 cmd1 cmd2 cmd3 cmd4 cmd5 cmd6 cmd7 cmd8 - cmd0="$(echo "${entry}" | jq -r '.command[0]')" - cmd1="$(echo "${entry}" | jq -r '.command[1]')" - cmd2="$(echo "${entry}" | jq -r '.command[2]')" - cmd3="$(echo "${entry}" | jq -r '.command[3]')" - cmd4="$(echo "${entry}" | jq -r '.command[4]')" - cmd5="$(echo "${entry}" | jq -r '.command[5]')" - cmd6="$(echo "${entry}" | jq -r '.command[6]')" - cmd7="$(echo "${entry}" | jq -r '.command[7]')" - cmd8="$(echo "${entry}" | jq -r '.command[8]')" - [ "$cmd0" = "doppler" ] - [ "$cmd1" = "run" ] - [ "$cmd2" = "-p" ] - [ "$cmd3" = "claude-code-config" ] - [ "$cmd4" = "-c" ] - [ "$cmd5" = "dev" ] - [ "$cmd6" = "--" ] - [ "$cmd7" = "npx" ] - [ "$cmd8" = "-y" ] + entry="$(_opencode_build_mcp_entry "@brave/brave-search-mcp-server@2.1.4" "fnox" "BRAVE_API_KEY")" + echo "${entry}" | python3 -c ' +import json, sys +c = json.load(sys.stdin)["command"] +assert c == ["mcp-fnox-inject", "BRAVE_API_KEY", "npx", "-y", "@brave/brave-search-mcp-server@2.1.4"], c +' } @test "_opencode_build_mcp_entry: envfile backend emits mcp-env-inject command" { @@ -543,7 +529,7 @@ EOF [ "$cmd1" = "npx" ] } -@test "_opencode_generate_json: respects active backend (doppler vs envfile)" { +@test "_opencode_generate_json: respects active backend (fnox vs envfile)" { INSTALL_MCP_SERVERS=("brave-search") mkdir -p "${OPENCODE_CONFIG_DIR_OVERRIDE}" @@ -558,13 +544,14 @@ EOF cmd0="$(jq -r '.mcp."brave-search".command[0]' "${OPENCODE_JSON}")" [ "$cmd0" = "mcp-env-inject" ] - # Now switch to doppler and regenerate - detect_mcp_backend() { echo "doppler"; } + # Now switch to fnox and regenerate + detect_mcp_backend() { echo "fnox"; } export -f detect_mcp_backend _opencode_generate_json cmd0="$(jq -r '.mcp."brave-search".command[0]' "${OPENCODE_JSON}")" - [ "$cmd0" = "doppler" ] + [ "$cmd0" = "mcp-fnox-inject" ] + [ "$(jq -r '.mcp."brave-search".command[1]' "${OPENCODE_JSON}")" = "BRAVE_API_KEY" ] unset -f detect_mcp_backend } diff --git a/tests/setup-ps.Tests.ps1 b/tests/setup-ps.Tests.ps1 index bed1fe3..727ebea 100644 --- a/tests/setup-ps.Tests.ps1 +++ b/tests/setup-ps.Tests.ps1 @@ -432,7 +432,7 @@ Describe "MCP module" { It "brave-search has required fields" { $server = $script:McpServers["brave-search"] $server.env_var | Should -Be "BRAVE_API_KEY" - $server.package | Should -Be "@brave/brave-search-mcp-server" + $server.package | Should -Be "@brave/brave-search-mcp-server@2.1.4" $server.signup_url | Should -Not -BeNullOrEmpty } From 9384bbcd634f92c3982e09d59af3a5dfa03aefd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hermann=20R=C3=B6sch?= Date: Sun, 15 Nov 2026 15:41:40 -0500 Subject: [PATCH 2/2] fix(setup): show fnox's reason when an MCP key cannot be decrypted The helper swallowed fnox's stderr, so a missing age key looked the same as a missing secret. The docs also say that keys already exported in Claude Code's own environment still reach every server. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014j4P2qfZbMTXBNFRaZdyiY --- bin/mcp-fnox-inject | 7 ++++++- docs/how-to-add-mcp-server.md | 3 ++- tests/mcp-fnox-inject.bats | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/bin/mcp-fnox-inject b/bin/mcp-fnox-inject index 0ec8c2d..8ae65b9 100755 --- a/bin/mcp-fnox-inject +++ b/bin/mcp-fnox-inject @@ -28,9 +28,14 @@ fi # A shim path survives fnox upgrades, for launchers whose PATH lacks mise's shims. fnox_bin="${MCP_FNOX_BIN:-$(command -v fnox || echo "${HOME}/.local/share/mise/shims/fnox")}" -if ! value="$(cd "${HOME}" && "${fnox_bin}" get "${var}" 2>/dev/null)" || [[ -z "${value}" ]]; then +# fnox's own reason (a missing age key, say) is shown only when it fails. +err="$(mktemp)" +if ! value="$(cd "${HOME}" && "${fnox_bin}" get "${var}" 2>"${err}")" || [[ -z "${value}" ]]; then + sed 's/^/mcp-fnox-inject: fnox: /' "${err}" >&2 + rm -f "${err}" echo "mcp-fnox-inject: fnox could not decrypt ${var}; not starting the server" >&2 exit 1 fi +rm -f "${err}" export "${var}=${value}" exec "$@" diff --git a/docs/how-to-add-mcp-server.md b/docs/how-to-add-mcp-server.md index c17c3d6..2b60b9d 100644 --- a/docs/how-to-add-mcp-server.md +++ b/docs/how-to-add-mcp-server.md @@ -39,7 +39,8 @@ If [fnox](https://github.com/jdx/fnox) can decrypt a server's key, setup registe `mcp-fnox-inject npx -y `. The helper reads only that key, from HOME (so a repository's `fnox.toml` is never read), exports it alone and `exec`s the server. `fnox exec` would hand the server every secret fnox holds. A key fnox cannot decrypt stops the server with -a message. Add the key from the directory that holds your fnox config: +fnox's reason. The helper only controls what it adds: keys exported in the shell that starts +Claude Code (a mise integration of fnox, say) still reach every server. Add the key from the directory that holds your fnox config: ```bash printf %s "$MY_API_KEY" | fnox set MY_API_KEY --provider age diff --git a/tests/mcp-fnox-inject.bats b/tests/mcp-fnox-inject.bats index c6e4717..a22f527 100644 --- a/tests/mcp-fnox-inject.bats +++ b/tests/mcp-fnox-inject.bats @@ -18,7 +18,7 @@ printf '%s|%s\n' "$(pwd)" "$*" >>"$(dirname "$0")/calls" case "$2" in BRAVE_API_KEY) echo brave-secret ;; EMPTY_KEY) echo "" ;; - *) exit 1 ;; + *) echo "no identity matched" >&2; exit 1 ;; esac STUB chmod +x "${STUB}" @@ -38,6 +38,7 @@ STUB run "${INJECT}" TAVILY_API_KEY sh -c 'echo started' [ "$status" -eq 1 ] [[ "$output" == *"could not decrypt TAVILY_API_KEY"* ]] + [[ "$output" == *"mcp-fnox-inject: fnox: no identity matched"* ]] [[ "$output" != *started* ]] }