|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/harness/harness-mcp/client" |
| 8 | + "github.com/harness/harness-mcp/client/dto" |
| 9 | + "github.com/harness/harness-mcp/cmd/harness-mcp-server/config" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +// ListSettingsTool creates a tool for listing settings in Harness |
| 15 | +func ListSettingsTool(config *config.Config, client *client.SettingsClient) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 16 | + return mcp.NewTool("list_settings", |
| 17 | + mcp.WithDescription("List settings in Harness with filtering options by category and group."), |
| 18 | + mcp.WithString("category", |
| 19 | + mcp.Required(), |
| 20 | + mcp.Description("Category to filter settings. Available categories: 'CD', 'CI', 'CE', 'CV', 'CF', 'STO', 'CORE', 'PMS', 'TEMPLATESERVICE', 'GOVERNANCE', 'CHAOS', 'SCIM', 'GIT_EXPERIENCE', 'CONNECTORS', 'EULA', 'NOTIFICATIONS', 'SUPPLY_CHAIN_ASSURANCE', 'USER', 'MODULES_VISIBILITY', 'DBOPS', 'IR'"), |
| 21 | + ), |
| 22 | + mcp.WithString("group", |
| 23 | + mcp.Description("Optional group to filter settings within a category"), |
| 24 | + ), |
| 25 | + mcp.WithBoolean("include_parent_scopes", |
| 26 | + mcp.Description("Flag to include the settings which only exist at the parent scopes"), |
| 27 | + ), |
| 28 | + WithScope(config, false), |
| 29 | + WithPagination(), |
| 30 | + ), |
| 31 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 32 | + scope, err := FetchScope(config, request, false) |
| 33 | + if err != nil { |
| 34 | + return mcp.NewToolResultError(err.Error()), nil |
| 35 | + } |
| 36 | + |
| 37 | + page, size, err := FetchPagination(request) |
| 38 | + if err != nil { |
| 39 | + return mcp.NewToolResultError(err.Error()), nil |
| 40 | + } |
| 41 | + |
| 42 | + category, err := OptionalParam[string](request, "category") |
| 43 | + if err != nil { |
| 44 | + return mcp.NewToolResultError(err.Error()), nil |
| 45 | + } |
| 46 | + |
| 47 | + group, err := OptionalParam[string](request, "group") |
| 48 | + if err != nil { |
| 49 | + return mcp.NewToolResultError(err.Error()), nil |
| 50 | + } |
| 51 | + |
| 52 | + // Default includeParentScopes to true |
| 53 | + includeParentScopes := true |
| 54 | + |
| 55 | + // Check if parameter was provided |
| 56 | + includeParentScopesParam, err := OptionalParam[bool](request, "include_parent_scopes") |
| 57 | + if err == nil { |
| 58 | + includeParentScopes = includeParentScopesParam |
| 59 | + } |
| 60 | + |
| 61 | + // Call the client to get the settings |
| 62 | + opts := &dto.SettingsListOptions{ |
| 63 | + Category: category, |
| 64 | + Group: group, |
| 65 | + IncludeParentScopes: includeParentScopes, |
| 66 | + PaginationOptions: dto.PaginationOptions{ |
| 67 | + Page: page, |
| 68 | + Size: size, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + response, err := client.List(ctx, scope, opts) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("failed to list settings: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + r, err := json.Marshal(response) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("failed to marshal settings list: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + return mcp.NewToolResultText(string(r)), nil |
| 83 | + } |
| 84 | +} |
0 commit comments