MCPcopy Create free account
hub / github.com/Chat2AnyLLM/code-agent-manager / AddServer

Function AddServer

internal/mcp/client.go:106–129  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

104// AddServer writes server into the client's config file at scope. Missing
105// files are created with restrictive permissions; existing files are merged.
106func 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.

Callers 5

AddMethod · 0.92
InstallFromRegistryMethod · 0.92
mcpAddCommandMethod · 0.92

Calls 4

readJSONFunction · 0.85
serverToMapFunction · 0.85
ResolvePathMethod · 0.80
writeJSONFunction · 0.70

Tested by 2