Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion internal/mcp/server_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,29 @@ func publicLimits(options Options) map[string]any {
}
}

const maxPublicRateLimits = 8

func publicRateLimits(rateLimits []stratz.RateLimit) []any {
result := make([]any, 0, len(rateLimits))
type rateLimitKey struct {
window string
source string
}
ordered := make([]rateLimitKey, 0, min(len(rateLimits), maxPublicRateLimits))
latest := make(map[rateLimitKey]stratz.RateLimit, min(len(rateLimits), maxPublicRateLimits))
for _, rateLimit := range rateLimits {
key := rateLimitKey{window: rateLimit.Window, source: rateLimit.Source}
if _, ok := latest[key]; !ok {
if len(ordered) >= maxPublicRateLimits {
continue
}
ordered = append(ordered, key)
}
latest[key] = rateLimit
}

result := make([]any, 0, len(ordered))
for _, key := range ordered {
rateLimit := latest[key]
var resetAt any
if rateLimit.ResetAt != nil {
resetAt = rateLimit.ResetAt.UTC().Format(time.RFC3339)
Expand Down
29 changes: 29 additions & 0 deletions internal/mcp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ func testServer(t *testing.T, logger *slog.Logger) *Server {
return server
}

func TestPublicRateLimitsDedupeAndCap(t *testing.T) {
firstRemaining := int64(149)
latestRemaining := int64(145)
minuteLimit := int64(150)
rateLimits := []stratz.RateLimit{
{Window: "minute", Limit: &minuteLimit, Remaining: &firstRemaining, Source: "fixture-minute"},
{Window: "minute", Limit: &minuteLimit, Remaining: &latestRemaining, Source: "fixture-minute"},
}
for index := range 10 {
limit := int64(100 + index)
remaining := int64(90 + index)
rateLimits = append(rateLimits, stratz.RateLimit{
Window: "unknown",
Limit: &limit,
Remaining: &remaining,
Source: fmt.Sprintf("extra-%d", index),
})
}

public := publicRateLimits(rateLimits)
if len(public) != maxPublicRateLimits {
t.Fatalf("rate limit count = %d, want %d", len(public), maxPublicRateLimits)
}
first := public[0].(map[string]any)
if first["window"] != "minute" || first["remaining"] != &latestRemaining {
t.Fatalf("first rate limit = %#v, want latest minute fixture", first)
}
}

func TestSDKConformance(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
Loading