Skip to content

feat: honour Retry-After on HTTP 429 (typescript-sdk#1892) - #45

Merged
shigechika merged 2 commits into
mainfrom
feat/retry-after-on-429
Apr 20, 2026
Merged

shigechika merged 2 commits into
mainfrom
feat/retry-after-on-429

Conversation

@shigechika

Copy link
Copy Markdown
Owner

Summary

  • On 429 Too Many Requests, parse Retry-After (RFC 7231 §7.1.3 — delta-seconds or HTTP-date), sleep up to a 60-second cap, and retry the request up to MAX_RETRIES times. Missing Retry-After reuses the existing transient-error backoff. Over-cap waits or exhausted retries propagate the 429 so nothing hangs 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.
  • Closes the gap called out by typescript-sdk#1892, which reports that the MCP TypeScript SDK's StreamableHTTP client transport surfaces a generic error on 429 without honouring Retry-After — mcp-remote inherits the behaviour.
  • README / README.ja cross-link added to ## 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, or None when the caller must give up (wait over cap, or this is the last allowed attempt).
  • Cap = 60 s. Chose a fixed cap over a proportional one because MCP tool calls are typically short and 60 s is well above any legitimate express-rate-limit window.
  • No --no-rate-limit-retry opt-out. Honouring Retry-After is spec-compliant HTTP behaviour and adding a flag to disable it has no obvious use case.
  • RFC 9331 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 / malformed
  • TestHandleRateLimit — present / absent (backoff) / over-cap / at-cap / last attempt
  • TestRun429Retry — retry-after-then-200, backoff-then-200, over-cap surfaces immediately, retries exhausted surfaces 429
  • TestRunSse429Retry — retry-after-then-200 on the SSE POST path

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.
@shigechika
shigechika merged commit fb5ac14 into main Apr 20, 2026
6 checks passed
@shigechika
shigechika deleted the feat/retry-after-on-429 branch April 20, 2026 19:56
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.

feat: honour Retry-After on HTTP 429 (typescript-sdk#1892)

1 participant