|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | +) |
| 10 | + |
| 11 | +// registerMCPServers attempts to register memoryd as an MCP server in every |
| 12 | +// supported coding agent found on this machine. Each registration is |
| 13 | +// idempotent and non-fatal — failures are logged and skipped. |
| 14 | +func registerMCPServers() { |
| 15 | + execPath, err := os.Executable() |
| 16 | + if err != nil { |
| 17 | + log.Printf("[mcp-register] could not determine executable path: %v", err) |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + home, err := os.UserHomeDir() |
| 22 | + if err != nil { |
| 23 | + log.Printf("[mcp-register] could not determine home directory: %v", err) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + entry := mcpEntry(execPath) |
| 28 | + |
| 29 | + registrars := []struct { |
| 30 | + name string |
| 31 | + fn func() error |
| 32 | + }{ |
| 33 | + {"Claude Code", func() error { return registerClaudeCode(home, entry) }}, |
| 34 | + {"Cursor", func() error { return registerStandard(home, cursorMCPPath(home), entry) }}, |
| 35 | + {"Windsurf", func() error { return registerStandard(home, windsurfMCPPath(home), entry) }}, |
| 36 | + {"Cline", func() error { return registerStandard(home, clineMCPPath(home), entry) }}, |
| 37 | + } |
| 38 | + |
| 39 | + for _, r := range registrars { |
| 40 | + if err := r.fn(); err != nil { |
| 41 | + log.Printf("[mcp-register] %s: %v", r.name, err) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// mcpEntry returns the standard MCP server entry for memoryd. |
| 47 | +func mcpEntry(execPath string) map[string]any { |
| 48 | + return map[string]any{ |
| 49 | + "command": execPath, |
| 50 | + "args": []string{"mcp"}, |
| 51 | + "env": map[string]any{}, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// --- Claude Code --- |
| 56 | + |
| 57 | +// registerClaudeCode writes to ~/.claude/settings.json under |
| 58 | +// projects[home].mcpServers. Created if it doesn't exist. |
| 59 | +func registerClaudeCode(home string, entry map[string]any) error { |
| 60 | + settingsPath := filepath.Join(home, ".claude", "settings.json") |
| 61 | + |
| 62 | + var settings map[string]any |
| 63 | + data, err := os.ReadFile(settingsPath) |
| 64 | + if err != nil && !os.IsNotExist(err) { |
| 65 | + return err |
| 66 | + } |
| 67 | + if len(data) > 0 { |
| 68 | + if err := json.Unmarshal(data, &settings); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + } |
| 72 | + if settings == nil { |
| 73 | + settings = map[string]any{} |
| 74 | + } |
| 75 | + |
| 76 | + projects, _ := settings["projects"].(map[string]any) |
| 77 | + if projects == nil { |
| 78 | + projects = map[string]any{} |
| 79 | + settings["projects"] = projects |
| 80 | + } |
| 81 | + |
| 82 | + homeProject, _ := projects[home].(map[string]any) |
| 83 | + if homeProject == nil { |
| 84 | + homeProject = map[string]any{} |
| 85 | + projects[home] = homeProject |
| 86 | + } |
| 87 | + |
| 88 | + mcpServers, _ := homeProject["mcpServers"].(map[string]any) |
| 89 | + if mcpServers == nil { |
| 90 | + mcpServers = map[string]any{} |
| 91 | + homeProject["mcpServers"] = mcpServers |
| 92 | + } |
| 93 | + |
| 94 | + if _, exists := mcpServers["memoryd"]; exists { |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + mcpServers["memoryd"] = entry |
| 99 | + |
| 100 | + return writeJSON(settingsPath, settings) |
| 101 | +} |
| 102 | + |
| 103 | +// --- Standard format (Cursor, Windsurf, Cline) --- |
| 104 | + |
| 105 | +// registerStandard writes an {"mcpServers":{"memoryd":{...}}} config file. |
| 106 | +// It only proceeds if the agent's config directory already exists (proving |
| 107 | +// the agent is installed). The config file itself is created if absent. |
| 108 | +func registerStandard(home, configPath string, entry map[string]any) error { |
| 109 | + dir := filepath.Dir(configPath) |
| 110 | + if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 111 | + return nil // agent not installed, skip silently |
| 112 | + } |
| 113 | + |
| 114 | + var cfg map[string]any |
| 115 | + data, err := os.ReadFile(configPath) |
| 116 | + if err != nil && !os.IsNotExist(err) { |
| 117 | + return err |
| 118 | + } |
| 119 | + if len(data) > 0 { |
| 120 | + if err := json.Unmarshal(data, &cfg); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + } |
| 124 | + if cfg == nil { |
| 125 | + cfg = map[string]any{} |
| 126 | + } |
| 127 | + |
| 128 | + mcpServers, _ := cfg["mcpServers"].(map[string]any) |
| 129 | + if mcpServers == nil { |
| 130 | + mcpServers = map[string]any{} |
| 131 | + cfg["mcpServers"] = mcpServers |
| 132 | + } |
| 133 | + |
| 134 | + if _, exists := mcpServers["memoryd"]; exists { |
| 135 | + return nil |
| 136 | + } |
| 137 | + |
| 138 | + mcpServers["memoryd"] = entry |
| 139 | + |
| 140 | + if err := writeJSON(configPath, cfg); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + log.Printf("[mcp-register] registered in %s", configPath) |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// --- Path helpers --- |
| 149 | + |
| 150 | +func cursorMCPPath(home string) string { |
| 151 | + return filepath.Join(home, ".cursor", "mcp.json") |
| 152 | +} |
| 153 | + |
| 154 | +func windsurfMCPPath(home string) string { |
| 155 | + return filepath.Join(home, ".codeium", "windsurf", "mcp_config.json") |
| 156 | +} |
| 157 | + |
| 158 | +func clineMCPPath(home string) string { |
| 159 | + switch runtime.GOOS { |
| 160 | + case "darwin": |
| 161 | + return filepath.Join(home, "Library", "Application Support", "Code", "User", |
| 162 | + "globalStorage", "saoudrizwan.claude-dev", "settings", "cline_mcp_settings.json") |
| 163 | + default: |
| 164 | + return filepath.Join(home, ".config", "Code", "User", |
| 165 | + "globalStorage", "saoudrizwan.claude-dev", "settings", "cline_mcp_settings.json") |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +// --- Helpers --- |
| 170 | + |
| 171 | +func writeJSON(path string, v any) error { |
| 172 | + out, err := json.MarshalIndent(v, "", " ") |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + return os.WriteFile(path, out, 0644) |
| 180 | +} |
0 commit comments