|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 11 | + |
| 12 | + "github.com/docker/mcp-gateway/pkg/db" |
| 13 | + "github.com/docker/mcp-gateway/pkg/log" |
| 14 | + "github.com/docker/mcp-gateway/pkg/oci" |
| 15 | + "github.com/docker/mcp-gateway/pkg/workingset" |
| 16 | +) |
| 17 | + |
| 18 | +func createProfileHandler(g *Gateway) mcp.ToolHandler { |
| 19 | + return func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 20 | + // Parse parameters |
| 21 | + var params struct { |
| 22 | + Name string `json:"name"` |
| 23 | + } |
| 24 | + |
| 25 | + if req.Params.Arguments == nil { |
| 26 | + return nil, fmt.Errorf("missing arguments") |
| 27 | + } |
| 28 | + |
| 29 | + paramsBytes, err := json.Marshal(req.Params.Arguments) |
| 30 | + if err != nil { |
| 31 | + return nil, fmt.Errorf("failed to marshal arguments: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + if err := json.Unmarshal(paramsBytes, ¶ms); err != nil { |
| 35 | + return nil, fmt.Errorf("failed to parse arguments: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + if params.Name == "" { |
| 39 | + return nil, fmt.Errorf("name parameter is required") |
| 40 | + } |
| 41 | + |
| 42 | + profileName := params.Name |
| 43 | + |
| 44 | + // Create DAO and OCI service |
| 45 | + dao, err := db.New() |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("failed to create database client: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + ociService := oci.NewService() |
| 51 | + |
| 52 | + // Build the working set from current gateway state |
| 53 | + servers := make([]workingset.Server, 0, len(g.configuration.serverNames)) |
| 54 | + for _, serverName := range g.configuration.serverNames { |
| 55 | + catalogServer, found := g.configuration.servers[serverName] |
| 56 | + if !found { |
| 57 | + log.Logf("Warning: server %s not found in catalog, skipping", serverName) |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + // Determine server type based on whether it has an image |
| 62 | + serverType := workingset.ServerTypeImage |
| 63 | + if catalogServer.Image == "" { |
| 64 | + // Skip servers without images for now (registry servers) |
| 65 | + log.Logf("Warning: server %s has no image, skipping", serverName) |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + // Get config for this server |
| 70 | + serverConfig := g.configuration.config[serverName] |
| 71 | + if serverConfig == nil { |
| 72 | + serverConfig = make(map[string]any) |
| 73 | + } |
| 74 | + |
| 75 | + // Get tools for this server |
| 76 | + var serverTools []string |
| 77 | + if g.configuration.tools.ServerTools != nil { |
| 78 | + serverTools = g.configuration.tools.ServerTools[serverName] |
| 79 | + } |
| 80 | + |
| 81 | + // Create server entry |
| 82 | + server := workingset.Server{ |
| 83 | + Type: serverType, |
| 84 | + Image: catalogServer.Image, |
| 85 | + Config: serverConfig, |
| 86 | + Secrets: "default", |
| 87 | + Tools: serverTools, |
| 88 | + Snapshot: &workingset.ServerSnapshot{ |
| 89 | + Server: catalogServer, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + servers = append(servers, server) |
| 94 | + } |
| 95 | + |
| 96 | + if len(servers) == 0 { |
| 97 | + return &mcp.CallToolResult{ |
| 98 | + Content: []mcp.Content{&mcp.TextContent{ |
| 99 | + Text: "No servers with images found in current gateway state. Cannot create profile.", |
| 100 | + }}, |
| 101 | + IsError: true, |
| 102 | + }, nil |
| 103 | + } |
| 104 | + |
| 105 | + // Add default secrets |
| 106 | + secrets := make(map[string]workingset.Secret) |
| 107 | + secrets["default"] = workingset.Secret{ |
| 108 | + Provider: workingset.SecretProviderDockerDesktop, |
| 109 | + } |
| 110 | + |
| 111 | + // Check if profile already exists |
| 112 | + existingProfile, err := dao.GetWorkingSet(ctx, profileName) |
| 113 | + isUpdate := false |
| 114 | + profileID := profileName |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + if !errors.Is(err, sql.ErrNoRows) { |
| 118 | + return nil, fmt.Errorf("failed to check for existing profile: %w", err) |
| 119 | + } |
| 120 | + // Profile doesn't exist, we'll create it |
| 121 | + } else { |
| 122 | + // Profile exists, we'll update it |
| 123 | + isUpdate = true |
| 124 | + profileID = existingProfile.ID |
| 125 | + } |
| 126 | + |
| 127 | + // Create working set |
| 128 | + ws := workingset.WorkingSet{ |
| 129 | + Version: workingset.CurrentWorkingSetVersion, |
| 130 | + ID: profileID, |
| 131 | + Name: profileName, |
| 132 | + Servers: servers, |
| 133 | + Secrets: secrets, |
| 134 | + } |
| 135 | + |
| 136 | + // Ensure snapshots are resolved |
| 137 | + if err := ws.EnsureSnapshotsResolved(ctx, ociService); err != nil { |
| 138 | + return nil, fmt.Errorf("failed to resolve snapshots: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + // Validate the working set |
| 142 | + if err := ws.Validate(); err != nil { |
| 143 | + return &mcp.CallToolResult{ |
| 144 | + Content: []mcp.Content{&mcp.TextContent{ |
| 145 | + Text: fmt.Sprintf("Profile validation failed: %v", err), |
| 146 | + }}, |
| 147 | + IsError: true, |
| 148 | + }, nil |
| 149 | + } |
| 150 | + |
| 151 | + // Create or update the profile |
| 152 | + if isUpdate { |
| 153 | + if err := dao.UpdateWorkingSet(ctx, ws.ToDb()); err != nil { |
| 154 | + return nil, fmt.Errorf("failed to update profile: %w", err) |
| 155 | + } |
| 156 | + log.Logf("Updated profile %s with %d servers", profileID, len(servers)) |
| 157 | + } else { |
| 158 | + if err := dao.CreateWorkingSet(ctx, ws.ToDb()); err != nil { |
| 159 | + return nil, fmt.Errorf("failed to create profile: %w", err) |
| 160 | + } |
| 161 | + log.Logf("Created profile %s with %d servers", profileID, len(servers)) |
| 162 | + } |
| 163 | + |
| 164 | + // Build success message |
| 165 | + var resultMessage string |
| 166 | + if isUpdate { |
| 167 | + resultMessage = fmt.Sprintf("Successfully updated profile '%s' (ID: %s) with %d servers:\n", profileName, profileID, len(servers)) |
| 168 | + } else { |
| 169 | + resultMessage = fmt.Sprintf("Successfully created profile '%s' (ID: %s) with %d servers:\n", profileName, profileID, len(servers)) |
| 170 | + } |
| 171 | + |
| 172 | + // List the servers in the profile |
| 173 | + for i, server := range servers { |
| 174 | + serverName := server.Snapshot.Server.Name |
| 175 | + resultMessage += fmt.Sprintf("\n%d. %s", i+1, serverName) |
| 176 | + if server.Image != "" { |
| 177 | + resultMessage += fmt.Sprintf(" (image: %s)", server.Image) |
| 178 | + } |
| 179 | + if len(server.Tools) > 0 { |
| 180 | + resultMessage += fmt.Sprintf(" - %d tools", len(server.Tools)) |
| 181 | + } |
| 182 | + if len(server.Config) > 0 { |
| 183 | + resultMessage += " - configured" |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return &mcp.CallToolResult{ |
| 188 | + Content: []mcp.Content{&mcp.TextContent{ |
| 189 | + Text: resultMessage, |
| 190 | + }}, |
| 191 | + }, nil |
| 192 | + } |
| 193 | +} |
0 commit comments