|
| 1 | +package mcpinit |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/spf13/afero" |
| 11 | +) |
| 12 | + |
| 13 | +// cursorClient implements the Client interface for Cursor |
| 14 | +type cursorClient struct { |
| 15 | + baseClient |
| 16 | +} |
| 17 | + |
| 18 | +func newCursorClient() *cursorClient { |
| 19 | + return &cursorClient{ |
| 20 | + baseClient: baseClient{ |
| 21 | + name: "cursor", |
| 22 | + displayName: "Cursor", |
| 23 | + installInstructions: "Download from https://cursor.sh", |
| 24 | + checkInstalled: func() bool { |
| 25 | + return commandExists("cursor") || appExists("Cursor") |
| 26 | + }, |
| 27 | + }, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (c *cursorClient) Configure(ctx context.Context, fsys afero.Fs) error { |
| 32 | + fmt.Println("Configuring Cursor...") |
| 33 | + fmt.Println() |
| 34 | + |
| 35 | + // Prompt for config scope |
| 36 | + fmt.Println("Where would you like to add the configuration?") |
| 37 | + fmt.Println(" 1. Project-local (in .cursor/mcp.json)") |
| 38 | + fmt.Println(" 2. Global (in your home directory)") |
| 39 | + fmt.Print("Choice [1]: ") |
| 40 | + |
| 41 | + var choice string |
| 42 | + if _, err := fmt.Scanln(&choice); err != nil && err.Error() != "unexpected newline" { |
| 43 | + return fmt.Errorf("failed to read choice: %w", err) |
| 44 | + } |
| 45 | + if choice == "" { |
| 46 | + choice = "1" |
| 47 | + } |
| 48 | + |
| 49 | + var configPath string |
| 50 | + if choice == "2" { |
| 51 | + // Global config |
| 52 | + homeDir, _ := os.UserHomeDir() |
| 53 | + configPath = filepath.Join(homeDir, ".cursor", "mcp.json") |
| 54 | + } else { |
| 55 | + // Project-local config |
| 56 | + cwd, _ := os.Getwd() |
| 57 | + configPath = filepath.Join(cwd, ".cursor", "mcp.json") |
| 58 | + } |
| 59 | + |
| 60 | + // Prepare the Supabase MCP server config |
| 61 | + supabaseConfig := map[string]interface{}{ |
| 62 | + "url": "https://mcp.supabase.com/mcp", |
| 63 | + } |
| 64 | + |
| 65 | + // Read existing config if it exists |
| 66 | + var config map[string]interface{} |
| 67 | + existingData, err := afero.ReadFile(fsys, configPath) |
| 68 | + if err == nil && len(existingData) > 0 { |
| 69 | + if err := json.Unmarshal(existingData, &config); err != nil { |
| 70 | + // If existing file is invalid JSON, start fresh |
| 71 | + config = make(map[string]interface{}) |
| 72 | + } |
| 73 | + } else { |
| 74 | + config = make(map[string]interface{}) |
| 75 | + } |
| 76 | + |
| 77 | + // Ensure mcpServers exists |
| 78 | + mcpServers, ok := config["mcpServers"].(map[string]interface{}) |
| 79 | + if !ok { |
| 80 | + mcpServers = make(map[string]interface{}) |
| 81 | + config["mcpServers"] = mcpServers |
| 82 | + } |
| 83 | + |
| 84 | + // Add or update Supabase server |
| 85 | + mcpServers["supabase"] = supabaseConfig |
| 86 | + |
| 87 | + // Ensure directory exists |
| 88 | + configDir := filepath.Dir(configPath) |
| 89 | + if err := fsys.MkdirAll(configDir, 0755); err != nil { |
| 90 | + return fmt.Errorf("failed to create config directory: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Write config |
| 94 | + configJSON, err := json.MarshalIndent(config, "", " ") |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("failed to marshal config: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + if err := afero.WriteFile(fsys, configPath, configJSON, 0644); err != nil { |
| 100 | + return fmt.Errorf("failed to write config file: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + fmt.Println() |
| 104 | + fmt.Printf("✓ Successfully configured Cursor at: %s\n", configPath) |
| 105 | + fmt.Println() |
| 106 | + fmt.Println("Configuration added:") |
| 107 | + fmt.Println(`{ |
| 108 | + "mcpServers": { |
| 109 | + "supabase": { |
| 110 | + "url": "https://mcp.supabase.com/mcp" |
| 111 | + } |
| 112 | + } |
| 113 | +}`) |
| 114 | + fmt.Println() |
| 115 | + fmt.Println("The Supabase MCP server is now available in Cursor!") |
| 116 | + return nil |
| 117 | +} |
0 commit comments