fix: v2.0.1 - MCP 写入 ~/.claude.json (Claude Code v2.1+ 正确位置)

settings.json 中的 mcpServers 在 Claude Code v2.1+ 不再被读取。
Phase 5 现在从渲染后的 settings.json 提取 mcpServers 写入 ~/.claude.json,
支持与用户已有配置合并。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
bookworm 2026-04-10 20:16:43 +08:00
parent 7d0e4edd16
commit f262f1bd21

View File

@ -16,7 +16,7 @@ param(
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
# ─── 版本号 (每次更新递增, build.ps1 自动读取) ────── # ─── 版本号 (每次更新递增, build.ps1 自动读取) ──────
$BWVersion = "2.0.0" $BWVersion = "2.0.1"
# ─── B4: 单实例保护 (防止双击两次导致竞态) ───────── # ─── B4: 单实例保护 (防止双击两次导致竞态) ─────────
$mutexCreated = $false $mutexCreated = $false
@ -1089,7 +1089,7 @@ if (Test-Path $templateFile) {
Set-Content $settingsFile -Value $content -Encoding UTF8 Set-Content $settingsFile -Value $content -Encoding UTF8
Log-OK "settings.json 已渲染 (ROOT=$claudeRoot, SHELL=$pwshJsonPath)" Log-OK "settings.json 已渲染 (ROOT=$claudeRoot, SHELL=$pwshJsonPath)"
# settings.local.template.json # settings.local.template.json (向后兼容)
$localTpl = Join-Path $ClaudeDir "settings.local.template.json" $localTpl = Join-Path $ClaudeDir "settings.local.template.json"
$localSet = Join-Path $ClaudeDir "settings.local.json" $localSet = Join-Path $ClaudeDir "settings.local.json"
if (Test-Path $localTpl) { if (Test-Path $localTpl) {
@ -1101,6 +1101,35 @@ if (Test-Path $templateFile) {
Set-Content $localSet -Value $lc -Encoding UTF8 Set-Content $localSet -Value $lc -Encoding UTF8
Log-OK "settings.local.json 已渲染" Log-OK "settings.local.json 已渲染"
} }
# ── ~/.claude.json (Claude Code v2.1+ MCP 服务器配置的正确位置) ──
# Claude Code 不再从 settings.json 读取 mcpServers, 必须写入 ~/.claude.json
$claudeJsonFile = Join-Path $env:USERPROFILE ".claude.json"
try {
$settingsObj = ConvertFrom-Json $content
if ($settingsObj.mcpServers) {
$mcpOnly = @{ mcpServers = $settingsObj.mcpServers }
# 如果已有 .claude.json, 合并 mcpServers (保留用户自定义项)
if (Test-Path $claudeJsonFile) {
try {
$existing = Get-Content $claudeJsonFile -Raw -ErrorAction SilentlyContinue | ConvertFrom-Json
if ($existing.mcpServers) {
# 以模板为基础, 保留用户新增的 server
$merged = @{}
foreach ($prop in $existing.mcpServers.PSObject.Properties) { $merged[$prop.Name] = $prop.Value }
foreach ($prop in $settingsObj.mcpServers.PSObject.Properties) { $merged[$prop.Name] = $prop.Value }
$mcpOnly.mcpServers = [PSCustomObject]$merged
}
} catch { <# 解析失败则覆盖 #> }
}
$mcpJson = $mcpOnly | ConvertTo-Json -Depth 10
Set-Content $claudeJsonFile -Value $mcpJson -Encoding UTF8
$mcpCount = ($settingsObj.mcpServers.PSObject.Properties | Where-Object { $_.Name -notlike '__*' }).Count
Log-OK ".claude.json 已写入 ($mcpCount 个 MCP 服务器)"
}
} catch {
Bw-Log "WARN" ".claude.json 生成失败: $_"
}
} else { } else {
Log-Warn "settings.template.json 不存在, 跳过渲染" Log-Warn "settings.template.json 不存在, 跳过渲染"
} }