feat: two-layer rate limiting for MCP servers#1802
Draft
simplesagar wants to merge 2 commits intomainfrom
Draft
Conversation
Add platform-level and customer-configurable rate limiting for MCP endpoints. Layer 1 (platform): HTTP middleware returns 429 for all MCP POST requests, keyed on MCP slug extracted from URL path. Uses Redis fixed-window counter with configurable per-slug overrides cached from platform_rate_limits table. Layer 2 (customer): Per-MCP-server rate limit via rate_limit_rpm column on toolsets table. Returns JSON-RPC error -32029 (not HTTP 429) to maintain MCP protocol compatibility. Reads from already-loaded toolset data (no extra DB query). Both layers fail open when Redis is unavailable. Includes OTel counters for observability and standard rate limit response headers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Introduce ratelimit.Limiter interface so middleware and MCP service can be unit tested with mock rate limiters instead of requiring Redis. Tests added: - writeJSONRPCRateLimitError: response shape, min retryAfterMs clamping - checkCustomerRateLimit: nil limiter, invalid RPM, zero RPM, allowed, blocked, fail-open on error - RateLimitMiddleware: skip GET, skip non-MCP, allowed with headers, blocked with 429, fail-open, config override, authenticated route key - resolveLimit: nil loader, no override, with override, config error - RecordRateLimitCheck: valid counter, nil counter Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
platform_rate_limitstable with Redis-cached lookups.rate_limit_rpmon the toolsets table. Returns JSON-RPC error code-32029(not HTTP 429) to maintain MCP protocol compatibility. Reads from already-loaded toolset data — no extra DB query.mcp.ratelimit.platform.check,mcp.ratelimit.customer.check) with layer/key/outcome attributes. StandardX-RateLimit-*response headers on all checked requests.New packages
server/internal/ratelimitserver/internal/middlewareSchema changes
toolsets.rate_limit_rpm— nullable integer column for customer-configured limitsplatform_rate_limits— new table for per-slug/project/org override limitsArchitecture decisions
toolset.IDas Redis key to avoid cross-org collisionsRateLimitConfigLoaderis an interface for testabilityKnown follow-ups (not in this PR)
platform_rate_limitsCRUD + cache invalidationidinto rate limit error responsesLayertype parameter onAllow()to make key namespacing structuralTest plan
extractMCPKeyURL path parsing (8 cases)rate_limit_rpmon a toolset and confirm enforcement🤖 Generated with Claude Code