|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "sort" |
| 8 | + |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | + |
| 11 | + "slices" |
| 12 | + |
| 13 | + "github.com/docker/mcp-gateway/cmd/docker-mcp/internal/catalog" |
| 14 | + "github.com/docker/mcp-gateway/cmd/docker-mcp/internal/config" |
| 15 | + "github.com/docker/mcp-gateway/cmd/docker-mcp/internal/docker" |
| 16 | +) |
| 17 | + |
| 18 | +func Disable(ctx context.Context, docker docker.Client, toolNames []string, serverName string) error { |
| 19 | + return update(ctx, docker, nil, toolNames, serverName) |
| 20 | +} |
| 21 | + |
| 22 | +func Enable(ctx context.Context, docker docker.Client, toolNames []string, serverName string) error { |
| 23 | + return update(ctx, docker, toolNames, nil, serverName) |
| 24 | +} |
| 25 | + |
| 26 | +func findServerByTool(mcpCatalog catalog.Catalog, toolName string) (string, error) { |
| 27 | + for serverName, server := range mcpCatalog.Servers { |
| 28 | + for _, tool := range server.Tools { |
| 29 | + if tool.Name == toolName { |
| 30 | + return serverName, nil |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + return "", fmt.Errorf("tool %q not found in any server", toolName) |
| 35 | +} |
| 36 | + |
| 37 | +func validateToolExistsInServer(mcpCatalog catalog.Catalog, serverName string, toolName string) error { |
| 38 | + if _, exists := mcpCatalog.Servers[serverName]; !exists { |
| 39 | + return fmt.Errorf("server %q not found in catalog", serverName) |
| 40 | + } |
| 41 | + |
| 42 | + for _, tool := range mcpCatalog.Servers[serverName].Tools { |
| 43 | + if tool.Name == toolName { |
| 44 | + return nil |
| 45 | + } |
| 46 | + } |
| 47 | + return fmt.Errorf("tool %q not found in server %q", toolName, serverName) |
| 48 | +} |
| 49 | + |
| 50 | +func update(ctx context.Context, docker docker.Client, add []string, remove []string, serverName string) error { |
| 51 | + toolsYAML, err := config.ReadTools(ctx, docker) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("reading tools: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + toolsConfig, err := config.ParseToolsConfig(toolsYAML) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("parsing tools: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + if toolsConfig.ServerTools == nil { |
| 62 | + toolsConfig.ServerTools = make(map[string][]string) |
| 63 | + } |
| 64 | + |
| 65 | + mcpCatalog, err := catalog.Get(ctx) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("reading catalog: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + for _, toolName := range add { |
| 71 | + var targetServerName string |
| 72 | + |
| 73 | + if serverName != "" { |
| 74 | + if err := validateToolExistsInServer(mcpCatalog, serverName, toolName); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + targetServerName = serverName |
| 78 | + } else { |
| 79 | + discoveredServerName, err := findServerByTool(mcpCatalog, toolName) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + targetServerName = discoveredServerName |
| 84 | + } |
| 85 | + |
| 86 | + toolAlreadyEnabled := slices.Contains(toolsConfig.ServerTools[targetServerName], toolName) |
| 87 | + if toolAlreadyEnabled { |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + toolsConfig.ServerTools[targetServerName] = append(toolsConfig.ServerTools[targetServerName], toolName) |
| 92 | + } |
| 93 | + |
| 94 | + for _, toolName := range remove { |
| 95 | + var targetServerName string |
| 96 | + |
| 97 | + if serverName != "" { |
| 98 | + if err := validateToolExistsInServer(mcpCatalog, serverName, toolName); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + targetServerName = serverName |
| 102 | + } else { |
| 103 | + discoveredServerName, err := findServerByTool(mcpCatalog, toolName) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + targetServerName = discoveredServerName |
| 108 | + } |
| 109 | + |
| 110 | + if _, exists := toolsConfig.ServerTools[targetServerName]; !exists { |
| 111 | + serverTools := mcpCatalog.Servers[targetServerName].Tools |
| 112 | + var allToolNames []string |
| 113 | + for _, tool := range serverTools { |
| 114 | + allToolNames = append(allToolNames, tool.Name) |
| 115 | + } |
| 116 | + |
| 117 | + toolsConfig.ServerTools[targetServerName] = []string{} |
| 118 | + for _, tool := range allToolNames { |
| 119 | + if tool != toolName { |
| 120 | + toolsConfig.ServerTools[targetServerName] = append(toolsConfig.ServerTools[targetServerName], tool) |
| 121 | + } |
| 122 | + } |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + tools := toolsConfig.ServerTools[targetServerName] |
| 127 | + newTools := make([]string, 0, len(tools)) |
| 128 | + for _, tool := range tools { |
| 129 | + if tool != toolName { |
| 130 | + newTools = append(newTools, tool) |
| 131 | + } |
| 132 | + } |
| 133 | + toolsConfig.ServerTools[targetServerName] = newTools |
| 134 | + } |
| 135 | + |
| 136 | + for serverName := range toolsConfig.ServerTools { |
| 137 | + sort.Strings(toolsConfig.ServerTools[serverName]) |
| 138 | + } |
| 139 | + |
| 140 | + var buf bytes.Buffer |
| 141 | + encoder := yaml.NewEncoder(&buf) |
| 142 | + encoder.SetIndent(2) |
| 143 | + if err := encoder.Encode(toolsConfig.ServerTools); err != nil { |
| 144 | + return fmt.Errorf("encoding tools: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + if err := config.WriteTools(buf.Bytes()); err != nil { |
| 148 | + return fmt.Errorf("writing tools: %w", err) |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
0 commit comments