|
| 1 | +package agents |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | +) |
| 10 | + |
| 11 | +type cursorConfig struct { |
| 12 | + McpServers map[string]mcpServer `json:"mcpServers"` |
| 13 | +} |
| 14 | + |
| 15 | +type mcpServer struct { |
| 16 | + Command string `json:"command"` |
| 17 | + Args []string `json:"args,omitempty"` |
| 18 | + Env map[string]string `json:"env,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +func getCursorConfigPath() (string, error) { |
| 22 | + if runtime.GOOS == "windows" { |
| 23 | + userProfile := os.Getenv("USERPROFILE") |
| 24 | + if userProfile == "" { |
| 25 | + return "", os.ErrNotExist |
| 26 | + } |
| 27 | + return filepath.Join(userProfile, ".cursor", "mcp.json"), nil |
| 28 | + } |
| 29 | + |
| 30 | + home, err := os.UserHomeDir() |
| 31 | + if err != nil { |
| 32 | + return "", err |
| 33 | + } |
| 34 | + return filepath.Join(home, ".cursor", "mcp.json"), nil |
| 35 | +} |
| 36 | + |
| 37 | +// DetectCursor checks if Cursor is installed by looking for its config directory. |
| 38 | +func DetectCursor() bool { |
| 39 | + configPath, err := getCursorConfigPath() |
| 40 | + if err != nil { |
| 41 | + return false |
| 42 | + } |
| 43 | + // Check if the .cursor directory exists (not just the mcp.json file) |
| 44 | + cursorDir := filepath.Dir(configPath) |
| 45 | + _, err = os.Stat(cursorDir) |
| 46 | + return err == nil |
| 47 | +} |
| 48 | + |
| 49 | +// InstallCursor installs the Databricks MCP server in Cursor. |
| 50 | +func InstallCursor() error { |
| 51 | + configPath, err := getCursorConfigPath() |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to determine Cursor config path: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Check if .cursor directory exists (not the file, we'll create that if needed) |
| 57 | + cursorDir := filepath.Dir(configPath) |
| 58 | + if _, err := os.Stat(cursorDir); err != nil { |
| 59 | + return fmt.Errorf("cursor directory not found at: %s\n\nPlease install Cursor from: https://cursor.sh", cursorDir) |
| 60 | + } |
| 61 | + |
| 62 | + // Read existing config |
| 63 | + var config cursorConfig |
| 64 | + data, err := os.ReadFile(configPath) |
| 65 | + if err != nil { |
| 66 | + // If file doesn't exist or can't be read, start with empty config |
| 67 | + config = cursorConfig{ |
| 68 | + McpServers: make(map[string]mcpServer), |
| 69 | + } |
| 70 | + } else { |
| 71 | + if err := json.Unmarshal(data, &config); err != nil { |
| 72 | + return fmt.Errorf("failed to parse Cursor config: %w", err) |
| 73 | + } |
| 74 | + if config.McpServers == nil { |
| 75 | + config.McpServers = make(map[string]mcpServer) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Add or update the Databricks MCP server entry |
| 80 | + config.McpServers["databricks"] = mcpServer{ |
| 81 | + Command: "databricks", |
| 82 | + Args: []string{"experimental", "aitools", "server"}, |
| 83 | + } |
| 84 | + |
| 85 | + // Write back to file with pretty printing |
| 86 | + updatedData, err := json.MarshalIndent(config, "", " ") |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to marshal Cursor config: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + if err := os.WriteFile(configPath, updatedData, 0o644); err != nil { |
| 92 | + return fmt.Errorf("failed to write Cursor config: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
0 commit comments