-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathconnect_cmd.go
More file actions
293 lines (253 loc) · 7.44 KB
/
Copy pathconnect_cmd.go
File metadata and controls
293 lines (253 loc) · 7.44 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
package main
import (
"fmt"
"strings"
"github.com/spf13/cobra"
clioutput "github.com/smart-mcp-proxy/mcpproxy-go/internal/cli/output"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/connect"
)
var (
connectList bool
connectAll bool
connectForce bool
connectServerName string
)
// GetConnectCommand returns the connect parent command.
func GetConnectCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "connect [client]",
Short: "Register MCPProxy in a client's MCP configuration",
Long: `Register MCPProxy as an MCP server in the configuration file of supported
AI coding clients. This modifies the client's config file to add an HTTP/SSE
entry pointing to the running MCPProxy instance.
Supported clients: claude-code, cursor, windsurf, vscode, codex, gemini, opencode
A backup of the original config file is created before any modification.
Examples:
mcpproxy connect --list # Show all clients and their status
mcpproxy connect claude-code # Register in Claude Code
mcpproxy connect cursor --force # Overwrite existing entry
mcpproxy connect codex --name my-proxy # Custom server name
mcpproxy connect opencode # Register in OpenCode
mcpproxy connect --all # Register in all supported clients`,
Args: cobra.MaximumNArgs(1),
RunE: runConnect,
}
cmd.Flags().BoolVar(&connectList, "list", false, "List all clients and their connection status")
cmd.Flags().BoolVar(&connectAll, "all", false, "Connect to all supported clients")
cmd.Flags().BoolVar(&connectForce, "force", false, "Overwrite existing entry")
cmd.Flags().StringVar(&connectServerName, "name", "", "Server name in client config (default: mcpproxy)")
return cmd
}
// GetDisconnectCommand returns the disconnect command.
func GetDisconnectCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "disconnect <client>",
Short: "Remove MCPProxy from a client's MCP configuration",
Long: `Remove the MCPProxy entry from the specified client's configuration file.
A backup of the original config file is created before any modification.
Examples:
mcpproxy disconnect claude-code
mcpproxy disconnect cursor --name my-proxy`,
Args: cobra.ExactArgs(1),
RunE: runDisconnect,
}
cmd.Flags().StringVar(&connectServerName, "name", "", "Server name to remove (default: mcpproxy)")
return cmd
}
func runConnect(cmd *cobra.Command, args []string) error {
cfg, err := loadConnectConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
svc := connect.NewService(cfg.Listen, cfg.APIKey)
format := clioutput.ResolveFormat(globalOutputFormat, globalJSONOutput)
formatter, err := clioutput.NewFormatter(format)
if err != nil {
return err
}
// --list mode
if connectList {
return printConnectStatus(svc, formatter, format)
}
// --all mode
if connectAll {
return connectAllClients(svc, formatter, format)
}
// Single client mode
if len(args) == 0 {
return fmt.Errorf("client ID is required (or use --list / --all)")
}
clientID := args[0]
result, err := svc.Connect(clientID, connectServerName, connectForce)
if err != nil {
return err
}
return printConnectResult(result, formatter, format)
}
func runDisconnect(cmd *cobra.Command, args []string) error {
cfg, err := loadConnectConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
svc := connect.NewService(cfg.Listen, cfg.APIKey)
format := clioutput.ResolveFormat(globalOutputFormat, globalJSONOutput)
formatter, err := clioutput.NewFormatter(format)
if err != nil {
return err
}
clientID := args[0]
result, err := svc.Disconnect(clientID, connectServerName)
if err != nil {
return err
}
return printConnectResult(result, formatter, format)
}
func printConnectStatus(svc *connect.Service, formatter clioutput.OutputFormatter, format string) error {
// Spec 075: GetAllStatus is content-read-free and leaves Connected/AccessState
// unresolved. Running `mcpproxy connect` is an explicit user action, so resolve
// each supported+installed client's connected state on demand via GetStatus to
// preserve the CONNECTED column. Unsupported/absent clients keep the cheap
// metadata-only listing (no content read).
statuses := svc.GetAllStatus()
for i := range statuses {
if statuses[i].Supported && statuses[i].Exists {
if st, err := svc.GetStatus(statuses[i].ID); err == nil {
statuses[i] = st
}
}
}
if format == "table" {
headers := []string{"CLIENT", "STATUS", "CONFIG PATH", "CONNECTED"}
var rows [][]string
for _, s := range statuses {
status := "supported"
if !s.Supported {
status = "unsupported"
}
connected := "-"
if s.Supported {
if s.Connected {
connected = "yes"
} else if s.Exists {
connected = "no"
} else {
connected = "no (no config)"
}
}
cfgPath := s.ConfigPath
if len(cfgPath) > 50 {
cfgPath = "..." + cfgPath[len(cfgPath)-47:]
}
rows = append(rows, []string{s.Name, status, cfgPath, connected})
}
out, err := formatter.FormatTable(headers, rows)
if err != nil {
return err
}
fmt.Print(out)
return nil
}
// JSON or YAML
out, err := formatter.Format(statuses)
if err != nil {
return err
}
fmt.Println(out)
return nil
}
func connectAllClients(svc *connect.Service, formatter clioutput.OutputFormatter, format string) error {
clients := connect.GetAllClients()
var results []*connect.ConnectResult
var errors []string
for _, c := range clients {
if !c.Supported {
continue
}
result, err := svc.Connect(c.ID, connectServerName, connectForce)
if err != nil {
errors = append(errors, fmt.Sprintf("%s: %v", c.Name, err))
continue
}
results = append(results, result)
}
if format == "table" {
headers := []string{"CLIENT", "ACTION", "MESSAGE"}
var rows [][]string
for _, r := range results {
client := connect.FindClient(r.Client)
name := r.Client
if client != nil {
name = client.Name
}
rows = append(rows, []string{name, r.Action, r.Message})
}
for _, e := range errors {
parts := strings.SplitN(e, ": ", 2)
msg := e
clientName := "unknown"
if len(parts) == 2 {
clientName = parts[0]
msg = parts[1]
}
rows = append(rows, []string{clientName, "error", msg})
}
out, err := formatter.FormatTable(headers, rows)
if err != nil {
return err
}
fmt.Print(out)
return nil
}
// JSON/YAML output
out, err := formatter.Format(map[string]interface{}{
"results": results,
"errors": errors,
})
if err != nil {
return err
}
fmt.Println(out)
return nil
}
func printConnectResult(result *connect.ConnectResult, formatter clioutput.OutputFormatter, format string) error {
if format == "table" {
if result.Success {
fmt.Printf("%s\n", result.Message)
if result.BackupPath != "" {
fmt.Printf("Backup: %s\n", result.BackupPath)
}
fmt.Printf("Config: %s\n", result.ConfigPath)
} else {
fmt.Printf("Failed: %s\n", result.Message)
}
return nil
}
// JSON/YAML
out, err := formatter.Format(result)
if err != nil {
return err
}
fmt.Println(out)
return nil
}
func loadConnectConfig() (*config.Config, error) {
if configFile != "" {
cfg, err := config.LoadFromFile(configFile)
if err != nil {
return nil, err
}
if dataDir != "" {
cfg.DataDir = dataDir
}
return cfg, nil
}
cfg, err := config.Load()
if err != nil {
return nil, err
}
if dataDir != "" {
cfg.DataDir = dataDir
}
return cfg, nil
}