From f262f1bd21d7022e2c38eca135077c2b9e9a8940 Mon Sep 17 00:00:00 2001 From: bookworm Date: Fri, 10 Apr 2026 20:16:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20v2.0.1=20-=20MCP=20=E5=86=99=E5=85=A5=20?= =?UTF-8?q?~/.claude.json=20(Claude=20Code=20v2.1+=20=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit settings.json 中的 mcpServers 在 Claude Code v2.1+ 不再被读取。 Phase 5 现在从渲染后的 settings.json 提取 mcpServers 写入 ~/.claude.json, 支持与用户已有配置合并。 Co-Authored-By: Claude Opus 4.6 (1M context) --- auto-setup.ps1 | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/auto-setup.ps1 b/auto-setup.ps1 index 14415d8..66e83c1 100644 --- a/auto-setup.ps1 +++ b/auto-setup.ps1 @@ -16,7 +16,7 @@ param( $ErrorActionPreference = "Stop" # ─── 版本号 (每次更新递增, build.ps1 自动读取) ────── -$BWVersion = "2.0.0" +$BWVersion = "2.0.1" # ─── B4: 单实例保护 (防止双击两次导致竞态) ───────── $mutexCreated = $false @@ -1089,7 +1089,7 @@ if (Test-Path $templateFile) { Set-Content $settingsFile -Value $content -Encoding UTF8 Log-OK "settings.json 已渲染 (ROOT=$claudeRoot, SHELL=$pwshJsonPath)" - # settings.local.template.json + # settings.local.template.json (向后兼容) $localTpl = Join-Path $ClaudeDir "settings.local.template.json" $localSet = Join-Path $ClaudeDir "settings.local.json" if (Test-Path $localTpl) { @@ -1101,6 +1101,35 @@ if (Test-Path $templateFile) { Set-Content $localSet -Value $lc -Encoding UTF8 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 { Log-Warn "settings.template.json 不存在, 跳过渲染" }