Repository navigation
feat: honour Retry-After on HTTP 429 (typescript-sdk#1892) - #45
Merged
Merged
Conversation
The MCP TypeScript SDK's StreamableHTTP client transport throws a generic error on 429 without reading Retry-After; mcp-stdio's current behaviour is the same — 429 falls through to the 4xx path and surfaces as a synthesized error to the client. That breaks any deployment behind a standard rate limiter. Parse Retry-After per RFC 7231 §7.1.3 (delta-seconds or HTTP-date), sleep for the indicated interval up to a 60-second cap, and retry the request up to MAX_RETRIES times. Missing Retry-After reuses the transient-error exponential backoff. Over-cap waits or exhausted retries propagate the 429 to the caller, so nothing hangs forever on an unreasonable server-requested wait. Applied to all three POST paths: _post_and_stream (Streamable HTTP), _post_parsed (pagination), and the inline POST inside run_sse. Unit tests cover the parser (delta-seconds / float / zero / negative / http-date past+future / malformed / NaN / inf), the retry decision helper (absent / present / over-cap / last attempt), and end-to-end retry-then-success / backoff / over-cap / retries-exhausted scenarios on both transports. Closes #44. Refs: modelcontextprotocol/typescript-sdk#1892
- SSE POST path: add over-cap (no sleep, single request, 429
surfaced) and retries-exhausted (3 POSTs, two 1s sleeps, 429
surfaced) cases. The streaming path already had these; parity
closes a regression-guard gap between transports.
- Pagination path: add _post_parsed 429 retry case
(tools/list → 429 → sleep → 200 → merged response emitted) so
a future refactor cannot silently regress the rate-limit retry
on the pagination helper.
- Style: swap `secs != secs or secs == float("inf")` for
`math.isnan(secs) or math.isinf(secs)` in _parse_retry_after;
behaviour unchanged, intent clearer.
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
429 Too Many Requests, parseRetry-After(RFC 7231 §7.1.3 — delta-seconds or HTTP-date), sleep up to a 60-second cap, and retry the request up toMAX_RETRIEStimes. MissingRetry-Afterreuses the existing transient-error backoff. Over-cap waits or exhausted retries propagate the 429 so nothing hangs on an unreasonable server-requested wait._post_and_stream(Streamable HTTP),_post_parsed(pagination), and the inline POST insiderun_sse.Retry-After— mcp-remote inherits the behaviour.## Workarounds > mcp-remote.Closes #44.
Design notes
_parse_retry_after(value)— permissive: accepts integer / float delta-seconds, clamps negatives to 0, rejects NaN / inf / malformed date; HTTP-date in the past returns 0 (retry immediately)._handle_rate_limit(headers, attempt)— returns seconds to sleep, orNonewhen the caller must give up (wait over cap, or this is the last allowed attempt).express-rate-limitwindow.--no-rate-limit-retryopt-out. HonouringRetry-Afteris spec-compliant HTTP behaviour and adding a flag to disable it has no obvious use case.RateLimit-*headers intentionally out of scope (optional in the upstream issue).Test plan
pytest tests/ -v→ 316 passed, 3 skipped (local, macOS / Python 3.12)TestParseRetryAfter— delta-seconds (int / float / zero / negative / NaN / inf), missing / empty / garbage, HTTP-date past / future / malformedTestHandleRateLimit— present / absent (backoff) / over-cap / at-cap / last attemptTestRun429Retry— retry-after-then-200, backoff-then-200, over-cap surfaces immediately, retries exhausted surfaces 429TestRunSse429Retry— retry-after-then-200 on the SSE POST path