|
| 1 | +package clients |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "sort" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/spf13/pflag" |
| 14 | + "golang.org/x/exp/maps" |
| 15 | + "gopkg.in/yaml.v3" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed config.yml |
| 19 | +var configYaml string |
| 20 | + |
| 21 | +var ( |
| 22 | + getProjectRoot = findGitProjectRoot |
| 23 | + errNotInGitRepo = errors.New("not in a git repo") |
| 24 | +) |
| 25 | + |
| 26 | +func NewClientCmd(ctx context.Context, cwd string) *cobra.Command { |
| 27 | + cfg := readConfig() |
| 28 | + cmd := &cobra.Command{ |
| 29 | + Use: "client", |
| 30 | + Short: "Connect/disconnect MCP clients.", |
| 31 | + } |
| 32 | + cmd.AddCommand(ListCommand(ctx, cwd, *cfg)) |
| 33 | + cmd.AddCommand(ConnectCommand(ctx, cwd, *cfg)) |
| 34 | + cmd.AddCommand(DisconnectCommand(ctx, cwd, *cfg)) |
| 35 | + return cmd |
| 36 | +} |
| 37 | + |
| 38 | +type Config struct { |
| 39 | + System map[string]globalCfg `yaml:"system"` |
| 40 | + Project map[string]localCfg `yaml:"project"` |
| 41 | +} |
| 42 | + |
| 43 | +func readConfig() *Config { |
| 44 | + var result Config |
| 45 | + _ = yaml.Unmarshal([]byte(configYaml), &result) |
| 46 | + return &result |
| 47 | +} |
| 48 | + |
| 49 | +func addGlobalFlag(flags *pflag.FlagSet, p *bool) { |
| 50 | + flags.BoolVarP(p, "global", "g", false, "Change the system wide configuration or the clients setup in your current git repo.") |
| 51 | +} |
| 52 | + |
| 53 | +func addQuietFlag(flags *pflag.FlagSet, p *bool) { |
| 54 | + flags.BoolVarP(p, "quiet", "q", false, "Only display errors.") |
| 55 | +} |
| 56 | + |
| 57 | +func findGitProjectRoot(dir string) string { |
| 58 | + for { |
| 59 | + gitPath := filepath.Join(dir, ".git") |
| 60 | + if _, err := os.Stat(gitPath); err == nil { |
| 61 | + return dir |
| 62 | + } |
| 63 | + parent := filepath.Dir(dir) |
| 64 | + if parent == dir { |
| 65 | + break |
| 66 | + } |
| 67 | + dir = parent |
| 68 | + } |
| 69 | + return "" |
| 70 | +} |
| 71 | + |
| 72 | +func getSupportedMCPClients(cfg Config) []string { |
| 73 | + tmp := map[string]struct{}{ |
| 74 | + vendorGordon: {}, |
| 75 | + } |
| 76 | + for k := range cfg.System { |
| 77 | + tmp[k] = struct{}{} |
| 78 | + } |
| 79 | + for k := range cfg.Project { |
| 80 | + tmp[k] = struct{}{} |
| 81 | + } |
| 82 | + result := maps.Keys(tmp) |
| 83 | + sort.Strings(result) |
| 84 | + return result |
| 85 | +} |
| 86 | + |
| 87 | +type ErrVendorNotFound struct { |
| 88 | + global bool |
| 89 | + vendor string |
| 90 | + config Config |
| 91 | +} |
| 92 | + |
| 93 | +func (e *ErrVendorNotFound) Error() string { |
| 94 | + var alternative string |
| 95 | + if e.global { |
| 96 | + if _, ok := e.config.Project[e.vendor]; ok { |
| 97 | + alternative = " Did you mean to not use the --global flag?" |
| 98 | + } |
| 99 | + } else { |
| 100 | + if _, ok := e.config.System[e.vendor]; ok { |
| 101 | + alternative = " Did you mean to use the --global flag?" |
| 102 | + } |
| 103 | + } |
| 104 | + return "Vendor not found: " + e.vendor + "." + alternative |
| 105 | +} |
| 106 | + |
| 107 | +type Updater func(key string, server *MCPServerSTDIO) error |
| 108 | + |
| 109 | +func newMCPGatewayServer(client string) *MCPServerSTDIO { |
| 110 | + return &MCPServerSTDIO{ |
| 111 | + Command: "docker", |
| 112 | + Args: []string{"run", "-l", fmt.Sprintf("mcp.client=%s", client), "--rm", "-i", "alpine/socat", "STDIO", "TCP:host.docker.internal:8811"}, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func GetUpdater(vendor string, global bool, cwd string, config Config) (Updater, error) { |
| 117 | + if global { |
| 118 | + cfg, ok := config.System[vendor] |
| 119 | + if !ok { |
| 120 | + return nil, &ErrVendorNotFound{vendor: vendor, global: global, config: config} |
| 121 | + } |
| 122 | + processor, err := NewGlobalCfgProcessor(cfg) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + return processor.Update, nil |
| 127 | + } |
| 128 | + projectRoot := getProjectRoot(cwd) |
| 129 | + if projectRoot == "" { |
| 130 | + return nil, errNotInGitRepo |
| 131 | + } |
| 132 | + cfg, ok := config.Project[vendor] |
| 133 | + if !ok { |
| 134 | + return nil, &ErrVendorNotFound{vendor: vendor, global: global, config: config} |
| 135 | + } |
| 136 | + processor, err := NewLocalCfgProcessor(cfg, projectRoot) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + return processor.Update, nil |
| 141 | +} |
| 142 | + |
| 143 | +type MCPClientCfgBase struct { |
| 144 | + DisplayName string `json:"displayName"` |
| 145 | + ConfigName string `json:"configName"` |
| 146 | + IsMCPCatalogConnected bool `json:"dockerMCPCatalogConnected"` |
| 147 | + Err *CfgError `json:"error"` |
| 148 | + |
| 149 | + cfg *MCPJSONLists |
| 150 | +} |
| 151 | + |
| 152 | +func (c *MCPClientCfgBase) setParseResult(lists *MCPJSONLists, err error) { |
| 153 | + c.Err = classifyError(err) |
| 154 | + if lists != nil { |
| 155 | + if containsMCPDocker(lists.STDIOServers) { |
| 156 | + c.IsMCPCatalogConnected = true |
| 157 | + } |
| 158 | + } |
| 159 | + c.cfg = lists |
| 160 | +} |
0 commit comments