Answer rate-limited requests with 429 and Retry-After, not 401 invalid_token #126
Labels
No labels
bug
enhancement
epic
needs-info
needs-triage
ready-for-agent
ready-for-human
svd:go
svd:hold
wontfix
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#126
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
FileTokenVerifier.verify_tokenreturnsNonewhen the per-token bucket is empty, so FastMCP answers401 invalid_tokenfor a rate-limited request. A valid client cannot tell "wrong token" from "slow down": People Mind's mailmind Source classified it asmailmind-authand gave up on every pass (dev/people-mind-mcp#35; 20mail_changespages in 0.58 s against the defaultsrate_per_second = 5.0,rate_burst = 20). The API log also printsAuth error returned: invalid_token (status=401)for what is a throttle, which misleads an operator reading it.Fix
Answer a rate-limited request with
429 Too Many Requestsand aRetry-Afterheader (whole seconds until one bucket token refills, minimum 1), and keep 401 for an unknown token. The verifier's contract cannot express 429, so the limiter moves out ofverify_tokeninto an ASGI middleware that runs after authentication and keys the bucket on the authenticated token'sclient_idas today. Log the throttle asrate-limited scope=<client_id>at INFO, counts and codes only.Acceptance
Retry-After, the first two answer 200, and a request with an unknown token still answers 401.tests/test_auth.pykeeps the token-bucket unit tests; nothing about scopes changes.rate_per_secondandrate_burstkeep their names and defaults.Primary files
src/mail_mind_mcp/auth.pysrc/mail_mind_mcp/server.py(build_app)tests/test_auth.pyBlocked by
None.
⏸ SVD architect gate: dev/mail-mind-mcp#126 answer-rate-limited-requests-with-429-and-retry-after-not-40
Status: CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity
standard· dispatch effortmediumRun:
20261102T023307Z-390c63f8Held because: CLEAN but 3 open risk(s) (2 verification · 1 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
initializePOST with Accept 'application/json, text/event-stream', expected to answer 200 under stateless_http=True. Check: in the dispatch worktree runuv run python3 -m pytest -q tests/test_auth.py -k rate_limitedand see statuses [200, 200, 429] plus a 401 for the unknown token. If initialize does not answer 200, use a request the served fixture already proves and record it in deviations. [verification]Change: 1. Add RateLimiter.take(key, *, now=None) -> int | None in auth.py. It returns None when it consumes a token. Otherwise it returns max(1, ceil((1 - tokens) / rate)). Reimplement allow as
take(...) is None.2. Drop the limiter parameter and the allow check from FileTokenVerifier (auth.py:96-110).
3. Add a pure-ASGI RateLimitMiddleware(app, limiter) in auth.py, mirroring AuthContextMiddleware (mcp/server/auth/middleware/auth_context.py:24-48). When scope['user'] is an AuthenticatedUser, it keys the bucket on user.access_token.client_id. On a throttle it logs INFO 'rate-limited scope=%s' with extra seconds, and answers PlainTextResponse('rate limited', 429, Retry-After=str(wait)). Unauthenticated requests pass through, so RequireAuthMiddleware still answers 401.
4. In build_app (server.py:1105-1115), build FileTokenVerifier(tokens) and pass middleware=[host_check, Middleware(RateLimitMiddleware, limiter=limiter)]. FastMCP 3.4.11 appends user middleware after its AuthenticationMiddleware (fastmcp/server/http.py:650,699-700), so scope['user'] is set when the middleware runs.
5. Update the SECURITY.md:10 row.
6. Rewrite the throttling assertions in test_auth.py::test_verifier and test_evidence.py::test_the_evidence_token_has_its_own_bucket. Add a uvicorn-served burst-2 HTTP test (200, 200, 429 with Retry-After, unknown token 401), mirroring tests/test_server.py:55-89.
Files:
src/mail_mind_mcp/auth.py·src/mail_mind_mcp/server.py·tests/test_auth.py·tests/test_evidence.py·SECURITY.mdDecision: accept the 3 open risk(s) above and dispatch this spec (
svd:go), or park (svd:skip) / re-spec (svd:respec)?Your call (when this issue is held at the gate via
svd:hold):svd:go— approve; Half-B dispatch accepts exactly this spec (open risks accepted)svd:firebesidesvd:go— approve AND authorise the Half-B fire from the phone; never on its ownsvd:skip— park it; dispatch refuses it and the issue stays for a humansvd:respec— discard this spec; the next Half-A run re-produces it with this record as prior-attempt contextSpec:
/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-answer-rate-limited-requests-with-429-and-retry-after-not-40.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-answer-rate-limited-requests-with-429-and-retry-after-not-40.mdspec-verify-dispatch Half B for dev/mail-mind-mcp#126 answer-rate-limited-requests-with-429-and-retry-after-not-40 completed integration verify.
0302441ef5codex-svd ship --run-id 20261102T031327Z-ed8b59de --confirm-merge)