Skip to content

Commit 60cf250

Browse files
author
Nitish Agarwal
committed
feat: implement daily publish rate limiting (modelcontextprotocol#21)
- Add database table publish_attempts to track daily publish counts - Configure rate limiting via environment variables (default 10/day) - Support namespace exemptions with wildcard patterns - Add comprehensive unit tests for rate limiting logic - Integrate rate limiting into publish flow Rate limiting can be disabled via MCP_REGISTRY_RATE_LIMIT_ENABLED=false Exempt namespaces via MCP_REGISTRY_RATE_LIMIT_EXEMPTIONS (comma-separated, supports wildcards)
1 parent 9d63afb commit 60cf250

9 files changed

Lines changed: 346 additions & 7 deletions

File tree

‎.env.example‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ MCP_REGISTRY_OIDC_EXTRA_CLAIMS=[{"hd":"modelcontextprotocol.io"}]
3939
# Grant admin permissions to OIDC-authenticated users
4040
MCP_REGISTRY_OIDC_EDIT_PERMISSIONS=*
4141
MCP_REGISTRY_OIDC_PUBLISH_PERMISSIONS=*
42+
43+
# Rate Limiting Configuration
44+
# Enable/disable rate limiting for publish operations
45+
MCP_REGISTRY_RATE_LIMIT_ENABLED=true
46+
47+
# Maximum number of servers a namespace can publish per day
48+
MCP_REGISTRY_RATE_LIMIT_PER_DAY=10
49+
50+
# Comma-separated list of namespaces exempt from rate limiting
51+
# Supports wildcards: io.github.modelcontextprotocol/*
52+
MCP_REGISTRY_RATE_LIMIT_EXEMPTIONS=

‎internal/config/config.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type Config struct {
3333
OIDCExtraClaims string `env:"OIDC_EXTRA_CLAIMS" envDefault:""`
3434
OIDCEditPerms string `env:"OIDC_EDIT_PERMISSIONS" envDefault:""`
3535
OIDCPublishPerms string `env:"OIDC_PUBLISH_PERMISSIONS" envDefault:""`
36+
37+
// Rate Limiting Configuration
38+
RateLimitEnabled bool `env:"RATE_LIMIT_ENABLED" envDefault:"true"`
39+
RateLimitPerDay int `env:"RATE_LIMIT_PER_DAY" envDefault:"10"`
40+
RateLimitExemptions string `env:"RATE_LIMIT_EXEMPTIONS" envDefault:""` // comma-separated
3641
}
3742

3843
// NewConfig creates a new configuration with default values

‎internal/database/database.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ type Database interface {
4040
UpdateServer(ctx context.Context, id string, server *apiv0.ServerJSON) (*apiv0.ServerJSON, error)
4141
// Close closes the database connection
4242
Close() error
43+
44+
// Rate limiting methods
45+
IncrementPublishCount(ctx context.Context, namespace string) error
46+
GetPublishCount(ctx context.Context, namespace string, date time.Time) (int, error)
4347
}
4448

4549
// ConnectionType represents the type of database connection

‎internal/database/memory.go‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ import (
66
"sort"
77
"strings"
88
"sync"
9+
"time"
910

1011
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
1112
)
1213

1314
// MemoryDB is an in-memory implementation of the Database interface
1415
type MemoryDB struct {
15-
entries map[string]*apiv0.ServerJSON // maps registry metadata ID to ServerJSON
16-
mu sync.RWMutex
16+
entries map[string]*apiv0.ServerJSON // maps registry metadata ID to ServerJSON
17+
publishAttempts map[string]map[string]int // namespace -> date -> count
18+
mu sync.RWMutex
1719
}
1820

1921
func NewMemoryDB() *MemoryDB {
2022
// Convert input ServerJSON entries to have proper metadata
2123
serverRecords := make(map[string]*apiv0.ServerJSON)
2224
return &MemoryDB{
23-
entries: serverRecords,
25+
entries: serverRecords,
26+
publishAttempts: make(map[string]map[string]int),
2427
}
2528
}
2629

@@ -139,6 +142,31 @@ func (db *MemoryDB) UpdateServer(ctx context.Context, id string, server *apiv0.S
139142
return server, nil
140143
}
141144

145+
// IncrementPublishCount increments the publish count for a namespace today
146+
func (db *MemoryDB) IncrementPublishCount(_ context.Context, namespace string) error {
147+
db.mu.Lock()
148+
defer db.mu.Unlock()
149+
150+
today := time.Now().Format("2006-01-02")
151+
if db.publishAttempts[namespace] == nil {
152+
db.publishAttempts[namespace] = make(map[string]int)
153+
}
154+
db.publishAttempts[namespace][today]++
155+
return nil
156+
}
157+
158+
// GetPublishCount returns the number of publishes for a namespace on a specific date
159+
func (db *MemoryDB) GetPublishCount(_ context.Context, namespace string, date time.Time) (int, error) {
160+
db.mu.RLock()
161+
defer db.mu.RUnlock()
162+
163+
dateStr := date.Format("2006-01-02")
164+
if db.publishAttempts[namespace] == nil {
165+
return 0, nil
166+
}
167+
return db.publishAttempts[namespace][dateStr], nil
168+
}
169+
142170
// For an in-memory database, this is a no-op
143171
func (db *MemoryDB) Close() error {
144172
return nil
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Add rate limiting table to track publish attempts by namespace and date
2+
CREATE TABLE publish_attempts (
3+
namespace VARCHAR(255) NOT NULL,
4+
attempt_date DATE NOT NULL DEFAULT CURRENT_DATE,
5+
attempt_count INTEGER NOT NULL DEFAULT 0,
6+
first_attempt_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
7+
last_attempt_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
8+
PRIMARY KEY (namespace, attempt_date)
9+
);
10+
11+
-- Index for efficient lookups by namespace
12+
CREATE INDEX idx_publish_attempts_namespace ON publish_attempts(namespace);
13+
14+
-- Index for cleanup queries by date
15+
CREATE INDEX idx_publish_attempts_date ON publish_attempts(attempt_date);
16+
17+
-- Comment for documentation
18+
COMMENT ON TABLE publish_attempts IS 'Tracks daily publish attempts per namespace for rate limiting';
19+
COMMENT ON COLUMN publish_attempts.namespace IS 'The reverse-DNS namespace (e.g., io.github.username)';
20+
COMMENT ON COLUMN publish_attempts.attempt_date IS 'The date of the attempts (resets daily)';
21+
COMMENT ON COLUMN publish_attempts.attempt_count IS 'Number of successful publishes on this date';

‎internal/database/postgres.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,41 @@ func (db *PostgreSQL) UpdateServer(ctx context.Context, id string, server *apiv0
281281
return server, nil
282282
}
283283

284+
// IncrementPublishCount atomically increments the publish count for a namespace today
285+
func (db *PostgreSQL) IncrementPublishCount(ctx context.Context, namespace string) error {
286+
query := `
287+
INSERT INTO publish_attempts (namespace, attempt_date, attempt_count, first_attempt_at, last_attempt_at)
288+
VALUES ($1, CURRENT_DATE, 1, NOW(), NOW())
289+
ON CONFLICT (namespace, attempt_date)
290+
DO UPDATE SET
291+
attempt_count = publish_attempts.attempt_count + 1,
292+
last_attempt_at = NOW()
293+
`
294+
_, err := db.pool.Exec(ctx, query, namespace)
295+
if err != nil {
296+
return fmt.Errorf("failed to increment publish count: %w", err)
297+
}
298+
return nil
299+
}
300+
301+
// GetPublishCount returns the number of publishes for a namespace on a specific date
302+
func (db *PostgreSQL) GetPublishCount(ctx context.Context, namespace string, date time.Time) (int, error) {
303+
var count int
304+
query := `
305+
SELECT COALESCE(attempt_count, 0)
306+
FROM publish_attempts
307+
WHERE namespace = $1 AND attempt_date = $2
308+
`
309+
err := db.pool.QueryRow(ctx, query, namespace, date.Format("2006-01-02")).Scan(&count)
310+
if err != nil {
311+
if errors.Is(err, pgx.ErrNoRows) {
312+
return 0, nil // No attempts yet
313+
}
314+
return 0, fmt.Errorf("failed to get publish count: %w", err)
315+
}
316+
return count, nil
317+
}
318+
284319
// Close closes the database connection
285320
func (db *PostgreSQL) Close() error {
286321
db.pool.Close()

‎internal/service/rate_limit.go‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package service
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
"github.com/modelcontextprotocol/registry/internal/config"
10+
"github.com/modelcontextprotocol/registry/internal/database"
11+
)
12+
13+
// RateLimitService handles rate limiting for publish operations
14+
type RateLimitService interface {
15+
CheckAndIncrementPublishLimit(ctx context.Context, namespace string) error
16+
IsExempt(namespace string) bool
17+
}
18+
19+
type rateLimitService struct {
20+
db database.Database
21+
cfg *config.Config
22+
exemptions []string
23+
}
24+
25+
// NewRateLimitService creates a new rate limit service
26+
func NewRateLimitService(db database.Database, cfg *config.Config) RateLimitService {
27+
exemptions := []string{}
28+
if cfg.RateLimitExemptions != "" {
29+
for _, e := range strings.Split(cfg.RateLimitExemptions, ",") {
30+
exemptions = append(exemptions, strings.TrimSpace(e))
31+
}
32+
}
33+
34+
return &rateLimitService{
35+
db: db,
36+
cfg: cfg,
37+
exemptions: exemptions,
38+
}
39+
}
40+
41+
// CheckAndIncrementPublishLimit checks if the namespace has exceeded the rate limit
42+
// and increments the count if under the limit
43+
func (r *rateLimitService) CheckAndIncrementPublishLimit(ctx context.Context, namespace string) error {
44+
// Skip if rate limiting is disabled
45+
if !r.cfg.RateLimitEnabled {
46+
return nil
47+
}
48+
49+
// Skip if namespace is exempt
50+
if r.IsExempt(namespace) {
51+
return nil
52+
}
53+
54+
// Get current count for today
55+
today := time.Now()
56+
count, err := r.db.GetPublishCount(ctx, namespace, today)
57+
if err != nil {
58+
return fmt.Errorf("failed to check rate limit: %w", err)
59+
}
60+
61+
// Check if limit exceeded
62+
if count >= r.cfg.RateLimitPerDay {
63+
return fmt.Errorf("rate limit exceeded: you have published %d servers today (limit: %d per day). If you need a higher limit, please open an issue at https://github.com/modelcontextprotocol/registry/issues",
64+
count, r.cfg.RateLimitPerDay)
65+
}
66+
67+
// Increment the count
68+
if err := r.db.IncrementPublishCount(ctx, namespace); err != nil {
69+
return fmt.Errorf("failed to record publish: %w", err)
70+
}
71+
72+
return nil
73+
}
74+
75+
// IsExempt checks if a namespace is exempt from rate limiting
76+
func (r *rateLimitService) IsExempt(namespace string) bool {
77+
for _, exemption := range r.exemptions {
78+
if exemption == "" {
79+
continue
80+
}
81+
82+
// Handle wildcard exemptions
83+
if strings.HasSuffix(exemption, "/*") {
84+
prefix := strings.TrimSuffix(exemption, "/*")
85+
// Match namespace exactly or with a separator (. or /)
86+
if namespace == prefix || strings.HasPrefix(namespace, prefix+".") || strings.HasPrefix(namespace, prefix+"/") {
87+
return true
88+
}
89+
} else if exemption == namespace {
90+
return true
91+
}
92+
}
93+
return false
94+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package service_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/modelcontextprotocol/registry/internal/config"
8+
"github.com/modelcontextprotocol/registry/internal/database"
9+
"github.com/modelcontextprotocol/registry/internal/service"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestRateLimitService_CheckAndIncrementPublishLimit(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
namespace string
18+
existingCount int
19+
limit int
20+
enabled bool
21+
exemptions string
22+
expectError bool
23+
}{
24+
{
25+
name: "under limit allows publish",
26+
namespace: "io.github.testuser",
27+
existingCount: 5,
28+
limit: 10,
29+
enabled: true,
30+
expectError: false,
31+
},
32+
{
33+
name: "at limit blocks publish",
34+
namespace: "io.github.testuser",
35+
existingCount: 10,
36+
limit: 10,
37+
enabled: true,
38+
expectError: true,
39+
},
40+
{
41+
name: "disabled allows any",
42+
namespace: "io.github.testuser",
43+
existingCount: 100,
44+
limit: 10,
45+
enabled: false,
46+
expectError: false,
47+
},
48+
{
49+
name: "exempt namespace bypasses",
50+
namespace: "io.github.modelcontextprotocol",
51+
existingCount: 100,
52+
limit: 10,
53+
enabled: true,
54+
exemptions: "io.github.modelcontextprotocol",
55+
expectError: false,
56+
},
57+
{
58+
name: "wildcard exemption works",
59+
namespace: "com.anthropic.test",
60+
existingCount: 100,
61+
limit: 10,
62+
enabled: true,
63+
exemptions: "com.anthropic/*",
64+
expectError: false,
65+
},
66+
}
67+
68+
for _, tt := range tests {
69+
t.Run(tt.name, func(t *testing.T) {
70+
// Setup
71+
db := database.NewMemoryDB()
72+
cfg := &config.Config{
73+
RateLimitEnabled: tt.enabled,
74+
RateLimitPerDay: tt.limit,
75+
RateLimitExemptions: tt.exemptions,
76+
}
77+
78+
// Pre-populate existing attempts
79+
ctx := context.Background()
80+
for i := 0; i < tt.existingCount; i++ {
81+
err := db.IncrementPublishCount(ctx, tt.namespace)
82+
require.NoError(t, err)
83+
}
84+
85+
// Test
86+
svc := service.NewRateLimitService(db, cfg)
87+
err := svc.CheckAndIncrementPublishLimit(ctx, tt.namespace)
88+
89+
if tt.expectError {
90+
assert.Error(t, err)
91+
assert.Contains(t, err.Error(), "rate limit exceeded")
92+
} else {
93+
assert.NoError(t, err)
94+
}
95+
})
96+
}
97+
}
98+
99+
func TestRateLimitService_IsExempt(t *testing.T) {
100+
cfg := &config.Config{
101+
RateLimitExemptions: "io.github.modelcontextprotocol,com.anthropic/*,org.example.test",
102+
}
103+
104+
// Use the public interface for testing
105+
svc := service.NewRateLimitService(database.NewMemoryDB(), cfg)
106+
107+
tests := []struct {
108+
namespace string
109+
exempt bool
110+
}{
111+
{"io.github.modelcontextprotocol", true},
112+
{"com.anthropic", true},
113+
{"com.anthropic.subsystem", true},
114+
{"org.example.test", true},
115+
{"org.example", false},
116+
{"io.github.other", false},
117+
}
118+
119+
for _, tt := range tests {
120+
t.Run(tt.namespace, func(t *testing.T) {
121+
// Since we're testing through the public interface, we can't directly
122+
// access IsExempt, but we can verify the behavior by checking rate limiting
123+
assert.Equal(t, tt.exempt, svc.IsExempt(tt.namespace))
124+
})
125+
}
126+
}

0 commit comments

Comments
 (0)