Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): read a 429 as rate_limited and keep its Retry-After

Alfredo Lopez984e3c0b6254
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 69a172cf9..cf4ea675d 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.441",
+ "version": "0.1.442",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.441",
+ "version": "0.1.442",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index efdd06341..7c2897178 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.441",
+ "version": "0.1.442",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/llm/codexLlmProxyClient.ts b/mcp-host/src/llm/codexLlmProxyClient.ts
index 24b622d99..46fdb3b4e 100644
--- a/mcp-host/src/llm/codexLlmProxyClient.ts
+++ b/mcp-host/src/llm/codexLlmProxyClient.ts
@@ -2,6 +2,7 @@ import {
buildCodexProxyEnvelope,
parseCodexCompletionRequest,
} from '@clerum/llm-provider-attempt-contract'
+import { retryAfterMs } from './retryAfter'
export const CODEX_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/codex/completions'
@@ -15,7 +16,9 @@ export class CodexProxyError extends Error {
constructor(
readonly code: string,
message: string,
- readonly dispatched: boolean = true
+ readonly dispatched: boolean = true,
+ // The delay a 429 advised through Retry-After (G1-6).
+ readonly retryAfterMs?: number
) {
super(message)
this.name = 'CodexProxyError'
@@ -129,17 +132,23 @@ export class CodexLlmProxyClient {
return this.streamOnce(input, false)
}
const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>
+ // A 429 with no JSON code comes from a limiter in front of the proxy:
+ // a rate limit, not a provider outage (G1-6, #720).
const code =
response.status === 413
? 'payload_too_large'
: typeof payload.error === 'string'
? payload.error
- : 'provider_unavailable'
+ : response.status === 429
+ ? 'rate_limited'
+ : 'provider_unavailable'
throw new CodexProxyError(
code,
code === 'payload_too_large'
? 'Codex request is too large; use fewer or smaller images, or reduce context'
- : `proxy stream failed with ${response.status} (${code})`
+ : `proxy stream failed with ${response.status} (${code})`,
+ true,
+ response.status === 429 ? retryAfterMs(response) : undefined
)
}
if (!response.body) {
diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.ts
index f405f35ad..9075e689f 100644
--- a/mcp-host/src/llm/grokLlmProxyClient.ts
+++ b/mcp-host/src/llm/grokLlmProxyClient.ts
@@ -1,3 +1,5 @@
+import { retryAfterMs } from './retryAfter'
+
export const GROK_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/grok/completions'
/**
@@ -31,7 +33,9 @@ export class GrokProxyError extends Error {
constructor(
readonly code: string,
message: string,
- readonly dispatched: boolean = true
+ readonly dispatched: boolean = true,
+ // The delay a 429 advised through Retry-After (G1-6).
+ readonly retryAfterMs?: number
) {
super(message)
this.name = 'GrokProxyError'
@@ -117,14 +121,22 @@ export class GrokLlmProxyClient {
const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>
// A 413 with no JSON code comes from the gateway in front of the proxy
// (nginx `client_max_body_size`): a size refusal of this request, never a
- // provider outage (#739). A code the 413 carries still wins.
+ // provider outage (#739). A code the 413 carries still wins. A 429 with
+ // no JSON code likewise comes from a limiter: a rate limit (G1-6, #720).
const code =
typeof payload.error === 'string'
? payload.error
: response.status === 413
? 'payload_too_large'
- : 'provider_unavailable'
- throw new GrokProxyError(code, grokProxyErrorMessage(code, response.status))
+ : response.status === 429
+ ? 'rate_limited'
+ : 'provider_unavailable'
+ throw new GrokProxyError(
+ code,
+ grokProxyErrorMessage(code, response.status),
+ true,
+ response.status === 429 ? retryAfterMs(response) : undefined
+ )
}
if (!response.body) {
throw new GrokProxyError('provider_unavailable', 'proxy stream had no body')
diff --git a/mcp-host/src/llm/providerAttemptAuthorizer.ts b/mcp-host/src/llm/providerAttemptAuthorizer.ts
index 9c3f27ea7..ef54f4dbc 100644
--- a/mcp-host/src/llm/providerAttemptAuthorizer.ts
+++ b/mcp-host/src/llm/providerAttemptAuthorizer.ts
@@ -126,12 +126,16 @@ export class ProviderAttemptAuthorizer {
// Every 413 is a size refusal of this request, never a provider outage
// (#731): control-api answers `payload_too_large`, and the gateway in
// front of it (nginx `client_max_body_size`) answers with no JSON code.
+ // A 429 with no JSON code comes from a limiter in front of control-api:
+ // a rate limit, not a provider outage (G1-6, #720).
const code =
response.status === 413
? 'payload_too_large'
: typeof payload.error === 'string'
? payload.error
- : 'provider_unavailable'
+ : response.status === 429
+ ? 'rate_limited'
+ : 'provider_unavailable'
throw new CodexAuthorizeError(
code,
code === 'payload_too_large'
diff --git a/mcp-host/src/llm/retryAfter.ts b/mcp-host/src/llm/retryAfter.ts
new file mode 100644
index 000000000..73714db50
--- /dev/null
+++ b/mcp-host/src/llm/retryAfter.ts
@@ -0,0 +1,12 @@
+// G1-6 (#720): the delay a 429 advises, in milliseconds. Only delta-seconds
+// from 1 to 3600 are accepted, the rule `gfsClient.ts` applies to gfsc's
+// limiter; an HTTP-date, a fraction or anything out of range is treated as
+// absent, never guessed.
+const MAX_RETRY_AFTER_SECONDS = 3600
+
+export function retryAfterMs(response: Response): number | undefined {
+ const raw = response.headers.get('retry-after')?.trim()
+ if (raw === undefined || !/^[1-9][0-9]{0,3}$/.test(raw)) return undefined
+ const seconds = Number(raw)
+ return seconds <= MAX_RETRY_AFTER_SECONDS ? seconds * 1000 : undefined
+}