|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/dagger/container-use/mcpserver" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +type MCPServersConfig struct { |
| 14 | + MCPServers map[string]MCPServer `json:"mcpServers"` |
| 15 | +} |
| 16 | + |
| 17 | +type MCPServer struct { |
| 18 | + Command string `json:"command"` |
| 19 | + Args []string `json:"args"` |
| 20 | + Env map[string]string `json:"env,omitempty"` |
| 21 | + Timeout *int `json:"timeout,omitempty"` |
| 22 | + Disabled *bool `json:"disabled,omitempty"` |
| 23 | + AutoApprove []string `json:"autoApprove,omitempty"` |
| 24 | + AlwaysAllow []string `json:"alwaysAllow,omitempty"` |
| 25 | + WorkingDir *string `json:"working_directory,omitempty"` |
| 26 | + StartOnLaunch *bool `json:"start_on_launch,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +const ContainerUseBinary = "container-use" |
| 30 | + |
| 31 | +var configureCmd = &cobra.Command{ |
| 32 | + Use: "agent [agent]", |
| 33 | + Short: "Configure MCP server for different agents", |
| 34 | + Long: `Setup the container-use MCP server according to the specified agent including Claude Code, Goose, Cursor, and others.`, |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + if len(args) == 0 { |
| 37 | + return interactiveConfiguration() |
| 38 | + } |
| 39 | + agent, err := selectAgent(args[0]) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + return configureAgent(agent) |
| 44 | + }, |
| 45 | +} |
| 46 | + |
| 47 | +func interactiveConfiguration() error { |
| 48 | + selectedAgent, err := RunAgentSelector() |
| 49 | + if err != nil { |
| 50 | + // If the user quits, it's not an error, just exit gracefully. |
| 51 | + if err.Error() == "no agent selected" { |
| 52 | + return nil |
| 53 | + } |
| 54 | + return fmt.Errorf("failed to select agent: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + agent, err := selectAgent(selectedAgent) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + return configureAgent(agent) |
| 62 | +} |
| 63 | + |
| 64 | +type ConfigurableAgent interface { |
| 65 | + name() string |
| 66 | + description() string |
| 67 | + editMcpConfig() error |
| 68 | + editRules() error |
| 69 | + isInstalled() bool |
| 70 | +} |
| 71 | + |
| 72 | +// Add agents here |
| 73 | +func selectAgent(agentKey string) (ConfigurableAgent, error) { |
| 74 | + switch agentKey { |
| 75 | + case "claude": |
| 76 | + return &ConfigureClaude{}, nil |
| 77 | + case "goose": |
| 78 | + return &ConfigureGoose{}, nil |
| 79 | + case "cursor": |
| 80 | + return &ConfigureCursor{}, nil |
| 81 | + case "codex": |
| 82 | + return &ConfigureCodex{}, nil |
| 83 | + case "amazonq": |
| 84 | + return &ConfigureQ{}, nil |
| 85 | + } |
| 86 | + return nil, fmt.Errorf("unknown agent: %s", agentKey) |
| 87 | +} |
| 88 | + |
| 89 | +func configureAgent(agent ConfigurableAgent) error { |
| 90 | + fmt.Printf("Configuring %s...\n", agent.name()) |
| 91 | + |
| 92 | + // Save MCP config |
| 93 | + err := agent.editMcpConfig() |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + fmt.Printf("✓ Configured %s MCP configuration\n", agent.name()) |
| 98 | + |
| 99 | + // Save rules |
| 100 | + err = agent.editRules() |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + fmt.Printf("✓ Saved %s container-use rules\n", agent.name()) |
| 105 | + |
| 106 | + fmt.Printf("\n%s configuration complete!\n", agent.name()) |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// Helper functions |
| 111 | +func saveRulesFile(rulesFile, content string) error { |
| 112 | + dir := filepath.Dir(rulesFile) |
| 113 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + // Append to file if it exists, create if it doesn't TODO make it re-entrant with a marker |
| 118 | + existing, err := os.ReadFile(rulesFile) |
| 119 | + if err != nil && !os.IsNotExist(err) { |
| 120 | + return fmt.Errorf("failed to read existing rules: %w", err) |
| 121 | + } |
| 122 | + existingStr := string(existing) |
| 123 | + |
| 124 | + editedRules, err := editRulesFile(existingStr, content) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + err = os.WriteFile(rulesFile, []byte(editedRules), 0644) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to update rules: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func editRulesFile(existingRules, content string) (string, error) { |
| 138 | + // Look for section markers |
| 139 | + const marker = "<!-- container-use-rules -->" |
| 140 | + |
| 141 | + if strings.Contains(existingRules, marker) { |
| 142 | + // Update existing section |
| 143 | + parts := strings.Split(existingRules, marker) |
| 144 | + if len(parts) != 3 { |
| 145 | + return "", fmt.Errorf("malformed rules file - expected single section marked with %s", marker) |
| 146 | + } |
| 147 | + newContent := parts[0] + marker + "\n" + content + "\n" + marker + parts[2] |
| 148 | + return newContent, nil |
| 149 | + } else { |
| 150 | + // Append new section |
| 151 | + newContent := existingRules |
| 152 | + if len(newContent) > 0 && !strings.HasSuffix(newContent, "\n") { |
| 153 | + newContent += "\n" |
| 154 | + } |
| 155 | + newContent += "\n" + marker + "\n" + content + "\n" + marker + "\n" |
| 156 | + return newContent, nil |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func tools(prefix string) []string { |
| 161 | + tools := []string{} |
| 162 | + for _, t := range mcpserver.Tools() { |
| 163 | + tools = append(tools, fmt.Sprintf("%s%s", prefix, t.Definition.Name)) |
| 164 | + } |
| 165 | + return tools |
| 166 | +} |
| 167 | + |
| 168 | +func init() { |
| 169 | + configCmd.AddCommand(configureCmd) |
| 170 | +} |
0 commit comments