fix(mcp): say a switch-only entry turns the server off, not adds one

Turning a global server off for a project writes an entry that holds
only the switch. The Sync page listed it as "New server entry", and as
"Server entry will be removed" when the server was turned back on, so a
review read as servers coming and going.

The plan now marks those changes. It reads the entry itself rather than
the source, because once the switch is removed the source has nothing
left to say about it. The Sync page uses the mark for OpenCode, Kilo
Code and Pi as well as for Claude Code's off list.

Refs #289
This commit is contained in:
Willie
2026-09-22 00:52:52 +08:00
parent e91b6ac602
commit bc58d75dea
16 changed files with 91 additions and 33 deletions
+37
View File
@@ -2,6 +2,7 @@ package mcp
import (
"encoding/json"
"maps"
"os"
"path/filepath"
"slices"
@@ -253,3 +254,39 @@ func TestClaudeLocalScopeShadowIsReported(t *testing.T) {
}
}
}
// The plan says which changes only flip a switch, so a review can say "turned off here"
// rather than "new server entry". A removal has no source entry left to ask.
func TestPlanMarksSwitchOnlyChanges(t *testing.T) {
s := testService(t)
s.ProjectRoot = filepath.Join(s.Home, "project")
marks := func(source, action string) map[string]bool {
t.Helper()
if err := os.WriteFile(s.ConfigPath, []byte(source), 0600); err != nil {
t.Fatal(err)
}
plan, err := s.Preview()
if err != nil {
t.Fatal(err)
}
got := map[string]bool{}
for _, c := range plan.Changes {
if c.Action != action {
t.Fatalf("%s %s: %s, want %s", c.Target, c.Name, c.Action, action)
}
got[c.Target+"/"+c.Name] = c.Switch
}
if _, err = s.Apply(plan.Revision); err != nil {
t.Fatal(err)
}
return got
}
want := map[string]bool{"opencode/docs": true, "pi/docs": true, "claude/docs": true, "opencode/own": false}
added := marks("mcp:\n servers:\n docs:\n disabled: true\n piExtension: pi-mcp-adapter\n targets: [opencode, pi, claude]\n own:\n command: tool\n targets: [opencode]\n", "add")
if !maps.Equal(added, want) {
t.Fatalf("add: got %v want %v", added, want)
}
if removed := marks("mcp:\n servers: {}\n", "remove"); !maps.Equal(removed, want) {
t.Fatalf("remove: got %v want %v", removed, want)
}
}
+16 -1
View File
@@ -19,11 +19,23 @@ type Change struct {
Name string `json:"name"`
// Root is the mcp.projects folder this change belongs to, empty for a global one. The
// path usually says, but Claude Code's per-project off list lives in the global file.
Root string `json:"root,omitempty"`
Root string `json:"root,omitempty"`
// Switch marks an entry that only turns a global server off for one project. Adding it
// turns the server off there and removing it turns it back on; no server comes or goes.
Switch bool `json:"switch,omitempty"`
Action string `json:"action"`
Message string `json:"message,omitempty"`
}
// switchOnly reports an entry holding nothing but the switch renderDisabled writes. It reads
// the entry rather than the source, which has nothing left to say once the switch is removed.
func switchOnly(target string, entry map[string]any) bool {
if strings.HasPrefix(target, claudeOffPrefix) {
return true
}
return len(entry) == 1 && (entry["enabled"] == false || entry["disabled"] == true)
}
// changeRoot is the mcp.projects folder a file plan writes for. Claude Code's off list
// sits in ~/.claude.json, outside every root, so its native target carries the root.
func changeRoot(target, path string, roots []string) string {
@@ -418,6 +430,9 @@ func (s *Service) previewResolved(source *Source, resolutions []Resolution) (*Pl
p.state.Entries[key] = owned
}
change := Change{Target: shownTarget(target), Path: path, Name: name, Root: changeRoot(target, path, projectRoots)}
if change.Switch = switchOnly(target, want); want == nil {
change.Switch = switchOnly(target, current)
}
switch {
case managed && owned.Owner != source.ConfigPath:
change.Action, change.Message = "conflict", "managed by another Skillshare config: "+owned.Owner
+2 -1
View File
@@ -38,7 +38,8 @@ export interface MCPPlan {
revision: string;
sourcePath: string;
blocked: boolean;
changes: { target: string; path: string; name: string; root?: string; action: string; message?: string }[];
/** `switch`: the entry only turns a global server off for one project, so adding it turns the server off there. */
changes: { target: string; path: string; name: string; root?: string; switch?: boolean; action: string; message?: string }[];
}
export interface MCPResult { plan?: MCPPlan; applied: string[]; backupIds: string[] }
export interface MCPCandidate { name: string; server: MCPServer; problems: string[]; warnings: string[]; from?: string }
+12 -7
View File
@@ -88,12 +88,17 @@ describe('runSync', () => {
});
describe('mcpGroups', () => {
it("says a change to a project's Claude off list turns the server on or off there", () => {
const plan = { revision: 'r', sourcePath: '', blocked: false, changes: [
{ target: 'claude', path: '/home/u/.claude.json', name: 'context7', root: '/work/app', action: 'remove' },
{ target: 'claude', path: '/home/u/.claude.json', name: 'github', root: '/work/app', action: 'add' },
] };
const [group] = mcpGroups(plan);
expect([group.project, ...group.rows.map((r) => r.text)]).toEqual(['/work/app', 'sync.row.mcp.offList.remove', 'sync.row.mcp.offList.add']);
const plan = { revision: 'r', sourcePath: '', blocked: false, changes: [
{ target: 'claude', path: '/home/u/.claude.json', name: 'context7', root: '/work/app', switch: true, action: 'remove' },
{ target: 'opencode', path: '/work/app/opencode.json', name: 'context7', root: '/work/app', switch: true, action: 'add' },
{ target: 'opencode', path: '/work/app/opencode.json', name: 'own', root: '/work/app', action: 'add' },
] };
it('says a switch-only entry turns the server off or back on in the project', () => {
expect(mcpGroups(plan).flatMap((g) => g.rows.map((r) => r.text))).toEqual(['sync.row.mcp.switch.remove', 'sync.row.mcp.switch.add', 'sync.row.mcp.add']);
});
it("names the project on Claude's off list, which sits in the global file", () => {
expect(mcpGroups(plan).map((g) => g.project)).toEqual(['/work/app', undefined]);
});
});
+2 -2
View File
@@ -105,8 +105,8 @@ export function mcpGroups(plan: MCPPlan | null | undefined): ChangeGroup[] {
part: 'mcp',
name: c.name,
icon: MCP_ICON[c.action],
// In the off list, adding a name turns the server off and removing it turns it back on.
text: c.action === 'conflict' ? null : file.offListFor && c.action !== 'update' ? `sync.row.mcp.offList.${c.action}` : `sync.row.mcp.${c.action}`,
// A switch-only entry adds or removes no server: it turns one off for a project, or back on.
text: c.action === 'conflict' ? null : c.switch && c.action !== 'update' ? `sync.row.mcp.switch.${c.action}` : `sync.row.mcp.${c.action}`,
detail: c.message,
counts: c.action !== 'conflict',
})),
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "Neuer Server-Eintrag",
"sync.row.mcp.update": "Server-Eintrag wird aktualisiert",
"sync.row.mcp.remove": "Server-Eintrag wird entfernt",
"sync.row.mcp.offList.add": "Wird in diesem Projekt ausgeschaltet",
"sync.row.mcp.offList.remove": "Wird in diesem Projekt wieder eingeschaltet",
"sync.row.mcp.switch.add": "Wird in diesem Projekt ausgeschaltet",
"sync.row.mcp.switch.remove": "Wird in diesem Projekt wieder eingeschaltet",
"sync.mcp.offList": "Aus-Liste für {project}",
"sync.row.pruneIgnored": "In der Quelle ignoriert. Wird aus diesem Ziel entfernt",
"sync.mcpBlocked": "MCP hat Konflikte, daher sind seine Änderungen nicht Teil dieser Synchronisierung.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "New server entry",
"sync.row.mcp.update": "Server entry will be updated",
"sync.row.mcp.remove": "Server entry will be removed",
"sync.row.mcp.offList.add": "Will be turned off in this project",
"sync.row.mcp.offList.remove": "Will be turned back on in this project",
"sync.row.mcp.switch.add": "Will be turned off in this project",
"sync.row.mcp.switch.remove": "Will be turned back on in this project",
"sync.mcp.offList": "Off list for {project}",
"sync.row.pruneIgnored": "Ignored in source. Will be removed from this target",
"sync.mcpBlocked": "MCP has conflicts, so its changes are left out of this sync.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "Nueva entrada de servidor",
"sync.row.mcp.update": "Se actualizará la entrada del servidor",
"sync.row.mcp.remove": "Se eliminará la entrada del servidor",
"sync.row.mcp.offList.add": "Se desactivará en este proyecto",
"sync.row.mcp.offList.remove": "Se volverá a activar en este proyecto",
"sync.row.mcp.switch.add": "Se desactivará en este proyecto",
"sync.row.mcp.switch.remove": "Se volverá a activar en este proyecto",
"sync.mcp.offList": "Lista de desactivados de {project}",
"sync.row.pruneIgnored": "Ignorado en el origen. Se eliminará de este destino",
"sync.mcpBlocked": "MCP tiene conflictos, así que sus cambios quedan fuera de esta sincronización.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "ورودی سرور جدید",
"sync.row.mcp.update": "ورودی سرور به‌روزرسانی می‌شود",
"sync.row.mcp.remove": "ورودی سرور حذف می‌شود",
"sync.row.mcp.offList.add": "در این پروژه خاموش می‌شود",
"sync.row.mcp.offList.remove": "در این پروژه دوباره روشن می‌شود",
"sync.row.mcp.switch.add": "در این پروژه خاموش می‌شود",
"sync.row.mcp.switch.remove": "در این پروژه دوباره روشن می‌شود",
"sync.mcp.offList": "فهرست خاموش‌ها برای {project}",
"sync.row.pruneIgnored": "در منبع نادیده گرفته شده. از این مقصد حذف می‌شود",
"sync.mcpBlocked": "MCP تعارض دارد، پس تغییرات آن در این همگام‌سازی نیست.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "Nouvelle entrée de serveur",
"sync.row.mcp.update": "L'entrée du serveur sera mise à jour",
"sync.row.mcp.remove": "L'entrée du serveur sera supprimée",
"sync.row.mcp.offList.add": "Sera désactivé dans ce projet",
"sync.row.mcp.offList.remove": "Sera réactivé dans ce projet",
"sync.row.mcp.switch.add": "Sera désactivé dans ce projet",
"sync.row.mcp.switch.remove": "Sera réactivé dans ce projet",
"sync.mcp.offList": "Liste de désactivation de {project}",
"sync.row.pruneIgnored": "Ignoré dans la source. Sera retiré de cette cible",
"sync.mcpBlocked": "MCP a des conflits, ses changements sont exclus de cette synchronisation.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "Entri server baru",
"sync.row.mcp.update": "Entri server akan diperbarui",
"sync.row.mcp.remove": "Entri server akan dihapus",
"sync.row.mcp.offList.add": "Akan dimatikan di proyek ini",
"sync.row.mcp.offList.remove": "Akan dinyalakan kembali di proyek ini",
"sync.row.mcp.switch.add": "Akan dimatikan di proyek ini",
"sync.row.mcp.switch.remove": "Akan dinyalakan kembali di proyek ini",
"sync.mcp.offList": "Daftar mati untuk {project}",
"sync.row.pruneIgnored": "Diabaikan di sumber. Akan dihapus dari target ini",
"sync.mcpBlocked": "MCP memiliki konflik, jadi perubahannya tidak ikut sinkronisasi ini.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "新しいサーバーエントリ",
"sync.row.mcp.update": "サーバーエントリが更新されます",
"sync.row.mcp.remove": "サーバーエントリが削除されます",
"sync.row.mcp.offList.add": "このプロジェクトでオフになります",
"sync.row.mcp.offList.remove": "このプロジェクトで再びオンになります",
"sync.row.mcp.switch.add": "このプロジェクトでオフになります",
"sync.row.mcp.switch.remove": "このプロジェクトで再びオンになります",
"sync.mcp.offList": "{project} のオフリスト",
"sync.row.pruneIgnored": "ソースで無視されています。このターゲットから削除されます",
"sync.mcpBlocked": "MCP に競合があるため、その変更は今回の同期に含まれません。",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "새 서버 항목",
"sync.row.mcp.update": "서버 항목이 업데이트됩니다",
"sync.row.mcp.remove": "서버 항목이 제거됩니다",
"sync.row.mcp.offList.add": "이 프로젝트에서 꺼집니다",
"sync.row.mcp.offList.remove": "이 프로젝트에서 다시 켜집니다",
"sync.row.mcp.switch.add": "이 프로젝트에서 꺼집니다",
"sync.row.mcp.switch.remove": "이 프로젝트에서 다시 켜집니다",
"sync.mcp.offList": "{project}의 끄기 목록",
"sync.row.pruneIgnored": "소스에서 무시됨. 이 대상에서 제거됩니다",
"sync.mcpBlocked": "MCP에 충돌이 있어 이번 동기화에서 MCP 변경은 제외됩니다.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "Nova entrada de servidor",
"sync.row.mcp.update": "A entrada do servidor será atualizada",
"sync.row.mcp.remove": "A entrada do servidor será removida",
"sync.row.mcp.offList.add": "Será desligado neste projeto",
"sync.row.mcp.offList.remove": "Será ligado novamente neste projeto",
"sync.row.mcp.switch.add": "Será desligado neste projeto",
"sync.row.mcp.switch.remove": "Será ligado novamente neste projeto",
"sync.mcp.offList": "Lista de desligados de {project}",
"sync.row.pruneIgnored": "Ignorado na origem. Será removido deste destino",
"sync.mcpBlocked": "O MCP tem conflitos, então as alterações dele ficam fora desta sincronização.",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "新增服务器条目",
"sync.row.mcp.update": "服务器条目将被更新",
"sync.row.mcp.remove": "服务器条目将被移除",
"sync.row.mcp.offList.add": "将在这个项目关闭",
"sync.row.mcp.offList.remove": "将在这个项目重新启用",
"sync.row.mcp.switch.add": "将在这个项目关闭",
"sync.row.mcp.switch.remove": "将在这个项目重新启用",
"sync.mcp.offList": "{project} 的关闭列表",
"sync.row.pruneIgnored": "已在来源中忽略,将从此目标移除",
"sync.mcpBlocked": "MCP 有冲突,因此本次同步不包含其变更。",
+2 -2
View File
@@ -1233,8 +1233,8 @@
"sync.row.mcp.add": "新增伺服器項目",
"sync.row.mcp.update": "伺服器項目將被更新",
"sync.row.mcp.remove": "伺服器項目將被移除",
"sync.row.mcp.offList.add": "將在這個專案關閉",
"sync.row.mcp.offList.remove": "將在這個專案重新啟用",
"sync.row.mcp.switch.add": "將在這個專案關閉",
"sync.row.mcp.switch.remove": "將在這個專案重新啟用",
"sync.mcp.offList": "{project} 的關閉清單",
"sync.row.pruneIgnored": "已在來源中忽略,將從此目標移除",
"sync.mcpBlocked": "MCP 有衝突,因此本次同步不包含其變更。",