AddServer writes server into the client's config file at scope. Missing files are created with restrictive permissions; existing files are merged.
(client ClientSpec, scope Scope, server Server)
| 104 | // AddServer writes server into the client's config file at scope. Missing |
| 105 | // files are created with restrictive permissions; existing files are merged. |
| 106 | func AddServer(client ClientSpec, scope Scope, server Server) (string, error) { |
| 107 | path := client.ResolvePath(scope) |
| 108 | if path == "" { |
| 109 | return "", fmt.Errorf("mcp: client %s does not support scope %s", client.Name, scope) |
| 110 | } |
| 111 | // Lock per-file to prevent concurrent read-modify-write races. |
| 112 | mu, _ := fileMu.LoadOrStore(path, &sync.Mutex{}) |
| 113 | mu.(*sync.Mutex).Lock() |
| 114 | defer mu.(*sync.Mutex).Unlock() |
| 115 | data, err := readJSON(path) |
| 116 | if err != nil { |
| 117 | return "", err |
| 118 | } |
| 119 | container, _ := data[client.Container].(map[string]any) |
| 120 | if container == nil { |
| 121 | container = map[string]any{} |
| 122 | } |
| 123 | container[server.Name] = serverToMap(server) |
| 124 | data[client.Container] = container |
| 125 | if err := writeJSON(path, data); err != nil { |
| 126 | return "", err |
| 127 | } |
| 128 | return path, nil |
| 129 | } |
| 130 | |
| 131 | // RemoveServer deletes server from the client's config file at scope and |
| 132 | // reports whether the entry existed. |