RemoveServer deletes server from the client's config file at scope and reports whether the entry existed.
(client ClientSpec, scope Scope, serverName string)
| 131 | // RemoveServer deletes server from the client's config file at scope and |
| 132 | // reports whether the entry existed. |
| 133 | func RemoveServer(client ClientSpec, scope Scope, serverName string) (string, bool, error) { |
| 134 | path := client.ResolvePath(scope) |
| 135 | if path == "" { |
| 136 | return "", false, fmt.Errorf("mcp: client %s does not support scope %s", client.Name, scope) |
| 137 | } |
| 138 | // Lock per-file to prevent concurrent read-modify-write races. |
| 139 | mu, _ := fileMu.LoadOrStore(path, &sync.Mutex{}) |
| 140 | mu.(*sync.Mutex).Lock() |
| 141 | defer mu.(*sync.Mutex).Unlock() |
| 142 | data, err := readJSON(path) |
| 143 | if err != nil { |
| 144 | return path, false, err |
| 145 | } |
| 146 | container, _ := data[client.Container].(map[string]any) |
| 147 | if container == nil { |
| 148 | return path, false, nil |
| 149 | } |
| 150 | if _, ok := container[serverName]; !ok { |
| 151 | return path, false, nil |
| 152 | } |
| 153 | delete(container, serverName) |
| 154 | data[client.Container] = container |
| 155 | if err := writeJSON(path, data); err != nil { |
| 156 | return "", false, err |
| 157 | } |
| 158 | return path, true, nil |
| 159 | } |
| 160 | |
| 161 | // ListServers returns all servers installed for the client at scope. |
| 162 | func ListServers(client ClientSpec, scope Scope) ([]Server, string, error) { |