|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + sdkmcp "github.com/modelcontextprotocol/go-sdk/mcp" |
| 11 | + |
| 12 | + backendapp "github.com/slok/sloth/internal/http/backend/app" |
| 13 | + "github.com/slok/sloth/internal/http/backend/model" |
| 14 | + "github.com/slok/sloth/internal/log" |
| 15 | +) |
| 16 | + |
| 17 | +type SLIAvailabilityRangeLister interface { |
| 18 | + ListSLIAvailabilityRange(ctx context.Context, req backendapp.ListSLIAvailabilityRangeRequest) (*backendapp.ListSLIAvailabilityRangeResponse, error) |
| 19 | +} |
| 20 | + |
| 21 | +func NewGetSLOSLIAvailabilityRangeTool(app SLIAvailabilityRangeLister, logger log.Logger) (*sdkmcp.Tool, sdkmcp.ToolHandlerFor[GetSLOSLIAvailabilityRangeToolInput, GetSLOSLIAvailabilityRangeToolOutput]) { |
| 22 | + if logger == nil { |
| 23 | + logger = log.Noop |
| 24 | + } |
| 25 | + |
| 26 | + return &sdkmcp.Tool{ |
| 27 | + Name: "get_slo_sli_availability_range", |
| 28 | + Description: "Get SLI availability evolution for an SLO over a time range. Returns a compressed availability series where each comma-separated entry advances by one step from start_ts, and x means the value is missing at that step.", |
| 29 | + Annotations: &sdkmcp.ToolAnnotations{ReadOnlyHint: true}, |
| 30 | + }, newGetSLOSLIAvailabilityRangeToolHandler(app, logger) |
| 31 | +} |
| 32 | + |
| 33 | +type GetSLOSLIAvailabilityRangeToolInput struct { |
| 34 | + SLOID string `json:"slo_id" jsonschema:"required,The SLO ID to retrieve"` |
| 35 | + From string `json:"from" jsonschema:"required,The RFC3339 start timestamp of the availability range"` |
| 36 | + To string `json:"to,omitempty" jsonschema:"Optional RFC3339 end timestamp of the availability range. If omitted, now is used"` |
| 37 | +} |
| 38 | + |
| 39 | +type GetSLOSLIAvailabilityRangeToolOutput struct { |
| 40 | + StartTS string `json:"start_ts" jsonschema:"the RFC3339 timestamp of the first point in the series"` |
| 41 | + Step string `json:"step" jsonschema:"the fixed duration between series points"` |
| 42 | + AvailabilitySeries string `json:"availability_series" jsonschema:"comma-separated SLI availability percentage values from start_ts advancing by step. Use x when a value is missing"` |
| 43 | +} |
| 44 | + |
| 45 | +func newGetSLOSLIAvailabilityRangeToolHandler(app SLIAvailabilityRangeLister, logger log.Logger) sdkmcp.ToolHandlerFor[GetSLOSLIAvailabilityRangeToolInput, GetSLOSLIAvailabilityRangeToolOutput] { |
| 46 | + return func(ctx context.Context, _ *sdkmcp.CallToolRequest, input GetSLOSLIAvailabilityRangeToolInput) (*sdkmcp.CallToolResult, GetSLOSLIAvailabilityRangeToolOutput, error) { |
| 47 | + logger.WithValues(log.Kv{"input": fmt.Sprintf("%+v", input)}).Debugf("MCP tool called") |
| 48 | + |
| 49 | + from, err := time.Parse(time.RFC3339, input.From) |
| 50 | + if err != nil { |
| 51 | + return nil, GetSLOSLIAvailabilityRangeToolOutput{}, fmt.Errorf("invalid from time: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + to := time.Time{} |
| 55 | + if input.To != "" { |
| 56 | + to, err = time.Parse(time.RFC3339, input.To) |
| 57 | + if err != nil { |
| 58 | + return nil, GetSLOSLIAvailabilityRangeToolOutput{}, fmt.Errorf("invalid to time: %w", err) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + resp, err := app.ListSLIAvailabilityRange(ctx, backendapp.ListSLIAvailabilityRangeRequest{ |
| 63 | + SLOID: input.SLOID, |
| 64 | + From: from, |
| 65 | + To: to, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return nil, GetSLOSLIAvailabilityRangeToolOutput{}, err |
| 69 | + } |
| 70 | + |
| 71 | + startTS, step := getAvailabilitySeriesMeta(resp.AvailabilityDataPoints) |
| 72 | + |
| 73 | + return nil, GetSLOSLIAvailabilityRangeToolOutput{ |
| 74 | + StartTS: startTS, |
| 75 | + Step: step, |
| 76 | + AvailabilitySeries: compressAvailabilityDataPoints(resp.AvailabilityDataPoints), |
| 77 | + }, nil |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func getAvailabilitySeriesMeta(dps []model.DataPoint) (startTS, step string) { |
| 82 | + if len(dps) == 0 { |
| 83 | + return "", "" |
| 84 | + } |
| 85 | + |
| 86 | + startTS = dps[0].TS.Format(time.RFC3339) |
| 87 | + if len(dps) < 2 { |
| 88 | + return startTS, "0s" |
| 89 | + } |
| 90 | + |
| 91 | + return startTS, dps[1].TS.Sub(dps[0].TS).String() |
| 92 | +} |
| 93 | + |
| 94 | +func compressAvailabilityDataPoints(dps []model.DataPoint) string { |
| 95 | + parts := make([]string, 0, len(dps)) |
| 96 | + for _, dp := range dps { |
| 97 | + if dp.Missing { |
| 98 | + parts = append(parts, "x") |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + parts = append(parts, strconv.FormatFloat(dp.Value, 'f', -1, 64)) |
| 103 | + } |
| 104 | + |
| 105 | + return strings.Join(parts, ",") |
| 106 | +} |
0 commit comments