Files
brain/scripts/lib/merge-mcp.sh
T
Дмитрий 52584df34e fix(scripts): 5 critical bugs from Phase 9 self-test
Bug 1: merge-mcp.sh fails on Cyrillic brain repo paths (jq fopen UTF-8 issue)
  → use --argjson with $(cat ...) instead of --slurpfile

Bug 2: install.sh stripped only trailing .template, missed middle .template.
  → strip both '.template.' middle and '.template' trailing patterns

Bug 3: manifest.json used brain-repo paths but verify.sh checked target paths
  → restructure manifest.files into {project-mode, user-mode, brain-internal} maps
  → verify.sh now picks map based on detected target mode

Bug 4: make_backup_dir created empty backup dir, no file preservation
  → rsync/cp -r target tree to backup before any modifications

Bug 5: brain's project-files/README.md overwrote consumer's README.md
  → install.sh now skips brain-internal READMEs in project/user copy loops

Phase 9 self-test re-run on c:/tmp/test-consumer-fix: install + verify both PASS.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 01:24:52 +03:00

56 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# Merge brain MCP template into consumer's claude.json or project .mcp.json
# Mode: user (merge into ~/.claude.json:mcpServers) or project (merge into <repo>/.mcp.json:mcpServers)
# Preserves consumer-specific servers; brain-managed servers override on key collision.
#
# Usage: merge-mcp.sh --mode=user|project <target> <brain-mcp.json>
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/common.sh"
mode=""
case "${1:-}" in
--mode=user) mode="user"; shift ;;
--mode=project) mode="project"; shift ;;
*) log_error "First arg must be --mode=user or --mode=project"; exit 1 ;;
esac
target="$1"
brain_mcp="$2"
require_cmd jq || exit 1
[ -f "$brain_mcp" ] || { log_error "Brain MCP file not found: $brain_mcp"; exit 1; }
# If target missing, copy brain template
if [ ! -f "$target" ]; then
log_info "Target $target missing, creating from brain template"
cp "$brain_mcp" "$target"
exit 0
fi
# Backup
backup="${target}.bak.$(date +%s)"
cp "$target" "$backup"
# CORRECTION B (Bug 1 fix): use --argjson with shell-read content instead of
# --slurpfile. jq on Windows MSYS2 cannot open paths with non-ASCII characters
# (Cyrillic etc.) via its C-level fopen, but cat is a shell builtin/coreutil
# that handles UTF-8 paths fine. --argjson takes a pre-parsed JSON string, so
# this works regardless of path encoding.
#
# Merge semantics: target.mcpServers (or {} if missing) UNION brain.mcpServers,
# with brain entries overriding on key collision (laravel-boost stays — brain
# template doesn't define it; magic gets replaced if brain defines it).
brain_content=$(cat "$brain_mcp")
tmp="${target}.tmp"
jq --argjson brain "$brain_content" \
'.mcpServers = ((.mcpServers // {}) + $brain.mcpServers)' \
"$target" > "$tmp"
# Validate
jq empty "$tmp" || { log_error "Merge produced invalid JSON"; rm "$tmp"; exit 1; }
mv "$tmp" "$target"
log_info "MCP ($mode mode) merged (backup: $backup)"