Skip to content

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

TypeScript

refactor(mcp-host): take an options object in the proxy and authorize errors (#720)

Alfredo Lopezd9693e93789e
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index c37c5eea7..30e327790 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.455",
+ "version": "0.1.456",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.455",
+ "version": "0.1.456",
"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 dd7061a6e..5730a96ea 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.455",
+ "version": "0.1.456",
"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/__tests__/codexSubscription.test.ts b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
index 35f7c42a7..ef048660d 100644
--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
@@ -911,8 +911,9 @@ describe('CodexSubscriptionProvider', () => {
// The two pre-stream aborts are the only proxy errors that prove it.
expect(
- provider.classifyError(new CodexProxyError('canceled', 'aborted before authorize', false))
- .providerDispatched
+ provider.classifyError(
+ new CodexProxyError('canceled', 'aborted before authorize', { dispatched: false })
+ ).providerDispatched
).toBe(false)
// Anything else on the proxy happened after the request was issued.
@@ -1459,12 +1460,9 @@ describe('CodexSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
const ok = { text: 'after the wait', toolCalls: [], outcome: 'success' }
const limited = (retryAfterMs?: number) =>
- new CodexProxyError(
- 'rate_limited',
- 'proxy stream failed with 429 (rate_limited)',
- true,
- retryAfterMs
- )
+ new CodexProxyError('rate_limited', 'proxy stream failed with 429 (rate_limited)', {
+ retryAfterMs,
+ })
const authorizeTwice = () =>
vi
.fn()
@@ -1603,12 +1601,9 @@ describe('CodexSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
it('G1-9g: control_plane_unavailable is never retried here', async () => {
vi.useFakeTimers()
// Carries a delay on purpose, so only the code keeps it from being retried.
- const err = new CodexProxyError(
- 'control_plane_unavailable',
- 'proxy could not be reached',
- true,
- 1000
- )
+ const err = new CodexProxyError('control_plane_unavailable', 'proxy could not be reached', {
+ retryAfterMs: 1000,
+ })
const wired = deps({ stream: vi.fn().mockRejectedValueOnce(err).mockResolvedValueOnce(ok) })
const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index e4cca2e31..a9453a913 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -814,8 +814,9 @@ describe('GrokSubscriptionProvider', () => {
expect(classifyFailoverClass(limited.code, limited.retryable)).toBe('rate_limited')
expect(
- provider.classifyError(new GrokProxyError('canceled', 'aborted before authorize', false))
- .providerDispatched
+ provider.classifyError(
+ new GrokProxyError('canceled', 'aborted before authorize', { dispatched: false })
+ ).providerDispatched
).toBe(false)
expect(provider.classifyError(new Error('who knows')).providerDispatched).toBeUndefined()
})
@@ -844,12 +845,9 @@ describe('GrokSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
const ok = { text: 'after the wait', toolCalls: [], outcome: 'success' }
const limited = (retryAfterMs?: number) =>
- new GrokProxyError(
- 'rate_limited',
- 'proxy stream failed with 429 (rate_limited)',
- true,
- retryAfterMs
- )
+ new GrokProxyError('rate_limited', 'proxy stream failed with 429 (rate_limited)', {
+ retryAfterMs,
+ })
const authorizeTwice = () =>
vi
.fn()
@@ -988,12 +986,9 @@ describe('GrokSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
it('G1-9g: control_plane_unavailable is never retried here', async () => {
vi.useFakeTimers()
// Carries a delay on purpose, so only the code keeps it from being retried.
- const err = new GrokProxyError(
- 'control_plane_unavailable',
- 'proxy could not be reached',
- true,
- 1000
- )
+ const err = new GrokProxyError('control_plane_unavailable', 'proxy could not be reached', {
+ retryAfterMs: 1000,
+ })
const wired = deps({ stream: vi.fn().mockRejectedValueOnce(err).mockResolvedValueOnce(ok) })
const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
diff --git a/mcp-host/src/llm/codexLlmProxyClient.ts b/mcp-host/src/llm/codexLlmProxyClient.ts
index 3605e99af..6b681f60d 100644
--- a/mcp-host/src/llm/codexLlmProxyClient.ts
+++ b/mcp-host/src/llm/codexLlmProxyClient.ts
@@ -8,24 +8,35 @@ import { upstreamRejectedStatus } from './upstreamRejected'
export const CODEX_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/codex/completions'
-export class CodexProxyError extends Error {
+export type CodexProxyErrorOptions = {
/**
- * `dispatched` records whether a request had already left this process when
- * the error was raised. It defaults to `true` because every construction
- * site except the pre-stream abort happens after the fetch was issued, and
- * the safe default is the one that keeps the attempt fenced.
+ * Whether a request had already left this process when the error was
+ * raised. It defaults to `true` because every construction site except the
+ * pre-dispatch refusals happens after the fetch was issued, and the safe
+ * default is the one that keeps the attempt fenced.
*/
+ dispatched?: boolean
+ /** The delay a 429 advised through Retry-After (G1-6). */
+ retryAfterMs?: number
+ /** The upstream 4xx behind an upstream_rejected (R1-H2). */
+ upstreamStatus?: number
+}
+
+export class CodexProxyError extends Error {
+ readonly dispatched: boolean
+ readonly retryAfterMs?: number
+ readonly upstreamStatus?: number
+
constructor(
readonly code: string,
message: string,
- readonly dispatched: boolean = true,
- // The delay a 429 advised through Retry-After (G1-6).
- readonly retryAfterMs?: number,
- // The upstream 4xx behind an upstream_rejected (R1-H2).
- readonly upstreamStatus?: number
+ options: CodexProxyErrorOptions = {}
) {
super(message)
this.name = 'CodexProxyError'
+ this.dispatched = options.dispatched ?? true
+ this.retryAfterMs = options.retryAfterMs
+ this.upstreamStatus = options.upstreamStatus
}
}
@@ -69,7 +80,7 @@ export class CodexLlmProxyClient {
signal?: AbortSignal
}): Promise<CodexProxyStreamResult> {
if (input.signal?.aborted) {
- throw new CodexProxyError('canceled', 'aborted before proxy stream', false)
+ throw new CodexProxyError('canceled', 'aborted before proxy stream', { dispatched: false })
}
return this.streamOnce(input, Boolean(this.options.refreshOnUnauthorized))
}
@@ -95,12 +106,14 @@ export class CodexLlmProxyClient {
'codex-completion-request.v2'
) {
const parsed = parseCodexCompletionRequest(input.request)
- if (!parsed.ok) throw new CodexProxyError('invalid_request', parsed.message, false)
+ if (!parsed.ok) {
+ throw new CodexProxyError('invalid_request', parsed.message, { dispatched: false })
+ }
if (input.deadlineMs !== undefined && input.deadlineMs !== parsed.value.deadlineMs) {
throw new CodexProxyError(
'invalid_request',
'Codex deadline must match the authorized request',
- false
+ { dispatched: false }
)
}
const envelope = buildCodexProxyEnvelope({
@@ -115,7 +128,7 @@ export class CodexLlmProxyClient {
: envelope.code === 'request_hash_mismatch'
? 'request_hash_mismatch'
: 'invalid_request'
- throw new CodexProxyError(code, envelope.message, false)
+ throw new CodexProxyError(code, envelope.message, { dispatched: false })
}
body = envelope.value
}
@@ -166,9 +179,10 @@ export class CodexLlmProxyClient {
code === 'payload_too_large'
? 'Codex request is too large; use fewer or smaller images, or reduce context'
: `proxy stream failed with ${response.status} (${code})`,
- true,
- response.status === 429 ? retryAfterMs(response) : undefined,
- upstreamRejectedStatus(code, payload.upstreamStatus)
+ {
+ retryAfterMs: response.status === 429 ? retryAfterMs(response) : undefined,
+ upstreamStatus: upstreamRejectedStatus(code, payload.upstreamStatus),
+ }
)
}
if (!response.body) {
@@ -203,13 +217,9 @@ async function readProxySse(body: ReadableStream<Uint8Array>): Promise<CodexProx
if (!line) continue
const frame = JSON.parse(line.slice(6)) as CodexProxyFrame
if (frame.type === 'error') {
- throw new CodexProxyError(
- frame.code,
- `proxy stream failed with ${frame.code}`,
- true,
- undefined,
- upstreamRejectedStatus(frame.code, frame.upstreamStatus)
- )
+ throw new CodexProxyError(frame.code, `proxy stream failed with ${frame.code}`, {
+ upstreamStatus: upstreamRejectedStatus(frame.code, frame.upstreamStatus),
+ })
}
if (frame.type === 'text') text += frame.text
if (frame.type === 'tool_call') toolCalls.push(frame)
diff --git a/mcp-host/src/llm/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.ts
index 3003d323a..9fec8ff3d 100644
--- a/mcp-host/src/llm/codexSubscription.ts
+++ b/mcp-host/src/llm/codexSubscription.ts
@@ -564,7 +564,7 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {
}
) {
if (options?.signal?.aborted) {
- throw new CodexProxyError('canceled', 'aborted before authorize', false)
+ throw new CodexProxyError('canceled', 'aborted before authorize', { dispatched: false })
}
// An over-long history is reported as a context-length failure instead of
// a generic invalid request; it is thrown before authorize and dispatch.
diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.ts
index 2db15ab40..6994e5e64 100644
--- a/mcp-host/src/llm/grokLlmProxyClient.ts
+++ b/mcp-host/src/llm/grokLlmProxyClient.ts
@@ -25,24 +25,35 @@ export function grokProxyErrorMessage(code: string, status?: number): string {
: `proxy stream failed with ${status} (${code})`
}
-export class GrokProxyError extends Error {
+export type GrokProxyErrorOptions = {
/**
- * `dispatched` records whether a request had already left this process when
- * the error was raised. It defaults to `true` because every construction
- * site except the pre-stream abort happens after the fetch was issued, and
- * the safe default is the one that keeps the attempt fenced.
+ * Whether a request had already left this process when the error was
+ * raised. It defaults to `true` because every construction site except the
+ * pre-stream abort happens after the fetch was issued, and the safe default
+ * is the one that keeps the attempt fenced.
*/
+ dispatched?: boolean
+ /** The delay a 429 advised through Retry-After (G1-6). */
+ retryAfterMs?: number
+ /** The upstream 4xx behind an upstream_rejected (R1-H2). */
+ upstreamStatus?: number
+}
+
+export class GrokProxyError extends Error {
+ readonly dispatched: boolean
+ readonly retryAfterMs?: number
+ readonly upstreamStatus?: number
+
constructor(
readonly code: string,
message: string,
- readonly dispatched: boolean = true,
- // The delay a 429 advised through Retry-After (G1-6).
- readonly retryAfterMs?: number,
- // The upstream 4xx behind an upstream_rejected (R1-H2).
- readonly upstreamStatus?: number
+ options: GrokProxyErrorOptions = {}
) {
super(message)
this.name = 'GrokProxyError'
+ this.dispatched = options.dispatched ?? true
+ this.retryAfterMs = options.retryAfterMs
+ this.upstreamStatus = options.upstreamStatus
}
}
@@ -86,7 +97,7 @@ export class GrokLlmProxyClient {
signal?: AbortSignal
}): Promise<GrokProxyStreamResult> {
if (input.signal?.aborted) {
- throw new GrokProxyError('canceled', 'aborted before proxy stream', false)
+ throw new GrokProxyError('canceled', 'aborted before proxy stream', { dispatched: false })
}
return this.streamOnce(input, Boolean(this.options.refreshOnUnauthorized))
}
@@ -151,13 +162,10 @@ export class GrokLlmProxyClient {
: response.status === 413
? 'payload_too_large'
: 'provider_unavailable'
- throw new GrokProxyError(
- code,
- grokProxyErrorMessage(code, response.status),
- true,
- response.status === 429 ? retryAfterMs(response) : undefined,
- upstreamRejectedStatus(code, payload.upstreamStatus)
- )
+ throw new GrokProxyError(code, grokProxyErrorMessage(code, response.status), {
+ retryAfterMs: response.status === 429 ? retryAfterMs(response) : undefined,
+ upstreamStatus: upstreamRejectedStatus(code, payload.upstreamStatus),
+ })
}
if (!response.body) {
throw new GrokProxyError('provider_unavailable', 'proxy stream had no body')
@@ -191,13 +199,9 @@ async function readProxySse(body: ReadableStream<Uint8Array>): Promise<GrokProxy
if (!line) continue
const frame = JSON.parse(line.slice(6)) as GrokProxyFrame
if (frame.type === 'error') {
- throw new GrokProxyError(
- frame.code,
- grokProxyErrorMessage(frame.code),
- true,
- undefined,
- upstreamRejectedStatus(frame.code, frame.upstreamStatus)
- )
+ throw new GrokProxyError(frame.code, grokProxyErrorMessage(frame.code), {
+ upstreamStatus: upstreamRejectedStatus(frame.code, frame.upstreamStatus),
+ })
}
if (frame.type === 'text') text += frame.text
if (frame.type === 'tool_call') toolCalls.push(frame)
diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index 57dbdbadf..421c338b3 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -429,7 +429,7 @@ export class GrokSubscriptionProvider implements SingleTurnProvider {
}
) {
if (options?.signal?.aborted) {
- throw new GrokProxyError('canceled', 'aborted before authorize', false)
+ throw new GrokProxyError('canceled', 'aborted before authorize', { dispatched: false })
}
// An over-long history is reported as a context-length failure instead of
// a generic invalid request; it is thrown before authorize and dispatch.
diff --git a/mcp-host/src/llm/providerAttemptAuthorizer.ts b/mcp-host/src/llm/providerAttemptAuthorizer.ts
index 53caa7549..160caba40 100644
--- a/mcp-host/src/llm/providerAttemptAuthorizer.ts
+++ b/mcp-host/src/llm/providerAttemptAuthorizer.ts
@@ -17,15 +17,22 @@ export const AUTHORIZE_PATH = '/api/v1/mcp-host/llm/provider-attempts/authorize'
*/
export const AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES = ENVELOPE_ALLOWANCE_BYTES
+export type CodexAuthorizeErrorOptions = {
+ /** G1-11 (#720): the delay a 429 advised, read from its Retry-After. */
+ retryAfterMs?: number
+}
+
export class CodexAuthorizeError extends Error {
+ readonly retryAfterMs?: number
+
constructor(
readonly code: string,
message: string,
- // G1-11 (#720): the delay a 429 advised, read from its Retry-After.
- readonly retryAfterMs?: number
+ options: CodexAuthorizeErrorOptions = {}
) {
super(message)
this.name = 'CodexAuthorizeError'
+ this.retryAfterMs = options.retryAfterMs
}
}
@@ -160,7 +167,7 @@ export class ProviderAttemptAuthorizer {
code === 'payload_too_large'
? 'Codex request is too large; use fewer or smaller images, or reduce context'
: `authorize failed with ${response.status}`,
- response.status === 429 ? retryAfterMs(response) : undefined
+ { retryAfterMs: response.status === 429 ? retryAfterMs(response) : undefined }
)
}
for (const key of LEAK_KEYS) {