Repository navigation
Expand file tree
/
Copy pathmcp.go
More file actions
477 lines (414 loc) · 12 KB
/
Copy pathmcp.go
File metadata and controls
477 lines (414 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package mcp
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
// MCPCmd is the parent command for MCP operations
var MCPCmd = &cobra.Command{
Use: "mcp",
Short: "Configure Kernel MCP server for AI tools",
Long: "Commands for configuring the Kernel MCP server in AI development tools like Cursor, Claude, VS Code, and more.",
Run: func(cmd *cobra.Command, args []string) {
// If called without subcommands, show help
_ = cmd.Help()
},
}
// Target represents a supported MCP client target
type Target string
const (
TargetCursor Target = "cursor"
TargetClaude Target = "claude"
TargetClaudeCode Target = "claude-code"
TargetWindsurf Target = "windsurf"
TargetVSCode Target = "vscode"
TargetGoose Target = "goose"
TargetZed Target = "zed"
TargetFx Target = "fx"
)
// KernelMCPURL is the URL for the Kernel MCP server
const KernelMCPURL = "https://mcp.onkernel.com/mcp"
// AllTargets returns all supported targets
func AllTargets() []Target {
return []Target{
TargetCursor,
TargetClaude,
TargetClaudeCode,
TargetWindsurf,
TargetVSCode,
TargetGoose,
TargetZed,
TargetFx,
}
}
// getHomeDir returns the user's home directory
func getHomeDir() (string, error) {
return os.UserHomeDir()
}
// getConfigPath returns the config file path for a given target
func getConfigPath(target Target) (string, error) {
homeDir, err := getHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get home directory: %w", err)
}
switch target {
case TargetCursor:
return filepath.Join(homeDir, ".cursor", "mcp.json"), nil
case TargetClaude:
switch runtime.GOOS {
case "darwin":
return filepath.Join(homeDir, "Library", "Application Support", "Claude", "claude_desktop_config.json"), nil
case "windows":
appData := os.Getenv("APPDATA")
if appData == "" {
appData = filepath.Join(homeDir, "AppData", "Roaming")
}
return filepath.Join(appData, "Claude", "claude_desktop_config.json"), nil
default:
// Linux - Claude Desktop doesn't officially support Linux, but use XDG config
return filepath.Join(homeDir, ".config", "Claude", "claude_desktop_config.json"), nil
}
case TargetClaudeCode:
// Claude Code uses the ~/.claude.json file
return filepath.Join(homeDir, ".claude.json"), nil
case TargetWindsurf:
return filepath.Join(homeDir, ".codeium", "windsurf", "mcp_config.json"), nil
case TargetVSCode:
switch runtime.GOOS {
case "darwin":
return filepath.Join(homeDir, "Library", "Application Support", "Code", "User", "settings.json"), nil
case "windows":
appData := os.Getenv("APPDATA")
if appData == "" {
appData = filepath.Join(homeDir, "AppData", "Roaming")
}
return filepath.Join(appData, "Code", "User", "settings.json"), nil
default:
return filepath.Join(homeDir, ".config", "Code", "User", "settings.json"), nil
}
case TargetGoose:
return filepath.Join(homeDir, ".config", "goose", "config.yaml"), nil
case TargetZed:
return filepath.Join(homeDir, ".config", "zed", "settings.json"), nil
case TargetFx:
return filepath.Join(homeDir, ".fx", "mcp.json"), nil
default:
return "", fmt.Errorf("unsupported target: %s", target)
}
}
// MCPServerConfig represents the configuration for an MCP server
type MCPServerConfig struct {
URL string `json:"url,omitempty"`
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Type string `json:"type,omitempty"`
}
// stripJSONComments removes single-line (//) and multi-line (/* */) comments from JSON
// It properly handles strings to avoid removing // or /* */ that appear inside string literals
func stripJSONComments(data []byte) []byte {
content := string(data)
var result strings.Builder
i := 0
inString := false
inMultiLineComment := false
escapeNext := false
for i < len(content) {
char := content[i]
if escapeNext {
result.WriteByte(char)
escapeNext = false
i++
continue
}
// Check multi-line comment first - skip all content including quotes
if inMultiLineComment {
if i+1 < len(content) && char == '*' && content[i+1] == '/' {
inMultiLineComment = false
i += 2
continue
}
i++
continue
}
if char == '\\' && inString {
escapeNext = true
result.WriteByte(char)
i++
continue
}
if char == '"' {
inString = !inString
result.WriteByte(char)
i++
continue
}
if inString {
result.WriteByte(char)
i++
continue
}
if i+1 < len(content) && char == '/' && content[i+1] == '/' {
// Single-line comment - skip to end of line
for i < len(content) && content[i] != '\n' {
i++
}
if i < len(content) {
result.WriteByte('\n')
i++
}
continue
}
if i+1 < len(content) && char == '/' && content[i+1] == '*' {
inMultiLineComment = true
i += 2
continue
}
result.WriteByte(char)
i++
}
return []byte(result.String())
}
// readJSONFile reads and parses a JSON config file
func readJSONFile(path string) (map[string]interface{}, error) {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return make(map[string]interface{}), nil
}
return nil, err
}
// Handle empty files
if len(data) == 0 {
return make(map[string]interface{}), nil
}
// Strip comments to support JSON5 format (used by Zed)
data = stripJSONComments(data)
var config map[string]interface{}
if err := json.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse JSON: %w", err)
}
return config, nil
}
// writeJSONFile writes a config map to a JSON file with proper formatting
func writeJSONFile(path string, config map[string]interface{}) error {
// Ensure parent directory exists
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON: %w", err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
func writePrivateJSONFile(path string, config map[string]interface{}) error {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
if err := os.Chmod(dir, 0700); err != nil {
return fmt.Errorf("failed to secure directory: %w", err)
}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON: %w", err)
}
if err := os.WriteFile(path, data, 0600); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
if err := os.Chmod(path, 0600); err != nil {
return fmt.Errorf("failed to secure file: %w", err)
}
return nil
}
// installForCursor installs MCP config for Cursor
func installForCursor(configPath string) error {
config, err := readJSONFile(configPath)
if err != nil {
return err
}
// Get or create mcpServers section
mcpServers, ok := config["mcpServers"].(map[string]interface{})
if !ok {
mcpServers = make(map[string]interface{})
}
// Add kernel server config
mcpServers["kernel"] = map[string]interface{}{
"url": KernelMCPURL,
}
config["mcpServers"] = mcpServers
return writeJSONFile(configPath, config)
}
// installForClaude installs MCP config for Claude Desktop
func installForClaude(configPath string) error {
config, err := readJSONFile(configPath)
if err != nil {
return err
}
// Get or create mcpServers section
mcpServers, ok := config["mcpServers"].(map[string]interface{})
if !ok {
mcpServers = make(map[string]interface{})
}
// Claude Desktop uses stdio transport via mcp-remote
mcpServers["kernel"] = map[string]interface{}{
"command": "npx",
"args": []string{"-y", "mcp-remote", KernelMCPURL},
}
config["mcpServers"] = mcpServers
return writeJSONFile(configPath, config)
}
// installForClaudeCode installs MCP config for Claude Code CLI
func installForClaudeCode(configPath string) error {
config, err := readJSONFile(configPath)
if err != nil {
return err
}
// Get or create mcpServers section
mcpServers, ok := config["mcpServers"].(map[string]interface{})
if !ok {
mcpServers = make(map[string]interface{})
}
// Claude Code uses HTTP transport
mcpServers["kernel"] = map[string]interface{}{
"type": "http",
"url": KernelMCPURL,
}
config["mcpServers"] = mcpServers
return writeJSONFile(configPath, config)
}
// installForWindsurf installs MCP config for Windsurf
func installForWindsurf(configPath string) error {
config, err := readJSONFile(configPath)
if err != nil {
return err
}
// Get or create mcpServers section
mcpServers, ok := config["mcpServers"].(map[string]interface{})
if !ok {
mcpServers = make(map[string]interface{})
}
// Windsurf uses stdio transport via mcp-remote
mcpServers["kernel"] = map[string]interface{}{
"command": "npx",
"args": []string{"-y", "mcp-remote", KernelMCPURL},
}
config["mcpServers"] = mcpServers
return writeJSONFile(configPath, config)
}
// installForVSCode installs MCP config for VS Code
func installForVSCode(configPath string) error {
config, err := readJSONFile(configPath)
if err != nil {
return err
}
// Get or create mcp.servers section (VS Code uses dot notation in settings)
mcpServers, ok := config["mcp.servers"].(map[string]interface{})
if !ok {
mcpServers = make(map[string]interface{})
}
// VS Code uses HTTP transport
mcpServers["kernel"] = map[string]interface{}{
"url": KernelMCPURL,
"type": "http",
}
config["mcp.servers"] = mcpServers
return writeJSONFile(configPath, config)
}
// installForGoose installs MCP config for Goose (YAML format)
func installForGoose(configPath string) error {
// For Goose, we'll output instructions since it uses YAML format
// and we don't want to add a YAML dependency
pterm.Info.Println("Goose uses YAML configuration. Add the following to your Goose config:")
pterm.Println()
fmt.Println(`extensions:
kernel:
name: Kernel
type: stdio
cmd: npx
args:
- -y
- mcp-remote
- ` + KernelMCPURL)
pterm.Println()
pterm.Info.Printf("Config file location: %s\n", configPath)
return nil
}
// installForZed installs MCP config for Zed
func installForZed(configPath string) error {
config, err := readJSONFile(configPath)
if err != nil {
return err
}
// Get or create context_servers section
contextServers, ok := config["context_servers"].(map[string]interface{})
if !ok {
contextServers = make(map[string]interface{})
}
// Zed uses context_servers with custom source
contextServers["kernel"] = map[string]interface{}{
"source": "custom",
"command": "npx",
"args": []string{"-y", "mcp-remote", KernelMCPURL},
}
config["context_servers"] = contextServers
return writeJSONFile(configPath, config)
}
// installForFx installs MCP config for fx
func installForFx(configPath string) error {
config, err := readJSONFile(configPath)
if err != nil {
return err
}
mcpServers, ok := config["mcp"].(map[string]interface{})
if !ok {
mcpServers = make(map[string]interface{})
}
mcpServers["kernel"] = map[string]interface{}{
"type": "http",
"url": KernelMCPURL,
"oauth": map[string]interface{}{},
}
config["mcp"] = mcpServers
return writePrivateJSONFile(configPath, config)
}
// Install configures the MCP server for the specified target
func Install(target Target) error {
configPath, err := getConfigPath(target)
if err != nil {
return err
}
switch target {
case TargetCursor:
return installForCursor(configPath)
case TargetClaude:
return installForClaude(configPath)
case TargetClaudeCode:
return installForClaudeCode(configPath)
case TargetWindsurf:
return installForWindsurf(configPath)
case TargetVSCode:
return installForVSCode(configPath)
case TargetGoose:
return installForGoose(configPath)
case TargetZed:
return installForZed(configPath)
case TargetFx:
return installForFx(configPath)
default:
return fmt.Errorf("unsupported target: %s", target)
}
}
// GetConfigPath returns the config path for a target (exported for display)
func GetConfigPath(target Target) (string, error) {
return getConfigPath(target)
}