Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tf-mcp-auth-all-modes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@truefoundry/trueforge": patch
---

TrueFoundry MCP: live SFY auth status on single-server GET and mid-turn authorize gating for every auth mode.
78 changes: 36 additions & 42 deletions packages/trueforge/src/truefoundry/TrueFoundryMcpServerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,34 @@ export class TrueFoundryMcpServerStore<TTransaction = never> implements IMcpServ
this.#perServerHeaders = input.perServerHeaders ?? {};
}

/** Bearer resolved at connect time plus optional per-server overrides; oauth servers re-check auth first. */
/** Gateway Bearer (+ optional per-server overrides); SFY authorize first, else authRequired. */
resolveInvokeHeaders(input: { record: McpServerRecord; userRef: string }): RemoteMcpHeaders {
const { record, userRef } = input;
const headers = async (): Promise<Record<string, string>> => ({
...withoutAuthorization(this.#perServerHeaders[record.name]),
Authorization: `Bearer ${await this.#resolveAccessToken()}`,
});
if (record.manifest.auth?.type === 'dcr') {
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 }],
},
};
return async () => {
const status = await this.authorize({
tenant_id: record.tenant_id,
name: record.name,
userRef,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invoke requires OAuth public URL

Medium Severity

resolveInvokeHeaders now always calls authorize before returning gateway headers. authorize always builds a redirect URL via resolveAuthorizeRedirectURL, which fails with a 500 if PUBLIC_BASE_URL is empty. No-auth and header MCP invokes previously skipped this path, so a TrueFoundry deploy without PUBLIC_BASE_URL can now fail every mid-turn tool call, not just OAuth.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0b0ca27. Configure here.

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 { headers: await headers() };
};
}
return async () => ({ headers: await headers() });
return {
authRequired: {
servers: [{ id: record.name, name: record.name, auth_url: authUrl }],
},
};
}
return { headers: await headers() };
};
}

async listServers(input: ListMcpServersInput, transaction?: TTransaction): Promise<McpServerRecord[]> {
Expand Down Expand Up @@ -190,28 +187,25 @@ export class TrueFoundryMcpServerStore<TTransaction = never> implements IMcpServ
void input.userRef;
const out = new Map<string, McpAuthStatus>();

if (input.records.length > 1) {
for (const record of input.records) {
out.set(record.name, resolveMcpAuthStatus({ manifest: record.manifest }));
// List responses stay stubbed; single-server GET (e.g. GET /mcp-servers/{name}) hits SFY live status
// for every auth mode (oauth2, header/env per-user, …).
const [record] = input.records;
if (record === undefined || input.records.length !== 1) {
for (const item of input.records) {
out.set(item.name, resolveMcpAuthStatus({ manifest: item.manifest }));
Comment thread
heerambavi1998 marked this conversation as resolved.
Resolved
}
return out;
}

for (const record of input.records) {
if (record.manifest.auth?.type === 'dcr') {
out.set(
record.name,
await this.#client.getMcpAuthStatus({
accessToken: await this.#resolveAccessToken(),
mcpServerId: record.id,
subjectId: this.#subject.id,
subjectType: this.#subject.type,
}),
);
continue;
}
out.set(record.name, resolveMcpAuthStatus({ manifest: record.manifest }));
}
out.set(
record.name,
await this.#client.getMcpAuthStatus({
accessToken: await this.#resolveAccessToken(),
mcpServerId: record.id,
subjectId: this.#subject.id,
subjectType: this.#subject.type,
}),
);
return out;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getPublicBaseUrl } from '../../../src/config';
import { McpServerNotFoundError } from '../../../src/db/mcpServerStore';
import { McpServerNotFoundError, type McpServerRecord } from '../../../src/db/mcpServerStore';
import { createTrueFoundryRequestContext } from '../../../src/truefoundry/accessToken';
import { MCP_PROXY_BASE_URL_TEMPLATE } from '../../../src/truefoundry/mapSfyMcpServers';
import type { TrueFoundryMcpApiClient } from '../../../src/truefoundry/TrueFoundryMcpServerStore';
Expand Down Expand Up @@ -82,6 +82,23 @@ function dcrRecord(overrides: { name?: string } = {}) {
};
}

function truefoundryRecordWithoutAuth(overrides: { name?: string; id?: string } = {}) {
const name = overrides.name ?? 'private-api';
return {
id: overrides.id ?? 'mcp-id-header',
tenant_id: TENANT,
name,
manifest: {
type: 'truefoundry' as const,
name,
url: `https://gateway.example/mcp-server/${name}`,
description: 'Per-user token MCP',
},
created_at: '2026-01-15T12:00:00.000Z',
updated_at: '2026-01-16T12:00:00.000Z',
};
}

describe('resolveAuthorizeRedirectURL', () => {
it('builds an absolute FE landing from return_to', () => {
const returnTo = '/?screenType=mcp-auth&pUid=popup-1';
Expand Down Expand Up @@ -162,7 +179,7 @@ describe('TrueFoundryMcpServerStore', () => {
expect(statuses.get('slack')).toEqual({ status: 'authenticated' });
});

it('calls live status for a single record', async () => {
it('calls live status for a single dcr record', async () => {
const { store, client } = createStore();
client.getMcpAuthStatus.mockResolvedValue({
status: 'auth_required',
Expand All @@ -183,13 +200,30 @@ describe('TrueFoundryMcpServerStore', () => {
authorization_url: 'https://consent.example/authorize',
});
});

it('calls live status for a single truefoundry record without wire auth', async () => {
const { store, client } = createStore();
client.getMcpAuthStatus.mockResolvedValue({ status: 'auth_required' });
const record = truefoundryRecordWithoutAuth();
const statuses = await store.resolveAuthStatuses({
records: [record],
userRef: 'user-1',
});
expect(client.getMcpAuthStatus).toHaveBeenCalledWith({
accessToken: ACCESS_TOKEN,
mcpServerId: record.id,
subjectId: 'user-1',
subjectType: 'user',
});
expect(statuses.get(record.name)).toEqual({ status: 'auth_required' });
});
});

describe('resolveInvokeHeaders mid-turn', () => {
async function invoke(store: TrueFoundryMcpServerStore) {
const headers = store.resolveInvokeHeaders({ record: dcrRecord(), userRef: 'user-1' });
async function invoke(store: TrueFoundryMcpServerStore, record: McpServerRecord = dcrRecord()) {
const headers = store.resolveInvokeHeaders({ record, userRef: 'user-1' });
if (typeof headers !== 'function') {
throw new Error('expected async headers resolver for truefoundry+dcr');
throw new Error('expected async headers resolver for truefoundry MCP');
}
return headers();
}
Expand All @@ -207,6 +241,29 @@ describe('TrueFoundryMcpServerStore', () => {
});
});

it('gates non-dcr truefoundry servers through authorize as well', async () => {
const { store, client } = createStore();
const record = truefoundryRecordWithoutAuth();
client.getMcpServerByName.mockResolvedValue({
id: record.id,
name: record.name,
proxyUrl: `${MCP_PROXY_BASE_URL_TEMPLATE}/mcp-server/${record.name}`,
createdAt: record.created_at,
updatedAt: record.updated_at,
manifest: { description: record.manifest.description, auth_data: { type: 'header' } },
});
client.getMcpAuthorize.mockResolvedValue({
status: 'auth_required',
authorization_url: 'https://consent.example/auth-override',
});
await expect(invoke(store, record)).resolves.toEqual({
authRequired: {
servers: [{ id: record.name, name: record.name, auth_url: 'https://consent.example/auth-override' }],
},
});
expect(client.getMcpAuthorize).toHaveBeenCalled();
});

it('returns gateway Bearer when authorize reports authenticated', async () => {
const { store } = createStore();
await expect(invoke(store)).resolves.toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import {
function unusedClient(): TrueFoundryMcpApiClient {
const unused = (): Promise<never> => Promise.reject(new Error('unused'));
return {
getMcpServerByName: unused,
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: unused,
getMcpAuthorize: unused,
listGatewayInstallations: async () => [{ isDefault: true, manifest: { url: 'https://gateway.example' } }],
getMcpAuthorize: async () => ({ status: 'authenticated' }),
getMcpAuthStatus: unused,
deleteMcpAuth: unused,
vendToken: () => Promise.resolve('caller-token'),
Expand Down
Loading