Files
skillshare/internal/mcp/restore.go
T
Willie 86ef9788be feat(mcp): turn off a global server for Claude Code and tighten detection
Claude Code takes a whole server entry from one scope, so the lone switch
used for OpenCode, Kilo Code and Pi would replace the server in .mcp.json
instead of turning it off. In project mode a disabled entry for claude now
adds the name to that project's disabledMcpServers list in ~/.claude.json,
the list /mcp edits. It keys apart in the ledger from a user-scope server
of the same name that a global config may own in the same file, and a name
the person turned off themselves is never claimed or removed.

A local scope server of the same name silently wins over what Skillshare
writes, so project-mode plans now report it with the command to remove it.

Checks against each Agent's documentation also showed gaps:
- Detection missed Agents whose MCP file does not exist yet, such as a
  fresh Claude Code install, and listed every Agent in project mode.
  It now looks at the Agent's own folder and at project files.
- Cline moved its settings to ~/.cline/data and honors CLINE_DIR,
  CLINE_DATA_DIR and CLINE_MCP_SETTINGS_PATH; the old VS Code path is
  kept only while it is the one in use.
- Kilo Code ignores a project config that holds an environment
  reference, and Claude Code skips reserved names and reads its own
  credentials as empty for remote servers. These are refused up front,
  and the dashboard preview refuses them too.
- An entry owned by a config that no longer exists could never be
  released; an explicit resolution may now take it over.
- --disabled was accepted by commands that ignored it, and a relative
  XDG_CONFIG_HOME or Cline override was used as given.

Refs #286
2026-09-20 01:32:45 +08:00

96 lines
2.9 KiB
Go

package mcp
import (
"encoding/json"
"fmt"
"path/filepath"
"regexp"
)
var backupID = regexp.MustCompile(`^[0-9]+-[a-f0-9]{8}$`)
// PreviewRestore restores only entries from one operation, never the entire
// native file. Later changes to those entries require manual reconciliation.
func (s *Service) PreviewRestore(id string) (*Plan, error) {
if !backupID.MatchString(id) {
return nil, fmt.Errorf("invalid MCP backup ID")
}
data, exists, _, err := safeRead(filepath.Join(s.StateDir, "mcp", "backups", id+".json"))
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("MCP backup not found")
}
var backup backupRecord
if json.Unmarshal(data, &backup) != nil || backup.ID != id {
return nil, fmt.Errorf("invalid MCP backup")
}
source, err := LoadSource(s.ConfigPath)
if err != nil {
return nil, err
}
if backup.Owner != source.ConfigPath {
return nil, fmt.Errorf("backup belongs to another config or scope")
}
state, stateBytes, err := s.loadLedger()
if err != nil {
return nil, err
}
before, exists, mode, err := safeRead(backup.Path)
if err != nil {
return nil, err
}
native, err := ParseNative(backup.Target, before)
if err != nil {
return nil, err
}
p := &Plan{SourcePath: source.Path, source: source, state: state, stateBytes: stateBytes, Changes: []Change{}}
f := &filePlan{path: backup.Path, target: backup.Target, before: before, exists: exists, mode: mode, section: sectionDigest(native), changes: backup.Before}
for _, name := range sortedKeys(backup.Before) {
key := ownershipKey(backup.Target, backup.Path, name)
change := Change{Target: shownTarget(backup.Target), Path: backup.Path, Name: name, Action: "restore"}
owned, managed := state.Entries[key]
if !sameEntry(native.Entries[name], backup.After[name]) || managed && owned.Owner != backup.Owner {
change.Action, change.Message = "conflict", "entry changed after the backup; restore would overwrite newer changes"
p.Blocked = true
}
if previous, ok := backup.Ownership[key]; ok {
p.state.Entries[key] = previous
} else {
delete(p.state.Entries, key)
}
p.Changes = append(p.Changes, change)
}
if !p.Blocked {
f.after, err = native.Edit(f.changes)
if err != nil {
return nil, err
}
}
p.files = []*filePlan{f}
p.Revision = digest([]byte(digest(data) + f.section + digest(stateBytes) + digest(source.configBytes) + digest(source.bytes)))
return p, nil
}
// Restore creates a new backup before reverting native entries. Source settings
// remain unchanged, so a later sync may reapply them.
func (s *Service) Restore(id, revision string) (*Result, error) {
lock, err := s.lock()
if err != nil {
return nil, err
}
defer lock.Unlock()
if err := s.recoverPending(); err != nil {
return nil, err
}
p, err := s.PreviewRestore(id)
if err != nil {
return nil, err
}
if revision != "" && p.Revision != revision {
return nil, fmt.Errorf("configuration changed; preview restore again")
}
return s.applyPlan(p)
}