Skip to content

feat(mcp): per-credential rate limiting for the MCP tool-call surface - #679

Open
Makisuo wants to merge 1 commit into
mainfrom
feat/mcp-tool-rate-limiting
Open

feat(mcp): per-credential rate limiting for the MCP tool-call surface#679
Makisuo wants to merge 1 commit into
mainfrom
feat/mcp-tool-rate-limiting

Conversation

@Makisuo

@Makisuo Makisuo commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

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.

  • New McpToolRateLimiter built on makeRateLimitCheck, extracted from ApiV2RateLimiter: the same generic check(key) with fail-open semantics — allowed / limited / failed_open, always with maple.rate_limit.outcome telemetry, never a silent pass (deliberately not the bare-return fail-open in McpOAuthService).
  • Dedicated MCP_TOOLS_RATE_LIMITER binding, 120 req / 60 s (namespace 2026082901), declared in alchemy.run.ts and mirrored in wrangler.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.
  • Keyed per credential, never IP: 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 — and user:<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.
  • 429 with Retry-After: 60 on limited, using V2RateLimited's wording in the MCP surface's existing { error, message } envelope (the /mcp route 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: new "Rate limiting" section in docs/mcp-oauth.md. docs/api-v2.md untouched — it documents only the /v2 surface.

Reviewer notes

  • ApiV2RateLimiterApiRateLimiterApi rename: the interface stopped being v2-specific; only one type-import needed updating.
  • The credential identity travels as rateLimitCredentialId on the resolved MCP tenant (McpAuthenticatedTenant extends TenantContext structurally, so the chat-sessions caller of the same resolver is unaffected and unlimited).
  • Tests: new McpToolRateLimiter.test.ts (own binding + stage partition, fails open when only the v2 binding is present) and a new app.test.ts case asserting 429 / Retry-After / key:<id> bucket with the executor never invoked.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant