Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

Add connect + headers timeout retries for model and MCP (#894)

Chirag Jain4fde7e98e27e
diff --git a/.changeset/outbound-http-request-retries.md b/.changeset/outbound-http-request-retries.md
new file mode 100644
index 000000000..02717e20f
--- /dev/null
+++ b/.changeset/outbound-http-request-retries.md
@@ -0,0 +1,6 @@
+---
+'@truefoundry/trueforge-core': minor
+'@truefoundry/trueforge': minor
+---
+
+Split outbound HTTP into an agnostic SSRF `createOutboundFetch` plus separate generic/model/MCP Agents (`OUTBOUND_HTTP_*`, `MODEL_HTTP_*`, `MCP_HTTP_*`) with configurable connect/headers/body timeouts and retries.
diff --git a/packages/trueforge-core/src/core/index.ts b/packages/trueforge-core/src/core/index.ts
index 0386103ed..9f9cb7c8b 100644
--- a/packages/trueforge-core/src/core/index.ts
+++ b/packages/trueforge-core/src/core/index.ts
@@ -157,8 +157,35 @@ export type {
export { AgentHarnessError, McpConnectionError, McpDcrConfigurationError } from './errors';
export { REDIS_KEY_NAMESPACE, redisKey } from './redisKeys';
export { describeUnknownError, extractErrorLogFields } from './util/errorLogFields';
+export {
+ DEFAULT_MCP_HTTP_BODY_TIMEOUT_MS,
+ DEFAULT_MCP_HTTP_CONNECT_TIMEOUT_MS,
+ DEFAULT_MCP_HTTP_HEADERS_TIMEOUT_MS,
+ DEFAULT_MCP_HTTP_MAX_RETRIES,
+ DEFAULT_MODEL_HTTP_BODY_TIMEOUT_MS,
+ DEFAULT_MODEL_HTTP_CONNECT_TIMEOUT_MS,
+ DEFAULT_MODEL_HTTP_HEADERS_TIMEOUT_MS,
+ DEFAULT_MODEL_HTTP_MAX_RETRIES,
+ DEFAULT_OUTBOUND_HTTP_BODY_TIMEOUT_MS,
+ DEFAULT_OUTBOUND_HTTP_CONNECT_TIMEOUT_MS,
+ DEFAULT_OUTBOUND_HTTP_HEADERS_TIMEOUT_MS,
+ DEFAULT_OUTBOUND_HTTP_MAX_RETRIES,
+ configureOutboundFetches,
+ defaultMcpOutboundFetchOptions,
+ defaultModelOutboundFetchOptions,
+ defaultOutboundFetchOptions,
+ mcpSsrfFetch,
+ modelSsrfFetch,
+ ssrfFetch,
+} from './util/outboundFetch';
export { PromiseTimeoutError, withTimeout } from './util/promiseUtils';
-export { assertSafeOutboundUrl, configureOutboundUrlGuard, ssrfFetch } from './util/ssrfGuard';
+export {
+ assertSafeOutboundUrl,
+ configureOutboundUrlGuard,
+ createOutboundFetch,
+ isRetryableOutboundTransportError,
+} from './util/ssrfGuard';
+export type { OutboundFetch, OutboundFetchOptions } from './util/ssrfGuard';
// Sandbox (concrete implementation; provider details exported for composition)
export { CodeModeDispatcher } from './sandbox/codeMode/CodeModeDispatcher';
diff --git a/packages/trueforge-core/src/core/llm/VercelAILLM.ts b/packages/trueforge-core/src/core/llm/VercelAILLM.ts
index 16aa3be4e..ec126ba88 100644
--- a/packages/trueforge-core/src/core/llm/VercelAILLM.ts
+++ b/packages/trueforge-core/src/core/llm/VercelAILLM.ts
@@ -35,7 +35,7 @@ import type {
} from 'openai/resources/chat';
import type { Logger } from 'winston';
import { describeUnknownError, extractErrorLogFields } from '../util/errorLogFields';
-import { ssrfFetch } from '../util/ssrfGuard';
+import { modelSsrfFetch } from '../util/outboundFetch';
import type { ILLM, LLMCreateParams, LLMCreateParamsStreaming } from './ILLM';
import {
type CompletionUsage,
@@ -136,7 +136,7 @@ function compatibleModel(config: VercelAIProviderConfig): LanguageModel {
name: provider.type,
baseURL: baseUrl,
apiKey,
- fetch: ssrfFetch,
+ fetch: modelSsrfFetch,
// Without this the adapter silently downgrades json_schema to a schema-less json_object.
supportsStructuredOutputs: true,
// These endpoints omit token counts from streamed responses unless asked.
@@ -154,7 +154,7 @@ export function buildLanguageModel(config: VercelAIProviderConfig): LanguageMode
case 'openai': {
const client = createOpenAI({
apiKey,
- fetch: ssrfFetch,
+ fetch: modelSsrfFetch,
...(baseUrl !== undefined ? { baseURL: baseUrl } : {}),
...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),
});
@@ -163,7 +163,7 @@ export function buildLanguageModel(config: VercelAIProviderConfig): LanguageMode
case 'anthropic': {
const client = createAnthropic({
apiKey,
- fetch: ssrfFetch,
+ fetch: modelSsrfFetch,
...(baseUrl !== undefined ? { baseURL: baseUrl } : {}),
...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),
});
@@ -172,7 +172,7 @@ export function buildLanguageModel(config: VercelAIProviderConfig): LanguageMode
case 'google-gemini': {
const client = createGoogle({
apiKey,
- fetch: ssrfFetch,
+ fetch: modelSsrfFetch,
...(baseUrl !== undefined ? { baseURL: baseUrl } : {}),
...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),
});
@@ -181,7 +181,7 @@ export function buildLanguageModel(config: VercelAIProviderConfig): LanguageMode
case 'moonshot': {
const client = createMoonshotAI({
apiKey,
- fetch: ssrfFetch,
+ fetch: modelSsrfFetch,
...(baseUrl !== undefined ? { baseURL: baseUrl } : {}),
...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),
});
@@ -195,7 +195,7 @@ export function buildLanguageModel(config: VercelAIProviderConfig): LanguageMode
const client = createAlibaba({
apiKey,
baseURL: baseUrl,
- fetch: ssrfFetch,
+ fetch: modelSsrfFetch,
...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),
});
return client(model.id);
diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts
index 3ce324148..51b222f4d 100644
--- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts
+++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts
@@ -7,8 +7,8 @@ import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js';
import type { CallToolRequest, CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import { context, propagation } from '@opentelemetry/api';
import { McpConnectionError } from '../errors';
+import { mcpSsrfFetch } from '../util/outboundFetch';
import { withTimeout } from '../util/promiseUtils';
-import { mcpSsrfFetch } from '../util/ssrfGuard';
import type { ToolSchema } from './IMCPServer';
/** Networking for remote (url-based) MCP servers, kept separate so it can be mocked in tests. */
diff --git a/packages/trueforge-core/src/core/util/outboundFetch.ts b/packages/trueforge-core/src/core/util/outboundFetch.ts
new file mode 100644
index 000000000..208a43690
--- /dev/null
+++ b/packages/trueforge-core/src/core/util/outboundFetch.ts
@@ -0,0 +1,101 @@
+import { createOutboundFetch, type OutboundFetch, type OutboundFetchOptions } from './ssrfGuard';
+
+/** Generic outbound undici headersTimeout. Env: `OUTBOUND_HTTP_HEADERS_TIMEOUT_MS`. */
+export const DEFAULT_OUTBOUND_HTTP_HEADERS_TIMEOUT_MS = 5_000;
+/** Generic outbound undici connect timeout. Env: `OUTBOUND_HTTP_CONNECT_TIMEOUT_MS`. */
+export const DEFAULT_OUTBOUND_HTTP_CONNECT_TIMEOUT_MS = 5_000;
+/** Generic outbound undici bodyTimeout. Env: `OUTBOUND_HTTP_BODY_TIMEOUT_MS`. */
+export const DEFAULT_OUTBOUND_HTTP_BODY_TIMEOUT_MS = 5_000;
+/** Generic outbound fetch retries. Env: `OUTBOUND_HTTP_MAX_RETRIES`. */
+export const DEFAULT_OUTBOUND_HTTP_MAX_RETRIES = 2;
+
+/** Model outbound undici headersTimeout. Env: `MODEL_HTTP_HEADERS_TIMEOUT_MS`. */
+export const DEFAULT_MODEL_HTTP_HEADERS_TIMEOUT_MS = 5_000;
+/** Model outbound undici connect timeout. Env: `MODEL_HTTP_CONNECT_TIMEOUT_MS`. */
+export const DEFAULT_MODEL_HTTP_CONNECT_TIMEOUT_MS = 5_000;
+/** Model outbound undici bodyTimeout. Env: `MODEL_HTTP_BODY_TIMEOUT_MS`. */
+export const DEFAULT_MODEL_HTTP_BODY_TIMEOUT_MS = 5_000;
+/** Model outbound fetch retries. Env: `MODEL_HTTP_MAX_RETRIES`. */
+export const DEFAULT_MODEL_HTTP_MAX_RETRIES = 2;
+
+/**
+ * MCP outbound undici headersTimeout. Env: `MCP_HTTP_HEADERS_TIMEOUT_MS`.
+ * Matches the default MCP request timeout: JSON-response servers send headers only after the tool finishes.
+ */
+export const DEFAULT_MCP_HTTP_HEADERS_TIMEOUT_MS = 4 * 60 * 1000;
+/** MCP outbound undici connect timeout. Env: `MCP_HTTP_CONNECT_TIMEOUT_MS`. */
+export const DEFAULT_MCP_HTTP_CONNECT_TIMEOUT_MS = 5_000;
+/**
+ * MCP outbound undici bodyTimeout. Env: `MCP_HTTP_BODY_TIMEOUT_MS`.
+ * Default 30m so idle SSE/streamable-HTTP is not killed at undici's 300s.
+ */
+export const DEFAULT_MCP_HTTP_BODY_TIMEOUT_MS = 30 * 60 * 1000;
+/** MCP outbound fetch retries. Env: `MCP_HTTP_MAX_RETRIES`. */
+export const DEFAULT_MCP_HTTP_MAX_RETRIES = 2;
+
+export function defaultOutboundFetchOptions(): OutboundFetchOptions {
+ return {
+ connectTimeoutMs: DEFAULT_OUTBOUND_HTTP_CONNECT_TIMEOUT_MS,
+ headersTimeoutMs: DEFAULT_OUTBOUND_HTTP_HEADERS_TIMEOUT_MS,
+ bodyTimeoutMs: DEFAULT_OUTBOUND_HTTP_BODY_TIMEOUT_MS,
+ maxRetries: DEFAULT_OUTBOUND_HTTP_MAX_RETRIES,
+ idempotent: true,
+ };
+}
+
+export function defaultModelOutboundFetchOptions(): OutboundFetchOptions {
+ return {
+ connectTimeoutMs: DEFAULT_MODEL_HTTP_CONNECT_TIMEOUT_MS,
+ headersTimeoutMs: DEFAULT_MODEL_HTTP_HEADERS_TIMEOUT_MS,
+ bodyTimeoutMs: DEFAULT_MODEL_HTTP_BODY_TIMEOUT_MS,
+ maxRetries: DEFAULT_MODEL_HTTP_MAX_RETRIES,
+ idempotent: true,
+ };
+}
+
+export function defaultMcpOutboundFetchOptions(): OutboundFetchOptions {
+ return {
+ connectTimeoutMs: DEFAULT_MCP_HTTP_CONNECT_TIMEOUT_MS,
+ headersTimeoutMs: DEFAULT_MCP_HTTP_HEADERS_TIMEOUT_MS,
+ bodyTimeoutMs: DEFAULT_MCP_HTTP_BODY_TIMEOUT_MS,
+ maxRetries: DEFAULT_MCP_HTTP_MAX_RETRIES,
+ // Tool calls are not safe to replay once upstream may have received them.
+ idempotent: false,
+ };
+}
+
+let outbound: OutboundFetch = createOutboundFetch(defaultOutboundFetchOptions());
+let modelOutbound: OutboundFetch = createOutboundFetch(defaultModelOutboundFetchOptions());
+let mcpOutbound: OutboundFetch = createOutboundFetch(defaultMcpOutboundFetchOptions());
+
+/** Replace outbound Agents (timeouts/retries). Does not change SSRF allow/block lists. */
+export function configureOutboundFetches(config: {
+ outbound: OutboundFetchOptions;
+ model: OutboundFetchOptions;
+ mcp: OutboundFetchOptions;
+}): void {
+ const previousOutbound = outbound;
+ const previousModel = modelOutbound;
+ const previousMcp = mcpOutbound;
+ outbound = createOutboundFetch(config.outbound);
+ modelOutbound = createOutboundFetch(config.model);
+ mcpOutbound = createOutboundFetch(config.mcp);
+ void previousOutbound.close();
+ void previousModel.close();
+ void previousMcp.close();
+}
+
+/** Drop-in fetch for generic short-lived outbound HTTP (not model or MCP). */
+export async function ssrfFetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
+ return outbound.fetch(input, init);
+}
+
+/** Drop-in fetch for model-provider HTTP. */
+export async function modelSsrfFetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
+ return modelOutbound.fetch(input, init);
+}
+
+/** Drop-in fetch for remote MCP (long bodyTimeout; does not retry headers timeouts). */
+export async function mcpSsrfFetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
+ return mcpOutbound.fetch(input, init);
+}
diff --git a/packages/trueforge-core/src/core/util/ssrfGuard.ts b/packages/trueforge-core/src/core/util/ssrfGuard.ts
index b9d63937d..d04c77c44 100644
--- a/packages/trueforge-core/src/core/util/ssrfGuard.ts
+++ b/packages/trueforge-core/src/core/util/ssrfGuard.ts
@@ -8,6 +8,36 @@ let allowedHosts: string[] = [];
let blockedHosts: string[] = [];
let guardEnabled = true;
+const MAX_REDIRECTS = 20;
+const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
+const CROSS_ORIGIN_STRIPPED_HEADERS = ['authorization', 'proxy-authorization', 'cookie', 'host'];
+// Rejected before processing (timeout, rate limit, gateway never reached the origin).
+const UNPROCESSED_RETRY_STATUSES = new Set([408, 429, 521, 522, 523, 530]);
+// Upstream may have processed the request (409 conflict, 520 bad origin response, 524 origin timeout).
+const MAYBE_PROCESSED_RETRY_STATUSES = new Set([409, 520, 524]);
+const RETRY_INITIAL_DELAY_MS = 1_000;
+const RETRY_BACKOFF_FACTOR = 2;
+const RETRY_JITTER_FACTOR = 0.2;
+// Longer server-requested waits fall back to backoff rather than stalling the caller.
+const MAX_RETRY_AFTER_MS = 5_000;
+
+export interface OutboundFetchOptions {
+ connectTimeoutMs: number;
+ headersTimeoutMs: number;
+ bodyTimeoutMs: number;
+ maxRetries: number;
+ /**
+ * Whether requests are safe to replay after upstream may have received them. When false, headers
+ * timeouts and 409/520/524 are not retried.
+ */
+ idempotent: boolean;
+}
+
+export interface OutboundFetch {
+ fetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
+ close: () => Promise<void>;
+}
+
const URL_VERIFY = {
allowedProtocols: ['http:', 'https:'],
denyCidrsV4: [
@@ -69,20 +99,6 @@ function addDenyCidrs(cidrs: readonly string[], family: 'ipv4' | 'ipv6'): void {
addDenyCidrs(URL_VERIFY.denyCidrsV4, 'ipv4');
addDenyCidrs(URL_VERIFY.denyCidrsV6, 'ipv6');
-const MAX_REDIRECTS = 20;
-const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
-const CROSS_ORIGIN_STRIPPED_HEADERS = ['authorization', 'proxy-authorization', 'cookie', 'host'];
-
-export function configureOutboundUrlGuard(config: {
- enabled?: boolean;
- allowedHosts: readonly string[];
- blockedHosts: readonly string[];
-}): void {
- guardEnabled = config.enabled ?? true;
- allowedHosts = config.allowedHosts.map(normalizeHost);
- blockedHosts = config.blockedHosts.map(normalizeHost);
-}
-
function normalizeHost(hostname: string): string {
const host = hostname.replace(/\.$/, '').toLowerCase();
return host.startsWith('[') && host.endsWith(']') ? host.slice(1, -1) : host;
@@ -174,17 +190,16 @@ const guardedLookup: LookupFunction = (hostname, options: LookupOptions, callbac
});
};
-const MCP_BODY_TIMEOUT_MS = 30 * 60 * 1000;
-
-const outboundAgent = new Agent({
- connect: { lookup: guardedLookup },
-});
-
-const mcpOutboundAgent = new Agent({
- // MCP SSE/streamable-HTTP stays idle between tool calls; undici's 300s bodyTimeout kills it.
- bodyTimeout: MCP_BODY_TIMEOUT_MS,
- connect: { lookup: guardedLookup },
-});
+/** SSRF allow/block policy for all outbound guarded fetches. Timeouts live on `createOutboundFetch`. */
+export function configureOutboundUrlGuard(config: {
+ enabled?: boolean;
+ allowedHosts: readonly string[];
+ blockedHosts: readonly string[];
+}): void {
+ guardEnabled = config.enabled ?? true;
+ allowedHosts = config.allowedHosts.map(normalizeHost);
+ blockedHosts = config.blockedHosts.map(normalizeHost);
+}
export async function assertSafeOutboundUrl(input: string | URL | Request): Promise<void> {
const url = parseOutboundUrl(input);
@@ -260,6 +275,129 @@ function mergeRequestInit(input: string | URL | Request, init: RequestInit): Req
};
}
+/** Bodies that undici can send more than once without re-reading a stream. */
+function isReplayableBodyInit(body: NonNullable<RequestInit['body']>): boolean {
+ return (
+ typeof body === 'string' ||
+ body instanceof ArrayBuffer ||
+ ArrayBuffer.isView(body) ||
+ body instanceof Blob ||
+ body instanceof FormData ||
+ body instanceof URLSearchParams
+ );
+}
+
+/**
+ * Normalize to URL + init, buffering one-shot stream bodies so retries can replay them.
+ * `Request` bodies are always streams even when constructed from a string.
+ */
+async function materializeReplayableOutboundRequest(
+ input: string | URL | Request,
+ init: RequestInit,
+): Promise<{ input: URL; init: RequestInit }> {
+ const url = parseOutboundUrl(input);
+ const merged = mergeRequestInit(input, init);
+ const body = merged.body;
+ if (body == null || isReplayableBodyInit(body)) {
+ return { input: url, init: merged };
+ }
+ const buffer = await new Response(body).arrayBuffer();
+ return {
+ input: url,
+ init: {
+ ...merged,
+ body: buffer,
+ },
+ };
+}
+
+function getErrorCode(error: unknown): string | undefined {
+ let current: unknown = error;
+ for (let depth = 0; depth < 5 && current != null; depth += 1) {
+ if (typeof current === 'object' && 'code' in current) {
+ const code = Reflect.get(current, 'code');
+ if (typeof code === 'string') {
+ return code;
+ }
+ }
+ current = current instanceof Error ? current.cause : undefined;
+ }
+ return undefined;
+}
+
+/** Connect / headers-read timeouts from undici (including nested under TypeError: fetch failed). */
+export function isRetryableOutboundTransportError(error: unknown, options: { idempotent: boolean }): boolean {
+ const code = getErrorCode(error);
+ if (code === 'UND_ERR_CONNECT_TIMEOUT') {
+ return true;
+ }
+ return options.idempotent && code === 'UND_ERR_HEADERS_TIMEOUT';
+}
+
+function isRetryableOutboundStatus(status: number, options: { idempotent: boolean }): boolean {
+ return UNPROCESSED_RETRY_STATUSES.has(status) || (options.idempotent && MAYBE_PROCESSED_RETRY_STATUSES.has(status));
+}
+
+/** Server-requested delay from `retry-after-ms` or `retry-after` (seconds or HTTP date), if usable. */
+function retryAfterMs(headers: { get(name: string): string | null }): number | undefined {
+ let delayMs: number | undefined;
+ const retryAfterMsHeader = headers.get('retry-after-ms');
+ if (retryAfterMsHeader !== null) {
+ delayMs = Number.parseFloat(retryAfterMsHeader);
+ } else {
+ const retryAfter = headers.get('retry-after');
+ if (retryAfter !== null) {
+ const seconds = Number(retryAfter);
+ delayMs = Number.isFinite(seconds) ? seconds * 1000 : Date.parse(retryAfter) - Date.now();
+ }
+ }
+ if (delayMs === undefined || !Number.isFinite(delayMs) || delayMs < 0 || delayMs > MAX_RETRY_AFTER_MS) {
+ return undefined;
+ }
+ return delayMs;
+}
+
+function abortError(signal: AbortSignal): Error {
+ if (signal.reason instanceof Error) {
+ return signal.reason;
+ }
+ const aborted = new Error('This operation was aborted');
+ aborted.name = 'AbortError';
+ return aborted;
+}
+
+function retryDelayMs(attempt: number): number {
+ const base = RETRY_INITIAL_DELAY_MS * RETRY_BACKOFF_FACTOR ** attempt;
+ const jitterMultiplier = 1 + (Math.random() - 0.5) * RETRY_JITTER_FACTOR;
+ return Math.max(0, Math.round(base * jitterMultiplier));
+}
+
+function sleep(ms: number, signal: AbortSignal | undefined): Promise<void> {
+ return new Promise((resolve, reject) => {
+ if (signal?.aborted) {
+ reject(abortError(signal));
+ return;
+ }
+ const timer = setTimeout(() => {
+ if (signal !== undefined) {
+ signal.removeEventListener('abort', onAbort);
+ }
+ resolve();
+ }, ms);
+ const onAbort = (): void => {
+ clearTimeout(timer);
+ if (signal !== undefined) {
+ reject(abortError(signal));
+ return;
+ }
+ reject(new Error('This operation was aborted'));
+ };
+ if (signal !== undefined) {
+ signal.addEventListener('abort', onAbort, { once: true });
+ }
+ });
+}
+
async function guardedFetch(
input: string | URL | Request,
init: RequestInit,
@@ -304,11 +442,52 @@ async function guardedFetch(
return guardedFetch(hop.url, hop.init, hopsLeft - 1, agent);
}
-export async function ssrfFetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
- return guardedFetch(input, init ?? {}, MAX_REDIRECTS, outboundAgent);
+async function withOutboundHttpRetries(
+ input: string | URL | Request,
+ init: RequestInit,
+ agent: Agent,
+ options: { maxRetries: number; idempotent: boolean },
+): Promise<Response> {
+ const signal = init.signal ?? (input instanceof Request ? input.signal : undefined);
+ // Buffer stream / Request bodies once so a retry does not hit a consumed-body error.
+ const replayable = options.maxRetries > 0 ? await materializeReplayableOutboundRequest(input, init) : { input, init };
+ let attempt = 0;
+ for (;;) {
+ if (signal?.aborted) {
+ throw abortError(signal);
+ }
+ let delayMs = retryDelayMs(attempt);
+ try {
+ const response = await guardedFetch(replayable.input, replayable.init, MAX_REDIRECTS, agent);
+ if (!isRetryableOutboundStatus(response.status, options) || attempt >= options.maxRetries) {
+ return response;
+ }
+ delayMs = retryAfterMs(response.headers) ?? delayMs;
+ void response.body?.cancel().catch(() => undefined);
+ } catch (error) {
+ if (!isRetryableOutboundTransportError(error, options) || attempt >= options.maxRetries || signal?.aborted) {
+ throw error;
+ }
+ }
+ await sleep(delayMs, signal);
+ attempt += 1;
+ }
}
-/** Same as `ssrfFetch` with a 30m bodyTimeout for idle MCP SSE / streamable-HTTP. */
-export async function mcpSsrfFetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
- return guardedFetch(input, init ?? {}, MAX_REDIRECTS, mcpOutboundAgent);
+/** Builds a pooled undici Agent + SSRF-guarded fetch with the given timeouts/retries. */
+export function createOutboundFetch(options: OutboundFetchOptions): OutboundFetch {
+ const agent = new Agent({
+ bodyTimeout: options.bodyTimeoutMs,
+ headersTimeout: options.headersTimeoutMs,
+ connectTimeout: options.connectTimeoutMs,
+ connect: { lookup: guardedLookup, timeout: options.connectTimeoutMs },
+ });
+ const maxRetries = options.maxRetries;
+ const idempotent = options.idempotent;
+ return {
+ fetch: (input, init) => withOutboundHttpRetries(input, init ?? {}, agent, { maxRetries, idempotent }),
+ close: async () => {
+ await agent.close().catch(() => undefined);
+ },
+ };
}
diff --git a/packages/trueforge-core/src/core/web-search/ParallelWebSearchProvider.ts b/packages/trueforge-core/src/core/web-search/ParallelWebSearchProvider.ts
index c2ef604bf..460d20964 100644
--- a/packages/trueforge-core/src/core/web-search/ParallelWebSearchProvider.ts
+++ b/packages/trueforge-core/src/core/web-search/ParallelWebSearchProvider.ts
@@ -1,5 +1,5 @@
import Parallel from 'parallel-web';
-import { ssrfFetch } from '../util/ssrfGuard';
+import { ssrfFetch } from '../util/outboundFetch';
import {
WebSearchProviders,
type IWebSearchProvider,
diff --git a/packages/trueforge-core/tests/core/util/ssrfGuard.test.ts b/packages/trueforge-core/tests/core/util/ssrfGuard.test.ts
index e5d72d5ee..98aaf6cd0 100644
--- a/packages/trueforge-core/tests/core/util/ssrfGuard.test.ts
+++ b/packages/trueforge-core/tests/core/util/ssrfGuard.test.ts
@@ -1,8 +1,26 @@
import http from 'node:http';
-import { assertSafeOutboundUrl, configureOutboundUrlGuard, ssrfFetch } from '../../../src/core/util/ssrfGuard';
+import {
+ configureOutboundFetches,
+ defaultMcpOutboundFetchOptions,
+ defaultModelOutboundFetchOptions,
+ defaultOutboundFetchOptions,
+ mcpSsrfFetch,
+ modelSsrfFetch,
+ ssrfFetch,
+} from '../../../src/core/util/outboundFetch';
+import { assertSafeOutboundUrl, configureOutboundUrlGuard } from '../../../src/core/util/ssrfGuard';
-afterEach(() => {
+function resetOutbound(): void {
configureOutboundUrlGuard({ allowedHosts: [], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: defaultOutboundFetchOptions(),
+ model: defaultModelOutboundFetchOptions(),
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+}
+
+afterEach(() => {
+ resetOutbound();
});
async function listen(handler: http.RequestListener): Promise<{ server: http.Server; origin: string }> {
@@ -110,40 +128,282 @@ describe('ssrfFetch', () => {
}
});
- it('keeps method, headers, and body from a Request argument', async () => {
+ it('retries gateway 520 then returns success', async () => {
configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: { ...defaultOutboundFetchOptions(), maxRetries: 2 },
+ model: defaultModelOutboundFetchOptions(),
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+ let hits = 0;
+ const { server, origin } = await listen((_req, res) => {
+ hits += 1;
+ if (hits === 1) {
+ res.writeHead(520, { 'content-type': 'text/plain' });
+ res.end('gateway');
+ return;
+ }
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ res.end('ok');
+ });
+ try {
+ const response = await ssrfFetch(`${origin}/`);
+ expect(response.status).toBe(200);
+ await expect(response.text()).resolves.toBe('ok');
+ expect(hits).toBe(2);
+ } finally {
+ await closeServer(server);
+ }
+ });
+
+ it('retries Request POST body after gateway 520 (body is not left consumed)', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: { ...defaultOutboundFetchOptions(), maxRetries: 2 },
+ model: defaultModelOutboundFetchOptions(),
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+ const bodies: string[] = [];
+ let hits = 0;
const { server, origin } = await listen((req, res) => {
+ hits += 1;
const chunks: Buffer[] = [];
req.on('data', chunk => {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
});
req.on('end', () => {
- res.writeHead(200, { 'content-type': 'application/json' });
- res.end(
- JSON.stringify({
- method: req.method,
- authorization: req.headers.authorization ?? null,
- body: Buffer.concat(chunks).toString('utf8'),
- }),
- );
+ bodies.push(Buffer.concat(chunks).toString('utf8'));
+ if (hits === 1) {
+ res.writeHead(520, { 'content-type': 'text/plain', 'retry-after-ms': '0' });
+ res.end('gateway');
+ return;
+ }
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ res.end('ok');
});
});
try {
const response = await ssrfFetch(
new Request(`${origin}/echo`, {
method: 'POST',
- headers: { authorization: 'Bearer t', 'content-type': 'text/plain' },
- body: 'hello',
+ headers: { 'content-type': 'text/plain' },
+ body: 'hello-retry',
}),
);
expect(response.status).toBe(200);
- await expect(response.json()).resolves.toEqual({
- method: 'POST',
- authorization: 'Bearer t',
- body: 'hello',
+ await expect(response.text()).resolves.toBe('ok');
+ expect(hits).toBe(2);
+ expect(bodies).toEqual(['hello-retry', 'hello-retry']);
+ } finally {
+ await closeServer(server);
+ }
+ });
+
+ it('retries 429 after the server-requested Retry-After delay instead of backoff', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: { ...defaultOutboundFetchOptions(), maxRetries: 2 },
+ model: defaultModelOutboundFetchOptions(),
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+ for (const retryAfter of [{ 'retry-after-ms': '50' }, { 'retry-after': '0' }]) {
+ let hits = 0;
+ const { server, origin } = await listen((_req, res) => {
+ hits += 1;
+ if (hits === 1) {
+ res.writeHead(429, { 'content-type': 'text/plain', ...retryAfter });
+ res.end('slow down');
+ return;
+ }
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ res.end('ok');
});
+ try {
+ const started = Date.now();
+ const response = await ssrfFetch(`${origin}/`);
+ expect(response.status).toBe(200);
+ expect(hits).toBe(2);
+ // Default backoff is ~1s; a much shorter wait proves Retry-After was used.
+ expect(Date.now() - started).toBeLessThan(500);
+ } finally {
+ await closeServer(server);
+ }
+ }
+ });
+
+ it('falls back to backoff when Retry-After exceeds our cap', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: { ...defaultOutboundFetchOptions(), maxRetries: 2 },
+ model: defaultModelOutboundFetchOptions(),
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+ let hits = 0;
+ const { server, origin } = await listen((_req, res) => {
+ hits += 1;
+ if (hits === 1) {
+ res.writeHead(429, { 'content-type': 'text/plain', 'retry-after': '120' });
+ res.end('slow down');
+ return;
+ }
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ res.end('ok');
+ });
+ try {
+ const started = Date.now();
+ const response = await ssrfFetch(`${origin}/`);
+ expect(response.status).toBe(200);
+ expect(hits).toBe(2);
+ expect(Date.now() - started).toBeLessThan(3_000);
} finally {
await closeServer(server);
}
});
+
+ it('retries headers timeout then returns success', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: {
+ ...defaultOutboundFetchOptions(),
+ headersTimeoutMs: 50,
+ maxRetries: 2,
+ },
+ model: defaultModelOutboundFetchOptions(),
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+ let hits = 0;
+ const { server, origin } = await listen((_req, res) => {
+ hits += 1;
+ if (hits === 1) {
+ return;
+ }
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ res.end('ok');
+ });
+ try {
+ const response = await ssrfFetch(`${origin}/`);
+ expect(response.status).toBe(200);
+ await expect(response.text()).resolves.toBe('ok');
+ expect(hits).toBe(2);
+ } finally {
+ await closeServer(server);
+ }
+ }, 15_000);
+
+ it('stops retrying when aborted during retry backoff', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: { ...defaultOutboundFetchOptions(), maxRetries: 3 },
+ model: defaultModelOutboundFetchOptions(),
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+ const controller = new AbortController();
+ let hits = 0;
+ const { server, origin } = await listen((_req, res) => {
+ hits += 1;
+ res.writeHead(520, { 'content-type': 'text/plain' });
+ res.end('gateway', () => {
+ setTimeout(() => controller.abort(), 50);
+ });
+ });
+ try {
+ await expect(ssrfFetch(`${origin}/`, { signal: controller.signal })).rejects.toThrow(/abort/i);
+ expect(hits).toBe(1);
+ } finally {
+ await closeServer(server);
+ }
+ });
+});
+
+describe('modelSsrfFetch', () => {
+ it('uses the model Agent, not the generic outbound Agent', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: { ...defaultOutboundFetchOptions(), maxRetries: 0 },
+ model: { ...defaultModelOutboundFetchOptions(), maxRetries: 2 },
+ mcp: defaultMcpOutboundFetchOptions(),
+ });
+ let hits = 0;
+ const { server, origin } = await listen((_req, res) => {
+ hits += 1;
+ if (hits === 1) {
+ res.writeHead(520, { 'content-type': 'text/plain' });
+ res.end('gateway');
+ return;
+ }
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ res.end('ok');
+ });
+ try {
+ const response = await modelSsrfFetch(`${origin}/`);
+ expect(response.status).toBe(200);
+ expect(hits).toBe(2);
+ hits = 0;
+ const generic = await ssrfFetch(`${origin}/`);
+ expect(generic.status).toBe(520);
+ expect(hits).toBe(1);
+ } finally {
+ await closeServer(server);
+ }
+ });
+});
+
+describe('mcpSsrfFetch', () => {
+ it('retries statuses where the request was not processed but not 409/520/524', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: defaultOutboundFetchOptions(),
+ model: defaultModelOutboundFetchOptions(),
+ mcp: { ...defaultMcpOutboundFetchOptions(), maxRetries: 2 },
+ });
+ let firstStatus = 522;
+ let hits = 0;
+ const { server, origin } = await listen((_req, res) => {
+ hits += 1;
+ const status = hits === 1 ? firstStatus : 200;
+ res.writeHead(status, { 'content-type': 'text/plain', 'retry-after-ms': '0' });
+ res.end(String(status));
+ });
+ try {
+ for (const status of [408, 429, 522]) {
+ firstStatus = status;
+ hits = 0;
+ const retried = await mcpSsrfFetch(`${origin}/`);
+ expect(retried.status).toBe(200);
+ expect(hits).toBe(2);
+ }
+ for (const status of [409, 520, 524]) {
+ firstStatus = status;
+ hits = 0;
+ const response = await mcpSsrfFetch(`${origin}/`);
+ expect(response.status).toBe(status);
+ expect(hits).toBe(1);
+ }
+ } finally {
+ await closeServer(server);
+ }
+ });
+
+ it('does not retry headers timeouts so long tool POSTs are not replayed', async () => {
+ configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
+ configureOutboundFetches({
+ outbound: defaultOutboundFetchOptions(),
+ model: defaultModelOutboundFetchOptions(),
+ mcp: {
+ ...defaultMcpOutboundFetchOptions(),
+ headersTimeoutMs: 50,
+ maxRetries: 2,
+ },
+ });
+ let hits = 0;
+ const { server, origin } = await listen((_req, _res) => {
+ hits += 1;
+ });
+ try {
+ await expect(mcpSsrfFetch(`${origin}/`)).rejects.toThrow();
+ expect(hits).toBe(1);
+ } finally {
+ await closeServer(server);
+ }
+ }, 15_000);
});
diff --git a/packages/trueforge/.env.example b/packages/trueforge/.env.example
index 8e587073a..a5f59b940 100644
--- a/packages/trueforge/.env.example
+++ b/packages/trueforge/.env.example
@@ -140,18 +140,39 @@ TRUEFORGE_API_KEY=placeholder-value-please-generate-your-own
# MCP_DCR_OAUTH_CLIENT_NAME=truefoundry-harness
## MCP client timeouts. All optional.
-## Max ms for one MCP request. Default 240000 (4 minutes).
+## Max ms for one MCP JSON-RPC request end to end. Default 240000 (4 minutes).
# MCP_REQUEST_TIMEOUT_MS=240000
-## Max ms for an MCP transport connection. Default 30000.
+## Max ms for the MCP initialize handshake per transport attempt. Default 30000.
# MCP_CONNECT_TIMEOUT_MS=30000
## Max bytes for one remote MCP tool-call HTTP response body (JSON / POST SSE). Default 50 MB.
# MCP_TOOL_CALL_MAX_RESPONSE_BYTES=52428800
+## undici headersTimeout for MCP fetch. JSON-response servers send headers only after the tool
+## finishes, so keep this >= MCP_REQUEST_TIMEOUT_MS. Default MCP_REQUEST_TIMEOUT_MS.
+# MCP_HTTP_HEADERS_TIMEOUT_MS=240000
+## undici connect timeout for MCP fetch. Default 5000.
+# MCP_HTTP_CONNECT_TIMEOUT_MS=5000
+## undici bodyTimeout for MCP fetch (idle between body chunks). Default 1800000 (30m).
+# MCP_HTTP_BODY_TIMEOUT_MS=1800000
+## Retries for MCP connect timeouts and 408/429/521–523/530 (not headers timeouts or 409/520/524). Honors Retry-After up to 5s, else backoff. Default 2.
+# MCP_HTTP_MAX_RETRIES=2
## Outbound URL guard (MCP url / model-provider base_url). Default on; false skips it.
## Host matrix: charts/trueforge/README.md (networkPolicy).
# NETWORK_POLICY_ENABLED=true
# OUTBOUND_URL_ALLOWED_HOSTS=["localhost","127.0.0.1","llm-gateway.internal"]
# OUTBOUND_URL_BLOCKED_HOSTS=["evil.example.com"]
+## Generic outbound undici timeouts/retries (not model or MCP).
+# OUTBOUND_HTTP_HEADERS_TIMEOUT_MS=5000
+# OUTBOUND_HTTP_CONNECT_TIMEOUT_MS=5000
+# OUTBOUND_HTTP_BODY_TIMEOUT_MS=5000
+# OUTBOUND_HTTP_MAX_RETRIES=2
+## Model-provider undici timeouts/retries.
+# MODEL_HTTP_HEADERS_TIMEOUT_MS=5000
+# MODEL_HTTP_CONNECT_TIMEOUT_MS=5000
+## undici bodyTimeout for model fetch (idle between body chunks). Default 5000.
+# MODEL_HTTP_BODY_TIMEOUT_MS=5000
+## Retries for model connect/headers timeouts and 408/409/429/520–524/530. Honors Retry-After up to 5s, else backoff. Default 2.
+# MODEL_HTTP_MAX_RETRIES=2
## ---------------------------------------------------------------------------
## Sandbox process knobs.
diff --git a/packages/trueforge/src/config.ts b/packages/trueforge/src/config.ts
index fc99cf0ba..c819f8dd8 100644
--- a/packages/trueforge/src/config.ts
+++ b/packages/trueforge/src/config.ts
@@ -724,10 +724,6 @@ export interface SharedServerConfiguration {
* monorepo `packages/frontend/dist` — always absolute, independent of CWD.
*/
FRONTEND_DIR: string;
- /** Max milliseconds for one MCP request. Env: `MCP_REQUEST_TIMEOUT_MS`. Default 4 minutes. */
- MCP_REQUEST_TIMEOUT_MS: number;
- /** Max milliseconds for an MCP transport connection. Env: `MCP_CONNECT_TIMEOUT_MS`. Default 30 seconds. */
- MCP_CONNECT_TIMEOUT_MS: number;
/** Max bytes for one remote MCP tool-call HTTP response body (not GET SSE). Env: `MCP_TOOL_CALL_MAX_RESPONSE_BYTES`. Default 50 MB. */
MCP_TOOL_CALL_MAX_RESPONSE_BYTES: number;
/**
@@ -833,6 +829,71 @@ export interface SharedServerConfiguration {
OUTBOUND_URL_ALLOWED_HOSTS: string[];
/** Hosts always blocked. Env: `OUTBOUND_URL_BLOCKED_HOSTS` (JSON string array). Empty = none. */
OUTBOUND_URL_BLOCKED_HOSTS: string[];
+ /**
+ * undici headersTimeout for generic outbound fetch (not model or MCP).
+ * Env: `OUTBOUND_HTTP_HEADERS_TIMEOUT_MS`. Default 5000.
+ */
+ OUTBOUND_HTTP_HEADERS_TIMEOUT_MS: number;
+ /**
+ * undici connect timeout for generic outbound fetch.
+ * Env: `OUTBOUND_HTTP_CONNECT_TIMEOUT_MS`. Default 5000.
+ */
+ OUTBOUND_HTTP_CONNECT_TIMEOUT_MS: number;
+ /**
+ * undici bodyTimeout for generic outbound fetch (idle between body chunks).
+ * Env: `OUTBOUND_HTTP_BODY_TIMEOUT_MS`. Default 5000.
+ */
+ OUTBOUND_HTTP_BODY_TIMEOUT_MS: number;
+ /**
+ * Retries after the first generic outbound fetch for connect/headers timeouts and 408/409/429/520–524/530 (honors Retry-After up to 5s, else backoff).
+ * Env: `OUTBOUND_HTTP_MAX_RETRIES`. Default 2.
+ */
+ OUTBOUND_HTTP_MAX_RETRIES: number;
+ /**
+ * undici connect timeout for model-provider outbound fetch.
+ * Env: `MODEL_HTTP_CONNECT_TIMEOUT_MS`. Default 5000.
+ */
+ MODEL_HTTP_CONNECT_TIMEOUT_MS: number;
+ /**
+ * undici headersTimeout for model-provider outbound fetch.
+ * Env: `MODEL_HTTP_HEADERS_TIMEOUT_MS`. Default 5000.
+ */
+ MODEL_HTTP_HEADERS_TIMEOUT_MS: number;
+ /**
+ * undici bodyTimeout for model-provider outbound fetch (idle between body chunks).
+ * Env: `MODEL_HTTP_BODY_TIMEOUT_MS`. Default 5000.
+ */
+ MODEL_HTTP_BODY_TIMEOUT_MS: number;
+ /**
+ * Retries after the first model fetch for connect/headers timeouts and 408/409/429/520–524/530 (honors Retry-After up to 5s, else backoff).
+ * Env: `MODEL_HTTP_MAX_RETRIES`. Default 2.
+ */
+ MODEL_HTTP_MAX_RETRIES: number;
+ /** Max milliseconds for one MCP JSON-RPC request end to end. Env: `MCP_REQUEST_TIMEOUT_MS`. Default 4 minutes. */
+ MCP_REQUEST_TIMEOUT_MS: number;
+ /** Max milliseconds for the MCP initialize handshake per transport attempt. Env: `MCP_CONNECT_TIMEOUT_MS`. Default 30 seconds. */
+ MCP_CONNECT_TIMEOUT_MS: number;
+ /**
+ * undici connect timeout for MCP outbound fetch.
+ * Env: `MCP_HTTP_CONNECT_TIMEOUT_MS`. Default 5000.
+ */
+ MCP_HTTP_CONNECT_TIMEOUT_MS: number;
+ /**
+ * undici headersTimeout for MCP outbound fetch. JSON-response MCP servers send headers only after
+ * the tool finishes, so this should not be below `MCP_REQUEST_TIMEOUT_MS`.
+ * Env: `MCP_HTTP_HEADERS_TIMEOUT_MS`. Default `MCP_REQUEST_TIMEOUT_MS`.
+ */
+ MCP_HTTP_HEADERS_TIMEOUT_MS: number;
+ /**
+ * undici bodyTimeout for MCP outbound fetch (idle between body chunks). Default 30m so idle
+ * SSE/streamable-HTTP is not killed at undici's 300s. Env: `MCP_HTTP_BODY_TIMEOUT_MS`.
+ */
+ MCP_HTTP_BODY_TIMEOUT_MS: number;
+ /**
+ * Retries after the first MCP fetch for connect timeouts and 408/429/521–523/530 only (not headers
+ * timeouts or 409/520/524 — upstream may already have started the tool; honors Retry-After up to 5s, else backoff). Env: `MCP_HTTP_MAX_RETRIES`. Default 2.
+ */
+ MCP_HTTP_MAX_RETRIES: number;
SENTRY_ENABLED: boolean;
SENTRY_DSN: string | undefined;
SENTRY_ADDITIONAL_TAGS: Record<string, string>;
@@ -1015,6 +1076,12 @@ const serverExecutionTimeoutSeconds = parsePositiveInt({
defaultValue: 3600,
});
+const mcpRequestTimeoutMs = parsePositiveInt({
+ envKey: 'MCP_REQUEST_TIMEOUT_MS',
+ raw: getEnv('MCP_REQUEST_TIMEOUT_MS'),
+ defaultValue: 4 * 60 * 1000,
+});
+
const standalone = parseBoolean({
envKey: 'STANDALONE',
raw: getEnv('STANDALONE'),
@@ -1041,17 +1108,6 @@ const shared: SharedServerConfiguration = {
SANDBOX_CATALOG_PATH: resolveOptionalPathEnv('SANDBOX_CATALOG_PATH'),
WEB_SEARCH_CATALOG_PATH: resolveOptionalPathEnv('WEB_SEARCH_CATALOG_PATH'),
FRONTEND_DIR: resolveFrontendDir(),
-
- MCP_REQUEST_TIMEOUT_MS: parsePositiveInt({
- envKey: 'MCP_REQUEST_TIMEOUT_MS',
- raw: getEnv('MCP_REQUEST_TIMEOUT_MS'),
- defaultValue: 4 * 60 * 1000,
- }),
- MCP_CONNECT_TIMEOUT_MS: parsePositiveInt({
- envKey: 'MCP_CONNECT_TIMEOUT_MS',
- raw: getEnv('MCP_CONNECT_TIMEOUT_MS'),
- defaultValue: 30 * 1000,
- }),
MCP_TOOL_CALL_MAX_RESPONSE_BYTES: parsePositiveInt({
envKey: 'MCP_TOOL_CALL_MAX_RESPONSE_BYTES',
raw: getEnv('MCP_TOOL_CALL_MAX_RESPONSE_BYTES'),
@@ -1135,6 +1191,72 @@ const shared: SharedServerConfiguration = {
envKey: 'OUTBOUND_URL_BLOCKED_HOSTS',
raw: getEnv('OUTBOUND_URL_BLOCKED_HOSTS'),
}),
+ OUTBOUND_HTTP_CONNECT_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'OUTBOUND_HTTP_CONNECT_TIMEOUT_MS',
+ raw: getEnv('OUTBOUND_HTTP_CONNECT_TIMEOUT_MS'),
+ defaultValue: 5_000,
+ }),
+ OUTBOUND_HTTP_HEADERS_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'OUTBOUND_HTTP_HEADERS_TIMEOUT_MS',
+ raw: getEnv('OUTBOUND_HTTP_HEADERS_TIMEOUT_MS'),
+ defaultValue: 5_000,
+ }),
+ OUTBOUND_HTTP_BODY_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'OUTBOUND_HTTP_BODY_TIMEOUT_MS',
+ raw: getEnv('OUTBOUND_HTTP_BODY_TIMEOUT_MS'),
+ defaultValue: 5_000,
+ }),
+ OUTBOUND_HTTP_MAX_RETRIES: parseNonNegativeInt({
+ envKey: 'OUTBOUND_HTTP_MAX_RETRIES',
+ raw: getEnv('OUTBOUND_HTTP_MAX_RETRIES'),
+ defaultValue: 2,
+ }),
+ MODEL_HTTP_CONNECT_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'MODEL_HTTP_CONNECT_TIMEOUT_MS',
+ raw: getEnv('MODEL_HTTP_CONNECT_TIMEOUT_MS'),
+ defaultValue: 5_000,
+ }),
+ MODEL_HTTP_HEADERS_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'MODEL_HTTP_HEADERS_TIMEOUT_MS',
+ raw: getEnv('MODEL_HTTP_HEADERS_TIMEOUT_MS'),
+ defaultValue: 5_000,
+ }),
+ MODEL_HTTP_BODY_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'MODEL_HTTP_BODY_TIMEOUT_MS',
+ raw: getEnv('MODEL_HTTP_BODY_TIMEOUT_MS'),
+ defaultValue: 5_000,
+ }),
+ MODEL_HTTP_MAX_RETRIES: parseNonNegativeInt({
+ envKey: 'MODEL_HTTP_MAX_RETRIES',
+ raw: getEnv('MODEL_HTTP_MAX_RETRIES'),
+ defaultValue: 2,
+ }),
+ MCP_REQUEST_TIMEOUT_MS: mcpRequestTimeoutMs,
+ MCP_CONNECT_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'MCP_CONNECT_TIMEOUT_MS',
+ raw: getEnv('MCP_CONNECT_TIMEOUT_MS'),
+ defaultValue: 30 * 1000,
+ }),
+ MCP_HTTP_HEADERS_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'MCP_HTTP_HEADERS_TIMEOUT_MS',
+ raw: getEnv('MCP_HTTP_HEADERS_TIMEOUT_MS'),
+ defaultValue: mcpRequestTimeoutMs,
+ }),
+ MCP_HTTP_CONNECT_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'MCP_HTTP_CONNECT_TIMEOUT_MS',
+ raw: getEnv('MCP_HTTP_CONNECT_TIMEOUT_MS'),
+ defaultValue: 5_000,
+ }),
+ MCP_HTTP_BODY_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'MCP_HTTP_BODY_TIMEOUT_MS',
+ raw: getEnv('MCP_HTTP_BODY_TIMEOUT_MS'),
+ defaultValue: 1_800_000,
+ }),
+ MCP_HTTP_MAX_RETRIES: parseNonNegativeInt({
+ envKey: 'MCP_HTTP_MAX_RETRIES',
+ raw: getEnv('MCP_HTTP_MAX_RETRIES'),
+ defaultValue: 2,
+ }),
SENTRY_ENABLED: parseBoolean({
envKey: 'SENTRY_ENABLED',
raw: getEnv('SENTRY_ENABLED'),
diff --git a/packages/trueforge/src/main.ts b/packages/trueforge/src/main.ts
index 3ac3f06d5..0a0f0fe19 100644
--- a/packages/trueforge/src/main.ts
+++ b/packages/trueforge/src/main.ts
@@ -9,7 +9,11 @@
* (migrate, Redis, listen) are caught below and exit non-zero. SQLite vs
* Postgres store modules stay dynamic so only the active engine is loaded.
*/
-import { configureOutboundUrlGuard, extractErrorLogFields } from '@truefoundry/trueforge-core/core';
+import {
+ configureOutboundFetches,
+ configureOutboundUrlGuard,
+ extractErrorLogFields,
+} from '@truefoundry/trueforge-core/core';
import type { Context } from 'hono';
import { mkdir } from 'node:fs/promises';
import path from 'node:path';
@@ -41,6 +45,29 @@ try {
allowedHosts: configuration.OUTBOUND_URL_ALLOWED_HOSTS,
blockedHosts: configuration.OUTBOUND_URL_BLOCKED_HOSTS,
});
+ configureOutboundFetches({
+ outbound: {
+ connectTimeoutMs: configuration.OUTBOUND_HTTP_CONNECT_TIMEOUT_MS,
+ headersTimeoutMs: configuration.OUTBOUND_HTTP_HEADERS_TIMEOUT_MS,
+ bodyTimeoutMs: configuration.OUTBOUND_HTTP_BODY_TIMEOUT_MS,
+ maxRetries: configuration.OUTBOUND_HTTP_MAX_RETRIES,
+ idempotent: true,
+ },
+ model: {
+ connectTimeoutMs: configuration.MODEL_HTTP_CONNECT_TIMEOUT_MS,
+ headersTimeoutMs: configuration.MODEL_HTTP_HEADERS_TIMEOUT_MS,
+ bodyTimeoutMs: configuration.MODEL_HTTP_BODY_TIMEOUT_MS,
+ maxRetries: configuration.MODEL_HTTP_MAX_RETRIES,
+ idempotent: true,
+ },
+ mcp: {
+ connectTimeoutMs: configuration.MCP_HTTP_CONNECT_TIMEOUT_MS,
+ headersTimeoutMs: configuration.MCP_HTTP_HEADERS_TIMEOUT_MS,
+ bodyTimeoutMs: configuration.MCP_HTTP_BODY_TIMEOUT_MS,
+ maxRetries: configuration.MCP_HTTP_MAX_RETRIES,
+ idempotent: false,
+ },
+ });
} catch (error) {
console.error(
'Failed to start server: Failed to load configuration:',