|
| 1 | +package harness |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "math" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/harness/harness-mcp/client" |
| 11 | + "github.com/harness/harness-mcp/cmd/harness-mcp-server/config" |
| 12 | + "github.com/mark3labs/mcp-go/mcp" |
| 13 | + "github.com/mark3labs/mcp-go/server" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + minPage = 0 |
| 18 | + maxPage = 1000 |
| 19 | + minSize = 1 |
| 20 | + maxSize = 1000 |
| 21 | +) |
| 22 | + |
| 23 | +func convertDateToMilliseconds(timestamp string) int64 { |
| 24 | + t, err := time.Parse(time.RFC3339, timestamp) |
| 25 | + if err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + |
| 29 | + year := t.Year() |
| 30 | + month := int(t.Month()) |
| 31 | + day := t.Day() |
| 32 | + hour := t.Hour() |
| 33 | + minute := t.Minute() |
| 34 | + second := t.Second() |
| 35 | + |
| 36 | + t = time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Local) |
| 37 | + |
| 38 | + // Convert to Unix milliseconds |
| 39 | + unixMillis := t.UnixNano() / int64(time.Millisecond) |
| 40 | + |
| 41 | + return unixMillis |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +func getCurrentTime() string { |
| 46 | + now := time.Now().UTC().Format(time.RFC3339) |
| 47 | + return now |
| 48 | +} |
| 49 | + |
| 50 | +func previousWeek() string { |
| 51 | + oneWeekAgo := time.Now().AddDate(0, 0, -7).UTC().Format(time.RFC3339) |
| 52 | + return oneWeekAgo |
| 53 | +} |
| 54 | + |
| 55 | +// ListAuditsOfUser creates a tool for listing the audit trail. |
| 56 | +func ListUserAuditTrailTool(config *config.Config, auditClient *client.AuditService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 57 | + return mcp.NewTool("list_user_audits", |
| 58 | + mcp.WithDescription("List the audit trail of the user."), |
| 59 | + mcp.WithString("user_id", |
| 60 | + mcp.Required(), |
| 61 | + mcp.Description("The user id(emailId) used to retrieve the audit trail."), |
| 62 | + ), |
| 63 | + mcp.WithString("start_time", |
| 64 | + mcp.Description("Optional start time in ISO 8601 format (e.g., '2025-07-10T08:00:00Z')"), |
| 65 | + mcp.DefaultString(previousWeek()), |
| 66 | + ), |
| 67 | + mcp.WithString("end_time", |
| 68 | + mcp.Description("Optional end time in ISO 8601 format (e.g., '2025-07-10T08:00:00Z')"), |
| 69 | + mcp.DefaultString(getCurrentTime()), |
| 70 | + ), |
| 71 | + WithScope(config, false), |
| 72 | + WithPagination(), |
| 73 | + ), |
| 74 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 75 | + userID, err := requiredParam[string](request, "user_id") |
| 76 | + if err != nil { |
| 77 | + return mcp.NewToolResultError(err.Error()), nil |
| 78 | + } |
| 79 | + |
| 80 | + scope, err := fetchScope(config, request, false) |
| 81 | + if err != nil { |
| 82 | + return mcp.NewToolResultError(err.Error()), nil |
| 83 | + } |
| 84 | + |
| 85 | + page, size, err := fetchPagination(request) |
| 86 | + if err != nil { |
| 87 | + return mcp.NewToolResultError(err.Error()), nil |
| 88 | + } |
| 89 | + |
| 90 | + page = int(math.Min(math.Max(float64(page), float64(minPage)), float64(maxPage))) |
| 91 | + size = int(math.Min(math.Max(float64(size), float64(minSize)), float64(maxSize))) |
| 92 | + |
| 93 | + startTime, _ := OptionalParam[string](request, "start_time") |
| 94 | + endTime, _ := OptionalParam[string](request, "end_time") |
| 95 | + |
| 96 | + startTimeMilliseconds := convertDateToMilliseconds(startTime) |
| 97 | + endTimeMilliseconds := convertDateToMilliseconds(endTime) |
| 98 | + |
| 99 | + data, err := auditClient.ListUserAuditTrail(ctx, scope, userID, page, size, startTimeMilliseconds, endTimeMilliseconds, nil) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to list the audit logs: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + r, err := json.Marshal(data) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to marshal the audit logs: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return mcp.NewToolResultText(string(r)), nil |
| 110 | + } |
| 111 | +} |
0 commit comments