docker_mcp.go

 1package config
 2
 3import (
 4	"context"
 5	"fmt"
 6	"os/exec"
 7	"time"
 8)
 9
10var dockerMCPVersionRunner = func(ctx context.Context) error {
11	cmd := exec.CommandContext(ctx, "docker", "mcp", "version")
12	return cmd.Run()
13}
14
15// DockerMCPName is the name of the Docker MCP configuration.
16const DockerMCPName = "docker"
17
18// IsDockerMCPAvailable checks if Docker MCP is available by running
19// 'docker mcp version'.
20func IsDockerMCPAvailable() bool {
21	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
22	defer cancel()
23
24	err := dockerMCPVersionRunner(ctx)
25	return err == nil
26}
27
28// IsDockerMCPEnabled checks if Docker MCP is already configured.
29func (c *Config) IsDockerMCPEnabled() bool {
30	if c.MCP == nil {
31		return false
32	}
33	_, exists := c.MCP[DockerMCPName]
34	return exists
35}
36
37// EnableDockerMCP adds Docker MCP configuration and persists it.
38func (s *ConfigStore) EnableDockerMCP() error {
39	if !IsDockerMCPAvailable() {
40		return fmt.Errorf("docker mcp is not available, please ensure docker is installed and 'docker mcp version' succeeds")
41	}
42
43	mcpConfig := MCPConfig{
44		Type:     MCPStdio,
45		Command:  "docker",
46		Args:     []string{"mcp", "gateway", "run"},
47		Disabled: false,
48	}
49
50	// Add to in-memory config.
51	if s.config.MCP == nil {
52		s.config.MCP = make(map[string]MCPConfig)
53	}
54	s.config.MCP[DockerMCPName] = mcpConfig
55
56	// Persist to config file.
57	if err := s.SetConfigField(ScopeGlobal, "mcp."+DockerMCPName, mcpConfig); err != nil {
58		return fmt.Errorf("failed to persist docker mcp configuration: %w", err)
59	}
60
61	return nil
62}
63
64// DisableDockerMCP removes Docker MCP configuration and persists the change.
65func (s *ConfigStore) DisableDockerMCP() error {
66	if s.config.MCP == nil {
67		return nil
68	}
69
70	// Remove from in-memory config.
71	delete(s.config.MCP, DockerMCPName)
72
73	// Persist the updated MCP map to the config file.
74	if err := s.SetConfigField(ScopeGlobal, "mcp", s.config.MCP); err != nil {
75		return fmt.Errorf("failed to persist docker mcp removal: %w", err)
76	}
77
78	return nil
79}