feat(mcp): per-credential rate limiting for the MCP tool-call surface - #679
Open
Makisuo wants to merge 1 commit into
Open
feat(mcp): per-credential rate limiting for the MCP tool-call surface#679Makisuo wants to merge 1 commit into
Makisuo wants to merge 1 commit into
Conversation
…dential
POST /mcp had no rate limiting while its tool calls fan out into warehouse
queries and LLM calls, making it the most expensive unlimited endpoint.
- Extract the v2 limiter's fail-open check (allowed/limited/failed_open with
maple.rate_limit.outcome telemetry) into makeRateLimitCheck and build the
new McpToolRateLimiter on it, on a dedicated 120/60s binding: the budget
has to differ from the v2 API's 600/60s, and a limit is fixed per binding.
- Key per credential, never IP: key:<internal keyId> for OAuth tokens and
manual MCP keys (rolling a key starts a fresh budget; the secret never
reaches the counter), user:<userId> for session auth. The internal service
token is exempt - one shared token would put every internal caller in a
single bucket.
- Over-budget requests get 429 + Retry-After: 60 with V2RateLimited's wording
in the MCP surface's { error, message } envelope; span records outcome,
limit, and period.
- Bind MCP_TOOLS_RATE_LIMITER in alchemy.run.ts and mirror it in
wrangler.jsonc; document the limit in docs/mcp-oauth.md (api-v2.md covers
only the /v2 surface).
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.
What
Adds rate limiting to the authenticated MCP surface (
POST /mcp), which had none while the MCP OAuth handshake already did — leaving the endpoint whose tool calls run warehouse queries and LLM calls as the most expensive unlimited path in the API.McpToolRateLimiterbuilt onmakeRateLimitCheck, extracted fromApiV2RateLimiter: the same genericcheck(key)with fail-open semantics —allowed/limited/failed_open, always withmaple.rate_limit.outcometelemetry, never a silent pass (deliberately not the bare-return fail-open inMcpOAuthService).MCP_TOOLS_RATE_LIMITERbinding, 120 req / 60 s (namespace2026082901), declared inalchemy.run.tsand mirrored inwrangler.jsonc. A new binding rather than reuse because a limit is fixed per binding, and neither the v2 API's 600/60s nor the handshake's 60/60s fits this surface's cost profile.key:<internal keyId>for OAuth tokens and manual MCP keys — rolling a key starts a fresh budget and the raw secret never reaches the counter — anduser:<userId>for dashboard-session auth. The internal service token is exempt: it is one shared token, so a per-credential bucket would throttle every internal caller together.Retry-After: 60on limited, usingV2RateLimited's wording in the MCP surface's existing{ error, message }envelope (the/mcproute is not inside the v2 HttpApi serializer). The span records outcome, limit, and period; enforcement runs in the authorization middleware right after credential resolution, matching the v2 layer's order.docs/mcp-oauth.md.docs/api-v2.mduntouched — it documents only the/v2surface.Reviewer notes
ApiV2RateLimiterApi→RateLimiterApirename: the interface stopped being v2-specific; only one type-import needed updating.rateLimitCredentialIdon the resolved MCP tenant (McpAuthenticatedTenantextendsTenantContextstructurally, so thechat-sessionscaller of the same resolver is unaffected and unlimited).McpToolRateLimiter.test.ts(own binding + stage partition, fails open when only the v2 binding is present) and a newapp.test.tscase asserting 429 /Retry-After/key:<id>bucket with the executor never invoked.🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.