|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/docker/mcp-gateway/pkg/db" |
| 11 | + "github.com/docker/mcp-gateway/pkg/workingset" |
| 12 | +) |
| 13 | + |
| 14 | +func workingSetCommand() *cobra.Command { |
| 15 | + cmd := &cobra.Command{ |
| 16 | + Use: "workingset", |
| 17 | + Short: "Manage working sets", |
| 18 | + } |
| 19 | + |
| 20 | + cmd.AddCommand(exportWorkingSetCommand()) |
| 21 | + cmd.AddCommand(importWorkingSetCommand()) |
| 22 | + cmd.AddCommand(showWorkingSetCommand()) |
| 23 | + cmd.AddCommand(listWorkingSetsCommand()) |
| 24 | + cmd.AddCommand(pushWorkingSetCommand()) |
| 25 | + cmd.AddCommand(pullWorkingSetCommand()) |
| 26 | + cmd.AddCommand(createWorkingSetCommand()) |
| 27 | + cmd.AddCommand(removeWorkingSetCommand()) |
| 28 | + return cmd |
| 29 | +} |
| 30 | + |
| 31 | +func createWorkingSetCommand() *cobra.Command { |
| 32 | + var opts struct { |
| 33 | + ID string |
| 34 | + Name string |
| 35 | + Servers []string |
| 36 | + } |
| 37 | + |
| 38 | + cmd := &cobra.Command{ |
| 39 | + Use: "create --name <name> [--id <id>] --server <ref1> --server <ref2> ...", |
| 40 | + Short: "Create a new working set of MCP servers", |
| 41 | + Long: `Create a new working set that groups multiple MCP servers together. |
| 42 | +A working set allows you to organize and manage related servers as a single unit. |
| 43 | +Working sets are decoupled from catalogs. Servers can be: |
| 44 | + - MCP Registry references (e.g. http://registry.modelcontextprotocol.io/v0/servers/312e45a4-2216-4b21-b9a8-0f1a51425073) |
| 45 | + - OCI image references with docker:// prefix (e.g., "docker://mcp/github:latest")`, |
| 46 | + Example: ` # Create a working-set with multiple servers (OCI references) |
| 47 | + docker mcp working-set create --name dev-tools --server docker://mcp/github:latest --server docker://mcp/slack:latest |
| 48 | +
|
| 49 | + # Create a working-set with MCP Registry references |
| 50 | + docker mcp working-set create --name registry-servers --server http://registry.modelcontextprotocol.io/v0/servers/71de5a2a-6cfb-4250-a196-f93080ecc860 |
| 51 | +
|
| 52 | + # Mix MCP Registry references and OCI references |
| 53 | + docker mcp working-set create --name mixed --server http://registry.modelcontextprotocol.io/v0/servers/71de5a2a-6cfb-4250-a196-f93080ecc860 --server docker://mcp/github:latest`, |
| 54 | + Args: cobra.NoArgs, |
| 55 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 56 | + dao, err := db.New() |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + return workingset.Create(cmd.Context(), dao, opts.ID, opts.Name, opts.Servers) |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + flags := cmd.Flags() |
| 65 | + flags.StringVar(&opts.Name, "name", "", "Name of the working set (required)") |
| 66 | + flags.StringVar(&opts.ID, "id", "", "ID of the working set (defaults to a slugified version of the name)") |
| 67 | + flags.StringArrayVar(&opts.Servers, "server", []string{}, "Server to include: catalog name or OCI reference with docker:// prefix (can be specified multiple times)") |
| 68 | + |
| 69 | + _ = cmd.MarkFlagRequired("name") |
| 70 | + |
| 71 | + return cmd |
| 72 | +} |
| 73 | + |
| 74 | +func listWorkingSetsCommand() *cobra.Command { |
| 75 | + format := string(workingset.OutputFormatHumanReadable) |
| 76 | + |
| 77 | + cmd := &cobra.Command{ |
| 78 | + Use: "list", |
| 79 | + Aliases: []string{"ls"}, |
| 80 | + Short: "List working sets", |
| 81 | + Args: cobra.NoArgs, |
| 82 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 83 | + supported := slices.Contains(workingset.SupportedFormats(), format) |
| 84 | + if !supported { |
| 85 | + return fmt.Errorf("unsupported format: %s", format) |
| 86 | + } |
| 87 | + dao, err := db.New() |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + return workingset.List(cmd.Context(), dao, workingset.OutputFormat(format)) |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + flags := cmd.Flags() |
| 96 | + flags.StringVar(&format, "format", string(workingset.OutputFormatHumanReadable), fmt.Sprintf("Supported: %s.", strings.Join(workingset.SupportedFormats(), ", "))) |
| 97 | + |
| 98 | + return cmd |
| 99 | +} |
| 100 | + |
| 101 | +func showWorkingSetCommand() *cobra.Command { |
| 102 | + format := string(workingset.OutputFormatHumanReadable) |
| 103 | + |
| 104 | + cmd := &cobra.Command{ |
| 105 | + Use: "show <working-set-id>", |
| 106 | + Short: "Show working set", |
| 107 | + Args: cobra.ExactArgs(1), |
| 108 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 109 | + supported := slices.Contains(workingset.SupportedFormats(), format) |
| 110 | + if !supported { |
| 111 | + return fmt.Errorf("unsupported format: %s", format) |
| 112 | + } |
| 113 | + dao, err := db.New() |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + return workingset.Show(cmd.Context(), dao, args[0], workingset.OutputFormat(format)) |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + flags := cmd.Flags() |
| 122 | + flags.StringVar(&format, "format", string(workingset.OutputFormatHumanReadable), fmt.Sprintf("Supported: %s.", strings.Join(workingset.SupportedFormats(), ", "))) |
| 123 | + |
| 124 | + return cmd |
| 125 | +} |
| 126 | + |
| 127 | +func pullWorkingSetCommand() *cobra.Command { |
| 128 | + return &cobra.Command{ |
| 129 | + Use: "pull <oci-reference>", |
| 130 | + Short: "Pull working set from OCI registry", |
| 131 | + Args: cobra.ExactArgs(1), |
| 132 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 133 | + dao, err := db.New() |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + return workingset.Pull(cmd.Context(), dao, args[0]) |
| 138 | + }, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func pushWorkingSetCommand() *cobra.Command { |
| 143 | + return &cobra.Command{ |
| 144 | + Use: "push <working-set-id> <oci-reference>", |
| 145 | + Short: "Push working set to OCI registry", |
| 146 | + Args: cobra.ExactArgs(2), |
| 147 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 148 | + dao, err := db.New() |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + return workingset.Push(cmd.Context(), dao, args[0], args[1]) |
| 153 | + }, |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func exportWorkingSetCommand() *cobra.Command { |
| 158 | + return &cobra.Command{ |
| 159 | + Use: "export <working-set-id> <output-file>", |
| 160 | + Short: "Export working set to file", |
| 161 | + Args: cobra.ExactArgs(2), |
| 162 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 163 | + dao, err := db.New() |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + return workingset.Export(cmd.Context(), dao, args[0], args[1]) |
| 168 | + }, |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func importWorkingSetCommand() *cobra.Command { |
| 173 | + return &cobra.Command{ |
| 174 | + Use: "import <input-file>", |
| 175 | + Short: "Import working set from file", |
| 176 | + Args: cobra.ExactArgs(1), |
| 177 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 178 | + dao, err := db.New() |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + return workingset.Import(cmd.Context(), dao, args[0]) |
| 183 | + }, |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func removeWorkingSetCommand() *cobra.Command { |
| 188 | + return &cobra.Command{ |
| 189 | + Use: "remove <working-set-id>", |
| 190 | + Aliases: []string{"rm"}, |
| 191 | + Short: "Remove a working set", |
| 192 | + Args: cobra.ExactArgs(1), |
| 193 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 194 | + dao, err := db.New() |
| 195 | + if err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + return workingset.Remove(cmd.Context(), dao, args[0]) |
| 199 | + }, |
| 200 | + } |
| 201 | +} |
0 commit comments