Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/mcp-unauthorized-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@truefoundry/trueforge-core': patch
'@truefoundry/trueforge': patch
---

`RemoteMCP` header resolvers can return `onUnauthorized`, used once on a 401. An MCP server rejecting an `x-tfg-mcp-headers` `Authorization` now falls back to its regular authorization flow.
5 changes: 5 additions & 0 deletions .changeset/x-tfg-mcp-headers-auth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@truefoundry/trueforge': patch
---

`x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow: forwarded directly in open-source mode (along with other per-server headers), sent as `x-tfy-mcp-headers` in TrueFoundry mode.
39 changes: 36 additions & 3 deletions packages/trueforge-core/src/core/mcp/RemoteMCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { paginateWithCursorGuard } from './pagination';
import {
connectRemoteMcp,
DEFAULT_MAX_MCP_RESPONSE_BYTES,
isAuthError,
isSessionExpiredError,
type RemoteMcpConnection,
type RemoteMcpTransportType,
Expand All @@ -39,8 +40,13 @@ function redactUrlForTrace(url: string): string {
}
}

/** What a headers resolver returns: the headers to send, or a signal that auth is required. */
export type ResolveHeadersResult = { headers: Record<string, string> } | { authRequired: MCPAuthRequired };
/**
* What a headers resolver returns: the headers to send, or a signal that auth is required.
* `onUnauthorized` is consulted once if the upstream rejects `headers` with a 401.
*/
export type ResolveHeadersResult =
| { headers: Record<string, string>; onUnauthorized?: () => Promise<ResolveHeadersResult> }
| { authRequired: MCPAuthRequired };

/** Static headers, or a resolver (invoked at connect) returning headers or signalling auth-required. */
export type RemoteMcpHeaders = Record<string, string> | (() => Promise<ResolveHeadersResult>);
Expand Down Expand Up @@ -187,7 +193,34 @@ export class RemoteMCP implements ToolSource {
if ('authRequired' in headersResult) {
return { authRequired: headersResult.authRequired };
}
return this.connectAndRun(headersResult.headers, operation, true);
const { onUnauthorized } = headersResult;
if (onUnauthorized === undefined) {
return this.connectAndRun(headersResult.headers, operation, true);
}
let rejected: RemoteMcpConnection | undefined;
const tracked = (connection: RemoteMcpConnection): Promise<T> => {
rejected = connection;
return operation(connection);
};
try {
return await this.connectAndRun(headersResult.headers, tracked, true);
} catch (error) {
if (!isAuthError(error)) {
throw error;
}
this.logger.info(`Remote MCP ${this.name} rejected supplied credentials, falling back to its auth flow`);
// Reset only the rejected connection so a sibling's newer one survives; a failed connect keeps its stale session.
if (rejected !== undefined) {
await this.resetConnection(rejected);
} else if (!this.isConnected) {
this.sessionId = undefined;
}
const fallback = await onUnauthorized();
if ('authRequired' in fallback) {
return { authRequired: fallback.authRequired };
}
return this.connectAndRun(fallback.headers, operation, true);
}
}

private async connectAndRun<T>(
Expand Down
6 changes: 5 additions & 1 deletion packages/trueforge-core/src/core/mcp/remoteMcpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,14 @@ function mcpConnectOutcome(error: unknown): McpConnectOutcome {
return 'other';
}

function isAuthError(error: unknown): boolean {
/** Upstream rejected the credentials, whether at connect or on a later request. */
export function isAuthError(error: unknown): boolean {
if (!(error instanceof Error)) {
return false;
}
if (error instanceof McpConnectionError) {
return error.statusCode === 401;
}
if ('code' in error && error.code === 401) {
return true;
}
Expand Down
33 changes: 33 additions & 0 deletions packages/trueforge-core/tests/core/mcp/remoteMcpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,39 @@ describe('RemoteMCP + ToolSet', () => {
expect(state.callToolCalls).toBe(0);
});

it('falls back to onUnauthorized when supplied credentials are rejected at connect', async () => {
installFakeConnection({ connectError: new McpConnectionError('upstream 401', 401) });
const authRequired: MCPAuthRequired = {
servers: [{ id: 'inline-id-123', name: 'my-inline', auth_url: 'https://auth.example' }],
};
const onUnauthorized = jest.fn(() => Promise.resolve({ authRequired }));
const server = makeServer({
headers: () => Promise.resolve({ headers: { Authorization: 'Bearer caller' }, onUnauthorized }),
});

await expect(server.listTools()).resolves.toEqual({ authRequired });
expect(onUnauthorized).toHaveBeenCalledTimes(1);
});

it('reconnects with fallback headers when a call is rejected mid-session', async () => {
const state = installFakeConnection({ sessionId: 'sess-1' });
const server = makeServer({
headers: () =>
Promise.resolve({
headers: { Authorization: 'Bearer caller' },
onUnauthorized: () => Promise.resolve({ headers: { Authorization: 'Bearer fallback' } }),
}),
});

await server.listTools();
state.queueCallToolError(Object.assign(new Error('HTTP 401'), { code: 401 }));
const res = await server.callTool({ name: 'read_thing', arguments: {} });

expect(res).toMatchObject({ result: { content: [{ type: 'text', text: 'called read_thing' }] } });
expect(state.connectCalls).toBe(2);
expect(mockConnect.mock.lastCall?.[0].headers).toEqual({ Authorization: 'Bearer fallback' });
});

it('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 });
Expand Down
2 changes: 1 addition & 1 deletion packages/trueforge/src/apis/turns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import type { ISkillStore } from '../db/skillStore';
import type { TurnMetadata } from '../db/turnMetadata';
import type { IWebSearchProviderStore } from '../db/webSearchProviderStore';
import { currentLogContext, runWithLogContext, updateLogContext } from '../logger';
import { X_TFG_MCP_HEADERS } from '../mcp/perServerMcpHeaders';
import type { TenantMemoryDeps } from '../memory/providers';
import { resolveSessionMemory } from '../memory/sessionMemory';
import {
Expand Down Expand Up @@ -107,7 +108,6 @@ import { restoreTurnEventLog } from '../runtime/turnEventLogRestore';
import { MAX_SESSION_TITLE_LENGTH } from '../schemas/session';
import { assertGatewayMetadataRequestHeaders, X_TFY_METADATA } from '../truefoundry/gatewayMetadata';
import { X_TFG_MCP, X_TFG_MODELS, X_TFG_SKILLS, X_TFG_TOOLS } from '../truefoundry/inlineResources';
import { X_TFG_MCP_HEADERS } from '../truefoundry/perServerMcpHeaders';
import { newId } from '../utils/id';
import { resolveWebSearchProvider } from '../websearch/providers';
import { canReadAgentBoundResource, canReadSession } from './agentAccess';
Expand Down
50 changes: 49 additions & 1 deletion packages/trueforge/src/db/McpServerWithAuthStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { RemoteMcpHeaders } from '@truefoundry/trueforge-core/core';
import type { RemoteMcpHeaders, ResolveHeadersResult } from '@truefoundry/trueforge-core/core';
import { isMcpAuthRequired, resolveMcpAuth } from '../mcp/auth/mcpDcr';
import type { IOAuthTokenStore, OAuthClientRecord } from '../mcp/auth/types';
import {
hasAuthorizationHeader,
overrideHeaders,
withoutAuthorization,
type PerServerMcpHeaders,
} from '../mcp/perServerMcpHeaders';
import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer';
import {
McpServerNotFoundError,
Expand All @@ -18,19 +24,33 @@ import {
} from './mcpServerStore';
import type { TurnMetadata } from './turnMetadata';

async function resolveWithOverride(input: {
headers: RemoteMcpHeaders;
override: Record<string, string>;
}): Promise<ResolveHeadersResult> {
const result = typeof input.headers === 'function' ? await input.headers() : { headers: input.headers };
if ('authRequired' in result) {
return result;
}
return { headers: overrideHeaders({ base: result.headers, override: input.override }) };
}

export class McpServerWithAuthStore<TTransaction = never> implements IMcpServerWithAuthStore<TTransaction> {
readonly #store: IMcpServerStore<TTransaction>;
readonly #tokenStore: IOAuthTokenStore<TTransaction>;
readonly #clientName: string;
readonly #perServerHeaders: PerServerMcpHeaders;

constructor(input: {
store: IMcpServerStore<TTransaction>;
tokenStore: IOAuthTokenStore<TTransaction>;
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<McpServerRecord[]> {
Expand All @@ -57,11 +77,39 @@ export class McpServerWithAuthStore<TTransaction = never> implements IMcpServerW
return this.#store.deleteServer(input, transaction);
}

/** Caller headers for the server go straight to it; one with Authorization skips its consent flow. */
resolveInvokeHeaders(input: {
record: McpServerRecord;
userRef: string;
turnMetadata?: TurnMetadata;
}): RemoteMcpHeaders {
const registered = this.#resolveRegisteredHeaders(input);
const callerHeaders = this.#perServerHeaders[input.record.name];
if (callerHeaders === undefined) {
return registered;
}
if (hasAuthorizationHeader(callerHeaders)) {
const headers = overrideHeaders({
base: resolveConfiguredMcpRequestHeaders(input.record.manifest),
override: callerHeaders,
});
const override = withoutAuthorization(callerHeaders);
return () =>
Promise.resolve({ headers, onUnauthorized: () => resolveWithOverride({ headers: registered, override }) });
}
if (typeof registered !== 'function') {
return overrideHeaders({ base: registered, override: callerHeaders });
}
return async () => {
const result = await registered();
if ('authRequired' in result) {
return result;
}
return { headers: overrideHeaders({ base: result.headers, override: callerHeaders }) };
};
}

#resolveRegisteredHeaders(input: { record: McpServerRecord; userRef: string }): RemoteMcpHeaders {
const { record, userRef } = input;
if (record.manifest.auth?.type === 'dcr') {
return async () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/trueforge/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { z } from 'zod';
* TrueForge authenticates the whole turn as one caller, so every MCP connection would otherwise
* present that same identity. This carries the identity each individual server should see —
* typically the end user behind the caller, for a server that resolves its own permissions.
* An entry carrying Authorization is used instead of that server's consent flow unless rejected.
*/
export const X_TFG_MCP_HEADERS = 'x-tfg-mcp-headers';

Expand Down Expand Up @@ -35,3 +36,26 @@ export function parsePerServerMcpHeaders(raw: string): PerServerMcpHeaders {
}
return parsed.data;
}

export function hasAuthorizationHeader(headers: Record<string, string> | undefined): headers is Record<string, string> {
return headers !== undefined && Object.keys(headers).some(name => name.toLowerCase() === 'authorization');
}

export function withoutAuthorization(headers: Record<string, string> | undefined): Record<string, string> {
if (headers === undefined) {
return {};
}
return Object.fromEntries(Object.entries(headers).filter(([name]) => name.toLowerCase() !== 'authorization'));
}

/** Header names are case-insensitive, so an override must replace a base entry under any casing. */
export function overrideHeaders(input: {
base: Record<string, string>;
override: Record<string, string>;
}): Record<string, string> {
const overridden = new Set(Object.keys(input.override).map(name => name.toLowerCase()));
return {
...Object.fromEntries(Object.entries(input.base).filter(([name]) => !overridden.has(name.toLowerCase()))),
...input.override,
};
}
17 changes: 11 additions & 6 deletions packages/trueforge/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { ISandboxProviderStore } from './db/sandboxProviderStore';
import type { ISkillStore } from './db/skillStore';
import type { IWebSearchProviderStore } from './db/webSearchProviderStore';
import type { IOAuthTokenStore } from './mcp/auth/types';
import { parsePerServerMcpHeaders, X_TFG_MCP_HEADERS, type PerServerMcpHeaders } from './mcp/perServerMcpHeaders';
import type { MemoryProviderCache, TenantMemoryDeps } from './memory/providers';
import { headerValue } from './truefoundry/gatewayMetadata';
import { InlineMcpServerStore } from './truefoundry/InlineMcpServerStore';
Expand All @@ -42,11 +43,6 @@ import {
} from './truefoundry/inlineResources';
import { InlineSkillStore } from './truefoundry/InlineSkillStore';
import { InlineCapabilitiesStore } from './truefoundry/inlineTools';
import {
parsePerServerMcpHeaders,
X_TFG_MCP_HEADERS,
type PerServerMcpHeaders,
} from './truefoundry/perServerMcpHeaders';
import { TrueFoundryAgentStore } from './truefoundry/TrueFoundryAgentStore';
import { TrueFoundryMcpServerStore } from './truefoundry/TrueFoundryMcpServerStore';
import { TrueFoundryModelProviderStore } from './truefoundry/TrueFoundryModelProviderStore';
Expand Down Expand Up @@ -110,7 +106,8 @@ export function buildResolveModelProviderStore<TTransaction>(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<TTransaction>(options: {
persistenceStore: IMcpServerStore<TTransaction>;
Expand Down Expand Up @@ -138,6 +135,14 @@ export function buildResolveMcpServerStore<TTransaction>(options: {
}
return store;
}
if (input.perServerHeaders !== undefined) {
return new McpServerWithAuthStore<TTransaction>({
store: options.persistenceStore,
tokenStore: options.tokenStore,
clientName: configuration.MCP_DCR_OAUTH_CLIENT_NAME,
perServerHeaders: input.perServerHeaders,
});
}
return withAuthMcpPersistenceStore;
};
}
Expand Down
Loading
Loading