diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md new file mode 100644 index 0000000000..4429ce6fe2 --- /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` passes per-MCP-server headers. Open-source mode merges a server's entry over its registered headers, and an entry `Authorization` skips DCR. TrueFoundry mode sends the entry (flat, or a nested virtual MCP map) as `x-tfy-mcp-headers` and no longer calls authorize before connecting. On a 401: open-source fails when the entry carried `Authorization`, otherwise a DCR server restarts consent; TrueFoundry fails whenever the server had an entry, otherwise asks authorize for a consent URL and fails with 422 when authorize returns none or reports no consent is needed. diff --git a/packages/trueforge-core/src/core/mcp/RemoteMCP.ts b/packages/trueforge-core/src/core/mcp/RemoteMCP.ts index 2ee8958820..173e104f4e 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 RemoteMcpFetch, @@ -40,8 +41,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. + * On a 401, `authRequiredOnUnauthorized` may offer consent; without it, or if it returns undefined, the 401 fails. + */ +export type ResolveHeadersResult = + | { headers: Record; authRequiredOnUnauthorized?: () => Promise } + | { authRequired: MCPAuthRequired }; /** Static headers, or a resolver (invoked at connect) returning headers or signalling auth-required. */ export type RemoteMcpHeaders = Record | (() => Promise); @@ -184,7 +190,39 @@ export class RemoteMCP implements ToolSource { if ('authRequired' in headersResult) { return { authRequired: headersResult.authRequired }; } - return this.connectAndRun(headersResult.headers, operation, true); + let used: RemoteMcpConnection | undefined; + const tracked = (connection: RemoteMcpConnection): Promise => { + used = connection; + return operation(connection); + }; + try { + return await this.connectAndRun(headersResult.headers, tracked, true); + } catch (error) { + let authRequired: MCPAuthRequired | undefined = undefined; + try { + if (isAuthError(error)) { + authRequired = await headersResult.authRequiredOnUnauthorized?.(); + } + } catch (recoveryError) { + // Rethrow the recovery error unchanged so its status survives, with the 401 it replaced as + // `cause` — error logging already walks that chain. + if (recoveryError instanceof Error && recoveryError.cause === undefined) { + recoveryError.cause = error; + } + throw recoveryError; + } + if (authRequired === undefined) { + throw error; + } + // 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 }; + } } private async connectAndRun( diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 0e57b401c6..6892b30cdb 100644 --- a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts +++ b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts @@ -124,10 +124,13 @@ function mcpConnectOutcome(error: unknown): McpConnectOutcome { return 'other'; } -function isAuthError(error: unknown): boolean { +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 1ec97e9406..8ad979774b 100644 --- a/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts +++ b/packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts @@ -473,6 +473,57 @@ describe('RemoteMCP + ToolSet', () => { expect(state.callToolCalls).toBe(0); }); + it('returns authRequired when the resolver offers consent for a 401', async () => { + installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); + const authRequired: MCPAuthRequired = { + servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example/consent' }], + }; + const server = makeServer({ + headers: () => Promise.resolve({ headers: {}, authRequiredOnUnauthorized: () => Promise.resolve(authRequired) }), + }); + + await expect(server.listTools()).resolves.toEqual({ authRequired }); + }); + + it('drops the rejected connection when a call gets a 401 with consent, 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: {}, authRequiredOnUnauthorized: () => Promise.resolve(authRequired) }), + }); + + await server.listTools(); + state.queueCallToolError(Object.assign(new Error('HTTP 401'), { code: 401 })); + await expect(server.callTool({ name: 'read_thing', arguments: {} })).resolves.toEqual({ authRequired }); + await server.callTool({ name: 'read_thing', arguments: {} }); + + expect(state.connectCalls).toBe(2); + }); + + it('throws the 401 when the resolver offers no consent', async () => { + installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); + const server = makeServer({ + headers: () => Promise.resolve({ headers: {}, authRequiredOnUnauthorized: () => Promise.resolve(undefined) }), + }); + + await expect(server.listTools()).rejects.toMatchObject({ statusCode: 401 }); + }); + + it('propagates the resolver error when the consent lookup fails', async () => { + installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); + const server = makeServer({ + headers: () => + Promise.resolve({ headers: {}, authRequiredOnUnauthorized: () => Promise.reject(new Error('lookup failed')) }), + }); + + await expect(server.listTools()).rejects.toMatchObject({ + message: 'lookup failed', + cause: expect.objectContaining({ statusCode: 401 }), + }); + }); + 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 3019bc4a66..6923a13209 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -111,6 +111,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, @@ -123,7 +124,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'; @@ -832,7 +832,9 @@ function withTurnSessionHeader(connection: McpConnection, sessionId: string): Mc ...connection, headers: async (): Promise => { const resolved = await headers(); - return 'authRequired' in resolved ? resolved : { headers: { ...resolved.headers, ...sessionHeader } }; + return 'authRequired' in resolved + ? resolved + : { ...resolved, headers: { ...resolved.headers, ...sessionHeader } }; }, }; } diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts index 792fc9e2f9..355a9f9eeb 100644 --- a/packages/trueforge/src/db/McpServerWithAuthStore.ts +++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts @@ -1,6 +1,7 @@ -import type { RemoteMcpHeaders } from '@truefoundry/trueforge-core/core'; -import { isMcpAuthRequired, resolveMcpAuth } from '../mcp/auth/mcpDcr'; +import type { RemoteMcpHeaders, ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; +import { beginMcpAuthorization, isMcpAuthRequired, resolveMcpAuth } from '../mcp/auth/mcpDcr'; import type { IOAuthTokenStore, OAuthClientRecord } from '../mcp/auth/types'; +import { withServerOverrideHeaders, type PerServerMcpHeaders } from '../runtime/perServerMcpHeaders'; import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { McpServerNotFoundError, @@ -22,15 +23,18 @@ export class McpServerWithAuthStore implements IMcpServerW 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 { @@ -62,9 +66,18 @@ export class McpServerWithAuthStore implements IMcpServerW userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { + const { record } = input; + return withServerOverrideHeaders({ + serverOverrideHeaders: this.#perServerHeaders[record.name], + storeHeaders: this.#resolveStoreHeaders(input), + configuredHeaders: resolveConfiguredMcpRequestHeaders(record.manifest), + }); + } + + #resolveStoreHeaders(input: { record: McpServerRecord; userRef: string }): RemoteMcpHeaders { const { record, userRef } = input; if (record.manifest.auth?.type === 'dcr') { - return async () => { + return async (): Promise => { const result = await resolveMcpAuth({ tokenStore: this.#tokenStore, mcpServerStore: this.#store, @@ -82,7 +95,22 @@ export class McpServerWithAuthStore implements IMcpServerW }, }; } - return { headers: result.headers }; + return { + headers: result.headers, + // On a 401, start DCR consent; the stored token stays until the callback replaces it. + authRequiredOnUnauthorized: async () => { + const authorizationUrl = await beginMcpAuthorization({ + tokenStore: this.#tokenStore, + mcpServerStore: this.#store, + serverId: record.id, + userRef, + mcpServerUrl: record.manifest.url, + mcpServerName: record.name, + clientName: this.#clientName, + }); + return { servers: [{ id: record.name, name: record.name, auth_url: authorizationUrl.href }] }; + }, + }; }; } return resolveConfiguredMcpRequestHeaders(record.manifest); 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/mcp/auth/mcpDcr.ts b/packages/trueforge/src/mcp/auth/mcpDcr.ts index 5e5b9bea8d..3e52e6e846 100644 --- a/packages/trueforge/src/mcp/auth/mcpDcr.ts +++ b/packages/trueforge/src/mcp/auth/mcpDcr.ts @@ -177,7 +177,7 @@ export async function createMcpOAuthClient(params: { /** * Returns the stored OAuth client for `serverId`, or discovers + DCR-registers and persists one. - * Used from `resolveMcpAuth` only — callers that already hold the client should pass it through. + * Callers that already hold the client should pass it to `buildMcpAuthorizationUrl` directly. */ export async function ensureMcpClientRegistered(params: { mcpServerStore: IOAuthClientStore; @@ -237,6 +237,39 @@ export async function buildMcpAuthorizationUrl(params: { return { authorizationUrl: started.authorizationUrl, state }; } +/** + * Ensures a DCR client exists for the server, then builds the authorize URL and stores the pending + * PKCE row. Shared by the pre-connect consent path and the 401 recovery path. + */ +export async function beginMcpAuthorization(params: { + tokenStore: IOAuthTokenStore; + mcpServerStore: IOAuthClientStore; + serverId: string; + userRef: string; + mcpServerUrl: string; + mcpServerName: string; + clientName: string; + returnTo?: string; +}): Promise { + const client = await ensureMcpClientRegistered({ + mcpServerStore: params.mcpServerStore, + serverId: params.serverId, + mcpServerUrl: params.mcpServerUrl, + mcpServerName: params.mcpServerName, + clientName: params.clientName, + }); + const { authorizationUrl } = await buildMcpAuthorizationUrl({ + tokenStore: params.tokenStore, + client, + serverId: params.serverId, + userRef: params.userRef, + mcpServerUrl: params.mcpServerUrl, + mcpServerName: params.mcpServerName, + ...(params.returnTo !== undefined ? { returnTo: params.returnTo } : {}), + }); + return authorizationUrl; +} + export async function resolveMcpAuth(params: { tokenStore: IOAuthTokenStore; mcpServerStore: IOAuthClientStore; @@ -279,13 +312,14 @@ export async function resolveMcpAuth(params: { if (token) { await params.tokenStore.deleteToken(tokenKey); } - const { authorizationUrl: authUrl } = await buildMcpAuthorizationUrl({ + const authUrl = await beginMcpAuthorization({ tokenStore: params.tokenStore, - client, + mcpServerStore: params.mcpServerStore, serverId: params.serverId, userRef: params.userRef, mcpServerUrl: params.mcpServerUrl, mcpServerName: params.mcpServerName, + clientName: params.clientName, ...(params.returnTo !== undefined ? { returnTo: params.returnTo } : {}), }); return { authUrl }; diff --git a/packages/trueforge/src/resolvers.ts b/packages/trueforge/src/resolvers.ts index 6bfdbfa8f1..2a67820110 100644 --- a/packages/trueforge/src/resolvers.ts +++ b/packages/trueforge/src/resolvers.ts @@ -23,6 +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 } from './runtime/perServerMcpHeaders'; import { headerValue } from './truefoundry/gatewayMetadata'; import { InlineMcpServerStore } from './truefoundry/InlineMcpServerStore'; import { InlineModelProviderStore } from './truefoundry/InlineModelProviderStore'; @@ -42,11 +43,7 @@ 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 { parseTrueFoundryPerServerMcpHeaders } from './truefoundry/perServerMcpHeaders'; import { TrueFoundryAgentStore } from './truefoundry/TrueFoundryAgentStore'; import { TrueFoundryMcpServerStore } from './truefoundry/TrueFoundryMcpServerStore'; import { TrueFoundryModelProviderStore } from './truefoundry/TrueFoundryModelProviderStore'; @@ -67,7 +64,7 @@ export type ResolveModelProviderStore = (input: { export type ResolveMcpServerStore = (input: { rc: RequestContext; runAsAgent?: AgentRecord; - perServerHeaders?: PerServerMcpHeaders; + rawPerServerHeaders?: string; inlineMcpServers?: InlineMcpServers; }) => IMcpServerWithAuthStore; @@ -110,7 +107,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; @@ -131,13 +129,23 @@ 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.rawPerServerHeaders !== undefined) { + return new McpServerWithAuthStore({ + store: options.persistenceStore, + tokenStore: options.tokenStore, + clientName: configuration.MCP_DCR_OAUTH_CLIENT_NAME, + perServerHeaders: parsePerServerMcpHeaders(input.rawPerServerHeaders), + }); + } return withAuthMcpPersistenceStore; }; } @@ -313,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 new file mode 100644 index 0000000000..046a54dea2 --- /dev/null +++ b/packages/trueforge/src/runtime/perServerMcpHeaders.ts @@ -0,0 +1,102 @@ +import type { RemoteMcpHeaders, ResolveHeadersResult } from '@truefoundry/trueforge-core/core'; +import { HTTPException } from 'hono/http-exception'; +import { z } from 'zod'; + +/** + * Per-MCP-server request headers, keyed by server name. + * + * 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. + */ +export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; +export const McpHeaderMapSchema = z.record(z.string().min(1), z.string()); + +const PerServerMcpHeadersSchema = z.record(z.string().min(1), McpHeaderMapSchema); + +export type PerServerMcpHeaders = z.infer; + +// 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']); + +export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders { + return decodePerServerMcpHeaders({ raw, schema: PerServerMcpHeadersSchema }); +} + +/** + * Rejects a malformed value rather than dropping it: the alternative to failing is running a user's + * 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 decodePerServerMcpHeaders>>(input: { + raw: string; + schema: z.ZodType; +}): T { + let decoded: unknown; + try { + 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 = 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`, + }); + } + 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 have keys as one of ${[...MANAGED_MCP_HEADERS].join(', ')} (got ${managed})`, + }); + } + return parsed.data; +} + +function hasAuthorizationHeader(headers: Record): boolean { + return Object.keys(headers).some(name => name.toLowerCase() === 'authorization'); +} + +/** + * Sends a server's override headers over its store-resolved auth. An override Authorization goes out over only + * the configured static headers, so a DCR consent flow is skipped. + */ +export function withServerOverrideHeaders(input: { + serverOverrideHeaders: Record | undefined; + storeHeaders: RemoteMcpHeaders; + configuredHeaders: Record; +}): RemoteMcpHeaders { + const { serverOverrideHeaders, storeHeaders, configuredHeaders } = input; + if (serverOverrideHeaders === undefined) { + return storeHeaders; + } + if (hasAuthorizationHeader(serverOverrideHeaders)) { + return mergeOverrideHeaders({ headers: configuredHeaders, overrideHeaders: serverOverrideHeaders }); + } + return async (): Promise => { + const result = typeof storeHeaders === 'function' ? await storeHeaders() : { headers: storeHeaders }; + if ('authRequired' in result) { + return result; + } + return { + ...result, + headers: mergeOverrideHeaders({ headers: result.headers, overrideHeaders: serverOverrideHeaders }), + }; + }; +} + +// Header names are case-insensitive, so an override header must replace a same-named entry under any casing. +function mergeOverrideHeaders(input: { + headers: Record; + overrideHeaders: Record; +}): Record { + const overrideNames = new Set(Object.keys(input.overrideHeaders).map(name => name.toLowerCase())); + return { + ...Object.fromEntries(Object.entries(input.headers).filter(([name]) => !overrideNames.has(name.toLowerCase()))), + ...input.overrideHeaders, + }; +} diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts index 671384a9fe..daf86e3afb 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 MCPAuthRequired, type RemoteMcpHeaders } from '@truefoundry/trueforge-core/core'; import { HTTPException } from 'hono/http-exception'; import type { Logger } from 'winston'; import type { RequestContext, RequestSubject } from '../auth/identity'; @@ -37,7 +37,7 @@ import { toTrueFoundryMcpManifest, type SfyMcpServerSummary, } from './mapSfyMcpServers'; -import type { PerServerMcpHeaders } from './perServerMcpHeaders'; +import type { TrueFoundryPerServerMcpHeaders } 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`. @@ -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; @@ -109,42 +105,47 @@ export class TrueFoundryMcpServerStore implements IMcpServ this.#perServerHeaders = input.perServerHeaders ?? {}; } - /** Gateway Bearer (+ optional per-server overrides); SFY authorize first, else authRequired. */ + /** + * Gateway Bearer; an override entry (flat or virtual MCP nested map) goes in `x-tfy-mcp-headers`. + * Without one, SFY authorize runs only after a 401. + */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { const { record, userRef, turnMetadata } = input; + const serverOverrideHeaders = this.#perServerHeaders[record.name]; return async () => { - 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(this.#perServerHeaders[record.name]), - ...gatewayHeaders(authorization), + const headers = { + ...gatewayHeaders(await this.#resolveGatewayAuthorization()), ...gatewayMetadataHeadersForTurn(turnMetadata), }; - return { headers: invokeHeaders }; + if (serverOverrideHeaders !== undefined) { + return { headers: { ...headers, [X_TFY_MCP_HEADERS]: JSON.stringify(serverOverrideHeaders) } }; + } + return { headers, authRequiredOnUnauthorized: () => this.#authorizeAfterUnauthorized({ record, userRef }) }; }; } + /** Consent when SFY authorize returns a URL; otherwise the 401 fails as a 422. */ + async #authorizeAfterUnauthorized(input: { record: McpServerRecord; userRef: string }): Promise { + const { record, userRef } = input; + const status = await this.authorize({ tenant_id: record.tenant_id, name: record.name, userRef }); + if (status.status !== 'auth_required') { + throw new HTTPException(422, { + message: `MCP server "${record.name}" authorization status is in conflicted state`, + }); + } + const authUrl = status.authorization_url; + if (authUrl === undefined || authUrl.length === 0) { + throw new HTTPException(422, { + message: `MCP server "${record.name}" requires authentication but failed to discover authorization url`, + }); + } + return { servers: [{ id: record.name, name: record.name, auth_url: authUrl }] }; + } + async listServers(input: ListMcpServersInput, transaction?: TTransaction): Promise { void transaction; if (input.names?.length === 0) { diff --git a/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts b/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts index 549fd75a73..88b75c0c0d 100644 --- a/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts +++ b/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts @@ -1,37 +1,16 @@ -import { HTTPException } from 'hono/http-exception'; import { z } from 'zod'; +import { decodePerServerMcpHeaders, McpHeaderMapSchema } from '../runtime/perServerMcpHeaders'; -/** - * Per-MCP-server request headers, keyed by server name. - * - * 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. - */ -export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers'; +/** 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 PerServerMcpHeadersSchema = z.record(z.string().min(1), z.record(z.string().min(1), z.string())); +const TrueFoundryPerServerMcpHeadersSchema = z.record( + z.string().min(1), + z.union([McpHeaderMapSchema, VirtualMcpHeadersSchema]), +); -export type PerServerMcpHeaders = z.infer; +export type TrueFoundryPerServerMcpHeaders = z.infer; -/** - * Rejects a malformed value rather than dropping it: the alternative to failing is running a user's - * 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 { - let decoded: unknown; - try { - decoded = JSON.parse(raw); - } catch (error) { - throw new HTTPException(400, { message: `${X_TFG_MCP_HEADERS} must be a JSON object`, cause: error }); - } - - 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`, - }); - } - return parsed.data; +export function parseTrueFoundryPerServerMcpHeaders(raw: string): TrueFoundryPerServerMcpHeaders { + return decodePerServerMcpHeaders({ raw, schema: TrueFoundryPerServerMcpHeadersSchema }); } diff --git a/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts b/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts index 4a30f57550..e70d2e67a5 100644 --- a/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts +++ b/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts @@ -168,6 +168,7 @@ describe('getMcpConnection', () => { } await expect(connection.headers()).resolves.toEqual({ headers: { Authorization: 'Bearer live-access' }, + authRequiredOnUnauthorized: expect.any(Function), }); }); 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..c9f96c0b2a --- /dev/null +++ b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts @@ -0,0 +1,128 @@ +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 { InMemoryOAuthTokenStore } from '../../../src/mcp/auth/inMemoryStores'; +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 } })], + ['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' } })], + ])('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' } }, + }); + + expect(store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' })).toEqual({ + Authorization: 'Bearer user', + 'x-extra': '1', + }); + await db.destroy(); + }); + + it('keeps the DCR 401 consent hook when caller headers carry no Authorization', async () => { + const db = createSqliteDb(':memory:'); + const tokenStore = new InMemoryOAuthTokenStore(); + await tokenStore.saveToken({ + id: ossRecord.id, + userRef: 'user-1', + token: { + accessToken: 'stored', + refreshToken: null, + expiresAt: new Date(Date.now() + 60_000).toISOString(), + scope: null, + }, + }); + const store = new McpServerWithAuthStore({ + store: new SqliteMcpServerStore(db), + tokenStore, + clientName: 'test-client', + perServerHeaders: { 'oauth-mcp': { 'X-End-User': 'alice' } }, + }); + + const resolve = store.resolveInvokeHeaders({ record: ossRecord, userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + await expect(resolve()).resolves.toEqual({ + headers: { Authorization: 'Bearer stored', 'X-End-User': 'alice' }, + authRequiredOnUnauthorized: expect.any(Function), + }); + await db.destroy(); + }); + + 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: { 'header-mcp': { 'X-Tenant': 'caller' } }, + }); + const record: McpServerRecord = { + ...ossRecord, + name: 'header-mcp', + manifest: { + ...ossRecord.manifest, + name: 'header-mcp', + auth: { type: 'header', headers: { Authorization: 'Bearer server', 'x-tenant': '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', '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 9eafb93159..ce61aa6289 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -5,6 +5,10 @@ import { McpServerNotFoundError, type McpServerRecord } from '../../../src/db/mc 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'; +import { + parseTrueFoundryPerServerMcpHeaders, + type TrueFoundryPerServerMcpHeaders, +} from '../../../src/truefoundry/perServerMcpHeaders'; import type { TrueFoundryMcpApiClient } from '../../../src/truefoundry/TrueFoundryMcpServerStore'; import { resolveAuthorizeRedirectURL, @@ -298,20 +302,35 @@ describe('TrueFoundryMcpServerStore', () => { return headers(); } - it('returns authRequired when authorize reports auth_required', async () => { + async function consentOn401(store: TrueFoundryMcpServerStore, record: McpServerRecord = dcrRecord()) { + const resolved = await invoke(store, record); + if (!('headers' in resolved) || resolved.authRequiredOnUnauthorized === undefined) { + throw new Error('expected a 401 consent hook'); + } + return resolved.authRequiredOnUnauthorized(); + } + + it('returns the gateway Bearer without calling authorize up front', async () => { + const { store, client } = createStore(); + await expect(invoke(store)).resolves.toEqual({ + headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, + authRequiredOnUnauthorized: expect.any(Function), + }); + expect(client.getMcpAuthorize).not.toHaveBeenCalled(); + }); + + it('on a 401, returns authRequired when authorize reports auth_required', 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' }], - }, + await expect(consentOn401(store)).resolves.toEqual({ + servers: [{ id: 'github', name: 'github', auth_url: 'https://consent.example/authorize' }], }); }); - it('gates non-dcr truefoundry servers through authorize as well', async () => { + it('on a 401, gates non-dcr truefoundry servers through authorize as well', async () => { const { store, client } = createStore(); const record = truefoundryRecordWithoutAuth(); client.getMcpServerByName.mockResolvedValue({ @@ -326,23 +345,24 @@ describe('TrueFoundryMcpServerStore', () => { 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' }], - }, + await expect(consentOn401(store, record)).resolves.toEqual({ + servers: [{ id: record.name, name: record.name, auth_url: 'https://consent.example/auth-override' }], }); - expect(client.getMcpAuthorize).toHaveBeenCalled(); }); - it('returns gateway Bearer when authorize reports authenticated', async () => { + it('on a 401, throws 422 when auth_required lacks authorization_url', async () => { + const { store, client } = createStore(); + client.getMcpAuthorize.mockResolvedValue({ status: 'auth_required' }); + await expect(consentOn401(store)).rejects.toMatchObject({ status: 422 }); + }); + + it('on a 401, throws 422 when authorize reports authenticated', async () => { const { store } = createStore(); - await expect(invoke(store)).resolves.toEqual({ - headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, - }); + await expect(consentOn401(store)).rejects.toMatchObject({ status: 422 }); }); - 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', @@ -363,21 +383,12 @@ describe('TrueFoundryMcpServerStore', () => { [X_TFY_METADATA]: expect.any(String), [X_TFY_CONVERSATION_ID]: 'sess-1', }, + authRequiredOnUnauthorized: expect.any(Function), }); if (!('headers' in resolved) || resolved.headers === undefined) { 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 () => { @@ -387,10 +398,15 @@ describe('TrueFoundryMcpServerStore', () => { Authorization: `Bearer ${ACCESS_TOKEN}`, [ACTOR_AUTHORIZATION_HEADER]: `Bearer ${ACTOR_TOKEN}`, }, + authRequiredOnUnauthorized: expect.any(Function), }); + client.getMcpAuthorize.mockResolvedValue({ + status: 'auth_required', + authorization_url: 'https://consent.example/a', + }); + await consentOn401(store); 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.vendToken).toHaveBeenCalledTimes(1); }); @@ -398,15 +414,152 @@ describe('TrueFoundryMcpServerStore', () => { const { store, client } = createStore({ agent: AGENT, userCredential: null }); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${SUBJECT_TOKEN}` }, + authRequiredOnUnauthorized: expect.any(Function), + }); + client.getMcpAuthorize.mockResolvedValue({ + status: 'auth_required', + authorization_url: 'https://consent.example/a', }); + await consentOn401(store); 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 }); +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: TrueFoundryPerServerMcpHeaders): 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(parseTrueFoundryPerServerMcpHeaders(raw)), 'virtual-mcp'); + + expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': JSON.stringify(entry) }); + }); + + it("sends that server's caller entry without Authorization as x-tfy-mcp-headers too", async () => { + const headers = await invokeHeaders( + storeWith({ 'tfy-platform-mcp': { 'X-End-User': 'alice' } }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': JSON.stringify({ 'X-End-User': 'alice' }), + }); + }); + + 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('attaches the 401 consent hook only when the server has no caller entry', async () => { + const store = storeWith({ 'tfy-platform-mcp': { 'X-End-User': 'alice' } }); + const resolveFor = async (name: string) => { + const resolve = store.resolveInvokeHeaders({ record: record(name), userRef: 'user-1' }); + if (typeof resolve !== 'function') { + throw new Error('expected a headers resolver'); + } + return resolve(); + }; + + await expect(resolveFor('tfy-docs-mcp')).resolves.toHaveProperty('authRequiredOnUnauthorized'); + await expect(resolveFor('tfy-platform-mcp')).resolves.not.toHaveProperty('authRequiredOnUnauthorized'); + }); + + 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 }, + 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'); + } + + 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(); }); }); diff --git a/packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts b/packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts index 1114ba0b4e..d52ff2156a 100644 --- a/packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts @@ -1,154 +1,24 @@ import { HTTPException } from 'hono/http-exception'; -import { createLogger } from 'winston'; -import type { McpServerRecord } from '../../../src/db/mcpServerStore'; -import { createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken'; -import { parsePerServerMcpHeaders } from '../../../src/truefoundry/perServerMcpHeaders'; -import { - TrueFoundryMcpServerStore, - type TrueFoundryMcpApiClient, -} from '../../../src/truefoundry/TrueFoundryMcpServerStore'; +import { parseTrueFoundryPerServerMcpHeaders } from '../../../src/truefoundry/perServerMcpHeaders'; -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: Record>): 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"}' }, +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' } }, }); - }); - 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 } })], - ])('rejects %s rather than silently dropping the identity it carries', (_case, raw) => { - expect(() => parsePerServerMcpHeaders(raw)).toThrow(HTTPException); - }); + const parsed = parseTrueFoundryPerServerMcpHeaders(raw); - 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('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"}', + expect(parsed).toEqual({ + 'github-mcp': { Authorization: 'Bearer user' }, + 'virtual-mcp': { serv1: { Authorization: 'Bearer a' } }, }); }); - 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'])( - 'drops an override under %s, which object keys would otherwise keep beside the Bearer', - async name => { - const headers = await invokeHeaders( - storeWith({ - 'tfy-platform-mcp': { [name]: 'Bearer smuggled' }, - }), - 'tfy-platform-mcp', - ); - - expect(Object.values(headers)).toEqual(['Bearer caller-token']); - }, - ); - - 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"}' }, - }), - 'tfy-platform-mcp', - ); + it('rejects mixed nesting', () => { + const raw = JSON.stringify({ 'virtual-mcp': { serv1: { Authorization: 'Bearer a' }, 'x-h': '1' } }); - expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': '{"a":"b"}' }); + expect(() => parseTrueFoundryPerServerMcpHeaders(raw)).toThrow(HTTPException); }); });