From 6d8539386f04bf564e6071079daff9a83b791832 Mon Sep 17 00:00:00 2001 From: Rutvij Menavlikar <174306281+rutvij-menavlikar@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:48:07 +0530 Subject: [PATCH 01/20] feat: x-tfg-mcp-headers to pass MCP credentials and skip consent, with 401 fallback (AGE-2723) Co-authored-by: Cursor --- .changeset/x-tfg-mcp-headers-auth.md | 6 ++ .../trueforge-core/src/core/mcp/RemoteMCP.ts | 60 +++++++++++- .../src/core/mcp/remoteMcpClient.ts | 6 +- .../tests/core/mcp/remoteMcpServer.test.ts | 56 +++++++++++ packages/trueforge/src/apis/turns.ts | 2 +- .../src/db/McpServerWithAuthStore.ts | 50 +++++++++- packages/trueforge/src/main.ts | 10 +- .../perServerMcpHeaders.ts | 24 +++++ packages/trueforge/src/resolvers.ts | 17 ++-- .../truefoundry/TrueFoundryMcpServerStore.ts | 34 ++++--- .../perServerMcpHeaders.test.ts | 92 ++++++++++++++++--- 11 files changed, 317 insertions(+), 40 deletions(-) create mode 100644 .changeset/x-tfg-mcp-headers-auth.md rename packages/trueforge/src/{truefoundry => mcp}/perServerMcpHeaders.ts (57%) rename packages/trueforge/tests/unit/{truefoundry => mcp}/perServerMcpHeaders.test.ts (58%) diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md new file mode 100644 index 0000000000..2dfd7327cb --- /dev/null +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -0,0 +1,6 @@ +--- +'@truefoundry/trueforge-core': patch +'@truefoundry/trueforge': patch +--- + +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow: forwarded directly in open-source mode (along with other per-server headers), sent as `x-tfy-mcp-headers` in TrueFoundry mode. If the server rejects that credential with a 401, TrueForge falls back to the regular authorization flow via a new `RemoteMCP` `onUnauthorized` header-resolver hook. diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index f015f1e5cd..1aa2bc99dd 100644 --- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts +++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts @@ -24,6 +24,7 @@ import { paginateWithCursorGuard } from './pagination'; import { connectRemoteMcp, DEFAULT_MAX_MCP_RESPONSE_BYTES, + isAuthError, isSessionExpiredError, type RemoteMcpConnection, type RemoteMcpTransportType, @@ -39,8 +40,13 @@ function redactUrlForTrace(url: string): string { } } -/** What a headers resolver returns: the headers to send, or a signal that auth is required. */ -export type ResolveHeadersResult = { headers: Record } | { authRequired: MCPAuthRequired }; +/** + * What a headers resolver returns: the headers to send, or a signal that auth is required. + * `onUnauthorized` is consulted once if the upstream rejects `headers` with a 401. + */ +export type ResolveHeadersResult = + | { headers: Record; onUnauthorized?: () => Promise } + | { authRequired: MCPAuthRequired }; /** Static headers, or a resolver (invoked at connect) returning headers or signalling auth-required. */ export type RemoteMcpHeaders = Record | (() => Promise); @@ -88,6 +94,8 @@ export class RemoteMCP implements ToolSource { private cachedTools?: AgentToolSchema[] | undefined; private inflight = 0; private pendingClose: RemoteMcpConnection | undefined; + // Connection opened with onUnauthorized fallback headers; its auth is re-checked before reuse. + private fallbackConnection: RemoteMcpConnection | undefined; constructor(params: { name: string; @@ -187,7 +195,53 @@ export class RemoteMCP implements ToolSource { if ('authRequired' in headersResult) { return { authRequired: headersResult.authRequired }; } - return this.connectAndRun(headersResult.headers, operation, true); + const { onUnauthorized } = headersResult; + if (onUnauthorized === undefined) { + return this.connectAndRun(headersResult.headers, operation, true); + } + if (this.isConnected && this._connection !== undefined && this._connection === this.fallbackConnection) { + // Re-check the fallback auth before reusing its connection so a disconnected account stops here. + return this.runWithFallback(onUnauthorized, operation); + } + let rejected: RemoteMcpConnection | undefined; + const tracked = (connection: RemoteMcpConnection): Promise => { + rejected = connection; + return operation(connection); + }; + try { + return await this.connectAndRun(headersResult.headers, tracked, true); + } catch (error) { + if (!isAuthError(error)) { + throw error; + } + this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`); + // Reset only the rejected connection so a sibling's newer one survives; a failed connect keeps its stale session and tools. + if (rejected !== undefined) { + await this.resetConnection(rejected); + } else if (!this.isConnected) { + this.sessionId = undefined; + this.cachedTools = undefined; + } + return this.runWithFallback(onUnauthorized, operation); + } + } + + private async runWithFallback( + onUnauthorized: () => Promise, + operation: (connection: RemoteMcpConnection) => Promise, + ): Promise> { + const fallback = await onUnauthorized(); + if ('authRequired' in fallback) { + if (this.fallbackConnection !== undefined) { + await this.resetConnection(this.fallbackConnection); + } + return { authRequired: fallback.authRequired }; + } + const tracked = (connection: RemoteMcpConnection): Promise => { + this.fallbackConnection = connection; + return operation(connection); + }; + return this.connectAndRun(fallback.headers, tracked, true); } private async connectAndRun( diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 7393602a3b..7cc2f05f60 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -123,10 +123,14 @@ function mcpConnectOutcome(error: unknown): McpConnectOutcome { return 'other'; } -function isAuthError(error: unknown): boolean { +/** Upstream rejected the credentials, whether at connect or on a later request. */ +export function isAuthError(error: unknown): boolean { if (!(error instanceof Error)) { return false; } + if (error instanceof McpConnectionError) { + return error.statusCode === 401; + } if ('code' in error && error.code === 401) { return true; } diff --git a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts index 267f85bd69..559ab495ed 100644 --- a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts +++ b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts @@ -430,6 +430,62 @@ describe('RemoteMCP + ToolSet', () => { expect(state.callToolCalls).toBe(0); }); + it('falls back to onUnauthorized when supplied credentials are rejected at connect', async () => { + installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); + const authRequired: MCPAuthRequired = { + servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example' }], + }; + const onUnauthorized = jest.fn(() => Promise.resolve({ authRequired })); + const server = makeServer({ + headers: () => Promise.resolve({ headers: { Authorization: 'Bearer caller' }, onUnauthorized }), + }); + + await expect(server.listTools()).resolves.toEqual({ authRequired }); + expect(onUnauthorized).toHaveBeenCalledTimes(1); + }); + + it('reconnects with fallback headers when a call is rejected mid-session', async () => { + const state = installFakeConnection({ sessionId: 'sess-1' }); + const server = makeServer({ + headers: () => + Promise.resolve({ + headers: { Authorization: 'Bearer caller' }, + onUnauthorized: () => Promise.resolve({ headers: { Authorization: 'Bearer fallback' } }), + }), + }); + + await server.listTools(); + state.queueCallToolError(Object.assign(new Error('HTTP 401'), { code: 401 })); + const res = await server.callTool({ name: 'read_thing', arguments: {} }); + + expect(res).toMatchObject({ result: { content: [{ type: 'text', text: 'called read_thing' }] } }); + expect(state.connectCalls).toBe(2); + expect(mockConnect.mock.lastCall?.[0].headers).toEqual({ Authorization: 'Bearer fallback' }); + }); + + it('re-checks fallback auth before reusing a fallback connection', async () => { + const state = installFakeConnection({ sessionId: 'sess-1' }); + const authRequired: MCPAuthRequired = { + servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example' }], + }; + const onUnauthorized = jest + .fn } | { authRequired: MCPAuthRequired }>, []>() + .mockResolvedValueOnce({ headers: { Authorization: 'Bearer fallback' } }) + .mockResolvedValue({ authRequired }); + const server = makeServer({ + headers: () => Promise.resolve({ headers: { Authorization: 'Bearer caller' }, onUnauthorized }), + }); + + await server.listTools(); + state.queueCallToolError(Object.assign(new Error('HTTP 401'), { code: 401 })); + await server.callTool({ name: 'read_thing', arguments: {} }); + const callsBefore = state.callToolCalls; + + // The fallback account was disconnected: the reused connection must not run the call. + await expect(server.callTool({ name: 'read_thing', arguments: {} })).resolves.toEqual({ authRequired }); + expect(state.callToolCalls).toBe(callsBefore); + }); + it('emits init for a stateless (SSE) server on connect (matches gateway strict-compare behavior)', async () => { // Stateless connection reports a null session id. installFakeConnection({ sessionId: null }); diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts index bc0bed7dee..f4c1543d6d 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -74,6 +74,7 @@ import type { ISkillStore } from '../db/skillStore'; import type { TurnMetadata } from '../db/turnMetadata'; import type { IWebSearchProviderStore } from '../db/webSearchProviderStore'; import { currentLogContext, runWithLogContext, updateLogContext } from '../logger'; +import { X_TFG_MCP_HEADERS } from '../mcp/perServerMcpHeaders'; import type { TenantMemoryDeps } from '../memory/providers'; import { resolveSessionMemory } from '../memory/sessionMemory'; import { @@ -107,7 +108,6 @@ import { restoreTurnEventLog } from '../runtime/turnEventLogRestore'; import { MAX_SESSION_TITLE_LENGTH } from '../schemas/session'; import { assertGatewayMetadataRequestHeaders, X_TFY_METADATA } from '../truefoundry/gatewayMetadata'; import { X_TFG_MCP, X_TFG_MODELS, X_TFG_SKILLS, X_TFG_TOOLS } from '../truefoundry/inlineResources'; -import { X_TFG_MCP_HEADERS } from '../truefoundry/perServerMcpHeaders'; import { newId } from '../utils/id'; import { resolveWebSearchProvider } from '../websearch/providers'; import { canReadAgentBoundResource, canReadSession } from './agentAccess'; diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 792fc9e2f9..cb93b5b5ae 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -1,6 +1,12 @@ -import type { RemoteMcpHeaders } from '@truefoundry/trueforge-core/core'; +import type { RemoteMcpHeaders, ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; import { isMcpAuthRequired, resolveMcpAuth } from '../mcp/auth/mcpDcr'; import type { IOAuthTokenStore, OAuthClientRecord } from '../mcp/auth/types'; +import { + hasAuthorizationHeader, + overrideHeaders, + withoutAuthorization, + type PerServerMcpHeaders, +} from '../mcp/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { McpServerNotFoundError, @@ -18,19 +24,33 @@ import { } from './mcpServerStore'; import type { TurnMetadata } from './turnMetadata'; +async function resolveWithOverride(input: { + headers: RemoteMcpHeaders; + override: Record; +}): Promise { + const result = typeof input.headers === 'function' ? await input.headers() : { headers: input.headers }; + if ('authRequired' in result) { + return result; + } + return { headers: overrideHeaders({ base: result.headers, override: input.override }) }; +} + export class McpServerWithAuthStore implements IMcpServerWithAuthStore { readonly #store: IMcpServerStore; readonly #tokenStore: IOAuthTokenStore; readonly #clientName: string; + readonly #perServerHeaders: PerServerMcpHeaders; constructor(input: { store: IMcpServerStore; tokenStore: IOAuthTokenStore; clientName: string; + perServerHeaders?: PerServerMcpHeaders; }) { this.#store = input.store; this.#tokenStore = input.tokenStore; this.#clientName = input.clientName; + this.#perServerHeaders = input.perServerHeaders ?? {}; } listServers(input: ListMcpServersInput, transaction?: TTransaction): Promise { @@ -57,11 +77,39 @@ export class McpServerWithAuthStore implements IMcpServerW return this.#store.deleteServer(input, transaction); } + /** Caller headers for the server go straight to it; one with Authorization skips its consent flow. */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { + const registered = this.#resolveRegisteredHeaders(input); + const callerHeaders = this.#perServerHeaders[input.record.name]; + if (callerHeaders === undefined) { + return registered; + } + if (hasAuthorizationHeader(callerHeaders)) { + const headers = overrideHeaders({ + base: resolveConfiguredMcpRequestHeaders(input.record.manifest), + override: callerHeaders, + }); + const override = withoutAuthorization(callerHeaders); + return () => + Promise.resolve({ headers, onUnauthorized: () => resolveWithOverride({ headers: registered, override }) }); + } + if (typeof registered !== 'function') { + return overrideHeaders({ base: registered, override: callerHeaders }); + } + return async () => { + const result = await registered(); + if ('authRequired' in result) { + return result; + } + return { headers: overrideHeaders({ base: result.headers, override: callerHeaders }) }; + }; + } + + #resolveRegisteredHeaders(input: { record: McpServerRecord; userRef: string }): RemoteMcpHeaders { const { record, userRef } = input; if (record.manifest.auth?.type === 'dcr') { return async () => { diff --git a/packages/trueforge/src/main.ts b/packages/trueforge/src/main.ts index ea91204ce4..781474266c 100644 --- a/packages/trueforge/src/main.ts +++ b/packages/trueforge/src/main.ts @@ -319,8 +319,9 @@ async function createStandalonePersistence(options: { const tokenStore = new SqliteOAuthTokenStore(db); const agentStore = new SqliteAgentStore(db); const modelProviderStore = new SqliteModelProviderStore(db); + const mcpPersistenceStore = new SqliteMcpServerStore(db); const mcpServerStore = new McpServerWithAuthStore({ - store: new SqliteMcpServerStore(db), + store: mcpPersistenceStore, tokenStore, clientName: configuration.MCP_DCR_OAUTH_CLIENT_NAME, }); @@ -345,7 +346,12 @@ async function createStandalonePersistence(options: { mcpOAuthStore: mcpServerStore, redis: undefined, resolveAgentStore: () => agentStore, - resolveMcpServerStore: () => mcpServerStore, + resolveMcpServerStore: buildResolveMcpServerStore({ + persistenceStore: mcpPersistenceStore, + tokenStore, + client: undefined, + logger, + }), resolveModelProviderStore: () => modelProviderStore, resolveSkillStore: () => skillStore, resolveTurnSkillsStore: buildResolveTurnSkillsStore({ diff --git a/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts similarity index 57% rename from packages/trueforge/src/truefoundry/perServerMcpHeaders.ts rename to packages/trueforge/src/mcp/perServerMcpHeaders.ts index 549fd75a73..57b504c9cd 100644 --- a/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts +++ b/packages/trueforge/src/mcp/perServerMcpHeaders.ts @@ -7,6 +7,7 @@ import { z } from 'zod'; * TrueForge authenticates the whole turn as one caller, so every MCP connection would otherwise * present that same identity. This carries the identity each individual server should see — * typically the end user behind the caller, for a server that resolves its own permissions. + * An entry carrying Authorization is used instead of that server's consent flow unless rejected. */ export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; @@ -35,3 +36,26 @@ export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders { } return parsed.data; } + +export function hasAuthorizationHeader(headers: Record | undefined): headers is Record { + return headers !== undefined && Object.keys(headers).some(name => name.toLowerCase() === 'authorization'); +} + +export function withoutAuthorization(headers: Record | undefined): Record { + if (headers === undefined) { + return {}; + } + return Object.fromEntries(Object.entries(headers).filter(([name]) => name.toLowerCase() !== 'authorization')); +} + +/** Header names are case-insensitive, so an override must replace a base entry under any casing. */ +export function overrideHeaders(input: { + base: Record; + override: Record; +}): Record { + const overridden = new Set(Object.keys(input.override).map(name => name.toLowerCase())); + return { + ...Object.fromEntries(Object.entries(input.base).filter(([name]) => !overridden.has(name.toLowerCase()))), + ...input.override, + }; +} diff --git a/packages/trueforge/src/resolvers.ts b/packages/trueforge/src/resolvers.ts index 6bfdbfa8f1..27a2174f9c 100644 --- a/packages/trueforge/src/resolvers.ts +++ b/packages/trueforge/src/resolvers.ts @@ -22,6 +22,7 @@ import type { ISandboxProviderStore } from './db/sandboxProviderStore'; import type { ISkillStore } from './db/skillStore'; import type { IWebSearchProviderStore } from './db/webSearchProviderStore'; import type { IOAuthTokenStore } from './mcp/auth/types'; +import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './mcp/perServerMcpHeaders'; import type { MemoryProviderCache, TenantMemoryDeps } from './memory/providers'; import { headerValue } from './truefoundry/gatewayMetadata'; import { InlineMcpServerStore } from './truefoundry/InlineMcpServerStore'; @@ -42,11 +43,6 @@ import { } from './truefoundry/inlineResources'; import { InlineSkillStore } from './truefoundry/InlineSkillStore'; import { InlineCapabilitiesStore } from './truefoundry/inlineTools'; -import { - parsePerServerMcpHeaders, - X_TFG_MCP_HEADERS, - type PerServerMcpHeaders, -} from './truefoundry/perServerMcpHeaders'; import { TrueFoundryAgentStore } from './truefoundry/TrueFoundryAgentStore'; import { TrueFoundryMcpServerStore } from './truefoundry/TrueFoundryMcpServerStore'; import { TrueFoundryModelProviderStore } from './truefoundry/TrueFoundryModelProviderStore'; @@ -110,7 +106,8 @@ export function buildResolveModelProviderStore(options: { } /** - * Per-request MCP store over ServiceFoundry. `runAsAgent` selects a saved agent's token for turns. + * Per-request MCP store over ServiceFoundry, else the DB store with any `x-tfg-mcp-headers`. + * `runAsAgent` selects a saved agent's token for turns. */ export function buildResolveMcpServerStore(options: { persistenceStore: IMcpServerStore; @@ -138,6 +135,14 @@ export function buildResolveMcpServerStore(options: { } return store; } + if (input.perServerHeaders !== undefined) { + return new McpServerWithAuthStore({ + store: options.persistenceStore, + tokenStore: options.tokenStore, + clientName: configuration.MCP_DCR_OAUTH_CLIENT_NAME, + perServerHeaders: input.perServerHeaders, + }); + } return withAuthMcpPersistenceStore; }; } diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 671384a9fe..448b1a54ed 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -1,4 +1,4 @@ -import { McpConnectionError, type RemoteMcpHeaders } from '@truefoundry/trueforge-core/core'; +import { McpConnectionError, type RemoteMcpHeaders, type ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; import { HTTPException } from 'hono/http-exception'; import type { Logger } from 'winston'; import type { RequestContext, RequestSubject } from '../auth/identity'; @@ -20,6 +20,7 @@ import { } from '../db/mcpServerStore'; import type { TurnMetadata } from '../db/turnMetadata'; import type { OAuthClientRecord } from '../mcp/auth/types'; +import { hasAuthorizationHeader, withoutAuthorization, type PerServerMcpHeaders } from '../mcp/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, @@ -37,7 +38,6 @@ import { toTrueFoundryMcpManifest, type SfyMcpServerSummary, } from './mapSfyMcpServers'; -import type { PerServerMcpHeaders } from './perServerMcpHeaders'; import type { TrueFoundryServiceFoundryServerClient } from './TrueFoundryServiceFoundryServerClient'; export type TrueFoundryMcpApiClient = Pick< @@ -52,12 +52,8 @@ export type TrueFoundryMcpApiClient = Pick< | 'getTenantControlPlaneUrl' >; -function withoutAuthorization(headers: Record | undefined): Record { - if (headers === undefined) { - return {}; - } - return Object.fromEntries(Object.entries(headers).filter(([name]) => name.toLowerCase() !== 'authorization')); -} +/** Per-server upstream headers the TrueFoundry gateway applies after its own caller auth. */ +const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; /** * Absolute FE landing for the upstream authorize `redirectURL`. @@ -109,14 +105,18 @@ export class TrueFoundryMcpServerStore implements IMcpServ this.#perServerHeaders = input.perServerHeaders ?? {}; } - /** Gateway Bearer (+ optional per-server overrides); SFY authorize first, else authRequired. */ + /** + * Gateway Bearer (+ optional per-server overrides); SFY authorize first, else authRequired. + * A caller entry with Authorization is sent as `x-tfy-mcp-headers`; authorize runs only on a 401. + */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { const { record, userRef, turnMetadata } = input; - return async () => { + const callerHeaders = this.#perServerHeaders[record.name]; + const viaAuthorize = async (): Promise => { const status = await this.authorize({ tenant_id: record.tenant_id, name: record.name, @@ -137,12 +137,24 @@ export class TrueFoundryMcpServerStore implements IMcpServ } const authorization = await this.#resolveGatewayAuthorization(); const invokeHeaders = { - ...withoutAuthorization(this.#perServerHeaders[record.name]), + ...withoutAuthorization(callerHeaders), ...gatewayHeaders(authorization), ...gatewayMetadataHeadersForTurn(turnMetadata), }; return { headers: invokeHeaders }; }; + if (!hasAuthorizationHeader(callerHeaders)) { + return viaAuthorize; + } + // Each connection targets one server's gateway proxy, which takes a flat header map for it. + return async () => ({ + headers: { + ...gatewayHeaders(await this.#resolveGatewayAuthorization()), + ...gatewayMetadataHeadersForTurn(turnMetadata), + [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders), + }, + onUnauthorized: viaAuthorize, + }); } async listServers(input: ListMcpServersInput, transaction?: TTransaction): Promise { diff --git a/packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts similarity index 58% rename from packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts rename to packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts index 1114ba0b4e..eee33e842a 100644 --- a/packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -1,12 +1,16 @@ import { HTTPException } from 'hono/http-exception'; import { createLogger } from 'winston'; +import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore'; import type { McpServerRecord } from '../../../src/db/mcpServerStore'; -import { createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; -import { parsePerServerMcpHeaders } from '../../../src/truefoundry/perServerMcpHeaders'; +import { createSqliteDb } from '../../../src/db/sqlite/client'; +import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; +import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; +import { parsePerServerMcpHeaders } from '../../../src/mcp/perServerMcpHeaders'; import { TrueFoundryMcpServerStore, type TrueFoundryMcpApiClient, } from '../../../src/truefoundry/TrueFoundryMcpServerStore'; +import { createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; function unusedClient(): TrueFoundryMcpApiClient { const unused = (): Promise => Promise.reject(new Error('unused')); @@ -127,28 +131,86 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); }); - it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( - 'drops an override under %s, which object keys would otherwise keep beside the Bearer', + it.each(['Authorization', 'authorization', 'AUTHORIZATION'])( + 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', async name => { const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { [name]: 'Bearer smuggled' }, - }), + storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), 'tfy-platform-mcp', ); - expect(Object.values(headers)).toEqual(['Bearer caller-token']); + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), + }); }, ); - it('keeps the rest of an override that also carried an authorization key', async () => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { authorization: 'Bearer smuggled', 'x-tfy-mcp-headers': '{"a":"b"}' }, + it('skips authorize for a caller Authorization, and runs it only when the upstream rejects it', async () => { + const getMcpAuthorize = jest.fn(async () => ({ + status: 'auth_required' as const, + authorization_url: 'https://auth.example/consent', + })); + const store = new TrueFoundryMcpServerStore({ + client: { ...unusedClient(), getMcpAuthorize }, + requestContext: createTrueFoundryRequestContext({ + tenant_id: 'default', + subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, + roles: [], + user_credential: 'caller-token', }), - 'tfy-platform-mcp', - ); + agent: undefined, + logger: createLogger({ silent: true }), + perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user' } }, + }); + const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + + const resolved = await resolve(); + expect(getMcpAuthorize).not.toHaveBeenCalled(); + if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { + throw new Error('expected a fallback to the consent flow'); + } + await expect(resolved.onUnauthorized()).resolves.toEqual({ + authRequired: { + servers: [{ id: 'tfy-platform-mcp', name: 'tfy-platform-mcp', auth_url: 'https://auth.example/consent' }], + }, + }); + }); +}); + +describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { + const ossRecord: McpServerRecord = { + id: 'oauth-mcp', + tenant_id: 'default', + name: 'oauth-mcp', + manifest: { + type: 'remote', + name: 'oauth-mcp', + url: 'https://mcp.example/mcp', + description: '', + auth: { type: 'dcr' }, + }, + created_at: '2026-01-15T12:00:00.000Z', + updated_at: '2026-01-16T12:00:00.000Z', + }; + + it('forwards a caller Authorization directly instead of starting the DCR consent flow', async () => { + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, + }); - expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': '{"a":"b"}' }); + const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); + await db.destroy(); }); }); From b6007af779cc08fef2379fcfa59c9506d9ed7ded Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 14:16:57 +0000 Subject: [PATCH 02/20] feat: use gateway 401 authorization_urls for MCP auth_required; drop authorize on TrueFoundry turns (AGE-2723) --- .changeset/mcp-401-auth-urls.md | 6 ++ packages/trueforge-core/src/core/errors.ts | 12 ++++ .../trueforge-core/src/core/mcp/RemoteMCP.ts | 36 ++++++++++- .../src/core/mcp/remoteMcpClient.ts | 34 ++++++++++- .../tests/core/mcp/connectRemoteMcp.test.ts | 24 ++++++++ .../tests/core/mcp/remoteMcpServer.test.ts | 21 +++++++ .../truefoundry/TrueFoundryMcpServerStore.ts | 51 ++++------------ .../unit/mcp/perServerMcpHeaders.test.ts | 17 ++---- .../TrueFoundryMcpServerStore.test.ts | 61 +++---------------- 9 files changed, 157 insertions(+), 105 deletions(-) create mode 100644 .changeset/mcp-401-auth-urls.md diff --git a/.changeset/mcp-401-auth-urls.md b/.changeset/mcp-401-auth-urls.md new file mode 100644 index 0000000000..53a578410f --- /dev/null +++ b/.changeset/mcp-401-auth-urls.md @@ -0,0 +1,6 @@ +--- +'@truefoundry/trueforge-core': patch +'@truefoundry/trueforge': patch +--- + +TrueFoundry mode no longer calls the MCP authorize endpoint during turns. A 401 from an MCP server whose body includes `authorization_urls` now pauses the turn with `mcp.auth_required` and that consent URL; a bare 401 still fails as an MCP error. diff --git a/packages/trueforge-core/src/core/errors.ts b/packages/trueforge-core/src/core/errors.ts index 73f7a18ff1..f759724efa 100644 --- a/packages/trueforge-core/src/core/errors.ts +++ b/packages/trueforge-core/src/core/errors.ts @@ -106,6 +106,18 @@ export class McpConnectionError extends AgentHarnessError { } } +/** A 401 whose body carries consent URLs (`authorization_urls`, keyed by server name), so the caller can authorize. */ +export class McpAuthorizationRequiredError extends McpConnectionError { + constructor( + message: string, + readonly authorizationUrls: Readonly>, + options?: ErrorOptions, + ) { + super(message, 401, options); + this.name = 'McpAuthorizationRequiredError'; + } +} + /** * An MCP server's `auth.type: dcr` is unsatisfiable by configuration, not by a transient fault */ diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index f84ea346c3..b9c9159912 100644 --- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts +++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts @@ -1,6 +1,12 @@ import type { CallToolRequest } from '@modelcontextprotocol/sdk/types.js'; import type { Logger } from 'winston'; -import { applyErrorPrefix, MCP_ERROR_PREFIX, McpConnectionError, stripErrorPrefix } from '../errors'; +import { + applyErrorPrefix, + MCP_ERROR_PREFIX, + McpAuthorizationRequiredError, + McpConnectionError, + stripErrorPrefix, +} from '../errors'; import type { MCPServerInitInfo } from '../events/schema'; import type { InternalToolCallInfo } from '../llm/LLMTypes'; import { @@ -180,6 +186,32 @@ export class RemoteMCP implements ToolSource { private async executeWithSessionRetry( operation: (connection: RemoteMcpConnection) => Promise, + ): Promise> { + try { + return await this.executeWithAuthFallback(operation); + } catch (error) { + const authRequired = this.authRequiredFrom(error); + if (authRequired === undefined) { + throw error; + } + return { authRequired }; + } + } + + /** A 401 that carried consent URLs becomes authRequired; a bare 401 stays an error. */ + private authRequiredFrom(error: unknown): MCPAuthRequired | undefined { + for (let current: unknown = error; current instanceof Error; current = current.cause) { + if (current instanceof McpAuthorizationRequiredError) { + const urls = current.authorizationUrls; + const authUrl = urls[this.name] ?? Object.values(urls)[0]; + return authUrl === undefined ? undefined : { servers: [{ id: this.id, name: this.name, auth_url: authUrl }] }; + } + } + return undefined; + } + + private async executeWithAuthFallback( + operation: (connection: RemoteMcpConnection) => Promise, ): Promise> { // Auth is re-checked on every operation, not only on the first connect: a registered server's OAuth // can be revoked or expire mid-request, and callers must get authRequired rather than a generic @@ -204,7 +236,7 @@ export class RemoteMCP implements ToolSource { try { return await this.connectAndRun(headersResult.headers, tracked, true); } catch (error) { - if (!isAuthError(error)) { + if (!isAuthError(error) || this.authRequiredFrom(error) !== undefined) { throw error; } this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`); diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 514cf1dfd4..87a45485ac 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -6,7 +6,8 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/ 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 { z } from 'zod'; +import { McpAuthorizationRequiredError, McpConnectionError } from '../errors'; import { elapsedSeconds, getOperationMetrics, type McpConnectOutcome } from '../observability/operationMetrics'; import { mcpSsrfFetch } from '../util/outboundFetch'; import { PromiseTimeoutError, withTimeout } from '../util/promiseUtils'; @@ -69,6 +70,33 @@ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): Fetc }; } +const AuthorizationRequiredBodySchema = z.object({ + message: z.string().optional(), + authorization_urls: z.record(z.string(), z.string().min(1)), +}); + +/** A 401 whose body lists `authorization_urls` throws `McpAuthorizationRequiredError`; other responses pass through. */ +export function withAuthorizationRequired(fetchFn: FetchLike): FetchLike { + return async (url, init) => { + const response = await fetchFn(url, init); + if (response.status !== 401) { + return response; + } + const body: unknown = await response + .clone() + .json() + .catch(() => undefined); + const parsed = AuthorizationRequiredBodySchema.safeParse(body); + if (!parsed.success || Object.keys(parsed.data.authorization_urls).length === 0) { + return response; + } + throw new McpAuthorizationRequiredError( + parsed.data.message ?? 'upstream requires authorization', + parsed.data.authorization_urls, + ); + }; +} + class McpClientWithTimeout extends Client { constructor(private readonly requestTimeoutMs: number) { super(CLIENT_INFO, { capabilities: {} }); @@ -216,7 +244,9 @@ export async function connectRemoteMcp(params: { const customFetch = params.fetch; const transportFetch: RemoteMcpFetch = customFetch === undefined ? mcpSsrfFetch : (input, init) => customFetch(input, { ...init, redirect: 'manual' }); - const fetchFn = withMaxResponseBytes(transportFetch, params.maxResponseBytes ?? DEFAULT_MAX_MCP_RESPONSE_BYTES); + const fetchFn = withAuthorizationRequired( + withMaxResponseBytes(transportFetch, params.maxResponseBytes ?? DEFAULT_MAX_MCP_RESPONSE_BYTES), + ); const candidates = params.knownTransportType ? [params.knownTransportType, ...TRANSPORT_PROBE_ORDER.filter(t => t !== params.knownTransportType)] : TRANSPORT_PROBE_ORDER; diff --git a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts index a78f18594d..f3e78526a2 100644 --- a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts +++ b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts @@ -115,6 +115,30 @@ describe('connectRemoteMcp custom fetch', () => { expect(fetch).toHaveBeenCalledWith(endpoint, expect.objectContaining({ redirect: 'manual' })); }); + + it('turns a 401 with authorization_urls into McpAuthorizationRequiredError and passes a bare 401 through', async () => { + const urls = { github: 'https://auth.example/consent' }; + const fetch = jest + .fn, Parameters>() + .mockResolvedValueOnce( + new Response(JSON.stringify({ error: { type: 'McpAuthRequiredError' }, authorization_urls: urls }), { + status: 401, + }), + ) + .mockResolvedValueOnce(new Response('unauthorized', { status: 401 })); + await connectRemoteMcp({ ...baseParams(), url: endpoint, knownTransportType: 'streamable-http', fetch }); + const transportFetch = mockTransportFetches[0]; + if (transportFetch === undefined) { + throw new Error('expected transport fetch'); + } + + await expect(transportFetch(endpoint, {})).rejects.toMatchObject({ + name: 'McpAuthorizationRequiredError', + statusCode: 401, + authorizationUrls: urls, + }); + await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); + }); }); describe('connectRemoteMcp transport selection', () => { diff --git a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts index ebcdfbcbd3..dd7b11653c 100644 --- a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts +++ b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts @@ -1,5 +1,6 @@ import type { Logger } from 'winston'; import { McpConnectionError, RemoteMCP, ToolSet } from '../../../src/core'; +import { McpAuthorizationRequiredError } from '../../../src/core/errors'; import { isApprovalRequiredResponse, isCallToolResponseResult, @@ -448,6 +449,26 @@ describe('RemoteMCP + ToolSet', () => { expect(state.callToolCalls).toBe(0); }); + it('returns authRequired from a 401 that carries authorization URLs, without falling back', async () => { + installFakeConnection({ + connectError: new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), + }); + const onUnauthorized = jest.fn(() => Promise.resolve({ headers: {} })); + const server = makeServer({ headers: () => Promise.resolve({ headers: {}, onUnauthorized }) }); + + await expect(server.listTools()).resolves.toEqual({ + authRequired: { servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }] }, + }); + expect(onUnauthorized).not.toHaveBeenCalled(); + }); + + it('throws on a bare 401 when there is no fallback', async () => { + installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); + const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); + + await expect(server.listTools()).rejects.toMatchObject({ statusCode: 401 }); + }); + it('falls back to onUnauthorized when supplied credentials are rejected at connect', async () => { installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); const authRequired: MCPAuthRequired = { diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 448b1a54ed..8d15d67b3d 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -1,5 +1,4 @@ import { McpConnectionError, type RemoteMcpHeaders, type ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; -import { HTTPException } from 'hono/http-exception'; import type { Logger } from 'winston'; import type { RequestContext, RequestSubject } from '../auth/identity'; import { safeReturnTo } from '../auth/safeReturnTo'; @@ -106,54 +105,30 @@ export class TrueFoundryMcpServerStore implements IMcpServ } /** - * Gateway Bearer (+ optional per-server overrides); SFY authorize first, else authRequired. - * A caller entry with Authorization is sent as `x-tfy-mcp-headers`; authorize runs only on a 401. + * Gateway Bearer (+ optional per-server overrides). No authorize call: the gateway's 401 carries the consent URL. + * A caller entry with Authorization is sent as `x-tfy-mcp-headers`; on a 401 it retries with the gateway's own auth. */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { - const { record, userRef, turnMetadata } = input; + const { record, turnMetadata } = input; const callerHeaders = this.#perServerHeaders[record.name]; - const viaAuthorize = async (): Promise => { - const status = await this.authorize({ - tenant_id: record.tenant_id, - name: record.name, - userRef, - }); - if (status.status === 'auth_required') { - const authUrl = status.authorization_url; - if (authUrl === undefined || authUrl.length === 0) { - throw new HTTPException(422, { - message: `MCP server "${record.name}" requires authentication but returned no authorization URL`, - }); - } - return { - authRequired: { - servers: [{ id: record.name, name: record.name, auth_url: authUrl }], - }, - }; - } - const authorization = await this.#resolveGatewayAuthorization(); - const invokeHeaders = { - ...withoutAuthorization(callerHeaders), - ...gatewayHeaders(authorization), - ...gatewayMetadataHeadersForTurn(turnMetadata), - }; - return { headers: invokeHeaders }; - }; + const gatewayBase = async (): Promise> => ({ + ...gatewayHeaders(await this.#resolveGatewayAuthorization()), + ...gatewayMetadataHeadersForTurn(turnMetadata), + }); + const viaGateway = async (): Promise => ({ + headers: { ...withoutAuthorization(callerHeaders), ...(await gatewayBase()) }, + }); if (!hasAuthorizationHeader(callerHeaders)) { - return viaAuthorize; + return viaGateway; } // Each connection targets one server's gateway proxy, which takes a flat header map for it. return async () => ({ - headers: { - ...gatewayHeaders(await this.#resolveGatewayAuthorization()), - ...gatewayMetadataHeadersForTurn(turnMetadata), - [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders), - }, - onUnauthorized: viaAuthorize, + headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, + onUnauthorized: viaGateway, }); } diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts index eee33e842a..152d096ab9 100644 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -146,11 +146,8 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { }, ); - it('skips authorize for a caller Authorization, and runs it only when the upstream rejects it', async () => { - const getMcpAuthorize = jest.fn(async () => ({ - status: 'auth_required' as const, - authorization_url: 'https://auth.example/consent', - })); + it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { + const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); const store = new TrueFoundryMcpServerStore({ client: { ...unusedClient(), getMcpAuthorize }, requestContext: createTrueFoundryRequestContext({ @@ -161,7 +158,7 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { }), agent: undefined, logger: createLogger({ silent: true }), - perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user' } }, + perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user', 'X-End-User': 'alice' } }, }); const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); if (typeof resolve !== 'function') { @@ -169,15 +166,13 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { } const resolved = await resolve(); - expect(getMcpAuthorize).not.toHaveBeenCalled(); if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { - throw new Error('expected a fallback to the consent flow'); + throw new Error('expected a fallback to the gateway auth'); } await expect(resolved.onUnauthorized()).resolves.toEqual({ - authRequired: { - servers: [{ id: 'tfy-platform-mcp', name: 'tfy-platform-mcp', auth_url: 'https://auth.example/consent' }], - }, + headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, }); + expect(getMcpAuthorize).not.toHaveBeenCalled(); }); }); diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index 9eafb93159..897798bd92 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -298,51 +298,27 @@ describe('TrueFoundryMcpServerStore', () => { return headers(); } - it('returns authRequired when authorize reports auth_required', async () => { + it('never calls authorize, even when the account is not connected', async () => { const { store, client } = createStore(); client.getMcpAuthorize.mockResolvedValue({ status: 'auth_required', authorization_url: 'https://consent.example/authorize', }); await expect(invoke(store)).resolves.toEqual({ - authRequired: { - servers: [{ id: 'github', name: 'github', auth_url: 'https://consent.example/authorize' }], - }, - }); - }); - - it('gates non-dcr truefoundry servers through authorize as well', async () => { - const { store, client } = createStore(); - const record = truefoundryRecordWithoutAuth(); - client.getMcpServerByName.mockResolvedValue({ - id: record.id, - name: record.name, - proxyUrl: `${MCP_PROXY_BASE_URL_TEMPLATE}/mcp-server/${record.name}`, - createdAt: record.created_at, - updatedAt: record.updated_at, - manifest: { description: record.manifest.description, auth_data: { type: 'header' } }, - }); - client.getMcpAuthorize.mockResolvedValue({ - status: 'auth_required', - authorization_url: 'https://consent.example/auth-override', - }); - await expect(invoke(store, record)).resolves.toEqual({ - authRequired: { - servers: [{ id: record.name, name: record.name, auth_url: 'https://consent.example/auth-override' }], - }, + headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, }); - expect(client.getMcpAuthorize).toHaveBeenCalled(); + expect(client.getMcpAuthorize).not.toHaveBeenCalled(); }); - it('returns gateway Bearer when authorize reports authenticated', async () => { + it('returns the gateway Bearer', async () => { const { store } = createStore(); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, }); }); - it('merges turn metadata after auth headers and leaves authRequired untouched', async () => { - const { store, client } = createStore(); + it('merges turn metadata after auth headers', async () => { + const { store } = createStore(); const headers = store.resolveInvokeHeaders({ record: dcrRecord(), userRef: 'user-1', @@ -368,19 +344,9 @@ describe('TrueFoundryMcpServerStore', () => { throw new Error('expected invoke headers'); } expect(JSON.parse(resolved.headers[X_TFY_METADATA] ?? '')).toMatchObject({ env: 'prod' }); - - client.getMcpAuthorize.mockResolvedValue({ - status: 'auth_required', - authorization_url: 'https://consent.example/authorize', - }); - await expect(headers()).resolves.toEqual({ - authRequired: { - servers: [{ id: 'github', name: 'github', auth_url: 'https://consent.example/authorize' }], - }, - }); }); - it('delegates invoke: caller bearer, actor header, actor token for SFY lookups', async () => { + it('delegates invoke: caller bearer, actor header, no SFY lookups', async () => { const { store, client } = createStore({ agent: AGENT }); await expect(invoke(store)).resolves.toEqual({ headers: { @@ -388,9 +354,7 @@ describe('TrueFoundryMcpServerStore', () => { [ACTOR_AUTHORIZATION_HEADER]: `Bearer ${ACTOR_TOKEN}`, }, }); - expect(client.getMcpAuthorize).toHaveBeenCalledWith(expect.objectContaining({ accessToken: ACCESS_TOKEN })); - expect(client.getMcpServerByName).toHaveBeenCalledWith(expect.objectContaining({ accessToken: ACTOR_TOKEN })); - expect(client.listGatewayInstallations).toHaveBeenCalledWith(ACTOR_TOKEN); + expect(client.getMcpServerByName).not.toHaveBeenCalled(); expect(client.vendToken).toHaveBeenCalledTimes(1); }); @@ -399,14 +363,7 @@ describe('TrueFoundryMcpServerStore', () => { await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${SUBJECT_TOKEN}` }, }); - expect(client.getMcpAuthorize).toHaveBeenCalledWith(expect.objectContaining({ accessToken: SUBJECT_TOKEN })); - expect(client.getMcpServerByName).toHaveBeenCalledWith(expect.objectContaining({ accessToken: ACTOR_TOKEN })); - }); - - it('throws 422 when auth_required lacks authorization_url', async () => { - const { store, client } = createStore(); - client.getMcpAuthorize.mockResolvedValue({ status: 'auth_required' }); - await expect(invoke(store)).rejects.toMatchObject({ status: 422 }); + expect(client.getMcpServerByName).not.toHaveBeenCalled(); }); }); }); From cf1d6c50d8f262e27fc14ab5d4b9b93e315f8252 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 14:19:56 +0000 Subject: [PATCH 03/20] refactor: trim MCP auth changes and shorten comments (AGE-2723) --- packages/trueforge-core/src/core/errors.ts | 2 +- packages/trueforge-core/src/core/mcp/RemoteMCP.ts | 2 +- .../trueforge-core/src/core/mcp/remoteMcpClient.ts | 6 +----- packages/trueforge/src/db/McpServerWithAuthStore.ts | 11 +---------- .../src/truefoundry/TrueFoundryMcpServerStore.ts | 4 ++-- .../tests/unit/mcp/perServerMcpHeaders.test.ts | 2 +- .../truefoundry/TrueFoundryMcpServerStore.test.ts | 13 +------------ 7 files changed, 8 insertions(+), 32 deletions(-) diff --git a/packages/trueforge-core/src/core/errors.ts b/packages/trueforge-core/src/core/errors.ts index f759724efa..4cb40fe242 100644 --- a/packages/trueforge-core/src/core/errors.ts +++ b/packages/trueforge-core/src/core/errors.ts @@ -106,7 +106,7 @@ export class McpConnectionError extends AgentHarnessError { } } -/** A 401 whose body carries consent URLs (`authorization_urls`, keyed by server name), so the caller can authorize. */ +/** A 401 whose body lists consent URLs (`authorization_urls`, keyed by server name). */ export class McpAuthorizationRequiredError extends McpConnectionError { constructor( message: string, diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index b9c9159912..4c11470a7e 100644 --- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts +++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts @@ -240,7 +240,7 @@ export class RemoteMCP implements ToolSource { throw error; } this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`); - // Reset only the rejected connection so a sibling's newer one survives; a failed connect keeps its stale session and tools. + // Reset only the rejected connection; a failed connect still leaves the old session and tools. if (rejected !== undefined) { await this.resetConnection(rejected); } else if (!this.isConnected) { diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 87a45485ac..d7db4add06 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -71,7 +71,6 @@ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): Fetc } const AuthorizationRequiredBodySchema = z.object({ - message: z.string().optional(), authorization_urls: z.record(z.string(), z.string().min(1)), }); @@ -90,10 +89,7 @@ export function withAuthorizationRequired(fetchFn: FetchLike): FetchLike { if (!parsed.success || Object.keys(parsed.data.authorization_urls).length === 0) { return response; } - throw new McpAuthorizationRequiredError( - parsed.data.message ?? 'upstream requires authorization', - parsed.data.authorization_urls, - ); + throw new McpAuthorizationRequiredError('upstream requires authorization', parsed.data.authorization_urls); }; } diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index cb93b5b5ae..5677196fef 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -97,16 +97,7 @@ export class McpServerWithAuthStore implements IMcpServerW return () => Promise.resolve({ headers, onUnauthorized: () => resolveWithOverride({ headers: registered, override }) }); } - if (typeof registered !== 'function') { - return overrideHeaders({ base: registered, override: callerHeaders }); - } - return async () => { - const result = await registered(); - if ('authRequired' in result) { - return result; - } - return { headers: overrideHeaders({ base: result.headers, override: callerHeaders }) }; - }; + return () => resolveWithOverride({ headers: registered, override: callerHeaders }); } #resolveRegisteredHeaders(input: { record: McpServerRecord; userRef: string }): RemoteMcpHeaders { diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 8d15d67b3d..876e754e73 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -105,8 +105,8 @@ export class TrueFoundryMcpServerStore implements IMcpServ } /** - * Gateway Bearer (+ optional per-server overrides). No authorize call: the gateway's 401 carries the consent URL. - * A caller entry with Authorization is sent as `x-tfy-mcp-headers`; on a 401 it retries with the gateway's own auth. + * Gateway Bearer (+ per-server overrides); the gateway's 401 carries any consent URL. + * A caller entry with Authorization goes in `x-tfy-mcp-headers`, falling back to the gateway's auth on a 401. */ resolveInvokeHeaders(input: { record: McpServerRecord; diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts index 152d096ab9..663dfe61ba 100644 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -131,7 +131,7 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); }); - it.each(['Authorization', 'authorization', 'AUTHORIZATION'])( + it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', async name => { const headers = await invokeHeaders( diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index 897798bd92..7101c06e3f 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -298,25 +298,14 @@ describe('TrueFoundryMcpServerStore', () => { return headers(); } - it('never calls authorize, even when the account is not connected', async () => { + it('returns the gateway Bearer without calling authorize', async () => { const { store, client } = createStore(); - client.getMcpAuthorize.mockResolvedValue({ - status: 'auth_required', - authorization_url: 'https://consent.example/authorize', - }); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, }); expect(client.getMcpAuthorize).not.toHaveBeenCalled(); }); - it('returns the gateway Bearer', async () => { - const { store } = createStore(); - await expect(invoke(store)).resolves.toEqual({ - headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, - }); - }); - it('merges turn metadata after auth headers', async () => { const { store } = createStore(); const headers = store.resolveInvokeHeaders({ From a15ecff68f81e9b07a571d2e52663fff86b9f1ae Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 14:29:05 +0000 Subject: [PATCH 04/20] chore: merge changesets into one (AGE-2723) --- .changeset/mcp-401-auth-urls.md | 6 ------ .changeset/x-tfg-mcp-headers-auth.md | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 .changeset/mcp-401-auth-urls.md diff --git a/.changeset/mcp-401-auth-urls.md b/.changeset/mcp-401-auth-urls.md deleted file mode 100644 index 53a578410f..0000000000 --- a/.changeset/mcp-401-auth-urls.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@truefoundry/trueforge-core': patch -'@truefoundry/trueforge': patch ---- - -TrueFoundry mode no longer calls the MCP authorize endpoint during turns. A 401 from an MCP server whose body includes `authorization_urls` now pauses the turn with `mcp.auth_required` and that consent URL; a bare 401 still fails as an MCP error. diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md index 2dfd7327cb..82cac8184f 100644 --- a/.changeset/x-tfg-mcp-headers-auth.md +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow: forwarded directly in open-source mode (along with other per-server headers), sent as `x-tfy-mcp-headers` in TrueFoundry mode. If the server rejects that credential with a 401, TrueForge falls back to the regular authorization flow via a new `RemoteMCP` `onUnauthorized` header-resolver hook. +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow: forwarded directly in open-source mode (along with other per-server headers), sent as `x-tfy-mcp-headers` in TrueFoundry mode. A rejected credential falls back to the server's regular auth via a new `RemoteMCP` `onUnauthorized` hook. TrueFoundry mode no longer calls the MCP authorize endpoint during turns: a 401 whose body includes `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 still fails as an MCP error. From e57439b7d6f12218be0030aaa1d13981af0543db Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 14:29:42 +0000 Subject: [PATCH 05/20] fix: accept only http(s) consent URLs from MCP 401 bodies (AGE-2723) --- packages/trueforge-core/src/core/mcp/remoteMcpClient.ts | 2 +- .../trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index d7db4add06..3bd2d80cc2 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -71,7 +71,7 @@ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): Fetc } const AuthorizationRequiredBodySchema = z.object({ - authorization_urls: z.record(z.string(), z.string().min(1)), + authorization_urls: z.record(z.string(), z.url({ protocol: /^https?$/ })), }); /** A 401 whose body lists `authorization_urls` throws `McpAuthorizationRequiredError`; other responses pass through. */ diff --git a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts index f3e78526a2..e70b68cc4a 100644 --- a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts +++ b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts @@ -125,7 +125,10 @@ describe('connectRemoteMcp custom fetch', () => { status: 401, }), ) - .mockResolvedValueOnce(new Response('unauthorized', { status: 401 })); + .mockResolvedValueOnce(new Response('unauthorized', { status: 401 })) + .mockResolvedValueOnce( + new Response(JSON.stringify({ authorization_urls: { github: 'javascript:alert(1)' } }), { status: 401 }), + ); await connectRemoteMcp({ ...baseParams(), url: endpoint, knownTransportType: 'streamable-http', fetch }); const transportFetch = mockTransportFetches[0]; if (transportFetch === undefined) { @@ -138,6 +141,7 @@ describe('connectRemoteMcp custom fetch', () => { authorizationUrls: urls, }); await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); + await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); }); }); From 014c4bcb599244487f61c857b11d41601399fcb0 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 15:31:43 +0000 Subject: [PATCH 06/20] feat: accept nested x-tfg-mcp-headers entries for virtual MCPs (AGE-2723) --- .changeset/x-tfg-mcp-headers-auth.md | 2 +- .../trueforge/src/db/McpServerWithAuthStore.ts | 4 +++- packages/trueforge/src/mcp/perServerMcpHeaders.ts | 14 ++++++++++++-- .../src/truefoundry/TrueFoundryMcpServerStore.ts | 13 ++++++++++++- .../tests/unit/mcp/perServerMcpHeaders.test.ts | 14 ++++++++++++-- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md index 82cac8184f..54dd3c999b 100644 --- a/.changeset/x-tfg-mcp-headers-auth.md +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow: forwarded directly in open-source mode (along with other per-server headers), sent as `x-tfy-mcp-headers` in TrueFoundry mode. A rejected credential falls back to the server's regular auth via a new `RemoteMCP` `onUnauthorized` hook. TrueFoundry mode no longer calls the MCP authorize endpoint during turns: a 401 whose body includes `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 still fails as an MCP error. +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow: forwarded directly in open-source mode (along with other per-server headers), sent as `x-tfy-mcp-headers` in TrueFoundry mode. A rejected credential falls back to the server's regular auth via a new `RemoteMCP` `onUnauthorized` hook. TrueFoundry mode no longer calls the MCP authorize endpoint during turns: a 401 whose body includes `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 still fails as an MCP error. A virtual MCP's entry may nest one header map per underlying server; in TrueFoundry mode it is passed through as `x-tfy-mcp-headers`. diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 5677196fef..0886cf88cc 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -3,6 +3,7 @@ import { isMcpAuthRequired, resolveMcpAuth } from '../mcp/auth/mcpDcr'; import type { IOAuthTokenStore, OAuthClientRecord } from '../mcp/auth/types'; import { hasAuthorizationHeader, + isVirtualMcpHeaders, overrideHeaders, withoutAuthorization, type PerServerMcpHeaders, @@ -85,7 +86,8 @@ export class McpServerWithAuthStore implements IMcpServerW }): RemoteMcpHeaders { const registered = this.#resolveRegisteredHeaders(input); const callerHeaders = this.#perServerHeaders[input.record.name]; - if (callerHeaders === undefined) { + // Nested (virtual MCP) entries only apply to the TrueFoundry gateway. + if (callerHeaders === undefined || isVirtualMcpHeaders(callerHeaders)) { return registered; } if (hasAuthorizationHeader(callerHeaders)) { diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts index 57b504c9cd..3a462a2cad 100644 --- a/packages/trueforge/src/mcp/perServerMcpHeaders.ts +++ b/packages/trueforge/src/mcp/perServerMcpHeaders.ts @@ -8,12 +8,22 @@ import { z } from 'zod'; * present that same identity. This carries the identity each individual server should see — * typically the end user behind the caller, for a server that resolves its own permissions. * An entry carrying Authorization is used instead of that server's consent flow unless rejected. + * A virtual MCP's entry nests one header map per underlying server. */ export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; -const PerServerMcpHeadersSchema = z.record(z.string().min(1), z.record(z.string().min(1), z.string())); +const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); + +const VirtualMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); + +const PerServerMcpHeadersSchema = z.record(z.string().min(1), z.union([McpHeaderMapSchema, VirtualMcpHeadersSchema])); export type PerServerMcpHeaders = z.infer; +type VirtualMcpHeaders = z.infer; + +export function isVirtualMcpHeaders(entry: PerServerMcpHeaders[string]): entry is VirtualMcpHeaders { + return Object.values(entry).some(value => typeof value === 'object'); +} /** * Rejects a malformed value rather than dropping it: the alternative to failing is running a user's @@ -31,7 +41,7 @@ export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders { const parsed = PerServerMcpHeadersSchema.safeParse(decoded); if (!parsed.success) { throw new HTTPException(400, { - message: `${X_TFG_MCP_HEADERS} must map each MCP server name to a header map`, + message: `${X_TFG_MCP_HEADERS} must map each MCP server name to a header map, or to per-server header maps`, }); } return parsed.data; diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 876e754e73..57048ca88e 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -19,7 +19,12 @@ import { } from '../db/mcpServerStore'; import type { TurnMetadata } from '../db/turnMetadata'; import type { OAuthClientRecord } from '../mcp/auth/types'; -import { hasAuthorizationHeader, withoutAuthorization, type PerServerMcpHeaders } from '../mcp/perServerMcpHeaders'; +import { + hasAuthorizationHeader, + isVirtualMcpHeaders, + withoutAuthorization, + type PerServerMcpHeaders, +} from '../mcp/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, @@ -119,6 +124,12 @@ export class TrueFoundryMcpServerStore implements IMcpServ ...gatewayHeaders(await this.#resolveGatewayAuthorization()), ...gatewayMetadataHeadersForTurn(turnMetadata), }); + if (callerHeaders !== undefined && isVirtualMcpHeaders(callerHeaders)) { + // The gateway fans a virtual MCP's nested map out to its servers itself. + return async () => ({ + headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, + }); + } const viaGateway = async (): Promise => ({ headers: { ...withoutAuthorization(callerHeaders), ...(await gatewayBase()) }, }); diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts index 663dfe61ba..27ed452bd2 100644 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -5,7 +5,7 @@ import type { McpServerRecord } from '../../../src/db/mcpServerStore'; import { createSqliteDb } from '../../../src/db/sqlite/client'; import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; -import { parsePerServerMcpHeaders } from '../../../src/mcp/perServerMcpHeaders'; +import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/mcp/perServerMcpHeaders'; import { TrueFoundryMcpServerStore, type TrueFoundryMcpApiClient, @@ -47,7 +47,7 @@ const record = (name: string): McpServerRecord => ({ updated_at: '2026-01-16T12:00:00.000Z', }); -const storeWith = (perServerHeaders: Record>): TrueFoundryMcpServerStore => +const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => new TrueFoundryMcpServerStore({ client: unusedClient(), requestContext: createTrueFoundryRequestContext({ @@ -88,6 +88,7 @@ describe('parsePerServerMcpHeaders', () => { ['a scalar', '"nope"'], ['a server mapped to a string', JSON.stringify({ 'tfy-platform-mcp': 'Bearer user' })], ['a header mapped to a number', JSON.stringify({ 'tfy-platform-mcp': { 'x-h': 1 } })], + ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); }); @@ -100,6 +101,15 @@ describe('parsePerServerMcpHeaders', () => { }); describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { + it('passes a virtual MCP entry through as x-tfy-mcp-headers', async () => { + const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; + const raw = JSON.stringify({ 'virtual-mcp': entry }); + + const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); + }); + it('sends only the gateway Bearer when a server has no override', async () => { await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ Authorization: 'Bearer caller-token', From 0550d0edeb6725844b7f747f71518acdfd1dbb30 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 15:33:06 +0000 Subject: [PATCH 07/20] chore: keep x-tfg-mcp-headers error message and shorten changeset (AGE-2723) --- .changeset/x-tfg-mcp-headers-auth.md | 2 +- packages/trueforge/src/mcp/perServerMcpHeaders.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md index 54dd3c999b..db3ae410b2 100644 --- a/.changeset/x-tfg-mcp-headers-auth.md +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow: forwarded directly in open-source mode (along with other per-server headers), sent as `x-tfy-mcp-headers` in TrueFoundry mode. A rejected credential falls back to the server's regular auth via a new `RemoteMCP` `onUnauthorized` hook. TrueFoundry mode no longer calls the MCP authorize endpoint during turns: a 401 whose body includes `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 still fails as an MCP error. A virtual MCP's entry may nest one header map per underlying server; in TrueFoundry mode it is passed through as `x-tfy-mcp-headers`. +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode; nested virtual MCP entries pass through as is), falling back to the server's regular auth on a 401. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts index 3a462a2cad..1217d7bdd2 100644 --- a/packages/trueforge/src/mcp/perServerMcpHeaders.ts +++ b/packages/trueforge/src/mcp/perServerMcpHeaders.ts @@ -41,7 +41,7 @@ export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders { const parsed = PerServerMcpHeadersSchema.safeParse(decoded); if (!parsed.success) { throw new HTTPException(400, { - message: `${X_TFG_MCP_HEADERS} must map each MCP server name to a header map, or to per-server header maps`, + message: `${X_TFG_MCP_HEADERS} must map each MCP server name to a header map`, }); } return parsed.data; From 76eb522f2fc8350d32014807a9a288f4efc9675f Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 16:03:25 +0000 Subject: [PATCH 08/20] feat: forward nested x-tfg-mcp-headers entries in open-source mode too (AGE-2723) --- .changeset/x-tfg-mcp-headers-auth.md | 2 +- .../src/db/McpServerWithAuthStore.ts | 10 +++++-- .../trueforge/src/mcp/perServerMcpHeaders.ts | 3 ++ .../truefoundry/TrueFoundryMcpServerStore.ts | 4 +-- .../unit/mcp/perServerMcpHeaders.test.ts | 29 +++++++++++++++++++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md index db3ae410b2..5f3a9db488 100644 --- a/.changeset/x-tfg-mcp-headers-auth.md +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode; nested virtual MCP entries pass through as is), falling back to the server's regular auth on a 401. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode; nested virtual MCP entries are sent as `x-tfy-mcp-headers` in both), falling back to the server's regular auth on a 401. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 0886cf88cc..3bafd6634b 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -6,6 +6,7 @@ import { isVirtualMcpHeaders, overrideHeaders, withoutAuthorization, + X_TFY_MCP_HEADERS, type PerServerMcpHeaders, } from '../mcp/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; @@ -78,7 +79,7 @@ export class McpServerWithAuthStore implements IMcpServerW return this.#store.deleteServer(input, transaction); } - /** Caller headers for the server go straight to it; one with Authorization skips its consent flow. */ + /** Caller headers go straight to the server (a nested entry as `x-tfy-mcp-headers`); Authorization skips consent. */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; @@ -86,10 +87,13 @@ export class McpServerWithAuthStore implements IMcpServerW }): RemoteMcpHeaders { const registered = this.#resolveRegisteredHeaders(input); const callerHeaders = this.#perServerHeaders[input.record.name]; - // Nested (virtual MCP) entries only apply to the TrueFoundry gateway. - if (callerHeaders === undefined || isVirtualMcpHeaders(callerHeaders)) { + if (callerHeaders === undefined) { return registered; } + if (isVirtualMcpHeaders(callerHeaders)) { + const override = { [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }; + return () => resolveWithOverride({ headers: registered, override }); + } if (hasAuthorizationHeader(callerHeaders)) { const headers = overrideHeaders({ base: resolveConfiguredMcpRequestHeaders(input.record.manifest), diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts index 1217d7bdd2..679ef89f49 100644 --- a/packages/trueforge/src/mcp/perServerMcpHeaders.ts +++ b/packages/trueforge/src/mcp/perServerMcpHeaders.ts @@ -12,6 +12,9 @@ import { z } from 'zod'; */ export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; +/** Per-server upstream headers a TrueFoundry gateway (or virtual MCP) applies after its own caller auth. */ +export const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; + const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); const VirtualMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 57048ca88e..36bfc7ade2 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -23,6 +23,7 @@ import { hasAuthorizationHeader, isVirtualMcpHeaders, withoutAuthorization, + X_TFY_MCP_HEADERS, type PerServerMcpHeaders, } from '../mcp/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; @@ -56,9 +57,6 @@ export type TrueFoundryMcpApiClient = Pick< | 'getTenantControlPlaneUrl' >; -/** Per-server upstream headers the TrueFoundry gateway applies after its own caller auth. */ -const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; - /** * Absolute FE landing for the upstream authorize `redirectURL`. * Origin from the tenant control-plane URL; UI path prefix from process diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts index 27ed452bd2..7caaff7b75 100644 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -218,4 +218,33 @@ describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); await db.destroy(); }); + + it('sends a virtual MCP entry as x-tfy-mcp-headers on top of the registered auth', async () => { + const entry = { serv1: { Authorization: 'Bearer a' } }; + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'virtual-mcp': entry }, + }); + const record: McpServerRecord = { + ...ossRecord, + name: 'virtual-mcp', + manifest: { + ...ossRecord.manifest, + name: 'virtual-mcp', + auth: { type: 'header', headers: { Authorization: 'Bearer gateway' } }, + }, + }; + + const resolve = store.resolveInvokeHeaders({ record, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toEqual({ + headers: { Authorization: 'Bearer gateway', 'x-tfy-mcp-headers': JSON.stringify(entry) }, + }); + await db.destroy(); + }); }); From d5efb54f112c4ab439cac020141be5284550a873 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 16:05:21 +0000 Subject: [PATCH 09/20] chore: keep original doc wording for moved MCP header helpers (AGE-2723) --- packages/trueforge/src/db/McpServerWithAuthStore.ts | 2 +- packages/trueforge/src/mcp/perServerMcpHeaders.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 3bafd6634b..4a0382812b 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -79,7 +79,7 @@ export class McpServerWithAuthStore implements IMcpServerW return this.#store.deleteServer(input, transaction); } - /** Caller headers go straight to the server (a nested entry as `x-tfy-mcp-headers`); Authorization skips consent. */ + /** Caller headers for the server go straight to it; one with Authorization skips its consent flow. */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts index 679ef89f49..37c54000dd 100644 --- a/packages/trueforge/src/mcp/perServerMcpHeaders.ts +++ b/packages/trueforge/src/mcp/perServerMcpHeaders.ts @@ -12,7 +12,7 @@ import { z } from 'zod'; */ export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; -/** Per-server upstream headers a TrueFoundry gateway (or virtual MCP) applies after its own caller auth. */ +/** Per-server upstream headers the TrueFoundry gateway applies after its own caller auth. */ export const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); From 0027e082eb945aaebd3d2d4dcf0760977fdafdeb Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 16:27:55 +0000 Subject: [PATCH 10/20] feat: send nested x-tfg-mcp-headers entries as plain headers in open-source mode (AGE-2723) --- .changeset/x-tfg-mcp-headers-auth.md | 2 +- packages/trueforge/src/db/McpServerWithAuthStore.ts | 5 +++-- packages/trueforge/src/mcp/perServerMcpHeaders.ts | 4 ---- .../trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts | 4 +++- .../trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts | 4 ++-- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md index 5f3a9db488..0a5be3cc43 100644 --- a/.changeset/x-tfg-mcp-headers-auth.md +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode; nested virtual MCP entries are sent as `x-tfy-mcp-headers` in both), falling back to the server's regular auth on a 401. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode; nested virtual MCP entries are sent as JSON-valued headers in open-source mode and as `x-tfy-mcp-headers` in TrueFoundry mode), falling back to the server's regular auth on a 401. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 4a0382812b..c76f519c16 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -6,7 +6,6 @@ import { isVirtualMcpHeaders, overrideHeaders, withoutAuthorization, - X_TFY_MCP_HEADERS, type PerServerMcpHeaders, } from '../mcp/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; @@ -91,7 +90,9 @@ export class McpServerWithAuthStore implements IMcpServerW return registered; } if (isVirtualMcpHeaders(callerHeaders)) { - const override = { [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }; + const override = Object.fromEntries( + Object.entries(callerHeaders).map(([name, value]) => [name, JSON.stringify(value)]), + ); return () => resolveWithOverride({ headers: registered, override }); } if (hasAuthorizationHeader(callerHeaders)) { diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts index 37c54000dd..9fe50f436e 100644 --- a/packages/trueforge/src/mcp/perServerMcpHeaders.ts +++ b/packages/trueforge/src/mcp/perServerMcpHeaders.ts @@ -11,10 +11,6 @@ import { z } from 'zod'; * A virtual MCP's entry nests one header map per underlying server. */ export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; - -/** Per-server upstream headers the TrueFoundry gateway applies after its own caller auth. */ -export const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; - const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); const VirtualMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 36bfc7ade2..57048ca88e 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -23,7 +23,6 @@ import { hasAuthorizationHeader, isVirtualMcpHeaders, withoutAuthorization, - X_TFY_MCP_HEADERS, type PerServerMcpHeaders, } from '../mcp/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; @@ -57,6 +56,9 @@ export type TrueFoundryMcpApiClient = Pick< | 'getTenantControlPlaneUrl' >; +/** Per-server upstream headers the TrueFoundry gateway applies after its own caller auth. */ +const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; + /** * Absolute FE landing for the upstream authorize `redirectURL`. * Origin from the tenant control-plane URL; UI path prefix from process diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts index 7caaff7b75..2a87f176a3 100644 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -219,7 +219,7 @@ describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { await db.destroy(); }); - it('sends a virtual MCP entry as x-tfy-mcp-headers on top of the registered auth', async () => { + it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { const entry = { serv1: { Authorization: 'Bearer a' } }; const db = createSqliteDb(':memory:'); const store = new McpServerWithAuthStore({ @@ -243,7 +243,7 @@ describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { throw new Error('expected a headers resolver'); } await expect(resolve()).resolves.toEqual({ - headers: { Authorization: 'Bearer gateway', 'x-tfy-mcp-headers': JSON.stringify(entry) }, + headers: { Authorization: 'Bearer gateway', serv1: JSON.stringify(entry.serv1) }, }); await db.destroy(); }); From 11b104682b1e11255726a23d56ce6aa6d21cb258 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 17:06:14 +0000 Subject: [PATCH 11/20] fix: reject managed MCP transport headers in x-tfg-mcp-headers (AGE-2723) --- packages/trueforge/src/mcp/perServerMcpHeaders.ts | 9 +++++++++ .../trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts | 2 ++ 2 files changed, 11 insertions(+) diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts index 9fe50f436e..cfca059fa8 100644 --- a/packages/trueforge/src/mcp/perServerMcpHeaders.ts +++ b/packages/trueforge/src/mcp/perServerMcpHeaders.ts @@ -18,6 +18,9 @@ const VirtualMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); const PerServerMcpHeadersSchema = z.record(z.string().min(1), z.union([McpHeaderMapSchema, VirtualMcpHeadersSchema])); export type PerServerMcpHeaders = z.infer; + +// Transport headers TrueForge manages; a caller value would override them in the MCP client. +const MANAGED_MCP_HEADERS = new Set(['mcp-session-id', 'mcp-protocol-version']); type VirtualMcpHeaders = z.infer; export function isVirtualMcpHeaders(entry: PerServerMcpHeaders[string]): entry is VirtualMcpHeaders { @@ -43,6 +46,12 @@ export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders { message: `${X_TFG_MCP_HEADERS} must map each MCP server name to a header map`, }); } + const managed = Object.values(parsed.data) + .flatMap(entry => Object.keys(entry)) + .find(name => MANAGED_MCP_HEADERS.has(name.toLowerCase())); + if (managed !== undefined) { + throw new HTTPException(400, { message: `${X_TFG_MCP_HEADERS} must not set ${managed}` }); + } return parsed.data; } diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts index 2a87f176a3..807c3e45ad 100644 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -89,6 +89,8 @@ describe('parsePerServerMcpHeaders', () => { ['a server mapped to a string', JSON.stringify({ 'tfy-platform-mcp': 'Bearer user' })], ['a header mapped to a number', JSON.stringify({ 'tfy-platform-mcp': { 'x-h': 1 } })], ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], + ['a managed session header', JSON.stringify({ 'tfy-platform-mcp': { 'Mcp-Session-Id': 'other' } })], + ['a managed protocol header', JSON.stringify({ 'tfy-platform-mcp': { 'mcp-protocol-version': '2025-06-18' } })], ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); }); From 59979c3b0e537df3f2c7f2f61e6dfeb4765ebc99 Mon Sep 17 00:00:00 2001 From: Rutvij Menavlikar <174306281+rutvij-menavlikar@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:12:47 +0530 Subject: [PATCH 12/20] fix: keep onUnauthorized fallback when stamping session header (AGE-2723) Co-authored-by: Cursor --- packages/trueforge/src/apis/turns.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts index e19333424c..c12aec4426 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -826,17 +826,31 @@ function withTurnSessionHeader(connection: McpConnection, sessionId: string): Mc const sessionHeader = { [TRUEFORGE_SESSION_HEADER]: sessionId }; const headers = connection.headers; if (typeof headers === 'function') { - return { - ...connection, - headers: async (): Promise => { - const resolved = await headers(); - return 'authRequired' in resolved ? resolved : { headers: { ...resolved.headers, ...sessionHeader } }; - }, - }; + return { ...connection, headers: withSessionHeaderResolver({ resolve: headers, sessionHeader }) }; } return { ...connection, headers: { ...(headers ?? {}), ...sessionHeader } }; } +function withSessionHeaderResolver(input: { + resolve: () => Promise; + sessionHeader: Record; +}): () => Promise { + const { resolve, sessionHeader } = input; + return async () => { + const resolved = await resolve(); + if ('authRequired' in resolved) { + return resolved; + } + const { onUnauthorized } = resolved; + return { + headers: { ...resolved.headers, ...sessionHeader }, + ...(onUnauthorized === undefined + ? {} + : { onUnauthorized: withSessionHeaderResolver({ resolve: onUnauthorized, sessionHeader }) }), + }; + }; +} + /** * Shared create-turn engine: persist the turn, start execution, and return the * drain inputs. Does not wait for events and does not write HTTP/SSE. From 64fd20f392aa7e055d961527737283b121e86814 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 08:05:34 +0000 Subject: [PATCH 13/20] fix: cap 401 SSE bodies and drop the rejected MCP session on consent 401 (AGE-2723) --- .../trueforge-core/src/core/mcp/RemoteMCP.ts | 26 +++++++++++++------ .../src/core/mcp/remoteMcpClient.ts | 3 ++- .../core/mcp/mcpResponseSizeLimit.test.ts | 11 ++++++++ .../tests/core/mcp/remoteMcpServer.test.ts | 14 ++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index b438ed64d4..589dee0b6f 100644 --- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts +++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts @@ -191,17 +191,33 @@ export class RemoteMCP implements ToolSource { private async executeWithSessionRetry( operation: (connection: RemoteMcpConnection) => Promise, ): Promise> { + let used: RemoteMcpConnection | undefined; + const tracked = (connection: RemoteMcpConnection): Promise => { + used = connection; + return operation(connection); + }; try { - return await this.executeWithAuthFallback(operation); + return await this.executeWithAuthFallback(tracked); } catch (error) { const authRequired = this.authRequiredFrom(error); if (authRequired === undefined) { throw error; } + await this.discardRejected(used); return { authRequired }; } } + // Reset only the rejected connection; a failed connect still leaves the old session and tools. + private async discardRejected(rejected: RemoteMcpConnection | undefined): Promise { + if (rejected !== undefined) { + await this.resetConnection(rejected); + } else if (!this.isConnected) { + this.sessionId = undefined; + this.cachedTools = undefined; + } + } + /** A 401 that carried consent URLs becomes authRequired; a bare 401 stays an error. */ private authRequiredFrom(error: unknown): MCPAuthRequired | undefined { for (let current: unknown = error; current instanceof Error; current = current.cause) { @@ -244,13 +260,7 @@ export class RemoteMCP implements ToolSource { throw error; } this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`); - // Reset only the rejected connection; a failed connect still leaves the old session and tools. - if (rejected !== undefined) { - await this.resetConnection(rejected); - } else if (!this.isConnected) { - this.sessionId = undefined; - this.cachedTools = undefined; - } + await this.discardRejected(rejected); return this.runWithFallback(onUnauthorized, operation); } } diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 3bd2d80cc2..68b84c0d5c 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -36,11 +36,12 @@ const TRANSPORT_PROBE_ORDER: RemoteMcpTransportType[] = ['streamable-http', 'sse export const DEFAULT_MAX_MCP_RESPONSE_BYTES = 50 * 1024 * 1024; -/** GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ +/** A successful GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): FetchLike { return async (url, init) => { const response = await fetchFn(url, init); const isGetSse = + response.ok && (init?.method ?? 'GET').toUpperCase() === 'GET' && (response.headers.get('content-type') ?? '').toLowerCase().includes('text/event-stream'); if (isGetSse || !response.body) { diff --git a/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts b/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts index 77b328aa48..c67f045ae6 100644 --- a/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts +++ b/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts @@ -33,4 +33,15 @@ describe('withMaxResponseBytes', () => { expect((await response.arrayBuffer()).byteLength).toBe(80); }); + + it('caps a non-OK GET text/event-stream body', async () => { + const fetchFn = withMaxResponseBytes( + async () => + new Response(streamOf(new Uint8Array(80)), { status: 401, headers: { 'content-type': 'text/event-stream' } }), + 50, + ); + const response = await fetchFn('https://mcp.example.com', { method: 'GET' }); + + await expect(response.arrayBuffer()).rejects.toThrow(/exceeded max 50 bytes/); + }); }); diff --git a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts index a4c125a536..837162dd48 100644 --- a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts +++ b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts @@ -487,6 +487,20 @@ describe('RemoteMCP + ToolSet', () => { expect(onUnauthorized).not.toHaveBeenCalled(); }); + it('drops the rejected connection when a call returns consent URLs, so the next call reconnects', async () => { + const state = installFakeConnection({ sessionId: 'sess-1' }); + const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); + + await server.listTools(); + state.queueCallToolError( + new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), + ); + await expect(server.callTool({ name: 'read_thing', arguments: {} })).resolves.toHaveProperty('authRequired'); + await server.callTool({ name: 'read_thing', arguments: {} }); + + expect(state.connectCalls).toBe(2); + }); + it('throws on a bare 401 when there is no fallback', async () => { installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); From 5f8755e8b2bbd29cebaf13ee0ce38af6fe70e6c5 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 05:36:14 +0000 Subject: [PATCH 14/20] refactor: move per-server MCP headers out of src/mcp and keep TrueFoundry details out of general tests (AGE-2723) Co-authored-by: Rutvij Menavlikar --- .../tests/core/mcp/connectRemoteMcp.test.ts | 2 +- packages/trueforge/src/apis/turns.ts | 2 +- .../src/db/McpServerWithAuthStore.ts | 2 +- packages/trueforge/src/resolvers.ts | 2 +- .../{mcp => runtime}/perServerMcpHeaders.ts | 0 .../truefoundry/TrueFoundryMcpServerStore.ts | 2 +- .../unit/mcp/perServerMcpHeaders.test.ts | 252 ------------------ .../unit/runtime/perServerMcpHeaders.test.ts | 99 +++++++ .../TrueFoundryMcpServerStore.test.ts | 148 ++++++++++ 9 files changed, 252 insertions(+), 257 deletions(-) rename packages/trueforge/src/{mcp => runtime}/perServerMcpHeaders.ts (100%) delete mode 100644 packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts create mode 100644 packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts diff --git a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts index e70b68cc4a..20a021d821 100644 --- a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts +++ b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts @@ -121,7 +121,7 @@ describe('connectRemoteMcp custom fetch', () => { const fetch = jest .fn, Parameters>() .mockResolvedValueOnce( - new Response(JSON.stringify({ error: { type: 'McpAuthRequiredError' }, authorization_urls: urls }), { + new Response(JSON.stringify({ authorization_urls: urls }), { status: 401, }), ) diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts index c12aec4426..880d9332ad 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -87,7 +87,6 @@ import type { TurnMetadata } from '../db/turnMetadata'; import type { IWebSearchProviderStore } from '../db/webSearchProviderStore'; import { createTlsFetch, normalizeTlsUrl } from '../http/tls'; import { currentLogContext, runWithLogContext, updateLogContext } from '../logger'; -import { X_TFG_MCP_HEADERS } from '../mcp/perServerMcpHeaders'; import { TRUEFORGE_SESSION_HEADER, X_TFG_TRUEFORGE_MCP } from '../mcp/server/trueforge/headers'; import type { TenantMemoryDeps } from '../memory/providers'; import { resolveSessionMemory } from '../memory/sessionMemory'; @@ -111,6 +110,7 @@ import { type EventSubscriptionPollOptions, type EventSubscriptionRegistry, } from '../runtime/event-subscription'; +import { X_TFG_MCP_HEADERS } from '../runtime/perServerMcpHeaders'; import { validateSandboxFilePath } from '../runtime/sandboxFilePath'; import { buildTurnSandbox, diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index c76f519c16..4951c66e4f 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -7,7 +7,7 @@ import { overrideHeaders, withoutAuthorization, type PerServerMcpHeaders, -} from '../mcp/perServerMcpHeaders'; +} from '../runtime/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { McpServerNotFoundError, diff --git a/packages/trueforge/src/resolvers.ts b/packages/trueforge/src/resolvers.ts index 27a2174f9c..840e0d8b05 100644 --- a/packages/trueforge/src/resolvers.ts +++ b/packages/trueforge/src/resolvers.ts @@ -22,8 +22,8 @@ import type { ISandboxProviderStore } from './db/sandboxProviderStore'; import type { ISkillStore } from './db/skillStore'; import type { IWebSearchProviderStore } from './db/webSearchProviderStore'; import type { IOAuthTokenStore } from './mcp/auth/types'; -import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './mcp/perServerMcpHeaders'; import type { MemoryProviderCache, TenantMemoryDeps } from './memory/providers'; +import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './runtime/perServerMcpHeaders'; import { headerValue } from './truefoundry/gatewayMetadata'; import { InlineMcpServerStore } from './truefoundry/InlineMcpServerStore'; import { InlineModelProviderStore } from './truefoundry/InlineModelProviderStore'; diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/runtime/perServerMcpHeaders.ts similarity index 100% rename from packages/trueforge/src/mcp/perServerMcpHeaders.ts rename to packages/trueforge/src/runtime/perServerMcpHeaders.ts diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 57048ca88e..ee7bdc2adf 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -24,7 +24,7 @@ import { isVirtualMcpHeaders, withoutAuthorization, type PerServerMcpHeaders, -} from '../mcp/perServerMcpHeaders'; +} from '../runtime/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts deleted file mode 100644 index 807c3e45ad..0000000000 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ /dev/null @@ -1,252 +0,0 @@ -import { HTTPException } from 'hono/http-exception'; -import { createLogger } from 'winston'; -import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore'; -import type { McpServerRecord } from '../../../src/db/mcpServerStore'; -import { createSqliteDb } from '../../../src/db/sqlite/client'; -import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; -import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; -import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/mcp/perServerMcpHeaders'; -import { - TrueFoundryMcpServerStore, - type TrueFoundryMcpApiClient, -} from '../../../src/truefoundry/TrueFoundryMcpServerStore'; -import { createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; - -function unusedClient(): TrueFoundryMcpApiClient { - const unused = (): Promise => Promise.reject(new Error('unused')); - return { - getMcpServerByName: async ({ name }) => ({ - id: name, - name, - proxyUrl: `https://gateway.example/mcp-server/${name}`, - createdAt: '2026-01-15T12:00:00.000Z', - updatedAt: '2026-01-16T12:00:00.000Z', - manifest: { description: name }, - }), - listMcpServers: unused, - listGatewayInstallations: async () => [{ isDefault: true, manifest: { url: 'https://gateway.example' } }], - getMcpAuthorize: async () => ({ status: 'authenticated' }), - getMcpAuthStatus: unused, - deleteMcpAuth: unused, - vendToken: () => Promise.resolve({ subjectToken: 'caller-token', actorToken: 'caller-token' }), - getTenantControlPlaneUrl: async () => 'https://tenant.example.com', - }; -} - -const record = (name: string): McpServerRecord => ({ - id: name, - tenant_id: 'default', - name, - manifest: { - type: 'truefoundry', - name, - url: `https://gateway.example/mcp-server/${name}`, - description: name, - }, - created_at: '2026-01-15T12:00:00.000Z', - updated_at: '2026-01-16T12:00:00.000Z', -}); - -const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => - new TrueFoundryMcpServerStore({ - client: unusedClient(), - requestContext: createTrueFoundryRequestContext({ - tenant_id: 'default', - subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, - roles: [], - user_credential: 'caller-token', - }), - agent: undefined, - logger: createLogger({ silent: true }), - perServerHeaders, - }); - -async function invokeHeaders(store: TrueFoundryMcpServerStore, name: string): Promise> { - const resolve = store.resolveInvokeHeaders({ record: record(name), userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver so the token is read at connect time'); - } - const resolved = await resolve(); - if (!('headers' in resolved)) { - throw new Error('expected headers rather than an auth-required signal'); - } - return resolved.headers; -} - -describe('parsePerServerMcpHeaders', () => { - it('parses a header map per server name', () => { - const raw = JSON.stringify({ 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' } }); - - expect(parsePerServerMcpHeaders(raw)).toEqual({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }); - }); - - it.each([ - ['not json', 'not-json'], - ['an array', '[]'], - ['a scalar', '"nope"'], - ['a server mapped to a string', JSON.stringify({ 'tfy-platform-mcp': 'Bearer user' })], - ['a header mapped to a number', JSON.stringify({ 'tfy-platform-mcp': { 'x-h': 1 } })], - ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], - ['a managed session header', JSON.stringify({ 'tfy-platform-mcp': { 'Mcp-Session-Id': 'other' } })], - ['a managed protocol header', JSON.stringify({ 'tfy-platform-mcp': { 'mcp-protocol-version': '2025-06-18' } })], - ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { - expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); - }); - - it('keeps the parse failure as the cause, so a bad header can be debugged', () => { - expect(() => parsePerServerMcpHeaders('not-json')).toThrow( - expect.objectContaining({ cause: expect.any(SyntaxError) }), - ); - }); -}); - -describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { - it('passes a virtual MCP entry through as x-tfy-mcp-headers', async () => { - const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; - const raw = JSON.stringify({ 'virtual-mcp': entry }); - - const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); - - expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); - }); - - it('sends only the gateway Bearer when a server has no override', async () => { - await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ - Authorization: 'Bearer caller-token', - }); - }); - - it("merges that server's override on top of the Bearer", async () => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }), - 'tfy-platform-mcp', - ); - - expect(headers).toEqual({ - Authorization: 'Bearer caller-token', - 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}', - }); - }); - - it('gives one server nothing of another, so an identity cannot reach the wrong upstream', async () => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }), - 'tfy-pylon-mcp', - ); - - expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); - }); - - it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( - 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', - async name => { - const headers = await invokeHeaders( - storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), - 'tfy-platform-mcp', - ); - - expect(headers).toEqual({ - Authorization: 'Bearer caller-token', - 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), - }); - }, - ); - - it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { - const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); - const store = new TrueFoundryMcpServerStore({ - client: { ...unusedClient(), getMcpAuthorize }, - requestContext: createTrueFoundryRequestContext({ - tenant_id: 'default', - subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, - roles: [], - user_credential: 'caller-token', - }), - agent: undefined, - logger: createLogger({ silent: true }), - perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user', 'X-End-User': 'alice' } }, - }); - const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - - const resolved = await resolve(); - if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { - throw new Error('expected a fallback to the gateway auth'); - } - await expect(resolved.onUnauthorized()).resolves.toEqual({ - headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, - }); - expect(getMcpAuthorize).not.toHaveBeenCalled(); - }); -}); - -describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { - const ossRecord: McpServerRecord = { - id: 'oauth-mcp', - tenant_id: 'default', - name: 'oauth-mcp', - manifest: { - type: 'remote', - name: 'oauth-mcp', - url: 'https://mcp.example/mcp', - description: '', - auth: { type: 'dcr' }, - }, - created_at: '2026-01-15T12:00:00.000Z', - updated_at: '2026-01-16T12:00:00.000Z', - }; - - it('forwards a caller Authorization directly instead of starting the DCR consent flow', async () => { - const db = createSqliteDb(':memory:'); - const store = new McpServerWithAuthStore({ - store: new SqliteMcpServerStore(db), - tokenStore: new SqliteOAuthTokenStore(db), - clientName: 'test-client', - perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, - }); - - const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); - await db.destroy(); - }); - - it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { - const entry = { serv1: { Authorization: 'Bearer a' } }; - const db = createSqliteDb(':memory:'); - const store = new McpServerWithAuthStore({ - store: new SqliteMcpServerStore(db), - tokenStore: new SqliteOAuthTokenStore(db), - clientName: 'test-client', - perServerHeaders: { 'virtual-mcp': entry }, - }); - const record: McpServerRecord = { - ...ossRecord, - name: 'virtual-mcp', - manifest: { - ...ossRecord.manifest, - name: 'virtual-mcp', - auth: { type: 'header', headers: { Authorization: 'Bearer gateway' } }, - }, - }; - - const resolve = store.resolveInvokeHeaders({ record, userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - await expect(resolve()).resolves.toEqual({ - headers: { Authorization: 'Bearer gateway', serv1: JSON.stringify(entry.serv1) }, - }); - await db.destroy(); - }); -}); diff --git a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts new file mode 100644 index 0000000000..75797526ad --- /dev/null +++ b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts @@ -0,0 +1,99 @@ +import { HTTPException } from 'hono/http-exception'; +import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore'; +import type { McpServerRecord } from '../../../src/db/mcpServerStore'; +import { createSqliteDb } from '../../../src/db/sqlite/client'; +import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; +import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; +import { parsePerServerMcpHeaders } from '../../../src/runtime/perServerMcpHeaders'; + +describe('parsePerServerMcpHeaders', () => { + it('parses a header map per server name', () => { + const raw = JSON.stringify({ 'github-mcp': { Authorization: 'Bearer user' } }); + + expect(parsePerServerMcpHeaders(raw)).toEqual({ + 'github-mcp': { Authorization: 'Bearer user' }, + }); + }); + + it.each([ + ['not json', 'not-json'], + ['an array', '[]'], + ['a scalar', '"nope"'], + ['a server mapped to a string', JSON.stringify({ 'github-mcp': 'Bearer user' })], + ['a header mapped to a number', JSON.stringify({ 'github-mcp': { 'x-h': 1 } })], + ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], + ['a managed session header', JSON.stringify({ 'github-mcp': { 'Mcp-Session-Id': 'other' } })], + ['a managed protocol header', JSON.stringify({ 'github-mcp': { 'mcp-protocol-version': '2025-06-18' } })], + ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { + expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); + }); + + it('keeps the parse failure as the cause, so a bad header can be debugged', () => { + expect(() => parsePerServerMcpHeaders('not-json')).toThrow( + expect.objectContaining({ cause: expect.any(SyntaxError) }), + ); + }); +}); + +describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { + const ossRecord: McpServerRecord = { + id: 'oauth-mcp', + tenant_id: 'default', + name: 'oauth-mcp', + manifest: { + type: 'remote', + name: 'oauth-mcp', + url: 'https://mcp.example/mcp', + description: '', + auth: { type: 'dcr' }, + }, + created_at: '2026-01-15T12:00:00.000Z', + updated_at: '2026-01-16T12:00:00.000Z', + }; + + it('forwards a caller Authorization directly instead of starting the DCR consent flow', async () => { + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, + }); + + const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); + await db.destroy(); + }); + + it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { + const entry = { serv1: { Authorization: 'Bearer a' } }; + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'virtual-mcp': entry }, + }); + const record: McpServerRecord = { + ...ossRecord, + name: 'virtual-mcp', + manifest: { + ...ossRecord.manifest, + name: 'virtual-mcp', + auth: { type: 'header', headers: { Authorization: 'Bearer server' } }, + }, + }; + + const resolve = store.resolveInvokeHeaders({ record, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toEqual({ + headers: { Authorization: 'Bearer server', serv1: JSON.stringify(entry.serv1) }, + }); + await db.destroy(); + }); +}); diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index 7101c06e3f..e0d6c4625e 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -2,6 +2,7 @@ import { AgentSpecSchema } from '@truefoundry/trueforge-core/agent-session'; import { createLogger } from 'winston'; import type { AgentRecord } from '../../../src/db/agentStore'; import { McpServerNotFoundError, type McpServerRecord } from '../../../src/db/mcpServerStore'; +import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/runtime/perServerMcpHeaders'; import { ACTOR_AUTHORIZATION_HEADER, createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; import { X_TFY_CONVERSATION_ID, X_TFY_METADATA } from '../../../src/truefoundry/gatewayMetadata'; import { MCP_PROXY_BASE_URL_TEMPLATE } from '../../../src/truefoundry/mapSfyMcpServers'; @@ -356,3 +357,150 @@ describe('TrueFoundryMcpServerStore', () => { }); }); }); + +function unusedClient(): TrueFoundryMcpApiClient { + const unused = (): Promise => Promise.reject(new Error('unused')); + return { + getMcpServerByName: async ({ name }) => ({ + id: name, + name, + proxyUrl: `https://gateway.example/mcp-server/${name}`, + createdAt: '2026-01-15T12:00:00.000Z', + updatedAt: '2026-01-16T12:00:00.000Z', + manifest: { description: name }, + }), + listMcpServers: unused, + listGatewayInstallations: async () => [{ isDefault: true, manifest: { url: 'https://gateway.example' } }], + getMcpAuthorize: async () => ({ status: 'authenticated' }), + getMcpAuthStatus: unused, + deleteMcpAuth: unused, + vendToken: () => Promise.resolve({ subjectToken: 'caller-token', actorToken: 'caller-token' }), + getTenantControlPlaneUrl: async () => 'https://tenant.example.com', + }; +} + +const record = (name: string): McpServerRecord => ({ + id: name, + tenant_id: 'default', + name, + manifest: { + type: 'truefoundry', + name, + url: `https://gateway.example/mcp-server/${name}`, + description: name, + }, + created_at: '2026-01-15T12:00:00.000Z', + updated_at: '2026-01-16T12:00:00.000Z', +}); + +const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => + new TrueFoundryMcpServerStore({ + client: unusedClient(), + requestContext: createTrueFoundryRequestContext({ + tenant_id: 'default', + subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, + roles: [], + user_credential: 'caller-token', + }), + agent: undefined, + logger: createLogger({ silent: true }), + perServerHeaders, + }); + +async function invokeHeaders(store: TrueFoundryMcpServerStore, name: string): Promise> { + const resolve = store.resolveInvokeHeaders({ record: record(name), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver so the token is read at connect time'); + } + const resolved = await resolve(); + if (!('headers' in resolved)) { + throw new Error('expected headers rather than an auth-required signal'); + } + return resolved.headers; +} + +describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { + it('passes a virtual MCP entry through as x-tfy-mcp-headers', async () => { + const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; + const raw = JSON.stringify({ 'virtual-mcp': entry }); + + const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); + }); + + it('sends only the gateway Bearer when a server has no override', async () => { + await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ + Authorization: 'Bearer caller-token', + }); + }); + + it("merges that server's override on top of the Bearer", async () => { + const headers = await invokeHeaders( + storeWith({ + 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, + }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}', + }); + }); + + it('gives one server nothing of another, so an identity cannot reach the wrong upstream', async () => { + const headers = await invokeHeaders( + storeWith({ + 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, + }), + 'tfy-pylon-mcp', + ); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); + }); + + it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( + 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', + async name => { + const headers = await invokeHeaders( + storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), + }); + }, + ); + + it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { + const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); + const store = new TrueFoundryMcpServerStore({ + client: { ...unusedClient(), getMcpAuthorize }, + requestContext: createTrueFoundryRequestContext({ + tenant_id: 'default', + subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, + roles: [], + user_credential: 'caller-token', + }), + agent: undefined, + logger: createLogger({ silent: true }), + perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user', 'X-End-User': 'alice' } }, + }); + const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + + const resolved = await resolve(); + if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { + throw new Error('expected a fallback to the gateway auth'); + } + await expect(resolved.onUnauthorized()).resolves.toEqual({ + headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, + }); + expect(getMcpAuthorize).not.toHaveBeenCalled(); + }); +}); From 408c61d8e725f6dace1d5ef8b19c35e949183dbe Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 06:18:30 +0000 Subject: [PATCH 15/20] refactor: move TrueFoundry gateway 401 parsing into truefoundry/ (AGE-2723) Co-authored-by: Rutvij Menavlikar --- packages/trueforge-core/src/core/errors.ts | 11 +++-- .../trueforge-core/src/core/mcp/RemoteMCP.ts | 41 +++++++++++-------- .../src/core/mcp/remoteMcpClient.ts | 28 ++++--------- .../tests/core/mcp/connectRemoteMcp.test.ts | 22 +++------- .../core/mcp/mcpResponseSizeLimit.test.ts | 11 ----- .../tests/core/mcp/remoteMcpServer.test.ts | 31 ++++++++------ .../truefoundry/TrueFoundryMcpServerStore.ts | 33 ++++++++++++++- .../TrueFoundryMcpServerStore.test.ts | 18 ++++++++ 8 files changed, 109 insertions(+), 86 deletions(-) diff --git a/packages/trueforge-core/src/core/errors.ts b/packages/trueforge-core/src/core/errors.ts index 4cb40fe242..d6e2b99530 100644 --- a/packages/trueforge-core/src/core/errors.ts +++ b/packages/trueforge-core/src/core/errors.ts @@ -106,15 +106,14 @@ export class McpConnectionError extends AgentHarnessError { } } -/** A 401 whose body lists consent URLs (`authorization_urls`, keyed by server name). */ -export class McpAuthorizationRequiredError extends McpConnectionError { +/** An upstream 401 that keeps its response body, so a headers resolver can read consent details from it. */ +export class McpUnauthorizedError extends McpConnectionError { constructor( - message: string, - readonly authorizationUrls: Readonly>, + readonly body: string, options?: ErrorOptions, ) { - super(message, 401, options); - this.name = 'McpAuthorizationRequiredError'; + super('upstream returned 401 Unauthorized', 401, options); + this.name = 'McpUnauthorizedError'; } } diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index 589dee0b6f..8b870843f9 100644 --- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts +++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts @@ -3,8 +3,8 @@ import type { Logger } from 'winston'; import { applyErrorPrefix, MCP_ERROR_PREFIX, - McpAuthorizationRequiredError, McpConnectionError, + McpUnauthorizedError, stripErrorPrefix, } from '../errors'; import type { MCPServerInitInfo } from '../events/schema'; @@ -50,11 +50,18 @@ function redactUrlForTrace(url: string): string { /** * What a headers resolver returns: the headers to send, or a signal that auth is required. * `onUnauthorized` is consulted once if the upstream rejects `headers` with a 401. + * `authRequiredFromBody` reads consent details from a 401 body; without it a 401 stays an error. */ export type ResolveHeadersResult = - | { headers: Record; onUnauthorized?: () => Promise } + | { + headers: Record; + onUnauthorized?: () => Promise; + authRequiredFromBody?: (body: string) => MCPAuthRequired | undefined; + } | { authRequired: MCPAuthRequired }; +type HeadersResult = Extract }>; + /** Static headers, or a resolver (invoked at connect) returning headers or signalling auth-required. */ export type RemoteMcpHeaders = Record | (() => Promise); export interface McpConnection { @@ -191,15 +198,22 @@ export class RemoteMCP implements ToolSource { private async executeWithSessionRetry( operation: (connection: RemoteMcpConnection) => Promise, ): Promise> { + // Auth is re-checked on every operation, not only on the first connect: a registered server's OAuth + // can be revoked or expire mid-request, and callers must get authRequired rather than a generic + // upstream failure. When already connected the resolved headers are unused (connect is skipped). + const headersResult = await this.resolveHeaders(); + if ('authRequired' in headersResult) { + return { authRequired: headersResult.authRequired }; + } let used: RemoteMcpConnection | undefined; const tracked = (connection: RemoteMcpConnection): Promise => { used = connection; return operation(connection); }; try { - return await this.executeWithAuthFallback(tracked); + return await this.executeWithAuthFallback(headersResult, tracked); } catch (error) { - const authRequired = this.authRequiredFrom(error); + const authRequired = this.authRequiredFrom(error, headersResult); if (authRequired === undefined) { throw error; } @@ -218,28 +232,19 @@ export class RemoteMCP implements ToolSource { } } - /** A 401 that carried consent URLs becomes authRequired; a bare 401 stays an error. */ - private authRequiredFrom(error: unknown): MCPAuthRequired | undefined { + private authRequiredFrom(error: unknown, headersResult: HeadersResult): MCPAuthRequired | undefined { for (let current: unknown = error; current instanceof Error; current = current.cause) { - if (current instanceof McpAuthorizationRequiredError) { - const urls = current.authorizationUrls; - const authUrl = urls[this.name] ?? Object.values(urls)[0]; - return authUrl === undefined ? undefined : { servers: [{ id: this.id, name: this.name, auth_url: authUrl }] }; + if (current instanceof McpUnauthorizedError) { + return headersResult.authRequiredFromBody?.(current.body); } } return undefined; } private async executeWithAuthFallback( + headersResult: HeadersResult, operation: (connection: RemoteMcpConnection) => Promise, ): Promise> { - // Auth is re-checked on every operation, not only on the first connect: a registered server's OAuth - // can be revoked or expire mid-request, and callers must get authRequired rather than a generic - // upstream failure. When already connected the resolved headers are unused (connect is skipped). - const headersResult = await this.resolveHeaders(); - if ('authRequired' in headersResult) { - return { authRequired: headersResult.authRequired }; - } const { onUnauthorized } = headersResult; if (onUnauthorized === undefined) { return this.connectAndRun(headersResult.headers, operation, true); @@ -256,7 +261,7 @@ export class RemoteMCP implements ToolSource { try { return await this.connectAndRun(headersResult.headers, tracked, true); } catch (error) { - if (!isAuthError(error) || this.authRequiredFrom(error) !== undefined) { + if (!isAuthError(error) || this.authRequiredFrom(error, headersResult) !== undefined) { throw error; } this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`); diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 68b84c0d5c..b9fe162167 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -6,8 +6,7 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/ 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 { z } from 'zod'; -import { McpAuthorizationRequiredError, McpConnectionError } from '../errors'; +import { McpConnectionError, McpUnauthorizedError } from '../errors'; import { elapsedSeconds, getOperationMetrics, type McpConnectOutcome } from '../observability/operationMetrics'; import { mcpSsrfFetch } from '../util/outboundFetch'; import { PromiseTimeoutError, withTimeout } from '../util/promiseUtils'; @@ -36,12 +35,11 @@ const TRANSPORT_PROBE_ORDER: RemoteMcpTransportType[] = ['streamable-http', 'sse export const DEFAULT_MAX_MCP_RESPONSE_BYTES = 50 * 1024 * 1024; -/** A successful GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ +/** GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): FetchLike { return async (url, init) => { const response = await fetchFn(url, init); const isGetSse = - response.ok && (init?.method ?? 'GET').toUpperCase() === 'GET' && (response.headers.get('content-type') ?? '').toLowerCase().includes('text/event-stream'); if (isGetSse || !response.body) { @@ -71,26 +69,14 @@ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): Fetc }; } -const AuthorizationRequiredBodySchema = z.object({ - authorization_urls: z.record(z.string(), z.url({ protocol: /^https?$/ })), -}); - -/** A 401 whose body lists `authorization_urls` throws `McpAuthorizationRequiredError`; other responses pass through. */ -export function withAuthorizationRequired(fetchFn: FetchLike): FetchLike { +/** A non-GET 401 throws `McpUnauthorizedError` with its body; GET (SSE) and other responses pass through. */ +function withUnauthorizedBody(fetchFn: FetchLike): FetchLike { return async (url, init) => { const response = await fetchFn(url, init); - if (response.status !== 401) { - return response; - } - const body: unknown = await response - .clone() - .json() - .catch(() => undefined); - const parsed = AuthorizationRequiredBodySchema.safeParse(body); - if (!parsed.success || Object.keys(parsed.data.authorization_urls).length === 0) { + if (response.status !== 401 || (init?.method ?? 'GET').toUpperCase() === 'GET') { return response; } - throw new McpAuthorizationRequiredError('upstream requires authorization', parsed.data.authorization_urls); + throw new McpUnauthorizedError(await response.text().catch(() => '')); }; } @@ -241,7 +227,7 @@ export async function connectRemoteMcp(params: { const customFetch = params.fetch; const transportFetch: RemoteMcpFetch = customFetch === undefined ? mcpSsrfFetch : (input, init) => customFetch(input, { ...init, redirect: 'manual' }); - const fetchFn = withAuthorizationRequired( + const fetchFn = withUnauthorizedBody( withMaxResponseBytes(transportFetch, params.maxResponseBytes ?? DEFAULT_MAX_MCP_RESPONSE_BYTES), ); const candidates = params.knownTransportType diff --git a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts index 20a021d821..967e2ae443 100644 --- a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts +++ b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts @@ -116,32 +116,22 @@ describe('connectRemoteMcp custom fetch', () => { expect(fetch).toHaveBeenCalledWith(endpoint, expect.objectContaining({ redirect: 'manual' })); }); - it('turns a 401 with authorization_urls into McpAuthorizationRequiredError and passes a bare 401 through', async () => { - const urls = { github: 'https://auth.example/consent' }; + it('throws McpUnauthorizedError with the body for a non-GET 401 and passes a GET 401 through', async () => { const fetch = jest .fn, Parameters>() - .mockResolvedValueOnce( - new Response(JSON.stringify({ authorization_urls: urls }), { - status: 401, - }), - ) - .mockResolvedValueOnce(new Response('unauthorized', { status: 401 })) - .mockResolvedValueOnce( - new Response(JSON.stringify({ authorization_urls: { github: 'javascript:alert(1)' } }), { status: 401 }), - ); + .mockImplementation(async () => new Response('{"reason":"consent"}', { status: 401 })); await connectRemoteMcp({ ...baseParams(), url: endpoint, knownTransportType: 'streamable-http', fetch }); const transportFetch = mockTransportFetches[0]; if (transportFetch === undefined) { throw new Error('expected transport fetch'); } - await expect(transportFetch(endpoint, {})).rejects.toMatchObject({ - name: 'McpAuthorizationRequiredError', + await expect(transportFetch(endpoint, { method: 'POST' })).rejects.toMatchObject({ + name: 'McpUnauthorizedError', statusCode: 401, - authorizationUrls: urls, + body: '{"reason":"consent"}', }); - await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); - await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); + await expect(transportFetch(endpoint, { method: 'GET' })).resolves.toMatchObject({ status: 401 }); }); }); diff --git a/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts b/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts index c67f045ae6..77b328aa48 100644 --- a/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts +++ b/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts @@ -33,15 +33,4 @@ describe('withMaxResponseBytes', () => { expect((await response.arrayBuffer()).byteLength).toBe(80); }); - - it('caps a non-OK GET text/event-stream body', async () => { - const fetchFn = withMaxResponseBytes( - async () => - new Response(streamOf(new Uint8Array(80)), { status: 401, headers: { 'content-type': 'text/event-stream' } }), - 50, - ); - const response = await fetchFn('https://mcp.example.com', { method: 'GET' }); - - await expect(response.arrayBuffer()).rejects.toThrow(/exceeded max 50 bytes/); - }); }); diff --git a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts index 837162dd48..01b821b959 100644 --- a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts +++ b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts @@ -1,6 +1,6 @@ import type { Logger } from 'winston'; import { McpConnectionError, RemoteMCP, ToolSet } from '../../../src/core'; -import { McpAuthorizationRequiredError } from '../../../src/core/errors'; +import { McpUnauthorizedError } from '../../../src/core/errors'; import { isApprovalRequiredResponse, isCallToolResponseResult, @@ -474,27 +474,32 @@ describe('RemoteMCP + ToolSet', () => { expect(state.callToolCalls).toBe(0); }); - it('returns authRequired from a 401 that carries authorization URLs, without falling back', async () => { - installFakeConnection({ - connectError: new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), - }); + it('returns authRequired when the resolver reads consent details from the 401 body, without falling back', async () => { + installFakeConnection({ connectError: new McpUnauthorizedError('consent body') }); + const authRequired: MCPAuthRequired = { + servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }], + }; const onUnauthorized = jest.fn(() => Promise.resolve({ headers: {} })); - const server = makeServer({ headers: () => Promise.resolve({ headers: {}, onUnauthorized }) }); - - await expect(server.listTools()).resolves.toEqual({ - authRequired: { servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }] }, + const authRequiredFromBody = jest.fn((body: string) => (body === 'consent body' ? authRequired : undefined)); + const server = makeServer({ + headers: () => Promise.resolve({ headers: {}, onUnauthorized, authRequiredFromBody }), }); + + await expect(server.listTools()).resolves.toEqual({ authRequired }); expect(onUnauthorized).not.toHaveBeenCalled(); }); it('drops the rejected connection when a call returns consent URLs, so the next call reconnects', async () => { const state = installFakeConnection({ sessionId: 'sess-1' }); - const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); + const authRequired: MCPAuthRequired = { + servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }], + }; + const server = makeServer({ + headers: () => Promise.resolve({ headers: {}, authRequiredFromBody: () => authRequired }), + }); await server.listTools(); - state.queueCallToolError( - new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), - ); + state.queueCallToolError(new McpUnauthorizedError('consent body')); await expect(server.callTool({ name: 'read_thing', arguments: {} })).resolves.toHaveProperty('authRequired'); await server.callTool({ name: 'read_thing', arguments: {} }); diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index ee7bdc2adf..df75784766 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -1,5 +1,11 @@ -import { McpConnectionError, type RemoteMcpHeaders, type ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; +import { + McpConnectionError, + type MCPAuthRequired, + type RemoteMcpHeaders, + type ResolveHeadersResult, +} from '@truefoundry/trueforge-core/core'; import type { Logger } from 'winston'; +import { z } from 'zod'; import type { RequestContext, RequestSubject } from '../auth/identity'; import { safeReturnTo } from '../auth/safeReturnTo'; import { getPublicUiBasePath } from '../config'; @@ -59,6 +65,27 @@ export type TrueFoundryMcpApiClient = Pick< /** Per-server upstream headers the TrueFoundry gateway applies after its own caller auth. */ const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; +/** The gateway's 401 when outbound auth is missing: consent URLs keyed by server name. */ +const GatewayAuthRequiredBodySchema = z.object({ + authorization_urls: z.record(z.string(), z.url({ protocol: /^https?$/ })), +}); + +function gatewayAuthRequired(input: { name: string; body: string }): MCPAuthRequired | undefined { + let decoded: unknown; + try { + decoded = JSON.parse(input.body); + } catch { + return undefined; + } + const parsed = GatewayAuthRequiredBodySchema.safeParse(decoded); + if (!parsed.success) { + return undefined; + } + const urls = parsed.data.authorization_urls; + const authUrl = urls[input.name] ?? Object.values(urls)[0]; + return authUrl === undefined ? undefined : { servers: [{ id: input.name, name: input.name, auth_url: authUrl }] }; +} + /** * Absolute FE landing for the upstream authorize `redirectURL`. * Origin from the tenant control-plane URL; UI path prefix from process @@ -120,6 +147,7 @@ export class TrueFoundryMcpServerStore implements IMcpServ }): RemoteMcpHeaders { const { record, turnMetadata } = input; const callerHeaders = this.#perServerHeaders[record.name]; + const authRequiredFromBody = (body: string) => gatewayAuthRequired({ name: record.name, body }); const gatewayBase = async (): Promise> => ({ ...gatewayHeaders(await this.#resolveGatewayAuthorization()), ...gatewayMetadataHeadersForTurn(turnMetadata), @@ -128,10 +156,12 @@ export class TrueFoundryMcpServerStore implements IMcpServ // The gateway fans a virtual MCP's nested map out to its servers itself. return async () => ({ headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, + authRequiredFromBody, }); } const viaGateway = async (): Promise => ({ headers: { ...withoutAuthorization(callerHeaders), ...(await gatewayBase()) }, + authRequiredFromBody, }); if (!hasAuthorizationHeader(callerHeaders)) { return viaGateway; @@ -140,6 +170,7 @@ export class TrueFoundryMcpServerStore implements IMcpServ return async () => ({ headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, onUnauthorized: viaGateway, + authRequiredFromBody, }); } diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index e0d6c4625e..67ecf3965d 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -303,10 +303,24 @@ describe('TrueFoundryMcpServerStore', () => { const { store, client } = createStore(); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, + authRequiredFromBody: expect.any(Function), }); expect(client.getMcpAuthorize).not.toHaveBeenCalled(); }); + it('reads the consent URL from a gateway 401 body', async () => { + const resolved = await invoke(createStore().store); + if (!('headers' in resolved) || resolved.authRequiredFromBody === undefined) { + throw new Error('expected a 401 body reader'); + } + const body = (url: string) => JSON.stringify({ authorization_urls: { github: url } }); + expect(resolved.authRequiredFromBody(body('https://consent.example/authorize'))).toEqual({ + servers: [{ id: 'github', name: 'github', auth_url: 'https://consent.example/authorize' }], + }); + expect(resolved.authRequiredFromBody(body('javascript:alert(1)'))).toBeUndefined(); + expect(resolved.authRequiredFromBody('unauthorized')).toBeUndefined(); + }); + it('merges turn metadata after auth headers', async () => { const { store } = createStore(); const headers = store.resolveInvokeHeaders({ @@ -329,6 +343,7 @@ describe('TrueFoundryMcpServerStore', () => { [X_TFY_METADATA]: expect.any(String), [X_TFY_CONVERSATION_ID]: 'sess-1', }, + authRequiredFromBody: expect.any(Function), }); if (!('headers' in resolved) || resolved.headers === undefined) { throw new Error('expected invoke headers'); @@ -343,6 +358,7 @@ describe('TrueFoundryMcpServerStore', () => { Authorization: `Bearer ${ACCESS_TOKEN}`, [ACTOR_AUTHORIZATION_HEADER]: `Bearer ${ACTOR_TOKEN}`, }, + authRequiredFromBody: expect.any(Function), }); expect(client.getMcpServerByName).not.toHaveBeenCalled(); expect(client.vendToken).toHaveBeenCalledTimes(1); @@ -352,6 +368,7 @@ describe('TrueFoundryMcpServerStore', () => { const { store, client } = createStore({ agent: AGENT, userCredential: null }); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${SUBJECT_TOKEN}` }, + authRequiredFromBody: expect.any(Function), }); expect(client.getMcpServerByName).not.toHaveBeenCalled(); }); @@ -500,6 +517,7 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { } await expect(resolved.onUnauthorized()).resolves.toEqual({ headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, + authRequiredFromBody: expect.any(Function), }); expect(getMcpAuthorize).not.toHaveBeenCalled(); }); From 3aa59d9cb8843a991da9f4cae32343d722f8486a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 06:25:04 +0000 Subject: [PATCH 16/20] Revert "refactor: move TrueFoundry gateway 401 parsing into truefoundry/ (AGE-2723)" This reverts commit 408c61d8e725f6dace1d5ef8b19c35e949183dbe. Co-authored-by: Rutvij Menavlikar --- packages/trueforge-core/src/core/errors.ts | 11 ++--- .../trueforge-core/src/core/mcp/RemoteMCP.ts | 41 ++++++++----------- .../src/core/mcp/remoteMcpClient.ts | 28 +++++++++---- .../tests/core/mcp/connectRemoteMcp.test.ts | 22 +++++++--- .../core/mcp/mcpResponseSizeLimit.test.ts | 11 +++++ .../tests/core/mcp/remoteMcpServer.test.ts | 31 ++++++-------- .../truefoundry/TrueFoundryMcpServerStore.ts | 33 +-------------- .../TrueFoundryMcpServerStore.test.ts | 18 -------- 8 files changed, 86 insertions(+), 109 deletions(-) diff --git a/packages/trueforge-core/src/core/errors.ts b/packages/trueforge-core/src/core/errors.ts index d6e2b99530..4cb40fe242 100644 --- a/packages/trueforge-core/src/core/errors.ts +++ b/packages/trueforge-core/src/core/errors.ts @@ -106,14 +106,15 @@ export class McpConnectionError extends AgentHarnessError { } } -/** An upstream 401 that keeps its response body, so a headers resolver can read consent details from it. */ -export class McpUnauthorizedError extends McpConnectionError { +/** A 401 whose body lists consent URLs (`authorization_urls`, keyed by server name). */ +export class McpAuthorizationRequiredError extends McpConnectionError { constructor( - readonly body: string, + message: string, + readonly authorizationUrls: Readonly>, options?: ErrorOptions, ) { - super('upstream returned 401 Unauthorized', 401, options); - this.name = 'McpUnauthorizedError'; + super(message, 401, options); + this.name = 'McpAuthorizationRequiredError'; } } diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index 8b870843f9..589dee0b6f 100644 --- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts +++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts @@ -3,8 +3,8 @@ import type { Logger } from 'winston'; import { applyErrorPrefix, MCP_ERROR_PREFIX, + McpAuthorizationRequiredError, McpConnectionError, - McpUnauthorizedError, stripErrorPrefix, } from '../errors'; import type { MCPServerInitInfo } from '../events/schema'; @@ -50,18 +50,11 @@ function redactUrlForTrace(url: string): string { /** * What a headers resolver returns: the headers to send, or a signal that auth is required. * `onUnauthorized` is consulted once if the upstream rejects `headers` with a 401. - * `authRequiredFromBody` reads consent details from a 401 body; without it a 401 stays an error. */ export type ResolveHeadersResult = - | { - headers: Record; - onUnauthorized?: () => Promise; - authRequiredFromBody?: (body: string) => MCPAuthRequired | undefined; - } + | { headers: Record; onUnauthorized?: () => Promise } | { authRequired: MCPAuthRequired }; -type HeadersResult = Extract }>; - /** Static headers, or a resolver (invoked at connect) returning headers or signalling auth-required. */ export type RemoteMcpHeaders = Record | (() => Promise); export interface McpConnection { @@ -198,22 +191,15 @@ export class RemoteMCP implements ToolSource { private async executeWithSessionRetry( operation: (connection: RemoteMcpConnection) => Promise, ): Promise> { - // Auth is re-checked on every operation, not only on the first connect: a registered server's OAuth - // can be revoked or expire mid-request, and callers must get authRequired rather than a generic - // upstream failure. When already connected the resolved headers are unused (connect is skipped). - const headersResult = await this.resolveHeaders(); - if ('authRequired' in headersResult) { - return { authRequired: headersResult.authRequired }; - } let used: RemoteMcpConnection | undefined; const tracked = (connection: RemoteMcpConnection): Promise => { used = connection; return operation(connection); }; try { - return await this.executeWithAuthFallback(headersResult, tracked); + return await this.executeWithAuthFallback(tracked); } catch (error) { - const authRequired = this.authRequiredFrom(error, headersResult); + const authRequired = this.authRequiredFrom(error); if (authRequired === undefined) { throw error; } @@ -232,19 +218,28 @@ export class RemoteMCP implements ToolSource { } } - private authRequiredFrom(error: unknown, headersResult: HeadersResult): MCPAuthRequired | undefined { + /** A 401 that carried consent URLs becomes authRequired; a bare 401 stays an error. */ + private authRequiredFrom(error: unknown): MCPAuthRequired | undefined { for (let current: unknown = error; current instanceof Error; current = current.cause) { - if (current instanceof McpUnauthorizedError) { - return headersResult.authRequiredFromBody?.(current.body); + if (current instanceof McpAuthorizationRequiredError) { + const urls = current.authorizationUrls; + const authUrl = urls[this.name] ?? Object.values(urls)[0]; + return authUrl === undefined ? undefined : { servers: [{ id: this.id, name: this.name, auth_url: authUrl }] }; } } return undefined; } private async executeWithAuthFallback( - headersResult: HeadersResult, operation: (connection: RemoteMcpConnection) => Promise, ): Promise> { + // Auth is re-checked on every operation, not only on the first connect: a registered server's OAuth + // can be revoked or expire mid-request, and callers must get authRequired rather than a generic + // upstream failure. When already connected the resolved headers are unused (connect is skipped). + const headersResult = await this.resolveHeaders(); + if ('authRequired' in headersResult) { + return { authRequired: headersResult.authRequired }; + } const { onUnauthorized } = headersResult; if (onUnauthorized === undefined) { return this.connectAndRun(headersResult.headers, operation, true); @@ -261,7 +256,7 @@ export class RemoteMCP implements ToolSource { try { return await this.connectAndRun(headersResult.headers, tracked, true); } catch (error) { - if (!isAuthError(error) || this.authRequiredFrom(error, headersResult) !== undefined) { + if (!isAuthError(error) || this.authRequiredFrom(error) !== undefined) { throw error; } this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`); diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index b9fe162167..68b84c0d5c 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -6,7 +6,8 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/ 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, McpUnauthorizedError } from '../errors'; +import { z } from 'zod'; +import { McpAuthorizationRequiredError, McpConnectionError } from '../errors'; import { elapsedSeconds, getOperationMetrics, type McpConnectOutcome } from '../observability/operationMetrics'; import { mcpSsrfFetch } from '../util/outboundFetch'; import { PromiseTimeoutError, withTimeout } from '../util/promiseUtils'; @@ -35,11 +36,12 @@ const TRANSPORT_PROBE_ORDER: RemoteMcpTransportType[] = ['streamable-http', 'sse export const DEFAULT_MAX_MCP_RESPONSE_BYTES = 50 * 1024 * 1024; -/** GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ +/** A successful GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): FetchLike { return async (url, init) => { const response = await fetchFn(url, init); const isGetSse = + response.ok && (init?.method ?? 'GET').toUpperCase() === 'GET' && (response.headers.get('content-type') ?? '').toLowerCase().includes('text/event-stream'); if (isGetSse || !response.body) { @@ -69,14 +71,26 @@ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): Fetc }; } -/** A non-GET 401 throws `McpUnauthorizedError` with its body; GET (SSE) and other responses pass through. */ -function withUnauthorizedBody(fetchFn: FetchLike): FetchLike { +const AuthorizationRequiredBodySchema = z.object({ + authorization_urls: z.record(z.string(), z.url({ protocol: /^https?$/ })), +}); + +/** A 401 whose body lists `authorization_urls` throws `McpAuthorizationRequiredError`; other responses pass through. */ +export function withAuthorizationRequired(fetchFn: FetchLike): FetchLike { return async (url, init) => { const response = await fetchFn(url, init); - if (response.status !== 401 || (init?.method ?? 'GET').toUpperCase() === 'GET') { + if (response.status !== 401) { + return response; + } + const body: unknown = await response + .clone() + .json() + .catch(() => undefined); + const parsed = AuthorizationRequiredBodySchema.safeParse(body); + if (!parsed.success || Object.keys(parsed.data.authorization_urls).length === 0) { return response; } - throw new McpUnauthorizedError(await response.text().catch(() => '')); + throw new McpAuthorizationRequiredError('upstream requires authorization', parsed.data.authorization_urls); }; } @@ -227,7 +241,7 @@ export async function connectRemoteMcp(params: { const customFetch = params.fetch; const transportFetch: RemoteMcpFetch = customFetch === undefined ? mcpSsrfFetch : (input, init) => customFetch(input, { ...init, redirect: 'manual' }); - const fetchFn = withUnauthorizedBody( + const fetchFn = withAuthorizationRequired( withMaxResponseBytes(transportFetch, params.maxResponseBytes ?? DEFAULT_MAX_MCP_RESPONSE_BYTES), ); const candidates = params.knownTransportType diff --git a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts index 967e2ae443..20a021d821 100644 --- a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts +++ b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts @@ -116,22 +116,32 @@ describe('connectRemoteMcp custom fetch', () => { expect(fetch).toHaveBeenCalledWith(endpoint, expect.objectContaining({ redirect: 'manual' })); }); - it('throws McpUnauthorizedError with the body for a non-GET 401 and passes a GET 401 through', async () => { + it('turns a 401 with authorization_urls into McpAuthorizationRequiredError and passes a bare 401 through', async () => { + const urls = { github: 'https://auth.example/consent' }; const fetch = jest .fn, Parameters>() - .mockImplementation(async () => new Response('{"reason":"consent"}', { status: 401 })); + .mockResolvedValueOnce( + new Response(JSON.stringify({ authorization_urls: urls }), { + status: 401, + }), + ) + .mockResolvedValueOnce(new Response('unauthorized', { status: 401 })) + .mockResolvedValueOnce( + new Response(JSON.stringify({ authorization_urls: { github: 'javascript:alert(1)' } }), { status: 401 }), + ); await connectRemoteMcp({ ...baseParams(), url: endpoint, knownTransportType: 'streamable-http', fetch }); const transportFetch = mockTransportFetches[0]; if (transportFetch === undefined) { throw new Error('expected transport fetch'); } - await expect(transportFetch(endpoint, { method: 'POST' })).rejects.toMatchObject({ - name: 'McpUnauthorizedError', + await expect(transportFetch(endpoint, {})).rejects.toMatchObject({ + name: 'McpAuthorizationRequiredError', statusCode: 401, - body: '{"reason":"consent"}', + authorizationUrls: urls, }); - await expect(transportFetch(endpoint, { method: 'GET' })).resolves.toMatchObject({ status: 401 }); + await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); + await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); }); }); diff --git a/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts b/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts index 77b328aa48..c67f045ae6 100644 --- a/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts +++ b/packages/trueforge-core/tests/core/mcp/mcpResponseSizeLimit.test.ts @@ -33,4 +33,15 @@ describe('withMaxResponseBytes', () => { expect((await response.arrayBuffer()).byteLength).toBe(80); }); + + it('caps a non-OK GET text/event-stream body', async () => { + const fetchFn = withMaxResponseBytes( + async () => + new Response(streamOf(new Uint8Array(80)), { status: 401, headers: { 'content-type': 'text/event-stream' } }), + 50, + ); + const response = await fetchFn('https://mcp.example.com', { method: 'GET' }); + + await expect(response.arrayBuffer()).rejects.toThrow(/exceeded max 50 bytes/); + }); }); diff --git a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts index 01b821b959..837162dd48 100644 --- a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts +++ b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts @@ -1,6 +1,6 @@ import type { Logger } from 'winston'; import { McpConnectionError, RemoteMCP, ToolSet } from '../../../src/core'; -import { McpUnauthorizedError } from '../../../src/core/errors'; +import { McpAuthorizationRequiredError } from '../../../src/core/errors'; import { isApprovalRequiredResponse, isCallToolResponseResult, @@ -474,32 +474,27 @@ describe('RemoteMCP + ToolSet', () => { expect(state.callToolCalls).toBe(0); }); - it('returns authRequired when the resolver reads consent details from the 401 body, without falling back', async () => { - installFakeConnection({ connectError: new McpUnauthorizedError('consent body') }); - const authRequired: MCPAuthRequired = { - servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }], - }; - const onUnauthorized = jest.fn(() => Promise.resolve({ headers: {} })); - const authRequiredFromBody = jest.fn((body: string) => (body === 'consent body' ? authRequired : undefined)); - const server = makeServer({ - headers: () => Promise.resolve({ headers: {}, onUnauthorized, authRequiredFromBody }), + it('returns authRequired from a 401 that carries authorization URLs, without falling back', async () => { + installFakeConnection({ + connectError: new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), }); + const onUnauthorized = jest.fn(() => Promise.resolve({ headers: {} })); + const server = makeServer({ headers: () => Promise.resolve({ headers: {}, onUnauthorized }) }); - await expect(server.listTools()).resolves.toEqual({ authRequired }); + await expect(server.listTools()).resolves.toEqual({ + authRequired: { servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }] }, + }); expect(onUnauthorized).not.toHaveBeenCalled(); }); it('drops the rejected connection when a call returns consent URLs, so the next call reconnects', async () => { const state = installFakeConnection({ sessionId: 'sess-1' }); - const authRequired: MCPAuthRequired = { - servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }], - }; - const server = makeServer({ - headers: () => Promise.resolve({ headers: {}, authRequiredFromBody: () => authRequired }), - }); + const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); await server.listTools(); - state.queueCallToolError(new McpUnauthorizedError('consent body')); + state.queueCallToolError( + new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), + ); await expect(server.callTool({ name: 'read_thing', arguments: {} })).resolves.toHaveProperty('authRequired'); await server.callTool({ name: 'read_thing', arguments: {} }); diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index df75784766..ee7bdc2adf 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -1,11 +1,5 @@ -import { - McpConnectionError, - type MCPAuthRequired, - type RemoteMcpHeaders, - type ResolveHeadersResult, -} from '@truefoundry/trueforge-core/core'; +import { McpConnectionError, type RemoteMcpHeaders, type ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; import type { Logger } from 'winston'; -import { z } from 'zod'; import type { RequestContext, RequestSubject } from '../auth/identity'; import { safeReturnTo } from '../auth/safeReturnTo'; import { getPublicUiBasePath } from '../config'; @@ -65,27 +59,6 @@ export type TrueFoundryMcpApiClient = Pick< /** Per-server upstream headers the TrueFoundry gateway applies after its own caller auth. */ const X_TFY_MCP_HEADERS = 'x-tfy-mcp-headers'; -/** The gateway's 401 when outbound auth is missing: consent URLs keyed by server name. */ -const GatewayAuthRequiredBodySchema = z.object({ - authorization_urls: z.record(z.string(), z.url({ protocol: /^https?$/ })), -}); - -function gatewayAuthRequired(input: { name: string; body: string }): MCPAuthRequired | undefined { - let decoded: unknown; - try { - decoded = JSON.parse(input.body); - } catch { - return undefined; - } - const parsed = GatewayAuthRequiredBodySchema.safeParse(decoded); - if (!parsed.success) { - return undefined; - } - const urls = parsed.data.authorization_urls; - const authUrl = urls[input.name] ?? Object.values(urls)[0]; - return authUrl === undefined ? undefined : { servers: [{ id: input.name, name: input.name, auth_url: authUrl }] }; -} - /** * Absolute FE landing for the upstream authorize `redirectURL`. * Origin from the tenant control-plane URL; UI path prefix from process @@ -147,7 +120,6 @@ export class TrueFoundryMcpServerStore implements IMcpServ }): RemoteMcpHeaders { const { record, turnMetadata } = input; const callerHeaders = this.#perServerHeaders[record.name]; - const authRequiredFromBody = (body: string) => gatewayAuthRequired({ name: record.name, body }); const gatewayBase = async (): Promise> => ({ ...gatewayHeaders(await this.#resolveGatewayAuthorization()), ...gatewayMetadataHeadersForTurn(turnMetadata), @@ -156,12 +128,10 @@ export class TrueFoundryMcpServerStore implements IMcpServ // The gateway fans a virtual MCP's nested map out to its servers itself. return async () => ({ headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, - authRequiredFromBody, }); } const viaGateway = async (): Promise => ({ headers: { ...withoutAuthorization(callerHeaders), ...(await gatewayBase()) }, - authRequiredFromBody, }); if (!hasAuthorizationHeader(callerHeaders)) { return viaGateway; @@ -170,7 +140,6 @@ export class TrueFoundryMcpServerStore implements IMcpServ return async () => ({ headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, onUnauthorized: viaGateway, - authRequiredFromBody, }); } diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index 67ecf3965d..e0d6c4625e 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -303,24 +303,10 @@ describe('TrueFoundryMcpServerStore', () => { const { store, client } = createStore(); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, - authRequiredFromBody: expect.any(Function), }); expect(client.getMcpAuthorize).not.toHaveBeenCalled(); }); - it('reads the consent URL from a gateway 401 body', async () => { - const resolved = await invoke(createStore().store); - if (!('headers' in resolved) || resolved.authRequiredFromBody === undefined) { - throw new Error('expected a 401 body reader'); - } - const body = (url: string) => JSON.stringify({ authorization_urls: { github: url } }); - expect(resolved.authRequiredFromBody(body('https://consent.example/authorize'))).toEqual({ - servers: [{ id: 'github', name: 'github', auth_url: 'https://consent.example/authorize' }], - }); - expect(resolved.authRequiredFromBody(body('javascript:alert(1)'))).toBeUndefined(); - expect(resolved.authRequiredFromBody('unauthorized')).toBeUndefined(); - }); - it('merges turn metadata after auth headers', async () => { const { store } = createStore(); const headers = store.resolveInvokeHeaders({ @@ -343,7 +329,6 @@ describe('TrueFoundryMcpServerStore', () => { [X_TFY_METADATA]: expect.any(String), [X_TFY_CONVERSATION_ID]: 'sess-1', }, - authRequiredFromBody: expect.any(Function), }); if (!('headers' in resolved) || resolved.headers === undefined) { throw new Error('expected invoke headers'); @@ -358,7 +343,6 @@ describe('TrueFoundryMcpServerStore', () => { Authorization: `Bearer ${ACCESS_TOKEN}`, [ACTOR_AUTHORIZATION_HEADER]: `Bearer ${ACTOR_TOKEN}`, }, - authRequiredFromBody: expect.any(Function), }); expect(client.getMcpServerByName).not.toHaveBeenCalled(); expect(client.vendToken).toHaveBeenCalledTimes(1); @@ -368,7 +352,6 @@ describe('TrueFoundryMcpServerStore', () => { const { store, client } = createStore({ agent: AGENT, userCredential: null }); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${SUBJECT_TOKEN}` }, - authRequiredFromBody: expect.any(Function), }); expect(client.getMcpServerByName).not.toHaveBeenCalled(); }); @@ -517,7 +500,6 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { } await expect(resolved.onUnauthorized()).resolves.toEqual({ headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, - authRequiredFromBody: expect.any(Function), }); expect(getMcpAuthorize).not.toHaveBeenCalled(); }); From f7199c7c8d6a5d0f5ded3cf7432e33c3d57dab19 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 06:25:04 +0000 Subject: [PATCH 17/20] Revert "refactor: move per-server MCP headers out of src/mcp and keep TrueFoundry details out of general tests (AGE-2723)" This reverts commit 5f8755e8b2bbd29cebaf13ee0ce38af6fe70e6c5. Co-authored-by: Rutvij Menavlikar --- .../tests/core/mcp/connectRemoteMcp.test.ts | 2 +- packages/trueforge/src/apis/turns.ts | 2 +- .../src/db/McpServerWithAuthStore.ts | 2 +- .../{runtime => mcp}/perServerMcpHeaders.ts | 0 packages/trueforge/src/resolvers.ts | 2 +- .../truefoundry/TrueFoundryMcpServerStore.ts | 2 +- .../unit/mcp/perServerMcpHeaders.test.ts | 252 ++++++++++++++++++ .../unit/runtime/perServerMcpHeaders.test.ts | 99 ------- .../TrueFoundryMcpServerStore.test.ts | 148 ---------- 9 files changed, 257 insertions(+), 252 deletions(-) rename packages/trueforge/src/{runtime => mcp}/perServerMcpHeaders.ts (100%) create mode 100644 packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts delete mode 100644 packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts diff --git a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts index 20a021d821..e70b68cc4a 100644 --- a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts +++ b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts @@ -121,7 +121,7 @@ describe('connectRemoteMcp custom fetch', () => { const fetch = jest .fn, Parameters>() .mockResolvedValueOnce( - new Response(JSON.stringify({ authorization_urls: urls }), { + new Response(JSON.stringify({ error: { type: 'McpAuthRequiredError' }, authorization_urls: urls }), { status: 401, }), ) diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts index 880d9332ad..c12aec4426 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -87,6 +87,7 @@ import type { TurnMetadata } from '../db/turnMetadata'; import type { IWebSearchProviderStore } from '../db/webSearchProviderStore'; import { createTlsFetch, normalizeTlsUrl } from '../http/tls'; import { currentLogContext, runWithLogContext, updateLogContext } from '../logger'; +import { X_TFG_MCP_HEADERS } from '../mcp/perServerMcpHeaders'; import { TRUEFORGE_SESSION_HEADER, X_TFG_TRUEFORGE_MCP } from '../mcp/server/trueforge/headers'; import type { TenantMemoryDeps } from '../memory/providers'; import { resolveSessionMemory } from '../memory/sessionMemory'; @@ -110,7 +111,6 @@ import { type EventSubscriptionPollOptions, type EventSubscriptionRegistry, } from '../runtime/event-subscription'; -import { X_TFG_MCP_HEADERS } from '../runtime/perServerMcpHeaders'; import { validateSandboxFilePath } from '../runtime/sandboxFilePath'; import { buildTurnSandbox, diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 4951c66e4f..c76f519c16 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -7,7 +7,7 @@ import { overrideHeaders, withoutAuthorization, type PerServerMcpHeaders, -} from '../runtime/perServerMcpHeaders'; +} from '../mcp/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { McpServerNotFoundError, diff --git a/packages/trueforge/src/runtime/perServerMcpHeaders.ts b/packages/trueforge/src/mcp/perServerMcpHeaders.ts similarity index 100% rename from packages/trueforge/src/runtime/perServerMcpHeaders.ts rename to packages/trueforge/src/mcp/perServerMcpHeaders.ts diff --git a/packages/trueforge/src/resolvers.ts b/packages/trueforge/src/resolvers.ts index 840e0d8b05..27a2174f9c 100644 --- a/packages/trueforge/src/resolvers.ts +++ b/packages/trueforge/src/resolvers.ts @@ -22,8 +22,8 @@ import type { ISandboxProviderStore } from './db/sandboxProviderStore'; import type { ISkillStore } from './db/skillStore'; import type { IWebSearchProviderStore } from './db/webSearchProviderStore'; import type { IOAuthTokenStore } from './mcp/auth/types'; +import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './mcp/perServerMcpHeaders'; import type { MemoryProviderCache, TenantMemoryDeps } from './memory/providers'; -import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './runtime/perServerMcpHeaders'; import { headerValue } from './truefoundry/gatewayMetadata'; import { InlineMcpServerStore } from './truefoundry/InlineMcpServerStore'; import { InlineModelProviderStore } from './truefoundry/InlineModelProviderStore'; diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index ee7bdc2adf..57048ca88e 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -24,7 +24,7 @@ import { isVirtualMcpHeaders, withoutAuthorization, type PerServerMcpHeaders, -} from '../runtime/perServerMcpHeaders'; +} from '../mcp/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts new file mode 100644 index 0000000000..807c3e45ad --- /dev/null +++ b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts @@ -0,0 +1,252 @@ +import { HTTPException } from 'hono/http-exception'; +import { createLogger } from 'winston'; +import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore'; +import type { McpServerRecord } from '../../../src/db/mcpServerStore'; +import { createSqliteDb } from '../../../src/db/sqlite/client'; +import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; +import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; +import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/mcp/perServerMcpHeaders'; +import { + TrueFoundryMcpServerStore, + type TrueFoundryMcpApiClient, +} from '../../../src/truefoundry/TrueFoundryMcpServerStore'; +import { createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; + +function unusedClient(): TrueFoundryMcpApiClient { + const unused = (): Promise => Promise.reject(new Error('unused')); + return { + getMcpServerByName: async ({ name }) => ({ + id: name, + name, + proxyUrl: `https://gateway.example/mcp-server/${name}`, + createdAt: '2026-01-15T12:00:00.000Z', + updatedAt: '2026-01-16T12:00:00.000Z', + manifest: { description: name }, + }), + listMcpServers: unused, + listGatewayInstallations: async () => [{ isDefault: true, manifest: { url: 'https://gateway.example' } }], + getMcpAuthorize: async () => ({ status: 'authenticated' }), + getMcpAuthStatus: unused, + deleteMcpAuth: unused, + vendToken: () => Promise.resolve({ subjectToken: 'caller-token', actorToken: 'caller-token' }), + getTenantControlPlaneUrl: async () => 'https://tenant.example.com', + }; +} + +const record = (name: string): McpServerRecord => ({ + id: name, + tenant_id: 'default', + name, + manifest: { + type: 'truefoundry', + name, + url: `https://gateway.example/mcp-server/${name}`, + description: name, + }, + created_at: '2026-01-15T12:00:00.000Z', + updated_at: '2026-01-16T12:00:00.000Z', +}); + +const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => + new TrueFoundryMcpServerStore({ + client: unusedClient(), + requestContext: createTrueFoundryRequestContext({ + tenant_id: 'default', + subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, + roles: [], + user_credential: 'caller-token', + }), + agent: undefined, + logger: createLogger({ silent: true }), + perServerHeaders, + }); + +async function invokeHeaders(store: TrueFoundryMcpServerStore, name: string): Promise> { + const resolve = store.resolveInvokeHeaders({ record: record(name), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver so the token is read at connect time'); + } + const resolved = await resolve(); + if (!('headers' in resolved)) { + throw new Error('expected headers rather than an auth-required signal'); + } + return resolved.headers; +} + +describe('parsePerServerMcpHeaders', () => { + it('parses a header map per server name', () => { + const raw = JSON.stringify({ 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' } }); + + expect(parsePerServerMcpHeaders(raw)).toEqual({ + 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, + }); + }); + + it.each([ + ['not json', 'not-json'], + ['an array', '[]'], + ['a scalar', '"nope"'], + ['a server mapped to a string', JSON.stringify({ 'tfy-platform-mcp': 'Bearer user' })], + ['a header mapped to a number', JSON.stringify({ 'tfy-platform-mcp': { 'x-h': 1 } })], + ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], + ['a managed session header', JSON.stringify({ 'tfy-platform-mcp': { 'Mcp-Session-Id': 'other' } })], + ['a managed protocol header', JSON.stringify({ 'tfy-platform-mcp': { 'mcp-protocol-version': '2025-06-18' } })], + ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { + expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); + }); + + it('keeps the parse failure as the cause, so a bad header can be debugged', () => { + expect(() => parsePerServerMcpHeaders('not-json')).toThrow( + expect.objectContaining({ cause: expect.any(SyntaxError) }), + ); + }); +}); + +describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { + it('passes a virtual MCP entry through as x-tfy-mcp-headers', async () => { + const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; + const raw = JSON.stringify({ 'virtual-mcp': entry }); + + const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); + }); + + it('sends only the gateway Bearer when a server has no override', async () => { + await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ + Authorization: 'Bearer caller-token', + }); + }); + + it("merges that server's override on top of the Bearer", async () => { + const headers = await invokeHeaders( + storeWith({ + 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, + }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}', + }); + }); + + it('gives one server nothing of another, so an identity cannot reach the wrong upstream', async () => { + const headers = await invokeHeaders( + storeWith({ + 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, + }), + 'tfy-pylon-mcp', + ); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); + }); + + it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( + 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', + async name => { + const headers = await invokeHeaders( + storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), + }); + }, + ); + + it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { + const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); + const store = new TrueFoundryMcpServerStore({ + client: { ...unusedClient(), getMcpAuthorize }, + requestContext: createTrueFoundryRequestContext({ + tenant_id: 'default', + subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, + roles: [], + user_credential: 'caller-token', + }), + agent: undefined, + logger: createLogger({ silent: true }), + perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user', 'X-End-User': 'alice' } }, + }); + const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + + const resolved = await resolve(); + if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { + throw new Error('expected a fallback to the gateway auth'); + } + await expect(resolved.onUnauthorized()).resolves.toEqual({ + headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, + }); + expect(getMcpAuthorize).not.toHaveBeenCalled(); + }); +}); + +describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { + const ossRecord: McpServerRecord = { + id: 'oauth-mcp', + tenant_id: 'default', + name: 'oauth-mcp', + manifest: { + type: 'remote', + name: 'oauth-mcp', + url: 'https://mcp.example/mcp', + description: '', + auth: { type: 'dcr' }, + }, + created_at: '2026-01-15T12:00:00.000Z', + updated_at: '2026-01-16T12:00:00.000Z', + }; + + it('forwards a caller Authorization directly instead of starting the DCR consent flow', async () => { + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, + }); + + const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); + await db.destroy(); + }); + + it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { + const entry = { serv1: { Authorization: 'Bearer a' } }; + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'virtual-mcp': entry }, + }); + const record: McpServerRecord = { + ...ossRecord, + name: 'virtual-mcp', + manifest: { + ...ossRecord.manifest, + name: 'virtual-mcp', + auth: { type: 'header', headers: { Authorization: 'Bearer gateway' } }, + }, + }; + + const resolve = store.resolveInvokeHeaders({ record, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toEqual({ + headers: { Authorization: 'Bearer gateway', serv1: JSON.stringify(entry.serv1) }, + }); + await db.destroy(); + }); +}); diff --git a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts deleted file mode 100644 index 75797526ad..0000000000 --- a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { HTTPException } from 'hono/http-exception'; -import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore'; -import type { McpServerRecord } from '../../../src/db/mcpServerStore'; -import { createSqliteDb } from '../../../src/db/sqlite/client'; -import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; -import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; -import { parsePerServerMcpHeaders } from '../../../src/runtime/perServerMcpHeaders'; - -describe('parsePerServerMcpHeaders', () => { - it('parses a header map per server name', () => { - const raw = JSON.stringify({ 'github-mcp': { Authorization: 'Bearer user' } }); - - expect(parsePerServerMcpHeaders(raw)).toEqual({ - 'github-mcp': { Authorization: 'Bearer user' }, - }); - }); - - it.each([ - ['not json', 'not-json'], - ['an array', '[]'], - ['a scalar', '"nope"'], - ['a server mapped to a string', JSON.stringify({ 'github-mcp': 'Bearer user' })], - ['a header mapped to a number', JSON.stringify({ 'github-mcp': { 'x-h': 1 } })], - ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], - ['a managed session header', JSON.stringify({ 'github-mcp': { 'Mcp-Session-Id': 'other' } })], - ['a managed protocol header', JSON.stringify({ 'github-mcp': { 'mcp-protocol-version': '2025-06-18' } })], - ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { - expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); - }); - - it('keeps the parse failure as the cause, so a bad header can be debugged', () => { - expect(() => parsePerServerMcpHeaders('not-json')).toThrow( - expect.objectContaining({ cause: expect.any(SyntaxError) }), - ); - }); -}); - -describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { - const ossRecord: McpServerRecord = { - id: 'oauth-mcp', - tenant_id: 'default', - name: 'oauth-mcp', - manifest: { - type: 'remote', - name: 'oauth-mcp', - url: 'https://mcp.example/mcp', - description: '', - auth: { type: 'dcr' }, - }, - created_at: '2026-01-15T12:00:00.000Z', - updated_at: '2026-01-16T12:00:00.000Z', - }; - - it('forwards a caller Authorization directly instead of starting the DCR consent flow', async () => { - const db = createSqliteDb(':memory:'); - const store = new McpServerWithAuthStore({ - store: new SqliteMcpServerStore(db), - tokenStore: new SqliteOAuthTokenStore(db), - clientName: 'test-client', - perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, - }); - - const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); - await db.destroy(); - }); - - it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { - const entry = { serv1: { Authorization: 'Bearer a' } }; - const db = createSqliteDb(':memory:'); - const store = new McpServerWithAuthStore({ - store: new SqliteMcpServerStore(db), - tokenStore: new SqliteOAuthTokenStore(db), - clientName: 'test-client', - perServerHeaders: { 'virtual-mcp': entry }, - }); - const record: McpServerRecord = { - ...ossRecord, - name: 'virtual-mcp', - manifest: { - ...ossRecord.manifest, - name: 'virtual-mcp', - auth: { type: 'header', headers: { Authorization: 'Bearer server' } }, - }, - }; - - const resolve = store.resolveInvokeHeaders({ record, userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - await expect(resolve()).resolves.toEqual({ - headers: { Authorization: 'Bearer server', serv1: JSON.stringify(entry.serv1) }, - }); - await db.destroy(); - }); -}); diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index e0d6c4625e..7101c06e3f 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -2,7 +2,6 @@ import { AgentSpecSchema } from '@truefoundry/trueforge-core/agent-session'; import { createLogger } from 'winston'; import type { AgentRecord } from '../../../src/db/agentStore'; import { McpServerNotFoundError, type McpServerRecord } from '../../../src/db/mcpServerStore'; -import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/runtime/perServerMcpHeaders'; import { ACTOR_AUTHORIZATION_HEADER, createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; import { X_TFY_CONVERSATION_ID, X_TFY_METADATA } from '../../../src/truefoundry/gatewayMetadata'; import { MCP_PROXY_BASE_URL_TEMPLATE } from '../../../src/truefoundry/mapSfyMcpServers'; @@ -357,150 +356,3 @@ describe('TrueFoundryMcpServerStore', () => { }); }); }); - -function unusedClient(): TrueFoundryMcpApiClient { - const unused = (): Promise => Promise.reject(new Error('unused')); - return { - getMcpServerByName: async ({ name }) => ({ - id: name, - name, - proxyUrl: `https://gateway.example/mcp-server/${name}`, - createdAt: '2026-01-15T12:00:00.000Z', - updatedAt: '2026-01-16T12:00:00.000Z', - manifest: { description: name }, - }), - listMcpServers: unused, - listGatewayInstallations: async () => [{ isDefault: true, manifest: { url: 'https://gateway.example' } }], - getMcpAuthorize: async () => ({ status: 'authenticated' }), - getMcpAuthStatus: unused, - deleteMcpAuth: unused, - vendToken: () => Promise.resolve({ subjectToken: 'caller-token', actorToken: 'caller-token' }), - getTenantControlPlaneUrl: async () => 'https://tenant.example.com', - }; -} - -const record = (name: string): McpServerRecord => ({ - id: name, - tenant_id: 'default', - name, - manifest: { - type: 'truefoundry', - name, - url: `https://gateway.example/mcp-server/${name}`, - description: name, - }, - created_at: '2026-01-15T12:00:00.000Z', - updated_at: '2026-01-16T12:00:00.000Z', -}); - -const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => - new TrueFoundryMcpServerStore({ - client: unusedClient(), - requestContext: createTrueFoundryRequestContext({ - tenant_id: 'default', - subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, - roles: [], - user_credential: 'caller-token', - }), - agent: undefined, - logger: createLogger({ silent: true }), - perServerHeaders, - }); - -async function invokeHeaders(store: TrueFoundryMcpServerStore, name: string): Promise> { - const resolve = store.resolveInvokeHeaders({ record: record(name), userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver so the token is read at connect time'); - } - const resolved = await resolve(); - if (!('headers' in resolved)) { - throw new Error('expected headers rather than an auth-required signal'); - } - return resolved.headers; -} - -describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { - it('passes a virtual MCP entry through as x-tfy-mcp-headers', async () => { - const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; - const raw = JSON.stringify({ 'virtual-mcp': entry }); - - const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); - - expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); - }); - - it('sends only the gateway Bearer when a server has no override', async () => { - await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ - Authorization: 'Bearer caller-token', - }); - }); - - it("merges that server's override on top of the Bearer", async () => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }), - 'tfy-platform-mcp', - ); - - expect(headers).toEqual({ - Authorization: 'Bearer caller-token', - 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}', - }); - }); - - it('gives one server nothing of another, so an identity cannot reach the wrong upstream', async () => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }), - 'tfy-pylon-mcp', - ); - - expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); - }); - - it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( - 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', - async name => { - const headers = await invokeHeaders( - storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), - 'tfy-platform-mcp', - ); - - expect(headers).toEqual({ - Authorization: 'Bearer caller-token', - 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), - }); - }, - ); - - it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { - const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); - const store = new TrueFoundryMcpServerStore({ - client: { ...unusedClient(), getMcpAuthorize }, - requestContext: createTrueFoundryRequestContext({ - tenant_id: 'default', - subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, - roles: [], - user_credential: 'caller-token', - }), - agent: undefined, - logger: createLogger({ silent: true }), - perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user', 'X-End-User': 'alice' } }, - }); - const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - - const resolved = await resolve(); - if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { - throw new Error('expected a fallback to the gateway auth'); - } - await expect(resolved.onUnauthorized()).resolves.toEqual({ - headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, - }); - expect(getMcpAuthorize).not.toHaveBeenCalled(); - }); -}); From a7c46ddeed8d04b9148410b758ba047603958498 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 06:26:16 +0000 Subject: [PATCH 18/20] refactor: move per-server MCP headers to src/runtime and split TrueFoundry tests out (AGE-2723) Co-authored-by: Rutvij Menavlikar --- packages/trueforge/src/apis/turns.ts | 2 +- .../src/db/McpServerWithAuthStore.ts | 2 +- packages/trueforge/src/resolvers.ts | 2 +- .../{mcp => runtime}/perServerMcpHeaders.ts | 0 .../truefoundry/TrueFoundryMcpServerStore.ts | 2 +- .../unit/mcp/perServerMcpHeaders.test.ts | 252 ------------------ .../unit/runtime/perServerMcpHeaders.test.ts | 99 +++++++ .../TrueFoundryMcpServerStore.test.ts | 148 ++++++++++ 8 files changed, 251 insertions(+), 256 deletions(-) rename packages/trueforge/src/{mcp => runtime}/perServerMcpHeaders.ts (100%) delete mode 100644 packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts create mode 100644 packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts index c12aec4426..880d9332ad 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -87,7 +87,6 @@ import type { TurnMetadata } from '../db/turnMetadata'; import type { IWebSearchProviderStore } from '../db/webSearchProviderStore'; import { createTlsFetch, normalizeTlsUrl } from '../http/tls'; import { currentLogContext, runWithLogContext, updateLogContext } from '../logger'; -import { X_TFG_MCP_HEADERS } from '../mcp/perServerMcpHeaders'; import { TRUEFORGE_SESSION_HEADER, X_TFG_TRUEFORGE_MCP } from '../mcp/server/trueforge/headers'; import type { TenantMemoryDeps } from '../memory/providers'; import { resolveSessionMemory } from '../memory/sessionMemory'; @@ -111,6 +110,7 @@ import { type EventSubscriptionPollOptions, type EventSubscriptionRegistry, } from '../runtime/event-subscription'; +import { X_TFG_MCP_HEADERS } from '../runtime/perServerMcpHeaders'; import { validateSandboxFilePath } from '../runtime/sandboxFilePath'; import { buildTurnSandbox, diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index c76f519c16..4951c66e4f 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -7,7 +7,7 @@ import { overrideHeaders, withoutAuthorization, type PerServerMcpHeaders, -} from '../mcp/perServerMcpHeaders'; +} from '../runtime/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { McpServerNotFoundError, diff --git a/packages/trueforge/src/resolvers.ts b/packages/trueforge/src/resolvers.ts index 27a2174f9c..840e0d8b05 100644 --- a/packages/trueforge/src/resolvers.ts +++ b/packages/trueforge/src/resolvers.ts @@ -22,8 +22,8 @@ import type { ISandboxProviderStore } from './db/sandboxProviderStore'; import type { ISkillStore } from './db/skillStore'; import type { IWebSearchProviderStore } from './db/webSearchProviderStore'; import type { IOAuthTokenStore } from './mcp/auth/types'; -import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './mcp/perServerMcpHeaders'; import type { MemoryProviderCache, TenantMemoryDeps } from './memory/providers'; +import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './runtime/perServerMcpHeaders'; import { headerValue } from './truefoundry/gatewayMetadata'; import { InlineMcpServerStore } from './truefoundry/InlineMcpServerStore'; import { InlineModelProviderStore } from './truefoundry/InlineModelProviderStore'; diff --git a/packages/trueforge/src/mcp/perServerMcpHeaders.ts b/packages/trueforge/src/runtime/perServerMcpHeaders.ts similarity index 100% rename from packages/trueforge/src/mcp/perServerMcpHeaders.ts rename to packages/trueforge/src/runtime/perServerMcpHeaders.ts diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 57048ca88e..ee7bdc2adf 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -24,7 +24,7 @@ import { isVirtualMcpHeaders, withoutAuthorization, type PerServerMcpHeaders, -} from '../mcp/perServerMcpHeaders'; +} from '../runtime/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, diff --git a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts deleted file mode 100644 index 807c3e45ad..0000000000 --- a/packages/trueforge/tests/unit/mcp/perServerMcpHeaders.test.ts +++ /dev/null @@ -1,252 +0,0 @@ -import { HTTPException } from 'hono/http-exception'; -import { createLogger } from 'winston'; -import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore'; -import type { McpServerRecord } from '../../../src/db/mcpServerStore'; -import { createSqliteDb } from '../../../src/db/sqlite/client'; -import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; -import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; -import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/mcp/perServerMcpHeaders'; -import { - TrueFoundryMcpServerStore, - type TrueFoundryMcpApiClient, -} from '../../../src/truefoundry/TrueFoundryMcpServerStore'; -import { createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; - -function unusedClient(): TrueFoundryMcpApiClient { - const unused = (): Promise => Promise.reject(new Error('unused')); - return { - getMcpServerByName: async ({ name }) => ({ - id: name, - name, - proxyUrl: `https://gateway.example/mcp-server/${name}`, - createdAt: '2026-01-15T12:00:00.000Z', - updatedAt: '2026-01-16T12:00:00.000Z', - manifest: { description: name }, - }), - listMcpServers: unused, - listGatewayInstallations: async () => [{ isDefault: true, manifest: { url: 'https://gateway.example' } }], - getMcpAuthorize: async () => ({ status: 'authenticated' }), - getMcpAuthStatus: unused, - deleteMcpAuth: unused, - vendToken: () => Promise.resolve({ subjectToken: 'caller-token', actorToken: 'caller-token' }), - getTenantControlPlaneUrl: async () => 'https://tenant.example.com', - }; -} - -const record = (name: string): McpServerRecord => ({ - id: name, - tenant_id: 'default', - name, - manifest: { - type: 'truefoundry', - name, - url: `https://gateway.example/mcp-server/${name}`, - description: name, - }, - created_at: '2026-01-15T12:00:00.000Z', - updated_at: '2026-01-16T12:00:00.000Z', -}); - -const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => - new TrueFoundryMcpServerStore({ - client: unusedClient(), - requestContext: createTrueFoundryRequestContext({ - tenant_id: 'default', - subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, - roles: [], - user_credential: 'caller-token', - }), - agent: undefined, - logger: createLogger({ silent: true }), - perServerHeaders, - }); - -async function invokeHeaders(store: TrueFoundryMcpServerStore, name: string): Promise> { - const resolve = store.resolveInvokeHeaders({ record: record(name), userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver so the token is read at connect time'); - } - const resolved = await resolve(); - if (!('headers' in resolved)) { - throw new Error('expected headers rather than an auth-required signal'); - } - return resolved.headers; -} - -describe('parsePerServerMcpHeaders', () => { - it('parses a header map per server name', () => { - const raw = JSON.stringify({ 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' } }); - - expect(parsePerServerMcpHeaders(raw)).toEqual({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }); - }); - - it.each([ - ['not json', 'not-json'], - ['an array', '[]'], - ['a scalar', '"nope"'], - ['a server mapped to a string', JSON.stringify({ 'tfy-platform-mcp': 'Bearer user' })], - ['a header mapped to a number', JSON.stringify({ 'tfy-platform-mcp': { 'x-h': 1 } })], - ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], - ['a managed session header', JSON.stringify({ 'tfy-platform-mcp': { 'Mcp-Session-Id': 'other' } })], - ['a managed protocol header', JSON.stringify({ 'tfy-platform-mcp': { 'mcp-protocol-version': '2025-06-18' } })], - ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { - expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); - }); - - it('keeps the parse failure as the cause, so a bad header can be debugged', () => { - expect(() => parsePerServerMcpHeaders('not-json')).toThrow( - expect.objectContaining({ cause: expect.any(SyntaxError) }), - ); - }); -}); - -describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { - it('passes a virtual MCP entry through as x-tfy-mcp-headers', async () => { - const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; - const raw = JSON.stringify({ 'virtual-mcp': entry }); - - const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); - - expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); - }); - - it('sends only the gateway Bearer when a server has no override', async () => { - await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ - Authorization: 'Bearer caller-token', - }); - }); - - it("merges that server's override on top of the Bearer", async () => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }), - 'tfy-platform-mcp', - ); - - expect(headers).toEqual({ - Authorization: 'Bearer caller-token', - 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}', - }); - }); - - it('gives one server nothing of another, so an identity cannot reach the wrong upstream', async () => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, - }), - 'tfy-pylon-mcp', - ); - - expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); - }); - - it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( - 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', - async name => { - const headers = await invokeHeaders( - storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), - 'tfy-platform-mcp', - ); - - expect(headers).toEqual({ - Authorization: 'Bearer caller-token', - 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), - }); - }, - ); - - it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { - const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); - const store = new TrueFoundryMcpServerStore({ - client: { ...unusedClient(), getMcpAuthorize }, - requestContext: createTrueFoundryRequestContext({ - tenant_id: 'default', - subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, - roles: [], - user_credential: 'caller-token', - }), - agent: undefined, - logger: createLogger({ silent: true }), - perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user', 'X-End-User': 'alice' } }, - }); - const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - - const resolved = await resolve(); - if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { - throw new Error('expected a fallback to the gateway auth'); - } - await expect(resolved.onUnauthorized()).resolves.toEqual({ - headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, - }); - expect(getMcpAuthorize).not.toHaveBeenCalled(); - }); -}); - -describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { - const ossRecord: McpServerRecord = { - id: 'oauth-mcp', - tenant_id: 'default', - name: 'oauth-mcp', - manifest: { - type: 'remote', - name: 'oauth-mcp', - url: 'https://mcp.example/mcp', - description: '', - auth: { type: 'dcr' }, - }, - created_at: '2026-01-15T12:00:00.000Z', - updated_at: '2026-01-16T12:00:00.000Z', - }; - - it('forwards a caller Authorization directly instead of starting the DCR consent flow', async () => { - const db = createSqliteDb(':memory:'); - const store = new McpServerWithAuthStore({ - store: new SqliteMcpServerStore(db), - tokenStore: new SqliteOAuthTokenStore(db), - clientName: 'test-client', - perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, - }); - - const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); - await db.destroy(); - }); - - it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { - const entry = { serv1: { Authorization: 'Bearer a' } }; - const db = createSqliteDb(':memory:'); - const store = new McpServerWithAuthStore({ - store: new SqliteMcpServerStore(db), - tokenStore: new SqliteOAuthTokenStore(db), - clientName: 'test-client', - perServerHeaders: { 'virtual-mcp': entry }, - }); - const record: McpServerRecord = { - ...ossRecord, - name: 'virtual-mcp', - manifest: { - ...ossRecord.manifest, - name: 'virtual-mcp', - auth: { type: 'header', headers: { Authorization: 'Bearer gateway' } }, - }, - }; - - const resolve = store.resolveInvokeHeaders({ record, userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - await expect(resolve()).resolves.toEqual({ - headers: { Authorization: 'Bearer gateway', serv1: JSON.stringify(entry.serv1) }, - }); - await db.destroy(); - }); -}); diff --git a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts new file mode 100644 index 0000000000..75797526ad --- /dev/null +++ b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts @@ -0,0 +1,99 @@ +import { HTTPException } from 'hono/http-exception'; +import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore'; +import type { McpServerRecord } from '../../../src/db/mcpServerStore'; +import { createSqliteDb } from '../../../src/db/sqlite/client'; +import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore'; +import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore'; +import { parsePerServerMcpHeaders } from '../../../src/runtime/perServerMcpHeaders'; + +describe('parsePerServerMcpHeaders', () => { + it('parses a header map per server name', () => { + const raw = JSON.stringify({ 'github-mcp': { Authorization: 'Bearer user' } }); + + expect(parsePerServerMcpHeaders(raw)).toEqual({ + 'github-mcp': { Authorization: 'Bearer user' }, + }); + }); + + it.each([ + ['not json', 'not-json'], + ['an array', '[]'], + ['a scalar', '"nope"'], + ['a server mapped to a string', JSON.stringify({ 'github-mcp': 'Bearer user' })], + ['a header mapped to a number', JSON.stringify({ 'github-mcp': { 'x-h': 1 } })], + ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], + ['a managed session header', JSON.stringify({ 'github-mcp': { 'Mcp-Session-Id': 'other' } })], + ['a managed protocol header', JSON.stringify({ 'github-mcp': { 'mcp-protocol-version': '2025-06-18' } })], + ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { + expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); + }); + + it('keeps the parse failure as the cause, so a bad header can be debugged', () => { + expect(() => parsePerServerMcpHeaders('not-json')).toThrow( + expect.objectContaining({ cause: expect.any(SyntaxError) }), + ); + }); +}); + +describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { + const ossRecord: McpServerRecord = { + id: 'oauth-mcp', + tenant_id: 'default', + name: 'oauth-mcp', + manifest: { + type: 'remote', + name: 'oauth-mcp', + url: 'https://mcp.example/mcp', + description: '', + auth: { type: 'dcr' }, + }, + created_at: '2026-01-15T12:00:00.000Z', + updated_at: '2026-01-16T12:00:00.000Z', + }; + + it('forwards a caller Authorization directly instead of starting the DCR consent flow', async () => { + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, + }); + + const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); + await db.destroy(); + }); + + it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { + const entry = { serv1: { Authorization: 'Bearer a' } }; + const db = createSqliteDb(':memory:'); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore: new SqliteOAuthTokenStore(db), + clientName: 'test-client', + perServerHeaders: { 'virtual-mcp': entry }, + }); + const record: McpServerRecord = { + ...ossRecord, + name: 'virtual-mcp', + manifest: { + ...ossRecord.manifest, + name: 'virtual-mcp', + auth: { type: 'header', headers: { Authorization: 'Bearer server' } }, + }, + }; + + const resolve = store.resolveInvokeHeaders({ record, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toEqual({ + headers: { Authorization: 'Bearer server', serv1: JSON.stringify(entry.serv1) }, + }); + await db.destroy(); + }); +}); diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index 7101c06e3f..e0d6c4625e 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -2,6 +2,7 @@ import { AgentSpecSchema } from '@truefoundry/trueforge-core/agent-session'; import { createLogger } from 'winston'; import type { AgentRecord } from '../../../src/db/agentStore'; import { McpServerNotFoundError, type McpServerRecord } from '../../../src/db/mcpServerStore'; +import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/runtime/perServerMcpHeaders'; import { ACTOR_AUTHORIZATION_HEADER, createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; import { X_TFY_CONVERSATION_ID, X_TFY_METADATA } from '../../../src/truefoundry/gatewayMetadata'; import { MCP_PROXY_BASE_URL_TEMPLATE } from '../../../src/truefoundry/mapSfyMcpServers'; @@ -356,3 +357,150 @@ describe('TrueFoundryMcpServerStore', () => { }); }); }); + +function unusedClient(): TrueFoundryMcpApiClient { + const unused = (): Promise => Promise.reject(new Error('unused')); + return { + getMcpServerByName: async ({ name }) => ({ + id: name, + name, + proxyUrl: `https://gateway.example/mcp-server/${name}`, + createdAt: '2026-01-15T12:00:00.000Z', + updatedAt: '2026-01-16T12:00:00.000Z', + manifest: { description: name }, + }), + listMcpServers: unused, + listGatewayInstallations: async () => [{ isDefault: true, manifest: { url: 'https://gateway.example' } }], + getMcpAuthorize: async () => ({ status: 'authenticated' }), + getMcpAuthStatus: unused, + deleteMcpAuth: unused, + vendToken: () => Promise.resolve({ subjectToken: 'caller-token', actorToken: 'caller-token' }), + getTenantControlPlaneUrl: async () => 'https://tenant.example.com', + }; +} + +const record = (name: string): McpServerRecord => ({ + id: name, + tenant_id: 'default', + name, + manifest: { + type: 'truefoundry', + name, + url: `https://gateway.example/mcp-server/${name}`, + description: name, + }, + created_at: '2026-01-15T12:00:00.000Z', + updated_at: '2026-01-16T12:00:00.000Z', +}); + +const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => + new TrueFoundryMcpServerStore({ + client: unusedClient(), + requestContext: createTrueFoundryRequestContext({ + tenant_id: 'default', + subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, + roles: [], + user_credential: 'caller-token', + }), + agent: undefined, + logger: createLogger({ silent: true }), + perServerHeaders, + }); + +async function invokeHeaders(store: TrueFoundryMcpServerStore, name: string): Promise> { + const resolve = store.resolveInvokeHeaders({ record: record(name), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver so the token is read at connect time'); + } + const resolved = await resolve(); + if (!('headers' in resolved)) { + throw new Error('expected headers rather than an auth-required signal'); + } + return resolved.headers; +} + +describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { + it('passes a virtual MCP entry through as x-tfy-mcp-headers', async () => { + const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; + const raw = JSON.stringify({ 'virtual-mcp': entry }); + + const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); + }); + + it('sends only the gateway Bearer when a server has no override', async () => { + await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ + Authorization: 'Bearer caller-token', + }); + }); + + it("merges that server's override on top of the Bearer", async () => { + const headers = await invokeHeaders( + storeWith({ + 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, + }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}', + }); + }); + + it('gives one server nothing of another, so an identity cannot reach the wrong upstream', async () => { + const headers = await invokeHeaders( + storeWith({ + 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, + }), + 'tfy-pylon-mcp', + ); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token' }); + }); + + it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( + 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', + async name => { + const headers = await invokeHeaders( + storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), + }); + }, + ); + + it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { + const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); + const store = new TrueFoundryMcpServerStore({ + client: { ...unusedClient(), getMcpAuthorize }, + requestContext: createTrueFoundryRequestContext({ + tenant_id: 'default', + subject: { id: 'user-1', type: 'user', display_name: 'user-1' }, + roles: [], + user_credential: 'caller-token', + }), + agent: undefined, + logger: createLogger({ silent: true }), + perServerHeaders: { 'tfy-platform-mcp': { Authorization: 'Bearer user', 'X-End-User': 'alice' } }, + }); + const resolve = store.resolveInvokeHeaders({ record: record('tfy-platform-mcp'), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + + const resolved = await resolve(); + if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { + throw new Error('expected a fallback to the gateway auth'); + } + await expect(resolved.onUnauthorized()).resolves.toEqual({ + headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, + }); + expect(getMcpAuthorize).not.toHaveBeenCalled(); + }); +}); From 39ed31a621625255afb23757de49be6b08e0a78e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 11:57:05 +0000 Subject: [PATCH 19/20] refactor: accept virtual MCP header entries only in TrueFoundry mode and move caller-header merging out of db/ - Open-source x-tfg-mcp-headers is a flat map per server; nested virtual MCP entries parse only in TrueFoundry mode (truefoundry/virtualMcpHeaders.ts). - McpServerWithAuthStore supplies its registered and configured headers; runtime/perServerMcpHeaders.ts withCallerMcpHeaders merges caller headers over them. - Rename override helpers: overrideHeaders -> mergeCallerHeaders, resolveWithOverride -> resolveWithCallerHeaders. Co-authored-by: Rutvij Menavlikar --- .changeset/x-tfg-mcp-headers-auth.md | 2 +- .../src/db/McpServerWithAuthStore.ts | 48 ++---------- packages/trueforge/src/resolvers.ts | 15 ++-- .../src/runtime/perServerMcpHeaders.ts | 75 ++++++++++++++----- .../truefoundry/TrueFoundryMcpServerStore.ts | 14 ++-- .../src/truefoundry/virtualMcpHeaders.ts | 22 ++++++ .../unit/runtime/perServerMcpHeaders.test.ts | 15 ++-- .../TrueFoundryMcpServerStore.test.ts | 15 ++-- .../truefoundry/virtualMcpHeaders.test.ts | 28 +++++++ 9 files changed, 144 insertions(+), 90 deletions(-) create mode 100644 packages/trueforge/src/truefoundry/virtualMcpHeaders.ts create mode 100644 packages/trueforge/tests/unit/truefoundry/virtualMcpHeaders.test.ts diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md index 0a5be3cc43..6ccf41a0cc 100644 --- a/.changeset/x-tfg-mcp-headers-auth.md +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode; nested virtual MCP entries are sent as JSON-valued headers in open-source mode and as `x-tfy-mcp-headers` in TrueFoundry mode), falling back to the server's regular auth on a 401. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode), falling back to the server's regular auth on a 401. Nested virtual MCP entries are accepted only in TrueFoundry mode, where they are sent as `x-tfy-mcp-headers`. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 4951c66e4f..f5a82902c5 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -1,13 +1,7 @@ -import type { RemoteMcpHeaders, ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; +import type { RemoteMcpHeaders } from '@truefoundry/trueforge-core/core'; import { isMcpAuthRequired, resolveMcpAuth } from '../mcp/auth/mcpDcr'; import type { IOAuthTokenStore, OAuthClientRecord } from '../mcp/auth/types'; -import { - hasAuthorizationHeader, - isVirtualMcpHeaders, - overrideHeaders, - withoutAuthorization, - type PerServerMcpHeaders, -} from '../runtime/perServerMcpHeaders'; +import { withCallerMcpHeaders, type PerServerMcpHeaders } from '../runtime/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { McpServerNotFoundError, @@ -25,17 +19,6 @@ import { } from './mcpServerStore'; import type { TurnMetadata } from './turnMetadata'; -async function resolveWithOverride(input: { - headers: RemoteMcpHeaders; - override: Record; -}): Promise { - const result = typeof input.headers === 'function' ? await input.headers() : { headers: input.headers }; - if ('authRequired' in result) { - return result; - } - return { headers: overrideHeaders({ base: result.headers, override: input.override }) }; -} - export class McpServerWithAuthStore implements IMcpServerWithAuthStore { readonly #store: IMcpServerStore; readonly #tokenStore: IOAuthTokenStore; @@ -78,33 +61,16 @@ export class McpServerWithAuthStore implements IMcpServerW return this.#store.deleteServer(input, transaction); } - /** Caller headers for the server go straight to it; one with Authorization skips its consent flow. */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { - const registered = this.#resolveRegisteredHeaders(input); - const callerHeaders = this.#perServerHeaders[input.record.name]; - if (callerHeaders === undefined) { - return registered; - } - if (isVirtualMcpHeaders(callerHeaders)) { - const override = Object.fromEntries( - Object.entries(callerHeaders).map(([name, value]) => [name, JSON.stringify(value)]), - ); - return () => resolveWithOverride({ headers: registered, override }); - } - if (hasAuthorizationHeader(callerHeaders)) { - const headers = overrideHeaders({ - base: resolveConfiguredMcpRequestHeaders(input.record.manifest), - override: callerHeaders, - }); - const override = withoutAuthorization(callerHeaders); - return () => - Promise.resolve({ headers, onUnauthorized: () => resolveWithOverride({ headers: registered, override }) }); - } - return () => resolveWithOverride({ headers: registered, override: callerHeaders }); + return withCallerMcpHeaders({ + callerHeaders: this.#perServerHeaders[input.record.name], + registeredHeaders: this.#resolveRegisteredHeaders(input), + configuredHeaders: resolveConfiguredMcpRequestHeaders(input.record.manifest), + }); } #resolveRegisteredHeaders(input: { record: McpServerRecord; userRef: string }): RemoteMcpHeaders { diff --git a/packages/trueforge/src/resolvers.ts b/packages/trueforge/src/resolvers.ts index 840e0d8b05..36a11ee69e 100644 --- a/packages/trueforge/src/resolvers.ts +++ b/packages/trueforge/src/resolvers.ts @@ -23,7 +23,7 @@ import type { ISkillStore } from './db/skillStore'; import type { IWebSearchProviderStore } from './db/webSearchProviderStore'; import type { IOAuthTokenStore } from './mcp/auth/types'; import type { MemoryProviderCache, TenantMemoryDeps } from './memory/providers'; -import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './runtime/perServerMcpHeaders'; +import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS } from './runtime/perServerMcpHeaders'; import { headerValue } from './truefoundry/gatewayMetadata'; import { InlineMcpServerStore } from './truefoundry/InlineMcpServerStore'; import { InlineModelProviderStore } from './truefoundry/InlineModelProviderStore'; @@ -48,6 +48,7 @@ import { TrueFoundryMcpServerStore } from './truefoundry/TrueFoundryMcpServerSto import { TrueFoundryModelProviderStore } from './truefoundry/TrueFoundryModelProviderStore'; import type { TrueFoundryServiceFoundryServerClient } from './truefoundry/TrueFoundryServiceFoundryServerClient'; import { TrueFoundryAdminSkillStore, TrueFoundrySkillStore } from './truefoundry/TrueFoundrySkillStore'; +import { parseTrueFoundryPerServerMcpHeaders } from './truefoundry/virtualMcpHeaders'; /** ------------------------------------------------------------------------ * Persistence-layer model store resolve (RequestContext, not Hono Context) @@ -63,7 +64,7 @@ export type ResolveModelProviderStore = (input: { export type ResolveMcpServerStore = (input: { rc: RequestContext; runAsAgent?: AgentRecord; - perServerHeaders?: PerServerMcpHeaders; + rawPerServerHeaders?: string; inlineMcpServers?: InlineMcpServers; }) => IMcpServerWithAuthStore; @@ -128,19 +129,21 @@ export function buildResolveMcpServerStore(options: { requestContext: input.rc, agent: input.runAsAgent, logger: options.logger, - ...(input.perServerHeaders === undefined ? {} : { perServerHeaders: input.perServerHeaders }), + ...(input.rawPerServerHeaders === undefined + ? {} + : { perServerHeaders: parseTrueFoundryPerServerMcpHeaders(input.rawPerServerHeaders) }), }); if (input.inlineMcpServers) { return new InlineMcpServerStore({ inner: store, inline: input.inlineMcpServers }); } return store; } - if (input.perServerHeaders !== undefined) { + if (input.rawPerServerHeaders !== undefined) { return new McpServerWithAuthStore({ store: options.persistenceStore, tokenStore: options.tokenStore, clientName: configuration.MCP_DCR_OAUTH_CLIENT_NAME, - perServerHeaders: input.perServerHeaders, + perServerHeaders: parsePerServerMcpHeaders(input.rawPerServerHeaders), }); } return withAuthMcpPersistenceStore; @@ -318,7 +321,7 @@ export function createRequestStoreResolvers(input: { return input.resolveMcpServerStore({ rc: requestContext, ...(runAsAgent !== undefined ? { runAsAgent } : {}), - ...(rawPerServerHeaders !== undefined ? { perServerHeaders: parsePerServerMcpHeaders(rawPerServerHeaders) } : {}), + ...(rawPerServerHeaders !== undefined ? { rawPerServerHeaders } : {}), ...(rawInlineMcpHeader !== undefined ? { inlineMcpServers: parseInlineMcpServers(rawInlineMcpHeader) } : {}), }); }; diff --git a/packages/trueforge/src/runtime/perServerMcpHeaders.ts b/packages/trueforge/src/runtime/perServerMcpHeaders.ts index cfca059fa8..62a2afbab2 100644 --- a/packages/trueforge/src/runtime/perServerMcpHeaders.ts +++ b/packages/trueforge/src/runtime/perServerMcpHeaders.ts @@ -1,3 +1,4 @@ +import type { RemoteMcpHeaders, ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; import { HTTPException } from 'hono/http-exception'; import { z } from 'zod'; @@ -8,23 +9,19 @@ import { z } from 'zod'; * present that same identity. This carries the identity each individual server should see — * typically the end user behind the caller, for a server that resolves its own permissions. * An entry carrying Authorization is used instead of that server's consent flow unless rejected. - * A virtual MCP's entry nests one header map per underlying server. */ export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; -const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); +export const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); -const VirtualMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); - -const PerServerMcpHeadersSchema = z.record(z.string().min(1), z.union([McpHeaderMapSchema, VirtualMcpHeadersSchema])); +const PerServerMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); export type PerServerMcpHeaders = z.infer; -// Transport headers TrueForge manages; a caller value would override them in the MCP client. +// Transport headers TrueForge manages; a caller value would replace them in the MCP client. const MANAGED_MCP_HEADERS = new Set(['mcp-session-id', 'mcp-protocol-version']); -type VirtualMcpHeaders = z.infer; -export function isVirtualMcpHeaders(entry: PerServerMcpHeaders[string]): entry is VirtualMcpHeaders { - return Object.values(entry).some(value => typeof value === 'object'); +export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders { + return decodePerServerMcpHeaders({ raw, schema: PerServerMcpHeadersSchema }); } /** @@ -32,15 +29,18 @@ export function isVirtualMcpHeaders(entry: PerServerMcpHeaders[string]): entry i * tool calls under the caller's own identity, which is the thing these headers exist to prevent. * An absent header is the caller's to interpret. */ -export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders { +export function decodePerServerMcpHeaders>>(input: { + raw: string; + schema: z.ZodType; +}): T { let decoded: unknown; try { - decoded = JSON.parse(raw); + decoded = JSON.parse(input.raw); } catch (error) { throw new HTTPException(400, { message: `${X_TFG_MCP_HEADERS} must be a JSON object`, cause: error }); } - const parsed = PerServerMcpHeadersSchema.safeParse(decoded); + const parsed = input.schema.safeParse(decoded); if (!parsed.success) { throw new HTTPException(400, { message: `${X_TFG_MCP_HEADERS} must map each MCP server name to a header map`, @@ -66,14 +66,51 @@ export function withoutAuthorization(headers: Record | undefined return Object.fromEntries(Object.entries(headers).filter(([name]) => name.toLowerCase() !== 'authorization')); } -/** Header names are case-insensitive, so an override must replace a base entry under any casing. */ -export function overrideHeaders(input: { - base: Record; - override: Record; +/** + * Sends a server's caller headers over its registered auth. Caller Authorization goes out over only the + * configured static headers, so a DCR consent flow is skipped unless the upstream rejects it. + */ +export function withCallerMcpHeaders(input: { + callerHeaders: Record | undefined; + registeredHeaders: RemoteMcpHeaders; + configuredHeaders: Record; +}): RemoteMcpHeaders { + const { callerHeaders, registeredHeaders, configuredHeaders } = input; + if (callerHeaders === undefined) { + return registeredHeaders; + } + if (!hasAuthorizationHeader(callerHeaders)) { + return () => resolveWithCallerHeaders({ registeredHeaders, callerHeaders }); + } + const headers = mergeCallerHeaders({ headers: configuredHeaders, callerHeaders }); + return () => + Promise.resolve({ + headers, + onUnauthorized: () => + resolveWithCallerHeaders({ registeredHeaders, callerHeaders: withoutAuthorization(callerHeaders) }), + }); +} + +async function resolveWithCallerHeaders(input: { + registeredHeaders: RemoteMcpHeaders; + callerHeaders: Record; +}): Promise { + const { registeredHeaders, callerHeaders } = input; + const result = typeof registeredHeaders === 'function' ? await registeredHeaders() : { headers: registeredHeaders }; + if ('authRequired' in result) { + return result; + } + return { headers: mergeCallerHeaders({ headers: result.headers, callerHeaders }) }; +} + +// Header names are case-insensitive, so a caller header must replace a same-named entry under any casing. +function mergeCallerHeaders(input: { + headers: Record; + callerHeaders: Record; }): Record { - const overridden = new Set(Object.keys(input.override).map(name => name.toLowerCase())); + const callerNames = new Set(Object.keys(input.callerHeaders).map(name => name.toLowerCase())); return { - ...Object.fromEntries(Object.entries(input.base).filter(([name]) => !overridden.has(name.toLowerCase()))), - ...input.override, + ...Object.fromEntries(Object.entries(input.headers).filter(([name]) => !callerNames.has(name.toLowerCase()))), + ...input.callerHeaders, }; } diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index ee7bdc2adf..aa45f84aa5 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -19,12 +19,7 @@ import { } from '../db/mcpServerStore'; import type { TurnMetadata } from '../db/turnMetadata'; import type { OAuthClientRecord } from '../mcp/auth/types'; -import { - hasAuthorizationHeader, - isVirtualMcpHeaders, - withoutAuthorization, - type PerServerMcpHeaders, -} from '../runtime/perServerMcpHeaders'; +import { hasAuthorizationHeader, withoutAuthorization } from '../runtime/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, @@ -43,6 +38,7 @@ import { type SfyMcpServerSummary, } from './mapSfyMcpServers'; import type { TrueFoundryServiceFoundryServerClient } from './TrueFoundryServiceFoundryServerClient'; +import { isVirtualMcpHeaders, type TrueFoundryPerServerMcpHeaders } from './virtualMcpHeaders'; export type TrueFoundryMcpApiClient = Pick< TrueFoundryServiceFoundryServerClient, @@ -85,14 +81,14 @@ export class TrueFoundryMcpServerStore implements IMcpServ readonly #resolveServiceFoundryAuthorization: ResolveServiceFoundryAuthorization; readonly #resolveGatewayAuthorization: ResolveGatewayAuthorization; readonly #subject: RequestSubject; - readonly #perServerHeaders: PerServerMcpHeaders; + readonly #perServerHeaders: TrueFoundryPerServerMcpHeaders; #gatewayUrl: string | undefined; constructor(input: { client: TrueFoundryMcpApiClient; requestContext: RequestContext; agent: AgentRecord | undefined; - perServerHeaders?: PerServerMcpHeaders; + perServerHeaders?: TrueFoundryPerServerMcpHeaders; logger: Logger; }) { this.#client = input.client; @@ -110,7 +106,7 @@ export class TrueFoundryMcpServerStore implements IMcpServ } /** - * Gateway Bearer (+ per-server overrides); the gateway's 401 carries any consent URL. + * Gateway Bearer (+ the caller's headers for that server); the gateway's 401 carries any consent URL. * A caller entry with Authorization goes in `x-tfy-mcp-headers`, falling back to the gateway's auth on a 401. */ resolveInvokeHeaders(input: { diff --git a/packages/trueforge/src/truefoundry/virtualMcpHeaders.ts b/packages/trueforge/src/truefoundry/virtualMcpHeaders.ts new file mode 100644 index 0000000000..acf38e2ab9 --- /dev/null +++ b/packages/trueforge/src/truefoundry/virtualMcpHeaders.ts @@ -0,0 +1,22 @@ +import { z } from 'zod'; +import { decodePerServerMcpHeaders, McpHeaderMapSchema } from '../runtime/perServerMcpHeaders'; + +/** A virtual MCP's `x-tfg-mcp-headers` entry: one header map per underlying server. */ +const VirtualMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); + +const TrueFoundryPerServerMcpHeadersSchema = z.record( + z.string().min(1), + z.union([McpHeaderMapSchema, VirtualMcpHeadersSchema]), +); + +export type TrueFoundryPerServerMcpHeaders = z.infer; + +type VirtualMcpHeaders = z.infer; + +export function parseTrueFoundryPerServerMcpHeaders(raw: string): TrueFoundryPerServerMcpHeaders { + return decodePerServerMcpHeaders({ raw, schema: TrueFoundryPerServerMcpHeadersSchema }); +} + +export function isVirtualMcpHeaders(entry: TrueFoundryPerServerMcpHeaders[string]): entry is VirtualMcpHeaders { + return Object.values(entry).some(value => typeof value === 'object'); +} diff --git a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts index 75797526ad..beea720009 100644 --- a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts @@ -21,7 +21,7 @@ describe('parsePerServerMcpHeaders', () => { ['a scalar', '"nope"'], ['a server mapped to a string', JSON.stringify({ 'github-mcp': 'Bearer user' })], ['a header mapped to a number', JSON.stringify({ 'github-mcp': { 'x-h': 1 } })], - ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], + ['a nested virtual MCP map', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' } } })], ['a managed session header', JSON.stringify({ 'github-mcp': { 'Mcp-Session-Id': 'other' } })], ['a managed protocol header', JSON.stringify({ 'github-mcp': { 'mcp-protocol-version': '2025-06-18' } })], ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { @@ -68,22 +68,21 @@ describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { await db.destroy(); }); - it('sends a virtual MCP entry as JSON-valued headers on top of the registered auth', async () => { - const entry = { serv1: { Authorization: 'Bearer a' } }; + it('merges caller headers over the registered headers under any casing', async () => { const db = createSqliteDb(':memory:'); const store = new McpServerWithAuthStore({ store: new SqliteMcpServerStore(db), tokenStore: new SqliteOAuthTokenStore(db), clientName: 'test-client', - perServerHeaders: { 'virtual-mcp': entry }, + perServerHeaders: { 'header-mcp': { 'X-Tenant': 'caller' } }, }); const record: McpServerRecord = { ...ossRecord, - name: 'virtual-mcp', + name: 'header-mcp', manifest: { ...ossRecord.manifest, - name: 'virtual-mcp', - auth: { type: 'header', headers: { Authorization: 'Bearer server' } }, + name: 'header-mcp', + auth: { type: 'header', headers: { Authorization: 'Bearer server', 'x-tenant': 'server' } }, }, }; @@ -92,7 +91,7 @@ describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { throw new Error('expected a headers resolver'); } await expect(resolve()).resolves.toEqual({ - headers: { Authorization: 'Bearer server', serv1: JSON.stringify(entry.serv1) }, + headers: { Authorization: 'Bearer server', 'X-Tenant': 'caller' }, }); await db.destroy(); }); diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index e0d6c4625e..8ca24b1418 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -2,7 +2,6 @@ import { AgentSpecSchema } from '@truefoundry/trueforge-core/agent-session'; import { createLogger } from 'winston'; import type { AgentRecord } from '../../../src/db/agentStore'; import { McpServerNotFoundError, type McpServerRecord } from '../../../src/db/mcpServerStore'; -import { parsePerServerMcpHeaders, type PerServerMcpHeaders } from '../../../src/runtime/perServerMcpHeaders'; import { ACTOR_AUTHORIZATION_HEADER, createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; import { X_TFY_CONVERSATION_ID, X_TFY_METADATA } from '../../../src/truefoundry/gatewayMetadata'; import { MCP_PROXY_BASE_URL_TEMPLATE } from '../../../src/truefoundry/mapSfyMcpServers'; @@ -11,6 +10,10 @@ import { resolveAuthorizeRedirectURL, TrueFoundryMcpServerStore, } from '../../../src/truefoundry/TrueFoundryMcpServerStore'; +import { + parseTrueFoundryPerServerMcpHeaders, + type TrueFoundryPerServerMcpHeaders, +} from '../../../src/truefoundry/virtualMcpHeaders'; const TENANT = 'default'; const ACCESS_TOKEN = 'caller-access-token'; @@ -393,7 +396,7 @@ const record = (name: string): McpServerRecord => ({ updated_at: '2026-01-16T12:00:00.000Z', }); -const storeWith = (perServerHeaders: PerServerMcpHeaders): TrueFoundryMcpServerStore => +const storeWith = (perServerHeaders: TrueFoundryPerServerMcpHeaders): TrueFoundryMcpServerStore => new TrueFoundryMcpServerStore({ client: unusedClient(), requestContext: createTrueFoundryRequestContext({ @@ -424,18 +427,18 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { const entry = { serv1: { Authorization: 'Bearer a' }, serv2: { Authorization: 'Bearer b' } }; const raw = JSON.stringify({ 'virtual-mcp': entry }); - const headers = await invokeHeaders(storeWith(parsePerServerMcpHeaders(raw)), 'virtual-mcp'); + const headers = await invokeHeaders(storeWith(parseTrueFoundryPerServerMcpHeaders(raw)), 'virtual-mcp'); expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); }); - it('sends only the gateway Bearer when a server has no override', async () => { + it('sends only the gateway Bearer when a server has no caller headers', async () => { await expect(invokeHeaders(storeWith({}), 'tfy-docs-mcp')).resolves.toEqual({ Authorization: 'Bearer caller-token', }); }); - it("merges that server's override on top of the Bearer", async () => { + it("merges that server's caller headers on top of the Bearer", async () => { const headers = await invokeHeaders( storeWith({ 'tfy-platform-mcp': { 'x-tfy-mcp-headers': '{"Authorization":"Bearer user"}' }, @@ -461,7 +464,7 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { }); it.each(['Authorization', 'authorization', 'AUTHORIZATION', 'AuThOrIzAtIoN'])( - 'sends an override under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', + 'sends a caller header under %s to the upstream via x-tfy-mcp-headers, never as the gateway Bearer', async name => { const headers = await invokeHeaders( storeWith({ 'tfy-platform-mcp': { [name]: 'Bearer user' } }), diff --git a/packages/trueforge/tests/unit/truefoundry/virtualMcpHeaders.test.ts b/packages/trueforge/tests/unit/truefoundry/virtualMcpHeaders.test.ts new file mode 100644 index 0000000000..f9cb56bfb1 --- /dev/null +++ b/packages/trueforge/tests/unit/truefoundry/virtualMcpHeaders.test.ts @@ -0,0 +1,28 @@ +import { HTTPException } from 'hono/http-exception'; +import { isVirtualMcpHeaders, parseTrueFoundryPerServerMcpHeaders } from '../../../src/truefoundry/virtualMcpHeaders'; + +describe('parseTrueFoundryPerServerMcpHeaders', () => { + it('accepts a flat header map and a virtual MCP nested map', () => { + const raw = JSON.stringify({ + 'github-mcp': { Authorization: 'Bearer user' }, + 'virtual-mcp': { serv1: { Authorization: 'Bearer a' } }, + }); + + const parsed = parseTrueFoundryPerServerMcpHeaders(raw); + + expect(parsed).toEqual({ + 'github-mcp': { Authorization: 'Bearer user' }, + 'virtual-mcp': { serv1: { Authorization: 'Bearer a' } }, + }); + expect( + Object.fromEntries(Object.entries(parsed).map(([name, entry]) => [name, isVirtualMcpHeaders(entry)])), + ).toEqual({ 'github-mcp': false, 'virtual-mcp': true }); + }); + + it.each([ + ['mixed nesting', JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } })], + ['a managed session header', JSON.stringify({ 'github-mcp': { 'Mcp-Session-Id': 'other' } })], + ])('rejects %s', (_case, raw) => { + expect(() => parseTrueFoundryPerServerMcpHeaders(raw)).toThrow(HTTPException); + }); +}); From 32064989698a662dece2f10dee1fa305564b3c9b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 12:19:52 +0000 Subject: [PATCH 20/20] refactor: drop the onUnauthorized fallback; a 401 either carries consent URLs or fails (AGE-2723) Co-authored-by: Rutvij Menavlikar --- .changeset/x-tfg-mcp-headers-auth.md | 2 +- .../trueforge-core/src/core/mcp/RemoteMCP.ts | 92 ++++--------------- .../src/core/mcp/remoteMcpClient.ts | 3 +- .../tests/core/mcp/remoteMcpServer.test.ts | 64 +------------ packages/trueforge/src/apis/turns.ts | 28 ++---- .../src/runtime/perServerMcpHeaders.ts | 27 ++---- .../truefoundry/TrueFoundryMcpServerStore.ts | 22 ++--- .../unit/runtime/perServerMcpHeaders.test.ts | 9 +- .../TrueFoundryMcpServerStore.test.ts | 13 ++- 9 files changed, 52 insertions(+), 208 deletions(-) diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md index 6ccf41a0cc..b8aff61f03 100644 --- a/.changeset/x-tfg-mcp-headers-auth.md +++ b/.changeset/x-tfg-mcp-headers-auth.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode), falling back to the server's regular auth on a 401. Nested virtual MCP entries are accepted only in TrueFoundry mode, where they are sent as `x-tfy-mcp-headers`. TrueFoundry mode no longer calls MCP authorize during turns: a 401 with `authorization_urls` pauses the turn with `mcp.auth_required`, and a bare 401 fails as an MCP error. +`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode). Nested virtual MCP entries are accepted only in TrueFoundry mode, where they are sent as `x-tfy-mcp-headers`. TrueFoundry mode no longer calls MCP authorize during turns. A 401 with `authorization_urls` pauses the turn with `mcp.auth_required`; any other 401 fails as an MCP error. diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index 589dee0b6f..85e5b83cde 100644 --- a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts +++ b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts @@ -30,7 +30,6 @@ import { paginateWithCursorGuard } from './pagination'; import { connectRemoteMcp, DEFAULT_MAX_MCP_RESPONSE_BYTES, - isAuthError, isSessionExpiredError, type RemoteMcpConnection, type RemoteMcpFetch, @@ -47,13 +46,8 @@ function redactUrlForTrace(url: string): string { } } -/** - * What a headers resolver returns: the headers to send, or a signal that auth is required. - * `onUnauthorized` is consulted once if the upstream rejects `headers` with a 401. - */ -export type ResolveHeadersResult = - | { headers: Record; onUnauthorized?: () => Promise } - | { authRequired: MCPAuthRequired }; +/** What a headers resolver returns: the headers to send, or a signal that auth is required. */ +export type ResolveHeadersResult = { headers: Record } | { authRequired: MCPAuthRequired }; /** Static headers, or a resolver (invoked at connect) returning headers or signalling auth-required. */ export type RemoteMcpHeaders = Record | (() => Promise); @@ -99,8 +93,6 @@ export class RemoteMCP implements ToolSource { private cachedTools?: AgentToolSchema[] | undefined; private inflight = 0; private pendingClose: RemoteMcpConnection | undefined; - // Connection opened with onUnauthorized fallback headers; its auth is re-checked before reuse. - private fallbackConnection: RemoteMcpConnection | undefined; constructor(params: { id: string; @@ -191,33 +183,36 @@ export class RemoteMCP implements ToolSource { private async executeWithSessionRetry( operation: (connection: RemoteMcpConnection) => Promise, ): Promise> { + // Auth is re-checked on every operation, not only on the first connect: a registered server's OAuth + // can be revoked or expire mid-request, and callers must get authRequired rather than a generic + // upstream failure. When already connected the resolved headers are unused (connect is skipped). + const headersResult = await this.resolveHeaders(); + if ('authRequired' in headersResult) { + return { authRequired: headersResult.authRequired }; + } let used: RemoteMcpConnection | undefined; const tracked = (connection: RemoteMcpConnection): Promise => { used = connection; return operation(connection); }; try { - return await this.executeWithAuthFallback(tracked); + return await this.connectAndRun(headersResult.headers, tracked, true); } catch (error) { const authRequired = this.authRequiredFrom(error); if (authRequired === undefined) { throw error; } - await this.discardRejected(used); + // Reset only the rejected connection; a failed connect still leaves the old session and tools. + if (used !== undefined) { + await this.resetConnection(used); + } else if (!this.isConnected) { + this.sessionId = undefined; + this.cachedTools = undefined; + } return { authRequired }; } } - // Reset only the rejected connection; a failed connect still leaves the old session and tools. - private async discardRejected(rejected: RemoteMcpConnection | undefined): Promise { - if (rejected !== undefined) { - await this.resetConnection(rejected); - } else if (!this.isConnected) { - this.sessionId = undefined; - this.cachedTools = undefined; - } - } - /** A 401 that carried consent URLs becomes authRequired; a bare 401 stays an error. */ private authRequiredFrom(error: unknown): MCPAuthRequired | undefined { for (let current: unknown = error; current instanceof Error; current = current.cause) { @@ -230,59 +225,6 @@ export class RemoteMCP implements ToolSource { return undefined; } - private async executeWithAuthFallback( - operation: (connection: RemoteMcpConnection) => Promise, - ): Promise> { - // Auth is re-checked on every operation, not only on the first connect: a registered server's OAuth - // can be revoked or expire mid-request, and callers must get authRequired rather than a generic - // upstream failure. When already connected the resolved headers are unused (connect is skipped). - const headersResult = await this.resolveHeaders(); - if ('authRequired' in headersResult) { - return { authRequired: headersResult.authRequired }; - } - const { onUnauthorized } = headersResult; - if (onUnauthorized === undefined) { - return this.connectAndRun(headersResult.headers, operation, true); - } - if (this.isConnected && this._connection !== undefined && this._connection === this.fallbackConnection) { - // Re-check the fallback auth before reusing its connection so a disconnected account stops here. - return this.runWithFallback(onUnauthorized, operation); - } - let rejected: RemoteMcpConnection | undefined; - const tracked = (connection: RemoteMcpConnection): Promise => { - rejected = connection; - return operation(connection); - }; - try { - return await this.connectAndRun(headersResult.headers, tracked, true); - } catch (error) { - if (!isAuthError(error) || this.authRequiredFrom(error) !== undefined) { - throw error; - } - this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`); - await this.discardRejected(rejected); - return this.runWithFallback(onUnauthorized, operation); - } - } - - private async runWithFallback( - onUnauthorized: () => Promise, - operation: (connection: RemoteMcpConnection) => Promise, - ): Promise> { - const fallback = await onUnauthorized(); - if ('authRequired' in fallback) { - if (this.fallbackConnection !== undefined) { - await this.resetConnection(this.fallbackConnection); - } - return { authRequired: fallback.authRequired }; - } - const tracked = (connection: RemoteMcpConnection): Promise => { - this.fallbackConnection = connection; - return operation(connection); - }; - return this.connectAndRun(fallback.headers, tracked, true); - } - private async connectAndRun( headers: Record, operation: (connection: RemoteMcpConnection) => Promise, diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 68b84c0d5c..6f0db6d1eb 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -149,8 +149,7 @@ function mcpConnectOutcome(error: unknown): McpConnectOutcome { return 'other'; } -/** Upstream rejected the credentials, whether at connect or on a later request. */ -export function isAuthError(error: unknown): boolean { +function isAuthError(error: unknown): boolean { if (!(error instanceof Error)) { return false; } diff --git a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts index 837162dd48..162c709a37 100644 --- a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts +++ b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts @@ -474,17 +474,15 @@ describe('RemoteMCP + ToolSet', () => { expect(state.callToolCalls).toBe(0); }); - it('returns authRequired from a 401 that carries authorization URLs, without falling back', async () => { + it('returns authRequired from a 401 that carries authorization URLs', async () => { installFakeConnection({ connectError: new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), }); - const onUnauthorized = jest.fn(() => Promise.resolve({ headers: {} })); - const server = makeServer({ headers: () => Promise.resolve({ headers: {}, onUnauthorized }) }); + const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); await expect(server.listTools()).resolves.toEqual({ authRequired: { servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }] }, }); - expect(onUnauthorized).not.toHaveBeenCalled(); }); it('drops the rejected connection when a call returns consent URLs, so the next call reconnects', async () => { @@ -501,69 +499,13 @@ describe('RemoteMCP + ToolSet', () => { expect(state.connectCalls).toBe(2); }); - it('throws on a bare 401 when there is no fallback', async () => { + it('throws on a bare 401', async () => { installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); await expect(server.listTools()).rejects.toMatchObject({ statusCode: 401 }); }); - it('falls back to onUnauthorized when supplied credentials are rejected at connect', async () => { - installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); - const authRequired: MCPAuthRequired = { - servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example' }], - }; - const onUnauthorized = jest.fn(() => Promise.resolve({ authRequired })); - const server = makeServer({ - headers: () => Promise.resolve({ headers: { Authorization: 'Bearer caller' }, onUnauthorized }), - }); - - await expect(server.listTools()).resolves.toEqual({ authRequired }); - expect(onUnauthorized).toHaveBeenCalledTimes(1); - }); - - it('reconnects with fallback headers when a call is rejected mid-session', async () => { - const state = installFakeConnection({ sessionId: 'sess-1' }); - const server = makeServer({ - headers: () => - Promise.resolve({ - headers: { Authorization: 'Bearer caller' }, - onUnauthorized: () => Promise.resolve({ headers: { Authorization: 'Bearer fallback' } }), - }), - }); - - await server.listTools(); - state.queueCallToolError(Object.assign(new Error('HTTP 401'), { code: 401 })); - const res = await server.callTool({ name: 'read_thing', arguments: {} }); - - expect(res).toMatchObject({ result: { content: [{ type: 'text', text: 'called read_thing' }] } }); - expect(state.connectCalls).toBe(2); - expect(mockConnect.mock.lastCall?.[0].headers).toEqual({ Authorization: 'Bearer fallback' }); - }); - - it('re-checks fallback auth before reusing a fallback connection', async () => { - const state = installFakeConnection({ sessionId: 'sess-1' }); - const authRequired: MCPAuthRequired = { - servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example' }], - }; - const onUnauthorized = jest - .fn } | { authRequired: MCPAuthRequired }>, []>() - .mockResolvedValueOnce({ headers: { Authorization: 'Bearer fallback' } }) - .mockResolvedValue({ authRequired }); - const server = makeServer({ - headers: () => Promise.resolve({ headers: { Authorization: 'Bearer caller' }, onUnauthorized }), - }); - - await server.listTools(); - state.queueCallToolError(Object.assign(new Error('HTTP 401'), { code: 401 })); - await server.callTool({ name: 'read_thing', arguments: {} }); - const callsBefore = state.callToolCalls; - - // The fallback account was disconnected: the reused connection must not run the call. - await expect(server.callTool({ name: 'read_thing', arguments: {} })).resolves.toEqual({ authRequired }); - expect(state.callToolCalls).toBe(callsBefore); - }); - it('emits init for a stateless (SSE) server on connect (matches gateway strict-compare behavior)', async () => { // Stateless connection reports a null session id. installFakeConnection({ sessionId: null }); diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts index 880d9332ad..9ef08a3496 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -826,29 +826,15 @@ function withTurnSessionHeader(connection: McpConnection, sessionId: string): Mc const sessionHeader = { [TRUEFORGE_SESSION_HEADER]: sessionId }; const headers = connection.headers; if (typeof headers === 'function') { - return { ...connection, headers: withSessionHeaderResolver({ resolve: headers, sessionHeader }) }; - } - return { ...connection, headers: { ...(headers ?? {}), ...sessionHeader } }; -} - -function withSessionHeaderResolver(input: { - resolve: () => Promise; - sessionHeader: Record; -}): () => Promise { - const { resolve, sessionHeader } = input; - return async () => { - const resolved = await resolve(); - if ('authRequired' in resolved) { - return resolved; - } - const { onUnauthorized } = resolved; return { - headers: { ...resolved.headers, ...sessionHeader }, - ...(onUnauthorized === undefined - ? {} - : { onUnauthorized: withSessionHeaderResolver({ resolve: onUnauthorized, sessionHeader }) }), + ...connection, + headers: async (): Promise => { + const resolved = await headers(); + return 'authRequired' in resolved ? resolved : { headers: { ...resolved.headers, ...sessionHeader } }; + }, }; - }; + } + return { ...connection, headers: { ...(headers ?? {}), ...sessionHeader } }; } /** diff --git a/packages/trueforge/src/runtime/perServerMcpHeaders.ts b/packages/trueforge/src/runtime/perServerMcpHeaders.ts index 62a2afbab2..a8586671fe 100644 --- a/packages/trueforge/src/runtime/perServerMcpHeaders.ts +++ b/packages/trueforge/src/runtime/perServerMcpHeaders.ts @@ -8,7 +8,7 @@ import { z } from 'zod'; * TrueForge authenticates the whole turn as one caller, so every MCP connection would otherwise * present that same identity. This carries the identity each individual server should see — * typically the end user behind the caller, for a server that resolves its own permissions. - * An entry carrying Authorization is used instead of that server's consent flow unless rejected. + * An entry carrying Authorization is used instead of that server's consent flow. */ export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; export const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); @@ -55,20 +55,13 @@ export function decodePerServerMcpHeaders | undefined): headers is Record { - return headers !== undefined && Object.keys(headers).some(name => name.toLowerCase() === 'authorization'); -} - -export function withoutAuthorization(headers: Record | undefined): Record { - if (headers === undefined) { - return {}; - } - return Object.fromEntries(Object.entries(headers).filter(([name]) => name.toLowerCase() !== 'authorization')); +export function hasAuthorizationHeader(headers: Record): boolean { + return Object.keys(headers).some(name => name.toLowerCase() === 'authorization'); } /** * Sends a server's caller headers over its registered auth. Caller Authorization goes out over only the - * configured static headers, so a DCR consent flow is skipped unless the upstream rejects it. + * configured static headers, so a DCR consent flow is skipped. */ export function withCallerMcpHeaders(input: { callerHeaders: Record | undefined; @@ -79,16 +72,10 @@ export function withCallerMcpHeaders(input: { if (callerHeaders === undefined) { return registeredHeaders; } - if (!hasAuthorizationHeader(callerHeaders)) { - return () => resolveWithCallerHeaders({ registeredHeaders, callerHeaders }); + if (hasAuthorizationHeader(callerHeaders)) { + return mergeCallerHeaders({ headers: configuredHeaders, callerHeaders }); } - const headers = mergeCallerHeaders({ headers: configuredHeaders, callerHeaders }); - return () => - Promise.resolve({ - headers, - onUnauthorized: () => - resolveWithCallerHeaders({ registeredHeaders, callerHeaders: withoutAuthorization(callerHeaders) }), - }); + return () => resolveWithCallerHeaders({ registeredHeaders, callerHeaders }); } async function resolveWithCallerHeaders(input: { diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index aa45f84aa5..116a388d83 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -1,4 +1,4 @@ -import { McpConnectionError, type RemoteMcpHeaders, type ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; +import { McpConnectionError, type RemoteMcpHeaders } from '@truefoundry/trueforge-core/core'; import type { Logger } from 'winston'; import type { RequestContext, RequestSubject } from '../auth/identity'; import { safeReturnTo } from '../auth/safeReturnTo'; @@ -19,7 +19,7 @@ import { } from '../db/mcpServerStore'; import type { TurnMetadata } from '../db/turnMetadata'; import type { OAuthClientRecord } from '../mcp/auth/types'; -import { hasAuthorizationHeader, withoutAuthorization } from '../runtime/perServerMcpHeaders'; +import { hasAuthorizationHeader } from '../runtime/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, @@ -107,7 +107,7 @@ export class TrueFoundryMcpServerStore implements IMcpServ /** * Gateway Bearer (+ the caller's headers for that server); the gateway's 401 carries any consent URL. - * A caller entry with Authorization goes in `x-tfy-mcp-headers`, falling back to the gateway's auth on a 401. + * A caller entry with Authorization, or a virtual MCP's nested entry, goes in `x-tfy-mcp-headers`. */ resolveInvokeHeaders(input: { record: McpServerRecord; @@ -120,23 +120,13 @@ export class TrueFoundryMcpServerStore implements IMcpServ ...gatewayHeaders(await this.#resolveGatewayAuthorization()), ...gatewayMetadataHeadersForTurn(turnMetadata), }); - if (callerHeaders !== undefined && isVirtualMcpHeaders(callerHeaders)) { - // The gateway fans a virtual MCP's nested map out to its servers itself. + if (callerHeaders !== undefined && (isVirtualMcpHeaders(callerHeaders) || hasAuthorizationHeader(callerHeaders))) { + // Each connection targets one server's gateway proxy; a virtual MCP's nested map is fanned out by the gateway. return async () => ({ headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, }); } - const viaGateway = async (): Promise => ({ - headers: { ...withoutAuthorization(callerHeaders), ...(await gatewayBase()) }, - }); - if (!hasAuthorizationHeader(callerHeaders)) { - return viaGateway; - } - // Each connection targets one server's gateway proxy, which takes a flat header map for it. - return async () => ({ - headers: { ...(await gatewayBase()), [X_TFY_MCP_HEADERS]: JSON.stringify(callerHeaders) }, - onUnauthorized: viaGateway, - }); + return async () => ({ headers: { ...callerHeaders, ...(await gatewayBase()) } }); } async listServers(input: ListMcpServersInput, transaction?: TTransaction): Promise { diff --git a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts index beea720009..b704b28e34 100644 --- a/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts @@ -60,11 +60,10 @@ describe('McpServerWithAuthStore.resolveInvokeHeaders', () => { perServerHeaders: { 'oauth-mcp': { Authorization: 'Bearer user', 'x-extra': '1' } }, }); - const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); - if (typeof resolve !== 'function') { - throw new Error('expected a headers resolver'); - } - await expect(resolve()).resolves.toMatchObject({ headers: { Authorization: 'Bearer user', 'x-extra': '1' } }); + expect(store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' })).toEqual({ + Authorization: 'Bearer user', + 'x-extra': '1', + }); await db.destroy(); }); diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts index 8ca24b1418..4e6b1b3782 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -478,7 +478,7 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { }, ); - it('never calls authorize; a rejected caller Authorization falls back to the gateway auth', async () => { + it('never calls authorize for a caller Authorization', async () => { const getMcpAuthorize = jest.fn(async () => ({ status: 'authenticated' as const })); const store = new TrueFoundryMcpServerStore({ client: { ...unusedClient(), getMcpAuthorize }, @@ -497,12 +497,11 @@ describe('TrueFoundryMcpServerStore.resolveInvokeHeaders', () => { throw new Error('expected a headers resolver'); } - const resolved = await resolve(); - if (!('onUnauthorized' in resolved) || resolved.onUnauthorized === undefined) { - throw new Error('expected a fallback to the gateway auth'); - } - await expect(resolved.onUnauthorized()).resolves.toEqual({ - headers: { Authorization: 'Bearer caller-token', 'X-End-User': 'alice' }, + await expect(resolve()).resolves.toEqual({ + headers: { + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': JSON.stringify({ Authorization: 'Bearer user', 'X-End-User': 'alice' }), + }, }); expect(getMcpAuthorize).not.toHaveBeenCalled(); });