The open-source agent harness - the runtime layer that turns an LLM into a working agent.
fix: move resolveInvokeHeaders to McpServerWithAuthStore (#576)
Heer Ambavi80d5beea547a
diff --git a/.changeset/mcp-invoke-headers-with-auth.md b/.changeset/mcp-invoke-headers-with-auth.mdnew file mode 100644index 000000000..e4635c716--- /dev/null+++ b/.changeset/mcp-invoke-headers-with-auth.md@@ -0,0 +1,5 @@+---+"@truefoundry/trueforge": patch+---++Move `resolveInvokeHeaders` onto `IMcpServerWithAuthStore` (not `IMcpServerStore`) so DB backends stay CRUD-only and turn/MCP invoke paths take the request-scoped with-auth store for configured headers and TrueFoundry gateway Bearer.diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.tsindex 420069471..f026fd5a4 100644--- a/packages/trueforge/src/apis/turns.ts+++ b/packages/trueforge/src/apis/turns.ts@@ -32,7 +32,7 @@ import type { Logger } from 'winston';import type { ResolveUserContext, UserContext } from '../auth/identity';import configuration from '../config';import type { IAgentStore } from '../db/agentStore';-import type { IMcpServerStore } from '../db/mcpServerStore';+import type { IMcpServerWithAuthStore } from '../db/mcpServerStore';import type { IModelProviderStore } from '../db/modelProviderStore';import type { ISandboxProviderStore } from '../db/sandboxProviderStore';import type { ISkillStore } from '../db/skillStore';@@ -106,7 +106,7 @@ export interface TurnsRouterDeps {sessionStore: ISessionStore;activeTurns: ActiveTurnRegistry;resolveModelProviderStore: (c: Context) => IModelProviderStore;- resolveMcpServerStore: (c: Context) => IMcpServerStore;+ resolveMcpServerStore: (c: Context) => IMcpServerWithAuthStore;tokenStore: IOAuthTokenStore;skillStore: ISkillStore;agentStore: IAgentStore;@@ -127,7 +127,7 @@ export type BeginTurnExecutionDeps = Pick<'activeTurns' | 'eventSubscriptions' | 'tokenStore' | 'skillStore' | 'agentStore' | 'sandboxProviderStore' | 'logger'> & {modelProviderStore: IModelProviderStore;- mcpServerStore: IMcpServerStore;+ mcpServerStore: IMcpServerWithAuthStore;};/**@@ -135,7 +135,7 @@ export type BeginTurnExecutionDeps = Pick<* the same way: async factories over the corresponding stores.*/function createTurnResolver(deps: {- mcpServerStore: IMcpServerStore;+ mcpServerStore: IMcpServerWithAuthStore;tokenStore: IOAuthTokenStore;skillStore: ISkillStore;sandboxProviderStore: ISandboxProviderStore;diff --git a/packages/trueforge/src/db/McpServerWithAuthStore.ts b/packages/trueforge/src/db/McpServerWithAuthStore.tsindex f646ab292..47272af99 100644--- a/packages/trueforge/src/db/McpServerWithAuthStore.ts+++ b/packages/trueforge/src/db/McpServerWithAuthStore.ts@@ -1,10 +1,11 @@/*** Wraps a DB-backed {@link IMcpServerStore} with local DCR authorize / status / revoke- * so API handlers can call auth methods on the store without depending on a token store.+ * and configured invoke headers so API handlers can call auth methods on the store+ * without depending on a token store.*/import { isMcpAuthRequired, resolveMcpAuth } from '../mcp/auth/mcpDcr';import type { IOAuthTokenStore, OAuthClientRecord } from '../mcp/auth/types';-import { resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer';+import { resolveConfiguredMcpRequestHeaders, resolveMcpAuthStatus, type McpAuthStatus } from '../schemas/mcpServer';import {McpServerNotFoundError,type AuthorizeMcpServerInput,@@ -55,7 +56,7 @@ export class McpServerWithAuthStore<TTransaction = never> implements IMcpServerW}resolveInvokeHeaders(record: McpServerRecord): Record<string, string> {- return this.#store.resolveInvokeHeaders(record);+ return resolveConfiguredMcpRequestHeaders(record.manifest);}saveClient(params: { id: string; record: OAuthClientRecord }, transaction?: TTransaction): Promise<void> {diff --git a/packages/trueforge/src/db/mcpServerStore.ts b/packages/trueforge/src/db/mcpServerStore.tsindex 5889fcba8..225a8b69e 100644--- a/packages/trueforge/src/db/mcpServerStore.ts+++ b/packages/trueforge/src/db/mcpServerStore.ts@@ -4,7 +4,8 @@* - {@link IMcpServerStore}: CRUD + DCR client columns (`IOAuthClientStore`).* Implemented by PostgresMcpServerStore, SqliteMcpServerStore, and* TrueFoundryMcpServerStore (read-only ServiceFoundry listing).- * - {@link IMcpServerWithAuthStore}: persistence + authorize / status / revoke;+ * - {@link IMcpServerWithAuthStore}: persistence + authorize / status / revoke ++ * {@link IMcpServerWithAuthStore.resolveInvokeHeaders};* {@link McpServerWithAuthStore} composes an {@link IMcpServerStore} + a token store;* TrueFoundryMcpServerStore implements this directly for remote Connect UX stubs.*@@ -113,16 +114,10 @@ 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>;}/**- * Settings/MCP API store: persistence plus Connect UX auth.+ * Settings/MCP API store: persistence plus Connect UX auth and invoke headers.* McpServerWithAuthStore implements via a token store; remote-backed stores may call* an upstream status/authorize API instead.*/@@ -135,6 +130,13 @@ export interface IMcpServerWithAuthStore<TTransaction = never> extends IMcpServe/** Revoke this subject's authorization for the named server. */deleteAuthorization(input: DeleteMcpAuthorizationInput): Promise<void>;++ /**+ * 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.tsindex b13a46b4e..148531257 100644--- a/packages/trueforge/src/db/postgres/mcp-server-store/PostgresMcpServerStore.ts+++ b/packages/trueforge/src/db/postgres/mcp-server-store/PostgresMcpServerStore.ts@@ -1,6 +1,5 @@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,@@ -35,10 +34,6 @@ export class PostgresMcpServerStore implements IMcpServerStore<Transaction<Databthis.#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.tsindex 8b9a0a893..a4e13824c 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 { resolveConfiguredMcpRequestHeaders, type McpServerManifest } from '../../../schemas/mcpServer';+import type { McpServerManifest } from '../../../schemas/mcpServer';import { newId } from '../../../utils/id';import {fromStoredOAuthClientRecord,@@ -38,10 +38,6 @@ export class SqliteMcpServerStore implements IMcpServerStore<Transaction<Databasthis.#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.tsindex 862ea0674..ac4a8174f 100644--- a/packages/trueforge/src/runtime/sessionResources.ts+++ b/packages/trueforge/src/runtime/sessionResources.ts@@ -17,7 +17,7 @@ import { HTTPException } from 'hono/http-exception';import { join } from 'node:path';import type { Logger } from 'winston';import configuration from '../config';-import type { IMcpServerStore, McpServerRecord } from '../db/mcpServerStore';+import type { IMcpServerStore, IMcpServerWithAuthStore, McpServerRecord } from '../db/mcpServerStore';import type { IModelProviderStore } from '../db/modelProviderStore';import type { ISandboxProviderStore } from '../db/sandboxProviderStore';import type { ISkillStore } from '../db/skillStore';@@ -134,7 +134,7 @@ function dcrHeadersResolver(params: {/*** Load MCP url + headers for a configured server.* - Local `remote` + `dcr`: resolveMcpAuth via the harness token store.- * - Otherwise: {@link IMcpServerStore.resolveInvokeHeaders}+ * - Otherwise: {@link IMcpServerWithAuthStore.resolveInvokeHeaders}* (TrueFoundry gateway Bearer, configured header auth, or `{}`).* Returns undefined when the server is not registered — callers choose the response.*/@@ -148,7 +148,7 @@ export async function getMcpConnection({}: {tenant_id: string;name: string;- store: IMcpServerStore;+ store: IMcpServerWithAuthStore;tokenStore: IOAuthTokenStore;clientName: string;userRef: string;diff --git a/packages/trueforge/tests/db/mcpServerStoreContractSuite.ts b/packages/trueforge/tests/db/mcpServerStoreContractSuite.tsindex 3b91ef9ea..7d0d28f9f 100644--- a/packages/trueforge/tests/db/mcpServerStoreContractSuite.ts+++ b/packages/trueforge/tests/db/mcpServerStoreContractSuite.ts@@ -176,29 +176,4 @@ 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/apis/deletedSessionCrud.test.ts b/packages/trueforge/tests/unit/apis/deletedSessionCrud.test.tsindex d8f7a5d65..6212a27ae 100644--- a/packages/trueforge/tests/unit/apis/deletedSessionCrud.test.ts+++ b/packages/trueforge/tests/unit/apis/deletedSessionCrud.test.ts@@ -6,6 +6,7 @@ import { createLogger } from 'winston';import { createSessionsRouter, TENANT_ID } from '../../../src/apis/sessions';import { createTurnsRouter } from '../../../src/apis/turns';import { LOCAL_USER_CONTEXT } from '../../../src/auth/identity';+import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore';import { migrateSqliteToLatest } from '../../../src/db/migrateSqlite';import { SqliteAgentStore } from '../../../src/db/sqlite/agent-store/SqliteAgentStore';import { createSqliteDb } from '../../../src/db/sqlite/client';@@ -27,8 +28,12 @@ describe('public CRUD after session deletion', () => {const sessions = new Sessions({ sessionStore });const activeTurns = new ActiveTurnRegistry();const modelProviderStore = new SqliteModelProviderStore(db);- const mcpServerStore = new SqliteMcpServerStore(db);const tokenStore = new SqliteOAuthTokenStore(db);+ const mcpServerStore = new McpServerWithAuthStore({+ store: new SqliteMcpServerStore(db),+ tokenStore,+ clientName: 'test-client',+ });const skillStore = new SqliteSkillStore(db);const agentStore = new SqliteAgentStore(db);const sandboxProviderStore = new SqliteSandboxProviderStore(db);diff --git a/packages/trueforge/tests/unit/apis/sandboxFileDownload.test.ts b/packages/trueforge/tests/unit/apis/sandboxFileDownload.test.tsindex a70d6107a..1ace8b8ef 100644--- a/packages/trueforge/tests/unit/apis/sandboxFileDownload.test.ts+++ b/packages/trueforge/tests/unit/apis/sandboxFileDownload.test.ts@@ -5,6 +5,7 @@ import { createLogger } from 'winston';import { TENANT_ID } from '../../../src/apis/sessions';import { createTurnsRouter, toContentDisposition } from '../../../src/apis/turns';import { LOCAL_USER_CONTEXT } from '../../../src/auth/identity';+import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore';import { migrateSqliteToLatest } from '../../../src/db/migrateSqlite';import { SqliteAgentStore } from '../../../src/db/sqlite/agent-store/SqliteAgentStore';import { createSqliteDb } from '../../../src/db/sqlite/client';@@ -30,6 +31,7 @@ async function buildApp() {await migrateSqliteToLatest(db);const sessionStore = new SqliteSessionStore(db);const sessions = new Sessions({ sessionStore });+ const tokenStore = new SqliteOAuthTokenStore(db);const app = new OpenAPIHono();app.route(@@ -39,8 +41,13 @@ async function buildApp() {sessionStore,activeTurns: new ActiveTurnRegistry(),resolveModelProviderStore: () => new SqliteModelProviderStore(db),- resolveMcpServerStore: () => new SqliteMcpServerStore(db),- tokenStore: new SqliteOAuthTokenStore(db),+ resolveMcpServerStore: () =>+ new McpServerWithAuthStore({+ store: new SqliteMcpServerStore(db),+ tokenStore,+ clientName: 'test-client',+ }),+ tokenStore,skillStore: new SqliteSkillStore(db),agentStore: new SqliteAgentStore(db),eventSubscriptions: new EventSubscriptionRegistry(undefined),diff --git a/packages/trueforge/tests/unit/apis/turnHarnessErrorStatus.test.ts b/packages/trueforge/tests/unit/apis/turnHarnessErrorStatus.test.tsindex 2a7b6f76a..ee5c8a6c8 100644--- a/packages/trueforge/tests/unit/apis/turnHarnessErrorStatus.test.ts+++ b/packages/trueforge/tests/unit/apis/turnHarnessErrorStatus.test.ts@@ -4,6 +4,7 @@ import { AgentHarnessError } from '@truefoundry/trueforge-core/core';import { createLogger } from 'winston';import { createTurnsRouter } from '../../../src/apis/turns';import { LOCAL_USER_CONTEXT } from '../../../src/auth/identity';+import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore';import { migrateSqliteToLatest } from '../../../src/db/migrateSqlite';import { SqliteAgentStore } from '../../../src/db/sqlite/agent-store/SqliteAgentStore';import { createSqliteDb } from '../../../src/db/sqlite/client';@@ -20,6 +21,7 @@ async function postTurnRejectingWith(error: AgentHarnessError): Promise<Responseconst db = createSqliteDb(':memory:');await migrateSqliteToLatest(db);const modelProviderStore = new SqliteModelProviderStore(db);+ const tokenStore = new SqliteOAuthTokenStore(db);await modelProviderStore.upsertProvider({tenant_id: 'default',name: 'test-provider',@@ -56,8 +58,13 @@ async function postTurnRejectingWith(error: AgentHarnessError): Promise<ResponsesessionStore: new SqliteSessionStore(db),activeTurns: new ActiveTurnRegistry(),resolveModelProviderStore: () => modelProviderStore,- resolveMcpServerStore: () => new SqliteMcpServerStore(db),- tokenStore: new SqliteOAuthTokenStore(db),+ resolveMcpServerStore: () =>+ new McpServerWithAuthStore({+ store: new SqliteMcpServerStore(db),+ tokenStore,+ clientName: 'test-client',+ }),+ tokenStore,skillStore: new SqliteSkillStore(db),agentStore: new SqliteAgentStore(db),eventSubscriptions: new EventSubscriptionRegistry(undefined),diff --git a/packages/trueforge/tests/unit/apis/turns.test.ts b/packages/trueforge/tests/unit/apis/turns.test.tsindex 24cfe0f4e..8128cf964 100644--- a/packages/trueforge/tests/unit/apis/turns.test.ts+++ b/packages/trueforge/tests/unit/apis/turns.test.ts@@ -5,10 +5,12 @@ import {TurnNotFoundError,type TurnStreamingEvent,} from '@truefoundry/trueforge-core/agent-session';+import type { Kysely } from 'kysely';import { createLogger } from 'winston';import { TENANT_ID } from '../../../src/apis/sessions';import { createTurnsRouter, turnStreamId } from '../../../src/apis/turns';import { LOCAL_USER_CONTEXT } from '../../../src/auth/identity';+import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore';import { migrateSqliteToLatest } from '../../../src/db/migrateSqlite';import { SqliteAgentStore } from '../../../src/db/sqlite/agent-store/SqliteAgentStore';import { createSqliteDb } from '../../../src/db/sqlite/client';@@ -18,9 +20,18 @@ import { SqliteSandboxProviderStore } from '../../../src/db/sqlite/sandbox-proviimport { SqliteSessionStore } from '../../../src/db/sqlite/session-store/SqliteSessionStore';import { SqliteSkillStore } from '../../../src/db/sqlite/skill-store/SqliteSkillStore';import { SqliteOAuthTokenStore } from '../../../src/db/sqlite/token-store/SqliteOAuthTokenStore';+import type { Database } from '../../../src/db/sqlite/types';import { ActiveTurnRegistry } from '../../../src/runtime/activeTurns';import { EventSubscriptionRegistry } from '../../../src/runtime/event-subscription/index.js';+function mcpServerStoreWithAuth(db: Kysely<Database>, tokenStore: SqliteOAuthTokenStore) {+ return new McpServerWithAuthStore({+ store: new SqliteMcpServerStore(db),+ tokenStore,+ clientName: 'test-client',+ });+}+describe('turns', () => {describe('turn ownership', () => {it('returns 403 for all turn routes when the caller is not the session creator', async () => {@@ -45,6 +56,7 @@ describe('turns', () => {external_id: null,});+ const tokenStore = new SqliteOAuthTokenStore(db);const app = new OpenAPIHono();app.route('/',@@ -53,8 +65,8 @@ describe('turns', () => {sessionStore,activeTurns: new ActiveTurnRegistry(),resolveModelProviderStore: () => new SqliteModelProviderStore(db),- resolveMcpServerStore: () => new SqliteMcpServerStore(db),- tokenStore: new SqliteOAuthTokenStore(db),+ resolveMcpServerStore: () => mcpServerStoreWithAuth(db, tokenStore),+ tokenStore,skillStore: new SqliteSkillStore(db),agentStore: new SqliteAgentStore(db),eventSubscriptions: new EventSubscriptionRegistry(undefined),@@ -164,6 +176,7 @@ describe('turns', () => {} as unknown as Sessions;const eventSubscriptions = new EventSubscriptionRegistry<TurnStreamingEvent>(undefined);+ const tokenStore = new SqliteOAuthTokenStore(db);const app = new OpenAPIHono();app.route('/',@@ -173,8 +186,8 @@ describe('turns', () => {activeTurns: new ActiveTurnRegistry(),resolveModelProviderStore: () => modelProviderStore,agentStore: new SqliteAgentStore(db),- resolveMcpServerStore: () => new SqliteMcpServerStore(db),- tokenStore: new SqliteOAuthTokenStore(db),+ resolveMcpServerStore: () => mcpServerStoreWithAuth(db, tokenStore),+ tokenStore,skillStore: new SqliteSkillStore(db),eventSubscriptions,sandboxProviderStore: new SqliteSandboxProviderStore(db),@@ -264,6 +277,7 @@ describe('turns', () => {}),} as unknown as Sessions;+ const tokenStore = new SqliteOAuthTokenStore(db);const app = new OpenAPIHono();app.route('/',@@ -272,8 +286,8 @@ describe('turns', () => {sessionStore: new SqliteSessionStore(db),activeTurns: new ActiveTurnRegistry(),resolveModelProviderStore: () => modelProviderStore,- resolveMcpServerStore: () => new SqliteMcpServerStore(db),- tokenStore: new SqliteOAuthTokenStore(db),+ resolveMcpServerStore: () => mcpServerStoreWithAuth(db, tokenStore),+ tokenStore,skillStore: new SqliteSkillStore(db),agentStore: new SqliteAgentStore(db),eventSubscriptions: new EventSubscriptionRegistry(undefined),diff --git a/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts b/packages/trueforge/tests/unit/runtime/getMcpConnection.test.tsindex 2abf4f555..cf14c7205 100644--- a/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts+++ b/packages/trueforge/tests/unit/runtime/getMcpConnection.test.ts@@ -1,5 +1,7 @@import { TENANT_ID } from '../../../src/apis/sessions';import { LOCAL_USER_CONTEXT } from '../../../src/auth/identity';+import { McpServerWithAuthStore } from '../../../src/db/McpServerWithAuthStore';+import type { IMcpServerWithAuthStore } from '../../../src/db/mcpServerStore';import { migrateSqliteToLatest } from '../../../src/db/migrateSqlite';import { createSqliteDb } from '../../../src/db/sqlite/client';import { SqliteMcpServerStore } from '../../../src/db/sqlite/mcp-server-store/SqliteMcpServerStore';@@ -9,14 +11,18 @@ import { getMcpConnection } from '../../../src/runtime/sessionResources';describe('getMcpConnection', () => {let db: ReturnType<typeof createSqliteDb>;- let mcpServerStore: SqliteMcpServerStore;+ let mcpServerStore: IMcpServerWithAuthStore;let tokenStore: SqliteOAuthTokenStore;beforeAll(async () => {db = createSqliteDb(':memory:');await migrateSqliteToLatest(db);- mcpServerStore = new SqliteMcpServerStore(db);tokenStore = new SqliteOAuthTokenStore(db);+ mcpServerStore = new McpServerWithAuthStore({+ store: new SqliteMcpServerStore(db),+ tokenStore,+ clientName: 'test-client',+ });});it('returns an async DCR headers resolver keyed by server name on authRequired', async () => {