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
9 changes: 9 additions & 0 deletions .changeset/trueforge-mcp-approval-policies.md
Comment thread
heerambavi1998 marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@truefoundry/trueforge': patch
'@truefoundry/trueforge-core': patch
---

Built-in TrueForge MCP write and destructive tools require approval again, and "approve for this session" and timed approvals now work for them and carry over to later turns. `IToolSet` gains a required `supportsApprovals` flag, and approval policies apply to any tool set (system or user) where it is true. `AgentThread.getUserToolSets()` is replaced by `getToolSets()`.
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

- The built-in TrueForge MCP now tells the agent it can attach configured MCP servers and skills to the current chat. After attaching one, the agent asks the user to send another message, because the new tools are available from the next message.
- TrueForge MCP internal API tool calls use the process mTLS fetch and the caller's credential directly, instead of the Fern SDK passthrough client.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { Logger } from 'winston';
import type { AgentCapability } from '../core/capabilities/AgentCapability';
import type { ToolApprovalPolicy } from '../core/events/schema';
import type { ILLM } from '../core/llm/ILLM';
import type { ModelParams } from '../core/llm/model';
import type { VercelAIProviderName } from '../core/llm/VercelAILLM';
import type { ToolSource } from '../core/mcp/IMCPServer';
import { RemoteMCP, type McpConnection } from '../core/mcp/RemoteMCP';
import type { ToolSelectorConfig } from '../core/mcp/ToolSelectorPolicy';
import { DEFAULT_DISABLE_TOOLS, DEFAULT_ENABLE_TOOLS, DEFAULT_PRELOAD_TOOLS } from '../core/mcp/toolSelectors';
import {
DEFAULT_DISABLE_TOOLS,
DEFAULT_ENABLE_TOOLS,
DEFAULT_PRELOAD_TOOLS,
DEFAULT_REQUIRE_APPROVAL_FOR_TOOLS,
} from '../core/mcp/toolSelectors';
import { ToolSet } from '../core/mcp/ToolSet';
import type { AgentDefinition } from '../core/runtime/AgentDefinition';
import type { AgentInfo } from '../core/runtime/AgentThread.types';
Expand Down Expand Up @@ -42,8 +48,7 @@ const TRUEFORGE_MCP_SELECTORS: ToolSelectorConfig = {
enableTools: DEFAULT_ENABLE_TOOLS,
disableTools: DEFAULT_DISABLE_TOOLS,
preloadTools: DEFAULT_PRELOAD_TOOLS,
// Session approval policies do not target system tool sets yet.
requireApprovalForTools: [],
requireApprovalForTools: DEFAULT_REQUIRE_APPROVAL_FOR_TOOLS,
};

function toSelectors(entry: {
Expand Down Expand Up @@ -238,7 +243,11 @@ export class TurnResourceResolver<
});
}),
);
const trueforgeToolSet = await this.trueforgeToolSet({ signal, tracing });
const trueforgeToolSet = await this.trueforgeToolSet({
signal,
tracing,
approvalPolicies: previousTurn?.snapshot.mcp_servers?.[TRUEFORGE_MCP_ID]?.approval_policies,
});
const extraCapabilities = [
...(trueforgeToolSet === undefined ? [] : [{ systemToolSets: [trueforgeToolSet] }]),
...(agentInfo === undefined ? (this.deps.rootCapabilities?.() ?? []) : []),
Expand Down Expand Up @@ -272,7 +281,11 @@ export class TurnResourceResolver<
};
}

private async trueforgeToolSet(input: { signal: AbortSignal; tracing: AgentTracing }): Promise<ToolSet | undefined> {
private async trueforgeToolSet(input: {
signal: AbortSignal;
tracing: AgentTracing;
approvalPolicies: Record<string, ToolApprovalPolicy> | undefined;
}): Promise<ToolSet | undefined> {
const connection = await this.deps.mcp(TRUEFORGE_MCP_ID);
if (connection === undefined) {
return undefined;
Expand All @@ -298,7 +311,7 @@ export class TurnResourceResolver<
source,
selectors: TRUEFORGE_MCP_SELECTORS,
preload: false,
approvalPolicies: undefined,
approvalPolicies: input.approvalPolicies,
});
}

Expand Down
5 changes: 3 additions & 2 deletions packages/trueforge-core/src/core/mcp/IMCPServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ export interface IToolSet {
// back to the current unrestricted envelope; callTool still enforces policy.
getAllowedToolNamesForSandbox?(): string[] | undefined;

// Per-tool approval policies. User MCP tool sets record and expose policies;
// system tool sets are never approval-gated and implement these as no-ops.
// True when tools can be approval-gated and the policy methods below record
// policies. Local tool sets never require approval and implement them as no-ops.
readonly supportsApprovals: boolean;
setApprovalPolicy(toolName: string, policy: ToolApprovalPolicy): void;
getApprovalPolicies(): Record<string, ToolApprovalPolicy>;
hasApplicableApprovalPolicy(toolName: string): boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/trueforge-core/src/core/mcp/LocalToolMCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function defineTool<T extends z.ZodType>(config: {
* approval-gated), executed in-process within a local trace span. Base for all local/client-side tools.
*/
export abstract class LocalToolMCP implements IToolSet {
readonly supportsApprovals = false;
abstract readonly name: string;
abstract readonly displayName: string;
readonly description: string = '';
Expand Down
2 changes: 1 addition & 1 deletion packages/trueforge-core/src/core/mcp/ToolSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class ToolSet implements IToolSet {
}
readonly preload: boolean;
readonly hasPreloadedTools: boolean;

private readonly source: ToolSource;
private readonly toolSelectorPolicy: ToolSelectorPolicy;
readonly supportsApprovals = true;
Comment thread
heerambavi1998 marked this conversation as resolved.
Resolved
private readonly approvalPolicies = new Map<string, ToolApprovalPolicy>();

static isPolicyApplicable(policy: ToolApprovalPolicy, asOf: Date): boolean {
Expand Down
4 changes: 2 additions & 2 deletions packages/trueforge-core/src/core/runtime/AgentThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,8 @@ export class AgentThread {
return getOpenToolCallIds(this.context).has(toolCallId);
}

getUserToolSets(): readonly IToolSet[] {
return this.definition.toolSets ?? [];
getToolSets(): readonly IToolSet[] {
return [...this.systemToolSets, ...(this.definition.toolSets ?? [])];
}

isRunnable(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ export class AgentThreadOrchestrator {
yield* thread.send(messages);
}

// Pure: every `server_name` must match a currently-configured user MCP tool set. Returns one
// Pure: every `server_name` must match an approval tool set on some thread. Returns one
// error string per unknown name (empty when all valid). Applies nothing.
private validateApprovalPolicies(policies: ToolApprovalPolicyItem[]): string[] {
const knownServerNames = new Set<string>();
for (const thread of this.agentThreads.values()) {
for (const toolSet of thread.getUserToolSets()) {
for (const toolSet of thread.getToolSets().filter(t => t.supportsApprovals)) {
knownServerNames.add(toolSet.name);
}
}
Expand All @@ -299,7 +299,7 @@ export class AgentThreadOrchestrator {
return errors;
}

// Record each policy on its matching user tool sets, then resolve any
// Record each policy on its matching tool sets, then resolve any
// already-pending approvals the policy now covers.
// Last write wins for a given (server, tool).
//
Expand All @@ -319,7 +319,7 @@ export class AgentThreadOrchestrator {
let appliedAny = false;

for (const policy of policies) {
for (const toolSet of thread.getUserToolSets()) {
for (const toolSet of thread.getToolSets().filter(t => t.supportsApprovals)) {
if (toolSet.name !== policy.server_name) {
continue;
}
Expand Down Expand Up @@ -365,7 +365,7 @@ export class AgentThreadOrchestrator {
// Apply the policies to the thread tool sets.
for (const thread of this.agentThreads.values()) {
for (const policy of policies) {
for (const toolSet of thread.getUserToolSets()) {
for (const toolSet of thread.getToolSets().filter(t => t.supportsApprovals)) {
if (toolSet.name !== policy.server_name) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TRUEFORGE_MCP_ID, TurnResourceResolver } from '../../src/agent-session/TurnResourceResolver';
import type { TurnRecord } from '../../src/agent-session/models/TurnRecord';
import type { AgentSpec } from '../../src/agent-session/schemas/agentSpec';
import type { AgentCapability, AgentInfo } from '../../src/core';
import { makeAgentSpec, makeMockILLM, makeSilentLogger } from './testHelpers';
Expand Down Expand Up @@ -92,6 +93,47 @@ describe('TurnResourceResolver rootCapabilities', () => {
expect(root.extraCapabilities?.[0]?.systemToolSets?.[0]?.name).toBe(TRUEFORGE_MCP_ID);
});

it('restores TrueForge MCP approval policies from the previous turn', async () => {
const resolver = new TurnResourceResolver({
llm: () =>
Promise.resolve({
modelClient: makeMockILLM(),
defaultModelParams: {},
providerType: undefined,
}),
mcp: id =>
Promise.resolve(
id === TRUEFORGE_MCP_ID
? { url: 'http://internal.example/mcp', name: TRUEFORGE_MCP_ID, description: 'TrueForge management' }
: undefined,
),
mcpRequestTimeoutMs: 1_000,
mcpConnectTimeoutMs: 1_000,
logger: makeSilentLogger(),
});
const previousTurn = {
snapshot: {
mcp_servers: {
[TRUEFORGE_MCP_ID]: {
id: TRUEFORGE_MCP_ID,
name: TRUEFORGE_MCP_ID,
approval_policies: { create_agent: { type: 'allow_session' } },
},
},
},
} as unknown as TurnRecord;

const resolved = await resolver.resolveAgentDefinition({
spec: makeAgentSpec(),
previousTurn,
signal: new AbortController().signal,
tracing: resolver.createTracing(),
});

const toolSet = resolved.extraCapabilities?.[0]?.systemToolSets?.[0];
expect(toolSet?.getApprovalPolicies()).toEqual({ create_agent: { type: 'allow_session' } });
});

it('keeps direct capability sources distinct from configured MCPs with the same display name', async () => {
const resolver = new TurnResourceResolver({
llm: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function makeAuthCapability() {
toolCallInfo: jest.fn(),
setApprovalPolicy: jest.fn(),
getApprovalPolicies: jest.fn(() => ({})),
supportsApprovals: false,
hasApplicableApprovalPolicy: jest.fn(() => false),
};
return {
Expand Down
1 change: 1 addition & 0 deletions packages/trueforge-core/tests/core/harnessMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function makeMockIMCPServer(params: {
toolCallInfo: jest.fn(),
setApprovalPolicy: jest.fn(),
getApprovalPolicies: jest.fn(() => ({})),
supportsApprovals: false,
hasApplicableApprovalPolicy: jest.fn(() => false),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('convertMCPServersToTools initialization aggregation', () => {
toolCallInfo: jest.fn(),
setApprovalPolicy: jest.fn(),
getApprovalPolicies: jest.fn(() => ({})),
supportsApprovals: false,
hasApplicableApprovalPolicy: jest.fn(() => false),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AgentThread } from '../../src/core/runtime/AgentThread';
import { InternalEventType, type AgentThreadConstructorInput } from '../../src/core/runtime/AgentThread.types';
import { AgentThreadOrchestrator } from '../../src/core/runtime/AgentThreadOrchestrator';
import { NOOP_AGENT_TRACING } from '../../src/core/tracing/NoopAgentTracing';
import { makeSilentLogger, TEST_MODEL_NAME } from '../core/harnessMocks';
import { makeMockIMCPServer, makeSilentLogger, TEST_MODEL_NAME } from '../core/harnessMocks';
import {
driveUntilPauseOrDone,
llmCreateInputs,
Expand Down Expand Up @@ -578,6 +578,54 @@ describe('orchestration: a policy that lands mid-pause resolves an existing pend
await expect(finished).resolves.toMatchObject({ done: true });
});

it('applies a policy to a system tool set that supports approval policies', async () => {
const { orchestrator, callTool, toolSet } = makeApprovalHarness(ROOT_FINAL);

for await (const _event of orchestrator.send([{ type: EventType.USER_MESSAGE, content: 'hello' }])) {
void _event;
}
const iterator = orchestrator.execute({ signal: new AbortController().signal });
expect((await driveUntilPauseOrDone(iterator)).kind).toBe('paused');

for await (const _batch of orchestrator.send([
{
type: EventType.USER_TOOL_APPROVAL_POLICY,
id: newEventId(),
created_at: new Date().toISOString(),
policies: [{ server_name: POLICY_SERVER_NAME, name: WRITE_NOTE_TOOL_NAME, policy: { type: 'allow_session' } }],
},
])) {
void _batch;
}
orchestrator.wake();

const resumed = await driveUntilPauseOrDone(iterator);
expect(resumed.kind).toBe('done');
expect(toolSet.getApprovalPolicies()).toEqual({ [WRITE_NOTE_TOOL_NAME]: { type: 'allow_session' } });
expect(callTool).toHaveBeenCalledTimes(1);
});

it('send() rejects a policy for a system tool set whose capability does not support approvals', async () => {
const { orchestrator } = makeApprovalHarness(ROOT_FINAL, [
makeMockIMCPServer({ name: 'local-tools', preload: true }),
]);

await expect(
(async () => {
for await (const _batch of orchestrator.send([
{
type: EventType.USER_TOOL_APPROVAL_POLICY,
id: newEventId(),
created_at: new Date().toISOString(),
policies: [{ server_name: 'local-tools', name: 'tool_a', policy: { type: 'allow_session' } }],
},
])) {
void _batch;
}
})(),
).rejects.toThrow(/unknown server_name 'local-tools'/);
});

it('send() rejects a policy for an unknown server and applies nothing (fail-closed)', async () => {
const { orchestrator, toolSet } = makeApprovalHarnessWithUserToolSet(ROOT_FINAL);

Expand Down Expand Up @@ -765,10 +813,14 @@ describe('ToolSet: policy-aware is_approval_required', () => {
});
});

function makeApprovalHarness(finalReply: string): {
function makeApprovalHarness(
finalReply: string,
extraSystemToolSets: IToolSet[] = [],
): {
orchestrator: AgentThreadOrchestrator;
thread: AgentThread;
callTool: jest.Mock;
toolSet: IToolSet;
} {
const { toolSet, callTool } = makeApprovalGatedWriteNoteToolSet();
const agentThreadInput: AgentThreadConstructorInput = {
Expand Down Expand Up @@ -807,6 +859,7 @@ function makeApprovalHarness(finalReply: string): {
toolResponseProcessors: undefined,
instructionBuilders: undefined,
},
{ systemToolSets: extraSystemToolSets },
],
capabilityState: undefined,
tracing: NOOP_AGENT_TRACING,
Expand All @@ -822,7 +875,7 @@ function makeApprovalHarness(finalReply: string): {
pendingTurnEvents: [],
mcpServers: [],
});
return { orchestrator, thread, callTool };
return { orchestrator, thread, callTool, toolSet };
}

function makeApprovalHarnessWithUserToolSet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function makeAuthBlockedThread(threadId: string): AgentThread {
toolCallInfo: jest.fn(),
setApprovalPolicy: jest.fn(),
getApprovalPolicies: jest.fn(() => ({})),
supportsApprovals: false,
hasApplicableApprovalPolicy: jest.fn(() => false),
};
const input: AgentThreadConstructorInput = {
Expand Down
4 changes: 2 additions & 2 deletions packages/trueforge/src/apis/turns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,8 @@ function buildTrueForgeMcpConnection(input: {
return {
name: TRUEFORGE_MCP_ID,
description:
"Create, update, and read agents, schedules, and sandbox environments in this deployment. Read this chat's " +
'configuration and the configured models, MCP servers, and skills. Does not run agents or turns.',
'Manage agents, schedules, and sandbox environments in this deployment. Read and update own chat, ' +
'including attaching or removing configured MCP servers and skills to it.',
url: new URL('/mcp', normalizeTlsUrl({ url: configuration.SERVER_URL, enabled: configuration.MTLS_ENABLED })).href,
headers:
requestContext.user_credential === null ? {} : { Authorization: `Bearer ${requestContext.user_credential}` },
Expand Down
11 changes: 4 additions & 7 deletions packages/trueforge/src/mcp/server/trueforge/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* Authenticated, stateless Streamable HTTP endpoint for the TrueForge MCP (mounted at /mcp).
*
* Each request: authenticate → build a request-scoped SDK client + stateless MCP server → hand the Web
* Each request: authenticate → build a request-scoped wire client + stateless MCP server → hand the Web
* Request to the transport. Nothing is retained across requests.
*/
import { WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js';
import { TrueForge } from '@truefoundry/trueforge-sdk';
import { Hono } from 'hono';
import type { ResolveRequestContext } from '../../../auth/identity';
import configuration from '../../../config';
Expand Down Expand Up @@ -44,14 +43,12 @@ export function createTrueForgeMcpRouter(deps: TrueForgeMcpRouterDeps): Hono {

// Built per request: the server's tools close over this caller's credential, and stateless
// Streamable HTTP wants a fresh server+transport per request to isolate concurrent callers.
const apiClient = new TrueForge({
const client = createWireApiClient({
baseUrl,
timeoutInSeconds: 60,
maxRetries: 0,
...(requestContext.user_credential === null ? { auth: false } : { token: requestContext.user_credential }),
fetch: internalFetch,
token: requestContext.user_credential,
timeoutInSeconds: 60,
});
const client = createWireApiClient(apiClient, baseUrl);
const server = createTrueForgeMcpServer({ client, version: deps.version, tools: deps.resolveTools() });
const transport = new WebStandardStreamableHTTPServerTransport({
enableJsonResponse: true,
Expand Down
Loading
Loading