From a7bf8ad8fdffeb60ddc4b69b81e1e910a3a1504f Mon Sep 17 00:00:00 2001 From: Cody Rigney Date: Tue, 25 Nov 2025 10:22:49 -0500 Subject: [PATCH 1/2] Fix plugin prerun not running. --- cmd/docker-mcp/commands/catalog_next.go | 13 +--------- cmd/docker-mcp/commands/root.go | 32 +++++++++++++++++++++---- cmd/docker-mcp/commands/workingset.go | 13 +--------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/cmd/docker-mcp/commands/catalog_next.go b/cmd/docker-mcp/commands/catalog_next.go index 42ee735d..23162a6f 100644 --- a/cmd/docker-mcp/commands/catalog_next.go +++ b/cmd/docker-mcp/commands/catalog_next.go @@ -9,25 +9,14 @@ import ( catalognext "github.com/docker/mcp-gateway/pkg/catalog_next" "github.com/docker/mcp-gateway/pkg/db" - "github.com/docker/mcp-gateway/pkg/docker" - "github.com/docker/mcp-gateway/pkg/migrate" "github.com/docker/mcp-gateway/pkg/oci" "github.com/docker/mcp-gateway/pkg/workingset" ) -func catalogNextCommand(docker docker.Client) *cobra.Command { +func catalogNextCommand() *cobra.Command { cmd := &cobra.Command{ Use: "catalog-next", Short: "Manage catalogs (next generation)", - PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { - dao, err := db.New() - if err != nil { - return err - } - defer dao.Close() - migrate.MigrateConfig(cmd.Context(), docker, dao) - return nil - }, } cmd.AddCommand(createCatalogNextCommand()) diff --git a/cmd/docker-mcp/commands/root.go b/cmd/docker-mcp/commands/root.go index 24bf6eaf..783fd85e 100644 --- a/cmd/docker-mcp/commands/root.go +++ b/cmd/docker-mcp/commands/root.go @@ -3,14 +3,17 @@ package commands import ( "context" "os" + "slices" "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" "github.com/spf13/cobra" "github.com/docker/mcp-gateway/cmd/docker-mcp/version" + "github.com/docker/mcp-gateway/pkg/db" "github.com/docker/mcp-gateway/pkg/desktop" "github.com/docker/mcp-gateway/pkg/docker" + "github.com/docker/mcp-gateway/pkg/migrate" ) // Note: We use a custom help template to make it more brief. @@ -31,6 +34,8 @@ Examples: // Root returns the root command for the init plugin func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command { + dockerClient := docker.NewClient(dockerCli) + cmd := &cobra.Command{ Use: "mcp [OPTIONS]", Short: "Manage MCP servers and clients", @@ -46,6 +51,15 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command } if os.Getenv("DOCKER_MCP_IN_CONTAINER") != "1" { + if isSubcommandOf(cmd, []string{"catalog-next", "catalog", "profile"}) { + dao, err := db.New() + if err != nil { + return err + } + defer dao.Close() + migrate.MigrateConfig(cmd.Context(), dockerClient, dao) + } + runningInDockerCE, err := docker.RunningInDockerCE(ctx, dockerCli) if err != nil { return err @@ -68,11 +82,9 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command return []string{"--help"}, cobra.ShellCompDirectiveNoFileComp }) - dockerClient := docker.NewClient(dockerCli) - if isWorkingSetsFeatureEnabled(dockerCli) { - cmd.AddCommand(workingSetCommand(dockerClient)) - cmd.AddCommand(catalogNextCommand(dockerClient)) + cmd.AddCommand(workingSetCommand()) + cmd.AddCommand(catalogNextCommand()) } cmd.AddCommand(catalogCommand(dockerCli)) cmd.AddCommand(clientCommand(dockerCli, cwd)) @@ -101,3 +113,15 @@ func unhideHiddenCommands(cmd *cobra.Command) { unhideHiddenCommands(c) } } + +func isSubcommandOf(cmd *cobra.Command, names []string) bool { + if cmd == nil { + return false + } + + if slices.Contains(names, cmd.Name()) { + return true + } + + return isSubcommandOf(cmd.Parent(), names) +} diff --git a/cmd/docker-mcp/commands/workingset.go b/cmd/docker-mcp/commands/workingset.go index d1482014..f30576cf 100644 --- a/cmd/docker-mcp/commands/workingset.go +++ b/cmd/docker-mcp/commands/workingset.go @@ -9,29 +9,18 @@ import ( "github.com/docker/mcp-gateway/pkg/client" "github.com/docker/mcp-gateway/pkg/db" - "github.com/docker/mcp-gateway/pkg/docker" - "github.com/docker/mcp-gateway/pkg/migrate" "github.com/docker/mcp-gateway/pkg/oci" "github.com/docker/mcp-gateway/pkg/registryapi" "github.com/docker/mcp-gateway/pkg/sliceutil" "github.com/docker/mcp-gateway/pkg/workingset" ) -func workingSetCommand(docker docker.Client) *cobra.Command { +func workingSetCommand() *cobra.Command { cfg := client.ReadConfig() cmd := &cobra.Command{ Use: "profile", Short: "Manage profiles", - PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { - dao, err := db.New() - if err != nil { - return err - } - defer dao.Close() - migrate.MigrateConfig(cmd.Context(), docker, dao) - return nil - }, } cmd.AddCommand(exportWorkingSetCommand()) From 48f46efa3760bc0b3824f539d246866950627a3d Mon Sep 17 00:00:00 2001 From: Cody Rigney Date: Tue, 25 Nov 2025 10:34:06 -0500 Subject: [PATCH 2/2] Add feature check. --- cmd/docker-mcp/commands/root.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cmd/docker-mcp/commands/root.go b/cmd/docker-mcp/commands/root.go index 783fd85e..e9d5a441 100644 --- a/cmd/docker-mcp/commands/root.go +++ b/cmd/docker-mcp/commands/root.go @@ -51,13 +51,15 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command } if os.Getenv("DOCKER_MCP_IN_CONTAINER") != "1" { - if isSubcommandOf(cmd, []string{"catalog-next", "catalog", "profile"}) { - dao, err := db.New() - if err != nil { - return err + if isWorkingSetsFeatureEnabled(dockerCli) { + if isSubcommandOf(cmd, []string{"catalog-next", "catalog", "profile"}) { + dao, err := db.New() + if err != nil { + return err + } + defer dao.Close() + migrate.MigrateConfig(cmd.Context(), dockerClient, dao) } - defer dao.Close() - migrate.MigrateConfig(cmd.Context(), dockerClient, dao) } runningInDockerCE, err := docker.RunningInDockerCE(ctx, dockerCli)