diff --git a/.changeset/x-tfg-mcp-headers-auth.md b/.changeset/x-tfg-mcp-headers-auth.md new file mode 100644 index 0000000000..b8aff61f03 --- /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, 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/errors.ts b/packages/trueforge-core/src/core/errors.ts index 73f7a18ff1..4cb40fe242 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 lists consent URLs (`authorization_urls`, keyed by server name). */ +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 2ee8958820..85e5b83cde 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 { @@ -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) { + const authRequired = this.authRequiredFrom(error); + 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 }; + } + } + + /** 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 connectAndRun( diff --git a/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts b/packages/trueforge-core/src/core/mcp/remoteMcpClient.ts index 0e57b401c6..6f0db6d1eb 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'; @@ -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,6 +71,29 @@ 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 { + 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('upstream requires authorization', parsed.data.authorization_urls); + }; +} + class McpClientWithTimeout extends Client { constructor(private readonly requestTimeoutMs: number) { super(CLIENT_INFO, { capabilities: {} }); @@ -128,6 +153,9 @@ 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; } @@ -212,7 +240,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..e70b68cc4a 100644 --- a/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts +++ b/packages/trueforge-core/tests/core/mcp/connectRemoteMcp.test.ts @@ -115,6 +115,34 @@ 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 })) + .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, {})).rejects.toMatchObject({ + name: 'McpAuthorizationRequiredError', + statusCode: 401, + authorizationUrls: urls, + }); + await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); + await expect(transportFetch(endpoint, {})).resolves.toMatchObject({ status: 401 }); + }); }); describe('connectRemoteMcp transport selection', () => { 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 1ec97e9406..162c709a37 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, @@ -473,6 +474,38 @@ describe('RemoteMCP + ToolSet', () => { expect(state.callToolCalls).toBe(0); }); + it('returns authRequired from a 401 that carries authorization URLs', async () => { + installFakeConnection({ + connectError: new McpAuthorizationRequiredError('auth required', { 'my-inline': 'https://auth.example/consent' }), + }); + 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' }] }, + }); + }); + + 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', async () => { + installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) }); + const server = makeServer({ headers: () => Promise.resolve({ headers: {} }) }); + + await expect(server.listTools()).rejects.toMatchObject({ 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 528bb8010f..9ef08a3496 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -110,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, @@ -122,7 +123,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..f5a82902c5 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 { IOAuthTokenStore, OAuthClientRecord } from '../mcp/auth/types'; +import { withCallerMcpHeaders, 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,6 +66,14 @@ export class McpServerWithAuthStore implements IMcpServerW userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { + return withCallerMcpHeaders({ + callerHeaders: this.#perServerHeaders[input.record.name], + registeredHeaders: this.#resolveRegisteredHeaders(input), + configuredHeaders: resolveConfiguredMcpRequestHeaders(input.record.manifest), + }); + } + + #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/resolvers.ts b/packages/trueforge/src/resolvers.ts index 6bfdbfa8f1..36a11ee69e 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,16 +43,12 @@ 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'; 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) @@ -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..a8586671fe --- /dev/null +++ b/packages/trueforge/src/runtime/perServerMcpHeaders.ts @@ -0,0 +1,103 @@ +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 set ${managed}` }); + } + return parsed.data; +} + +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. + */ +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 mergeCallerHeaders({ headers: configuredHeaders, callerHeaders }); + } + return () => resolveWithCallerHeaders({ registeredHeaders, 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 callerNames = new Set(Object.keys(input.callerHeaders).map(name => name.toLowerCase())); + return { + ...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 671384a9fe..116a388d83 100644 --- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts +++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts @@ -1,5 +1,4 @@ import { McpConnectionError, 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'; import { safeReturnTo } from '../auth/safeReturnTo'; @@ -20,6 +19,7 @@ import { } from '../db/mcpServerStore'; import type { TurnMetadata } from '../db/turnMetadata'; import type { OAuthClientRecord } from '../mcp/auth/types'; +import { hasAuthorizationHeader } from '../runtime/perServerMcpHeaders'; import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer'; import { accessTokenForRequest, @@ -37,8 +37,8 @@ import { toTrueFoundryMcpManifest, type SfyMcpServerSummary, } from './mapSfyMcpServers'; -import type { PerServerMcpHeaders } from './perServerMcpHeaders'; import type { TrueFoundryServiceFoundryServerClient } from './TrueFoundryServiceFoundryServerClient'; +import { isVirtualMcpHeaders, type TrueFoundryPerServerMcpHeaders } from './virtualMcpHeaders'; export type TrueFoundryMcpApiClient = Pick< TrueFoundryServiceFoundryServerClient, @@ -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,40 +105,28 @@ export class TrueFoundryMcpServerStore implements IMcpServ this.#perServerHeaders = input.perServerHeaders ?? {}; } - /** Gateway Bearer (+ optional per-server overrides); SFY authorize first, else authRequired. */ + /** + * Gateway Bearer (+ the caller's headers for that server); the gateway's 401 carries any consent URL. + * A caller entry with Authorization, or a virtual MCP's nested entry, goes in `x-tfy-mcp-headers`. + */ resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string; turnMetadata?: TurnMetadata; }): RemoteMcpHeaders { - const { record, userRef, turnMetadata } = input; - return async () => { - const status = await this.authorize({ - tenant_id: record.tenant_id, - name: record.name, - userRef, + const { record, turnMetadata } = input; + const callerHeaders = this.#perServerHeaders[record.name]; + const gatewayBase = async (): Promise> => ({ + ...gatewayHeaders(await this.#resolveGatewayAuthorization()), + ...gatewayMetadataHeadersForTurn(turnMetadata), + }); + 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) }, }); - 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), - ...gatewayMetadataHeadersForTurn(turnMetadata), - }; - return { headers: invokeHeaders }; - }; + } + return async () => ({ headers: { ...callerHeaders, ...(await gatewayBase()) } }); } async listServers(input: ListMcpServersInput, transaction?: TTransaction): Promise { diff --git a/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts b/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts deleted file mode 100644 index 549fd75a73..0000000000 --- a/packages/trueforge/src/truefoundry/perServerMcpHeaders.ts +++ /dev/null @@ -1,37 +0,0 @@ -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. - */ -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())); - -export type PerServerMcpHeaders = 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; -} 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 new file mode 100644 index 0000000000..b704b28e34 --- /dev/null +++ b/packages/trueforge/tests/unit/runtime/perServerMcpHeaders.test.ts @@ -0,0 +1,97 @@ +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 } })], + ['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) => { + 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('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..4e6b1b3782 100644 --- a/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts +++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundryMcpServerStore.test.ts @@ -10,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'; @@ -298,51 +302,16 @@ describe('TrueFoundryMcpServerStore', () => { return headers(); } - it('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' }], - }, - }); - }); - - it('gates non-dcr truefoundry servers through authorize as well', async () => { + it('returns the gateway Bearer without calling authorize', 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' }], - }, - }); - expect(client.getMcpAuthorize).toHaveBeenCalled(); - }); - - it('returns gateway Bearer when authorize reports authenticated', async () => { - const { store } = createStore(); await expect(invoke(store)).resolves.toEqual({ headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, }); + expect(client.getMcpAuthorize).not.toHaveBeenCalled(); }); - 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 +337,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 +347,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 +356,153 @@ 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 })); + expect(client.getMcpServerByName).not.toHaveBeenCalled(); }); + }); +}); - 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 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 caller headers 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 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' } }), + 'tfy-platform-mcp', + ); + + expect(headers).toEqual({ + Authorization: 'Bearer caller-token', + 'x-tfy-mcp-headers': JSON.stringify({ [name]: 'Bearer user' }), + }); + }, + ); + + 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 deleted file mode 100644 index 1114ba0b4e..0000000000 --- a/packages/trueforge/tests/unit/truefoundry/perServerMcpHeaders.test.ts +++ /dev/null @@ -1,154 +0,0 @@ -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'; - -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"}' }, - }); - }); - - 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); - }); - - 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"}', - }); - }); - - 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', - ); - - expect(headers).toEqual({ Authorization: 'Bearer caller-token', 'x-tfy-mcp-headers': '{"a":"b"}' }); - }); -}); 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); + }); +});