Skip to content

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

fix: raise remote MCP undici bodyTimeout to 30m (#756)

Bhavesh Patel2cb51a5876af
diff --git a/.changeset/mcp-idle-body-timeout.md b/.changeset/mcp-idle-body-timeout.md
new file mode 100644
index 000000000..d0575a83e
--- /dev/null
+++ b/.changeset/mcp-idle-body-timeout.md
@@ -0,0 +1,5 @@
+---
+"@truefoundry/trueforge-core": patch
+---
+
+Raise remote MCP undici bodyTimeout to 30m so idle SSE streams are not killed at 5m; warn on Body Timeout and stringify other transport errors.
diff --git a/packages/trueforge-core/package.json b/packages/trueforge-core/package.json
index d3218950e..60e4d0559 100644
--- a/packages/trueforge-core/package.json
+++ b/packages/trueforge-core/package.json
@@ -117,6 +117,7 @@
"dedent": "^1.7.2",
"openai": "^7.5.0",
"ulid": "^3.0.2",
+ "undici": "7.29.0",
"winston": "^3.19.0",
"ws": "^8.21.3",
"zod": "^4.4.3"
diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts
index 080b060a2..ef4a7c312 100644
--- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts
+++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts
@@ -5,6 +5,7 @@ import type { MCPServerInitInfo } from '../events/schema';
import type { InternalToolCallInfo } from '../llm/LLMTypes';
import type { AgentTracing } from '../tracing/AgentTracing';
import { NOOP_AGENT_TRACING } from '../tracing/NoopAgentTracing';
+import { extractErrorLogFields } from '../util/errorLogFields';
import {
type AgentToolSchema,
type AuthRequiredResponse,
@@ -256,7 +257,13 @@ export class RemoteMCP implements ToolSource {
this.isConnected = false;
},
onError: error => {
- this.logger.error(`Error on remote MCP transport ${this.name}`, { error });
+ const fields = extractErrorLogFields(error);
+ const msg = `Error on remote MCP transport ${this.name}`;
+ if (fields.error.includes('Body Timeout')) {
+ this.logger.warn(msg, fields);
+ } else {
+ this.logger.error(msg, fields);
+ }
},
});
span.setOutput(JSON.stringify({ transport: conn.transportType, stateful: conn.sessionId !== null }));
diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts
index b76f75ac6..019ef9bce 100644
--- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts
+++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts
@@ -3,8 +3,10 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
+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 { Agent, fetch as undiciFetch } from 'undici';
import { McpConnectionError } from '../errors';
import { withTimeout } from '../util/promiseUtils';
import type { ToolSchema } from './IMCPServer';
@@ -29,6 +31,15 @@ type McpTransport = StreamableHTTPClientTransport | SSEClientTransport;
const CLIENT_INFO = { name: 'tfy-agent-mcp-client', version: '1.0.0' } as const;
const TRANSPORT_PROBE_ORDER: RemoteMcpTransportType[] = ['streamable-http', 'sse'];
+// MCP SSE/streamable-HTTP keeps a long-lived response open that is often idle between tool calls.
+// Node fetch (undici) defaults bodyTimeout to 300s of silence, then kills the stream with
+// `Body Timeout Error` — we reconnect and the ~5m cycle repeats in logs. 30m matches the
+// Gateway idle-body window; MCP request deadlines still come from requestTimeoutMs.
+const MCP_BODY_TIMEOUT_MS = 30 * 60 * 1000;
+const mcpHttpAgent = new Agent({ bodyTimeout: MCP_BODY_TIMEOUT_MS });
+const mcpFetch: FetchLike = (url, init) =>
+ undiciFetch(typeof url === 'string' ? url : url.href, { ...(init as object), dispatcher: mcpHttpAgent });
+
class McpClientWithTimeout extends Client {
constructor(private readonly requestTimeoutMs: number) {
super(CLIENT_INFO, { capabilities: {} });
@@ -50,10 +61,14 @@ function createTransport(
): McpTransport {
const requestInit = { headers };
if (type === 'streamable-http') {
- return new StreamableHTTPClientTransport(url, { requestInit, ...(sessionId !== undefined ? { sessionId } : {}) });
+ return new StreamableHTTPClientTransport(url, {
+ requestInit,
+ fetch: mcpFetch,
+ ...(sessionId !== undefined ? { sessionId } : {}),
+ });
}
// eslint-disable-next-line @typescript-eslint/no-deprecated -- dual-transport probe; see TRANSPORT_PROBE_ORDER
- return new SSEClientTransport(url, { requestInit });
+ return new SSEClientTransport(url, { requestInit, fetch: mcpFetch });
}
export function isSessionExpiredError(error: unknown): boolean {
diff --git a/packages/trueforge/tests/unit/truefoundry/listAgentSkillsPaging.test.ts b/packages/trueforge/tests/unit/truefoundry/listAgentSkillsPaging.test.ts
index f48394138..3831f980c 100644
--- a/packages/trueforge/tests/unit/truefoundry/listAgentSkillsPaging.test.ts
+++ b/packages/trueforge/tests/unit/truefoundry/listAgentSkillsPaging.test.ts
@@ -4,6 +4,7 @@ import { TrueFoundryServiceFoundryServerClient } from '../../../src/truefoundry/
const fetchMock = jest.fn();
jest.mock('undici', () => ({
+ Agent: class Agent {},
fetch: (...args: unknown[]) => fetchMock(...args),
}));
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 85bbacd41..4c1f7623e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -283,6 +283,9 @@ importers:
ulid:
specifier: ^3.0.2
version: 3.0.2
+ undici:
+ specifier: 7.29.0
+ version: 7.29.0
winston:
specifier: ^3.19.0
version: 3.19.0