|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/mark3labs/mcp-go/mcp" |
| 9 | + "github.com/sysdiglabs/sysdig-mcp-server/internal/infra/clock" |
| 10 | + "github.com/sysdiglabs/sysdig-mcp-server/internal/infra/sysdig" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + maxIntervalEnvVar = "SYSDIG_MCP_MAX_INTERVAL" |
| 15 | + defaultMaxInterval = 168 * time.Hour // 7 days |
| 16 | + futureClockSkewGrace = 60 * time.Second |
| 17 | + windowedQueryTimeout = "60s" |
| 18 | + timeParamStart = "start" |
| 19 | + timeParamEnd = "end" |
| 20 | + startParamDescription = "Start of the query window as an RFC3339 timestamp (e.g. 2026-04-01T00:00:00Z). When omitted, the tool returns an instant snapshot (current behavior). When provided without end, end defaults to now." |
| 21 | + endParamDescription = "End of the query window as an RFC3339 timestamp (e.g. 2026-04-01T01:00:00Z). Requires start. Must not be more than 60s in the future." |
| 22 | +) |
| 23 | + |
| 24 | +// TimeWindow is a resolved, validated [Start, End] pair for a historical PromQL query. |
| 25 | +// A zero-value TimeWindow means no window was requested — the caller should emit its |
| 26 | +// existing instant query and leave GetQueryV1Params.Time nil. |
| 27 | +type TimeWindow struct { |
| 28 | + Start time.Time |
| 29 | + End time.Time |
| 30 | +} |
| 31 | + |
| 32 | +// IsZero reports whether no time window was requested. |
| 33 | +func (w TimeWindow) IsZero() bool { |
| 34 | + return w.Start.IsZero() && w.End.IsZero() |
| 35 | +} |
| 36 | + |
| 37 | +// RangeSelector returns the PromQL range-selector literal for this window, e.g. "[3600s]". |
| 38 | +// The duration is rounded down to whole seconds so the selector is stable and debuggable. |
| 39 | +func (w TimeWindow) RangeSelector() string { |
| 40 | + return fmt.Sprintf("[%ds]", int64(w.End.Sub(w.Start).Seconds())) |
| 41 | +} |
| 42 | + |
| 43 | +// EvalTime returns a *sysdig.Time suitable for GetQueryV1Params.Time. The value is |
| 44 | +// the End instant as unix seconds — the native format accepted by Sysdig's internal |
| 45 | +// PromQL stack (confirmed against backend PrometheusFacadeController.java:113). |
| 46 | +func (w TimeWindow) EvalTime() (*sysdig.Time, error) { |
| 47 | + if w.IsZero() { |
| 48 | + return nil, nil |
| 49 | + } |
| 50 | + var qt sysdig.Time |
| 51 | + if err := qt.FromQueryTime1(w.End.Unix()); err != nil { |
| 52 | + return nil, fmt.Errorf("building eval time: %w", err) |
| 53 | + } |
| 54 | + return &qt, nil |
| 55 | +} |
| 56 | + |
| 57 | +// WithTimeWindowParams returns a ToolOption that declares the shared "start" and "end" |
| 58 | +// RFC3339 parameters on a tool. |
| 59 | +func WithTimeWindowParams() mcp.ToolOption { |
| 60 | + return func(t *mcp.Tool) { |
| 61 | + mcp.WithString(timeParamStart, mcp.Description(startParamDescription))(t) |
| 62 | + mcp.WithString(timeParamEnd, mcp.Description(endParamDescription))(t) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// ParseTimeWindow reads "start" and "end" from the request, validates them, and returns |
| 67 | +// the resolved TimeWindow. |
| 68 | +// |
| 69 | +// - Both absent: returns zero-value TimeWindow, nil error. |
| 70 | +// - end without start: error ("end requires start"). |
| 71 | +// - start without end: end = clk.Now(). |
| 72 | +// - invalid RFC3339: error naming the bad field. |
| 73 | +// - end <= start: error. |
| 74 | +// - end > clk.Now() + 60s: error (generous grace for client clock skew). |
| 75 | +// - end - start > maxInterval(): error referencing SYSDIG_MCP_MAX_INTERVAL. |
| 76 | +func ParseTimeWindow(request mcp.CallToolRequest, clk clock.Clock) (TimeWindow, error) { |
| 77 | + startStr := mcp.ParseString(request, timeParamStart, "") |
| 78 | + endStr := mcp.ParseString(request, timeParamEnd, "") |
| 79 | + |
| 80 | + if startStr == "" && endStr == "" { |
| 81 | + return TimeWindow{}, nil |
| 82 | + } |
| 83 | + |
| 84 | + if startStr == "" && endStr != "" { |
| 85 | + return TimeWindow{}, fmt.Errorf("end requires start") |
| 86 | + } |
| 87 | + |
| 88 | + start, err := time.Parse(time.RFC3339, startStr) |
| 89 | + if err != nil { |
| 90 | + return TimeWindow{}, fmt.Errorf("invalid start timestamp %q: must be RFC3339 (e.g. 2026-04-01T00:00:00Z)", startStr) |
| 91 | + } |
| 92 | + |
| 93 | + now := clk.Now() |
| 94 | + |
| 95 | + var end time.Time |
| 96 | + if endStr == "" { |
| 97 | + end = now |
| 98 | + } else { |
| 99 | + end, err = time.Parse(time.RFC3339, endStr) |
| 100 | + if err != nil { |
| 101 | + return TimeWindow{}, fmt.Errorf("invalid end timestamp %q: must be RFC3339 (e.g. 2026-04-01T01:00:00Z)", endStr) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if !end.After(start) { |
| 106 | + return TimeWindow{}, fmt.Errorf("end (%s) must be after start (%s)", end.Format(time.RFC3339), start.Format(time.RFC3339)) |
| 107 | + } |
| 108 | + |
| 109 | + if end.After(now.Add(futureClockSkewGrace)) { |
| 110 | + return TimeWindow{}, fmt.Errorf("end (%s) must not be more than %s in the future (server time: %s)", end.Format(time.RFC3339), futureClockSkewGrace, now.Format(time.RFC3339)) |
| 111 | + } |
| 112 | + |
| 113 | + window := end.Sub(start) |
| 114 | + if max := maxInterval(); window > max { |
| 115 | + return TimeWindow{}, fmt.Errorf("requested window (%s) exceeds maximum allowed (%s); set %s to raise the cap", window, max, maxIntervalEnvVar) |
| 116 | + } |
| 117 | + |
| 118 | + return TimeWindow{Start: start, End: end}, nil |
| 119 | +} |
| 120 | + |
| 121 | +// maxInterval returns the configured maximum window duration. The SYSDIG_MCP_MAX_INTERVAL |
| 122 | +// env var is read on every call (not cached) so tests can override it via t.Setenv without |
| 123 | +// bumping into sync.Once-style staleness. |
| 124 | +func maxInterval() time.Duration { |
| 125 | + if v := os.Getenv(maxIntervalEnvVar); v != "" { |
| 126 | + if d, err := time.ParseDuration(v); err == nil && d > 0 { |
| 127 | + return d |
| 128 | + } |
| 129 | + } |
| 130 | + return defaultMaxInterval |
| 131 | +} |
0 commit comments