Skip to content

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

feat: Pass caller TF access token as Bearer for tfy mcp invoke (#555)

Srajan Asthana32bf7d629b87
diff --git a/.changeset/tf-mcp-gateway-bearer.md b/.changeset/tf-mcp-gateway-bearer.md
new file mode 100644
index 000000000..441759f31
--- /dev/null
+++ b/.changeset/tf-mcp-gateway-bearer.md
@@ -0,0 +1,5 @@
+---
+"@truefoundry/trueforge": patch
+---
+
+TrueFoundry MCP invoke headers are owned by the MCP store (`resolveInvokeHeaders`), so gateway Bearer comes from the request-scoped store rather than being threaded through turn/tools APIs.
diff --git a/packages/trueforge/src/apis/mcpServers.ts b/packages/trueforge/src/apis/mcpServers.ts
index 32bb845bd..5427d88a2 100644
--- a/packages/trueforge/src/apis/mcpServers.ts
+++ b/packages/trueforge/src/apis/mcpServers.ts
@@ -354,7 +354,7 @@ export function createMcpServersRouter<TTransaction>(deps: McpServersRouterDeps<
const listToolsHandler: RouteHandler<typeof listMcpServerToolsRoute> = async c => {
const { name } = c.req.valid('param');
const userRef = deps.resolveUserContext(c).userRef;
- // Same url + header resolution as turn execution (DCR via resolveMcpAuth, header/no-auth static).
+ // Same url + header resolution as turn execution (store Bearer, DCR, or static headers).
const connection = await getMcpConnection({
tenant_id: TENANT_ID,
name,
diff --git a/packages/trueforge/src/auth/middleware.ts b/packages/trueforge/src/auth/middleware.ts
index 719de02df..2d2bf2f94 100644
--- a/packages/trueforge/src/auth/middleware.ts
+++ b/packages/trueforge/src/auth/middleware.ts
@@ -32,10 +32,16 @@ export function readBearerToken(c: Context): string | undefined {
return token.length > 0 ? token : undefined;
}
+export function readAccessToken(c: Context): string | undefined {
+ return readBearerToken(c) ?? readAccessTokenCookie({ context: c }) ?? readIdTokenCookie({ context: c });
+}
+
export function requireAccessToken(c: Context): string {
- const token = readBearerToken(c) ?? readAccessTokenCookie({ context: c }) ?? readIdTokenCookie({ context: c });
+ const token = readAccessToken(c);
if (!token) {
- throw new HTTPException(401, { message: 'Authentication token required to list or call TrueFoundry models' });
+ throw new HTTPException(401, {
+ message: 'Authentication token required to list or call TrueFoundry models and MCP servers',
+ });
}
return token;
}
diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.ts
index 9828d27fb..f646ab292 100644
--- a/packages/trueforge/src/db/McpServerWithAuthStore.ts
+++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts
@@ -54,6 +54,10 @@ export class McpServerWithAuthStore<TTransaction = never> implements IMcpServerW
return this.#store.upsertServer(input, transaction);
}
+ resolveInvokeHeaders(record: McpServerRecord): Record<string, string> {
+ return this.#store.resolveInvokeHeaders(record);
+ }
+
saveClient(params: { id: string; record: OAuthClientRecord }, transaction?: TTransaction): Promise<void> {
return this.#store.saveClient(params, transaction);
}
diff --git a/packages/trueforge/src/db/mcpServerStore.ts b/packages/trueforge/src/db/mcpServerStore.ts
index 99277c12c..5889fcba8 100644
--- a/packages/trueforge/src/db/mcpServerStore.ts
+++ b/packages/trueforge/src/db/mcpServerStore.ts
@@ -113,6 +113,12 @@ export interface IMcpServerStore<TTransaction = never> extends IOAuthClientStore
* Never overwrites `id`, `oauth_server`, or `oauth_client`.
*/
upsertServer(input: UpsertMcpServerInput, transaction?: TTransaction): Promise<McpServerRecord>;
+ /**
+ * Static HTTP headers for MCP invoke (tools/list, turns).
+ * Local DCR is handled separately in {@link getMcpConnection}; this covers
+ * TrueFoundry gateway Bearer, configured header auth, and no-auth (`{}`).
+ */
+ resolveInvokeHeaders(record: McpServerRecord): Record<string, string>;
}
/**
diff --git a/packages/trueforge/src/db/postgres/mcp-server-store/PostgresMcpServerStore.ts b/packages/trueforge/src/db/postgres/mcp-server-store/PostgresMcpServerStore.ts
index 148531257..b13a46b4e 100644
--- a/packages/trueforge/src/db/postgres/mcp-server-store/PostgresMcpServerStore.ts
+++ b/packages/trueforge/src/db/postgres/mcp-server-store/PostgresMcpServerStore.ts
@@ -1,5 +1,6 @@
import type { Kysely, Selectable, Transaction } from 'kysely';
import type { OAuthClientRecord } from '../../../mcp/auth/types';
+import { resolveConfiguredMcpRequestHeaders } from '../../../schemas/mcpServer';
import { newId } from '../../../utils/id';
import {
fromStoredOAuthClientRecord,
@@ -34,6 +35,10 @@ export class PostgresMcpServerStore implements IMcpServerStore<Transaction<Datab
this.#db = db;
}
+ resolveInvokeHeaders(record: McpServerRecord): Record<string, string> {
+ return resolveConfiguredMcpRequestHeaders(record.manifest);
+ }
+
async listServers(input: ListMcpServersInput, transaction?: Transaction<Database>): Promise<McpServerRecord[]> {
if (input.names?.length === 0) {
return [];
diff --git a/packages/trueforge/src/db/sqlite/mcp-server-store/SqliteMcpServerStore.ts b/packages/trueforge/src/db/sqlite/mcp-server-store/SqliteMcpServerStore.ts
index a4e13824c..8b9a0a893 100644
--- a/packages/trueforge/src/db/sqlite/mcp-server-store/SqliteMcpServerStore.ts
+++ b/packages/trueforge/src/db/sqlite/mcp-server-store/SqliteMcpServerStore.ts
@@ -1,6 +1,6 @@
import type { ExpressionBuilder, Kysely, Transaction } from 'kysely';
import type { OAuthClientRecord } from '../../../mcp/auth/types';
-import type { McpServerManifest } from '../../../schemas/mcpServer';
+import { resolveConfiguredMcpRequestHeaders, type McpServerManifest } from '../../../schemas/mcpServer';
import { newId } from '../../../utils/id';
import {
fromStoredOAuthClientRecord,
@@ -38,6 +38,10 @@ export class SqliteMcpServerStore implements IMcpServerStore<Transaction<Databas
this.#db = db;
}
+ resolveInvokeHeaders(record: McpServerRecord): Record<string, string> {
+ return resolveConfiguredMcpRequestHeaders(record.manifest);
+ }
+
async listServers(input: ListMcpServersInput, transaction?: Transaction<Database>): Promise<McpServerRecord[]> {
if (input.names?.length === 0) {
return [];
diff --git a/packages/trueforge/src/runtime/sessionResources.ts b/packages/trueforge/src/runtime/sessionResources.ts
index 0255aa27f..862ea0674 100644
--- a/packages/trueforge/src/runtime/sessionResources.ts
+++ b/packages/trueforge/src/runtime/sessionResources.ts
@@ -26,7 +26,6 @@ import type { IOAuthTokenStore } from '../mcp/auth/types';
import { LocalSandboxProvider } from '../sandbox/local/provider/LocalSandboxProvider';
import { getCachedLocalSandboxSupport, isLocalSandboxFallbackEnabled } from '../sandbox/localRuntime';
import { toDaytonaSandboxProvider } from '../sandbox/providerUtils';
-import { resolveConfiguredMcpRequestHeaders } from '../schemas/mcpServer';
import type { ReasoningEffort } from '../schemas/modelProvider';
export interface McpConnection {
@@ -134,8 +133,9 @@ function dcrHeadersResolver(params: {
/**
* Load MCP url + headers for a configured server.
- * - `remote` + `dcr`: resolveMcpAuth via the harness token store.
- * - header / no-auth: resolveConfiguredMcpRequestHeaders.
+ * - Local `remote` + `dcr`: resolveMcpAuth via the harness token store.
+ * - Otherwise: {@link IMcpServerStore.resolveInvokeHeaders}
+ * (TrueFoundry gateway Bearer, configured header auth, or `{}`).
* Returns undefined when the server is not registered — callers choose the response.
*/
export async function getMcpConnection({
@@ -157,7 +157,8 @@ export async function getMcpConnection({
if (record === undefined) {
return undefined;
}
- if (record.manifest.auth?.type === 'dcr') {
+ // TrueFoundry wire `dcr` is Connect UX only — invoke uses store Bearer, not local DCR.
+ if (record.manifest.type !== 'truefoundry' && record.manifest.auth?.type === 'dcr') {
return {
url: record.manifest.url,
headers: dcrHeadersResolver({
@@ -171,7 +172,7 @@ export async function getMcpConnection({
}
return {
url: record.manifest.url,
- headers: resolveConfiguredMcpRequestHeaders(record.manifest),
+ headers: store.resolveInvokeHeaders(record),
};
}
diff --git a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts
index 92593fa53..f1cdfa44d 100644
--- a/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts
+++ b/packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts
@@ -41,6 +41,11 @@ export class TrueFoundryMcpServerStore<TTransaction = never> implements IMcpServ
this.#accessToken = input.accessToken;
}
+ resolveInvokeHeaders(record: McpServerRecord): Record<string, string> {
+ void record;
+ return { Authorization: `Bearer ${this.#accessToken}` };
+ }
+
async listServers(input: ListMcpServersInput, transaction?: TTransaction): Promise<McpServerRecord[]> {
void transaction;
if (input.names?.length === 0) {
diff --git a/packages/trueforge/src/truefoundry/mapSfyMcpServers.ts b/packages/trueforge/src/truefoundry/mapSfyMcpServers.ts
index 97168f581..4cf844474 100644
--- a/packages/trueforge/src/truefoundry/mapSfyMcpServers.ts
+++ b/packages/trueforge/src/truefoundry/mapSfyMcpServers.ts
@@ -27,8 +27,8 @@ const SfyMcpServerRowSchema = z.object({
id: z.string().min(1),
name: z.string().min(1),
proxyUrl: z.string().min(1),
- createdAt: IsoInstantSchema.optional(),
- updatedAt: IsoInstantSchema.optional(),
+ createdAt: IsoInstantSchema,
+ updatedAt: IsoInstantSchema,
manifest: z
.object({
description: z.string().min(1).optional(),
@@ -55,15 +55,15 @@ export interface SfyMcpServerSummary {
/** Parse one SFY MCP list row. Throws ZodError on contract drift. */
export function parseSfyMcpServerSummary(row: unknown): SfyMcpServerSummary {
const parsed = SfyMcpServerRowSchema.parse(row);
- const now = new Date().toISOString();
return {
id: parsed.id,
name: parsed.name,
proxyUrl: parsed.proxyUrl,
+ // SFY description is optional; TrueForge manifests require a non-empty string.
description: parsed.manifest?.description ?? parsed.name,
authType: parsed.manifest?.auth_data?.type,
- createdAt: parsed.createdAt ?? now,
- updatedAt: parsed.updatedAt ?? now,
+ createdAt: parsed.createdAt,
+ updatedAt: parsed.updatedAt,
};
}
@@ -83,7 +83,7 @@ export function resolveMcpProxyUrl(input: { proxyUrl: string; gatewayBaseURL: st
/**
* Gateway proxy as `url`. SFY `oauth2` → wire `dcr` for Connect UX only (UI keys off auth type).
- * Invoke Bearer is intentionally not embedded as `header` auth — that breaks the UI and is a follow-up PR.
+ * Invoke Bearer comes from the MCP store's `resolveInvokeHeaders` — not wire `header` auth.
*/
export function toTrueFoundryMcpManifest(input: {
server: SfyMcpServerSummary;
diff --git a/packages/trueforge/tests/db/mcpServerStoreContractSuite.ts b/packages/trueforge/tests/db/mcpServerStoreContractSuite.ts
index 7d0d28f9f..3b91ef9ea 100644
--- a/packages/trueforge/tests/db/mcpServerStoreContractSuite.ts
+++ b/packages/trueforge/tests/db/mcpServerStoreContractSuite.ts
@@ -176,4 +176,29 @@ export function runMcpServerStoreContractSuite(getStore: () => IMcpServerStore):
const store = getStore();
expect(await store.getClient({ id: 'missing-id' })).toBeUndefined();
});
+
+ it('resolveInvokeHeaders returns configured headers or empty', async () => {
+ const store = getStore();
+ const open = await store.upsertServer({
+ tenant_id: TENANT,
+ name: 'open',
+ manifest: {
+ type: 'remote',
+ name: 'open',
+ url: 'https://mcp.open.example/mcp',
+ description: 'Open MCP server.',
+ },
+ });
+ expect(store.resolveInvokeHeaders(open)).toEqual({});
+
+ const headered = await store.upsertServer({
+ tenant_id: TENANT,
+ name: 'headered',
+ manifest: manifest({
+ name: 'headered',
+ auth: { type: 'header', headers: { Authorization: 'Bearer static' } },
+ }),
+ });
+ expect(store.resolveInvokeHeaders(headered)).toEqual({ Authorization: 'Bearer static' });
+ });
}
diff --git a/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts b/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts
index 5c5ee5be2..2abf4f555 100644
--- a/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts
+++ b/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts
@@ -211,6 +211,33 @@ describe('getMcpConnection', () => {
expect(connection.headers).toEqual({ Authorization: 'Bearer static-token' });
});
+ it('skips local DCR for truefoundry servers even when wire auth is dcr', async () => {
+ await mcpServerStore.upsertServer({
+ tenant_id: TENANT_ID,
+ name: 'tfy-mcp',
+ manifest: {
+ type: 'truefoundry',
+ name: 'tfy-mcp',
+ url: 'https://gateway.example/mcp-server/tfy-mcp',
+ description: 'TrueFoundry-managed MCP.',
+ auth: { type: 'dcr' },
+ },
+ });
+
+ const connection = await getMcpConnection({
+ tenant_id: TENANT_ID,
+ name: 'tfy-mcp',
+ store: mcpServerStore,
+ tokenStore,
+ clientName: 'test-client',
+ userRef: LOCAL_USER_CONTEXT.userRef,
+ });
+ expect(connection).toEqual({
+ url: 'https://gateway.example/mcp-server/tfy-mcp',
+ headers: {},
+ });
+ });
+
it('returns undefined when the server is not registered', async () => {
await expect(
getMcpConnection({
diff --git a/packages/trueforge/tests/unit/truefoundry/mapSfyMcpServers.test.ts b/packages/trueforge/tests/unit/truefoundry/mapSfyMcpServers.test.ts
index 1ad608a4d..efde13d77 100644
--- a/packages/trueforge/tests/unit/truefoundry/mapSfyMcpServers.test.ts
+++ b/packages/trueforge/tests/unit/truefoundry/mapSfyMcpServers.test.ts
@@ -66,6 +66,16 @@ describe('parseSfyMcpServerSummary', () => {
expect(parsed.updatedAt).toBe('2026-02-02T00:00:00.000Z');
});
+ it('throws ZodError when required timestamps are missing', () => {
+ expect(() =>
+ parseSfyMcpServerSummary({
+ id: 'mcp-1',
+ name: 'github',
+ proxyUrl: `${MCP_PROXY_BASE_URL_TEMPLATE}/mcp-server/github`,
+ }),
+ ).toThrow(ZodError);
+ });
+
it('throws ZodError on contract drift', () => {
expect(() => parseSfyMcpServerSummary({ id: 'mcp-1', name: 'github' })).toThrow(ZodError);
});