|
| 1 | +package harness |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/harness/harness-mcp/pkg/utils" |
| 8 | + "github.com/harness/harness-mcp/client" |
| 9 | + "github.com/harness/harness-mcp/client/dto" |
| 10 | + "github.com/harness/harness-mcp/cmd/harness-mcp-server/config" |
| 11 | + "github.com/mark3labs/mcp-go/mcp" |
| 12 | + "github.com/mark3labs/mcp-go/server" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// GetCcmOverview creates a tool for getting a ccm overview from an account |
| 17 | +func GetCcmOverviewTool(config *config.Config, client *client.CloudCostManagementService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 18 | + now := time.Now() |
| 19 | + defaultStartTime := utils.FormatUnixToMMDDYYYY(now.AddDate(0, 0, -60).Unix()) |
| 20 | + defaultEndTime:= utils.CurrentMMDDYYYY(); |
| 21 | + return mcp.NewTool("get_ccm_overview", |
| 22 | + mcp.WithDescription("Get an overview from an specific account in Harness Cloud Cost Management"), |
| 23 | + mcp.WithString("accountIdentifier", |
| 24 | + mcp.Description("The account identifier"), |
| 25 | + ), |
| 26 | + mcp.WithString("startTime", |
| 27 | + mcp.DefaultString(defaultStartTime), |
| 28 | + mcp.Description("Start time of the period in format MM/DD/YYYY. (e.g. 10/30/2025)"), |
| 29 | + ), |
| 30 | + mcp.WithString("endTime", |
| 31 | + mcp.DefaultString(defaultEndTime), |
| 32 | + mcp.Description("End time of the period in format MM/DD/YYYY. (e.g. 10/30/2025)"), |
| 33 | + ), |
| 34 | + mcp.WithString("groupBy", |
| 35 | + mcp.Description("Optional type to group by period"), |
| 36 | + mcp.DefaultString(dto.PeriodTypeHour), |
| 37 | + mcp.Enum(dto.PeriodTypeHour, dto.PeriodTypeDay, dto.PeriodTypeMonth, dto.PeriodTypeWeek, dto.PeriodTypeQuarter, dto.PeriodTypeYear), |
| 38 | + ), |
| 39 | + WithScope(config, false), |
| 40 | + ), |
| 41 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 42 | + accID, err := OptionalParam[string](request, "accountIdentifier") |
| 43 | + if accID == "" { |
| 44 | + accID, err = getAccountID(config, request) |
| 45 | + } |
| 46 | + if err != nil { |
| 47 | + return mcp.NewToolResultError(err.Error()), nil |
| 48 | + } |
| 49 | + |
| 50 | + startTimeStr, err := requiredParam[string](request, "startTime") |
| 51 | + if err != nil { |
| 52 | + return mcp.NewToolResultError(err.Error()), nil |
| 53 | + } |
| 54 | + |
| 55 | + endTimeStr, err := requiredParam[string](request, "endTime") |
| 56 | + if err != nil { |
| 57 | + return mcp.NewToolResultError(err.Error()), nil |
| 58 | + } |
| 59 | + |
| 60 | + startTime, err := utils.FormatMMDDYYYYToUnixMillis(startTimeStr) |
| 61 | + endTime, err := utils.FormatMMDDYYYYToUnixMillis(endTimeStr) |
| 62 | + |
| 63 | + groupBy, err := requiredParam[string](request, "groupBy") |
| 64 | + if err != nil { |
| 65 | + return mcp.NewToolResultError(err.Error()), nil |
| 66 | + } |
| 67 | + |
| 68 | + data, err := client.GetOverview(ctx, accID, startTime, endTime, groupBy) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to get CCM Overview: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + r, err := json.Marshal(data) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to marshal CCM Overview: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return mcp.NewToolResultText(string(r)), nil |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// getAccountID retrieves AccountID from the config file |
| 83 | +func getAccountID(config *config.Config, request mcp.CallToolRequest) (string, error) { |
| 84 | + scope, scopeErr := fetchScope(config, request, true) |
| 85 | + if scopeErr != nil { |
| 86 | + return "", nil |
| 87 | + } |
| 88 | + return scope.AccountID, nil |
| 89 | +} |
0 commit comments