|
4 | 4 | "context"
|
5 | 5 | "encoding/json"
|
6 | 6 | "fmt"
|
7 |
| - |
| 7 | + |
8 | 8 | "github.com/harness/harness-mcp/client"
|
| 9 | + "github.com/harness/harness-mcp/client/dto" |
9 | 10 | "github.com/harness/harness-mcp/cmd/harness-mcp-server/config"
|
10 | 11 | "github.com/mark3labs/mcp-go/mcp"
|
11 | 12 | "github.com/mark3labs/mcp-go/server"
|
@@ -46,3 +47,142 @@ func GetSecretTool(config *config.Config, client *client.SecretsClient) (tool mc
|
46 | 47 | return mcp.NewToolResultText(string(r)), nil
|
47 | 48 | }
|
48 | 49 | }
|
| 50 | + |
| 51 | +// Secret types supported by the Harness secrets API |
| 52 | +const ( |
| 53 | + SecretTypeSecretFile = "SecretFile" |
| 54 | + SecretTypeSecretText = "SecretText" |
| 55 | + SecretTypeSSHKey = "SSHKey" |
| 56 | + SecretTypeWinRmCredentials = "WinRmCredentials" |
| 57 | +) |
| 58 | + |
| 59 | +// Sort fields for secrets |
| 60 | +const ( |
| 61 | + SortByName = "name" |
| 62 | + SortByIdentifier = "identifier" |
| 63 | + SortByCreated = "created" |
| 64 | + SortByUpdated = "updated" |
| 65 | +) |
| 66 | + |
| 67 | +// ListSecretsTool creates a tool for listing secrets from Harness |
| 68 | +func ListSecretsTool(config *config.Config, client *client.SecretsClient) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 69 | + return mcp.NewTool("list_secrets", |
| 70 | + mcp.WithDescription("List secrets from Harness with filtering and pagination options."), |
| 71 | + mcp.WithArray("secret", |
| 72 | + mcp.WithStringItems(), |
| 73 | + mcp.Description("Identifier field of secrets"), |
| 74 | + ), |
| 75 | + mcp.WithArray("type", |
| 76 | + mcp.WithStringItems(), |
| 77 | + mcp.Description("Secret types on which the filter will be applied"), |
| 78 | + mcp.Enum( |
| 79 | + SecretTypeSecretFile, |
| 80 | + SecretTypeSecretText, |
| 81 | + SecretTypeSSHKey, |
| 82 | + SecretTypeWinRmCredentials, |
| 83 | + ), |
| 84 | + ), |
| 85 | + mcp.WithBoolean("recursive", |
| 86 | + mcp.Description("Expand current scope to include all child scopes"), |
| 87 | + ), |
| 88 | + mcp.WithString("search_term", |
| 89 | + mcp.Description("Filter resources having attributes matching with search term"), |
| 90 | + ), |
| 91 | + mcp.WithString("filter_type", |
| 92 | + mcp.Description("Type of resources to filter"), |
| 93 | + mcp.Enum( |
| 94 | + "Secret", "Connector", "DelegateProfile", "Delegate", "PipelineSetup", |
| 95 | + "PipelineExecution", "Deployment", "Audit", "Template", "Trigger", |
| 96 | + "EnvironmentGroup", "FileStore", "CCMRecommendation", "Anomaly", |
| 97 | + "RIInventory", "SPInventory", "Autocud", "CCMConnector", |
| 98 | + "CCMK8sConnector", "Environment", "RuleExecution", "Override", |
| 99 | + "InputSet", "Webhook", |
| 100 | + ), |
| 101 | + ), |
| 102 | + WithScope(config, false), |
| 103 | + WithPagination(), |
| 104 | + ), |
| 105 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 106 | + scope, err := FetchScope(config, request, false) |
| 107 | + if err != nil { |
| 108 | + return mcp.NewToolResultError(err.Error()), nil |
| 109 | + } |
| 110 | + |
| 111 | + // Get pagination parameters |
| 112 | + page, size, err := FetchPagination(request) |
| 113 | + if err != nil { |
| 114 | + return mcp.NewToolResultError(err.Error()), nil |
| 115 | + } |
| 116 | + |
| 117 | + // Get filter parameters |
| 118 | + secretIds, err := OptionalAnyArrayParam(request, "secret") |
| 119 | + if err != nil { |
| 120 | + return mcp.NewToolResultError(err.Error()), nil |
| 121 | + } |
| 122 | + // Convert []any to []string |
| 123 | + secretIdsStr := make([]string, 0, len(secretIds)) |
| 124 | + for _, id := range secretIds { |
| 125 | + if str, ok := id.(string); ok { |
| 126 | + secretIdsStr = append(secretIdsStr, str) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + secretTypes, err := OptionalAnyArrayParam(request, "type") |
| 131 | + if err != nil { |
| 132 | + return mcp.NewToolResultError(err.Error()), nil |
| 133 | + } |
| 134 | + // Convert []any to []string |
| 135 | + secretTypesStr := make([]string, 0, len(secretTypes)) |
| 136 | + for _, t := range secretTypes { |
| 137 | + if str, ok := t.(string); ok { |
| 138 | + secretTypesStr = append(secretTypesStr, str) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + recursive, err := OptionalParam[bool](request, "recursive") |
| 143 | + if err != nil { |
| 144 | + return mcp.NewToolResultError(err.Error()), nil |
| 145 | + } |
| 146 | + |
| 147 | + searchTerm, err := OptionalParam[string](request, "search_term") |
| 148 | + if err != nil { |
| 149 | + return mcp.NewToolResultError(err.Error()), nil |
| 150 | + } |
| 151 | + |
| 152 | + // Temporarily removing sort orders due to API compatibility issues |
| 153 | + // We'll revisit this once we have more information about the expected format |
| 154 | + sortOrders := []string{} |
| 155 | + |
| 156 | + // Get filter_type parameter |
| 157 | + filterType, err := OptionalParam[string](request, "filter_type") |
| 158 | + if err != nil { |
| 159 | + return mcp.NewToolResultError(err.Error()), nil |
| 160 | + } |
| 161 | + |
| 162 | + // Create filter properties |
| 163 | + filters := dto.SecretFilterProperties{ |
| 164 | + SecretTypes: secretTypesStr, |
| 165 | + SearchTerm: searchTerm, |
| 166 | + IncludeSecretsFromEverySubScope: recursive, |
| 167 | + FilterType: filterType, |
| 168 | + } |
| 169 | + |
| 170 | + // If secretIds is provided, use the first one as secretIdentifier |
| 171 | + if len(secretIdsStr) > 0 { |
| 172 | + filters.SecretIdentifier = secretIdsStr[0] |
| 173 | + } |
| 174 | + |
| 175 | + // Call the client to list secrets |
| 176 | + response, err := client.ListSecrets(ctx, scope, page, size, sortOrders, filters) |
| 177 | + if err != nil { |
| 178 | + return nil, fmt.Errorf("failed to list secrets: %w", err) |
| 179 | + } |
| 180 | + |
| 181 | + r, err := json.Marshal(response) |
| 182 | + if err != nil { |
| 183 | + return nil, fmt.Errorf("failed to marshal list secrets response: %w", err) |
| 184 | + } |
| 185 | + |
| 186 | + return mcp.NewToolResultText(string(r)), nil |
| 187 | + } |
| 188 | +} |
0 commit comments