diff --git a/.changeset/trueforge-mcp-approval-policies.md b/.changeset/trueforge-mcp-approval-policies.md new file mode 100644 index 000000000..d35fc96a9 --- /dev/null +++ b/.changeset/trueforge-mcp-approval-policies.md @@ -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()`. + +- 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. diff --git a/packages/trueforge-core/src/agent-session/TurnResourceResolver.ts b/packages/trueforge-core/src/agent-session/TurnResourceResolver.ts index 25bc1e3b9..5f4c8aad6 100644 --- a/packages/trueforge-core/src/agent-session/TurnResourceResolver.ts +++ b/packages/trueforge-core/src/agent-session/TurnResourceResolver.ts @@ -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'; @@ -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: { @@ -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?.() ?? []) : []), @@ -272,7 +281,11 @@ export class TurnResourceResolver< }; } - private async trueforgeToolSet(input: { signal: AbortSignal; tracing: AgentTracing }): Promise { + private async trueforgeToolSet(input: { + signal: AbortSignal; + tracing: AgentTracing; + approvalPolicies: Record | undefined; + }): Promise { const connection = await this.deps.mcp(TRUEFORGE_MCP_ID); if (connection === undefined) { return undefined; @@ -298,7 +311,7 @@ export class TurnResourceResolver< source, selectors: TRUEFORGE_MCP_SELECTORS, preload: false, - approvalPolicies: undefined, + approvalPolicies: input.approvalPolicies, }); } diff --git a/packages/trueforge-core/src/core/mcp/IMCPServer.ts b/packages/trueforge-core/src/core/mcp/IMCPServer.ts index 7a920ef5a..449fe8a3e 100644 --- a/packages/trueforge-core/src/core/mcp/IMCPServer.ts +++ b/packages/trueforge-core/src/core/mcp/IMCPServer.ts @@ -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; hasApplicableApprovalPolicy(toolName: string): boolean; diff --git a/packages/trueforge-core/src/core/mcp/LocalToolMCP.ts b/packages/trueforge-core/src/core/mcp/LocalToolMCP.ts index 9cf335963..babdadbe0 100644 --- a/packages/trueforge-core/src/core/mcp/LocalToolMCP.ts +++ b/packages/trueforge-core/src/core/mcp/LocalToolMCP.ts @@ -58,6 +58,7 @@ export function defineTool(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 = ''; diff --git a/packages/trueforge-core/src/core/mcp/ToolSet.ts b/packages/trueforge-core/src/core/mcp/ToolSet.ts index 44b9b6eb4..85cf89efb 100644 --- a/packages/trueforge-core/src/core/mcp/ToolSet.ts +++ b/packages/trueforge-core/src/core/mcp/ToolSet.ts @@ -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; private readonly approvalPolicies = new Map(); static isPolicyApplicable(policy: ToolApprovalPolicy, asOf: Date): boolean { diff --git a/packages/trueforge-core/src/core/runtime/AgentThread.ts b/packages/trueforge-core/src/core/runtime/AgentThread.ts index 4b6eee6ac..4470e498c 100644 --- a/packages/trueforge-core/src/core/runtime/AgentThread.ts +++ b/packages/trueforge-core/src/core/runtime/AgentThread.ts @@ -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 { diff --git a/packages/trueforge-core/src/core/runtime/AgentThreadOrchestrator.ts b/packages/trueforge-core/src/core/runtime/AgentThreadOrchestrator.ts index af34001d2..07b7c8a69 100644 --- a/packages/trueforge-core/src/core/runtime/AgentThreadOrchestrator.ts +++ b/packages/trueforge-core/src/core/runtime/AgentThreadOrchestrator.ts @@ -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(); 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); } } @@ -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). // @@ -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; } @@ -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; } diff --git a/packages/trueforge-core/tests/agent-session/rootCapabilities.test.ts b/packages/trueforge-core/tests/agent-session/rootCapabilities.test.ts index d41fff2a9..3e1851716 100644 --- a/packages/trueforge-core/tests/agent-session/rootCapabilities.test.ts +++ b/packages/trueforge-core/tests/agent-session/rootCapabilities.test.ts @@ -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'; @@ -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: () => diff --git a/packages/trueforge-core/tests/agent-session/turnResume.test.ts b/packages/trueforge-core/tests/agent-session/turnResume.test.ts index b6ea02d07..5ed680e82 100644 --- a/packages/trueforge-core/tests/agent-session/turnResume.test.ts +++ b/packages/trueforge-core/tests/agent-session/turnResume.test.ts @@ -110,6 +110,7 @@ function makeAuthCapability() { toolCallInfo: jest.fn(), setApprovalPolicy: jest.fn(), getApprovalPolicies: jest.fn(() => ({})), + supportsApprovals: false, hasApplicableApprovalPolicy: jest.fn(() => false), }; return { diff --git a/packages/trueforge-core/tests/core/harnessMocks.ts b/packages/trueforge-core/tests/core/harnessMocks.ts index d2b4a86c0..e79d78949 100644 --- a/packages/trueforge-core/tests/core/harnessMocks.ts +++ b/packages/trueforge-core/tests/core/harnessMocks.ts @@ -63,6 +63,7 @@ export function makeMockIMCPServer(params: { toolCallInfo: jest.fn(), setApprovalPolicy: jest.fn(), getApprovalPolicies: jest.fn(() => ({})), + supportsApprovals: false, hasApplicableApprovalPolicy: jest.fn(() => false), }; } diff --git a/packages/trueforge-core/tests/core/mcp/mcpInitializeAggregation.test.ts b/packages/trueforge-core/tests/core/mcp/mcpInitializeAggregation.test.ts index e5f6ce43f..be43ebd1a 100644 --- a/packages/trueforge-core/tests/core/mcp/mcpInitializeAggregation.test.ts +++ b/packages/trueforge-core/tests/core/mcp/mcpInitializeAggregation.test.ts @@ -95,6 +95,7 @@ describe('convertMCPServersToTools initialization aggregation', () => { toolCallInfo: jest.fn(), setApprovalPolicy: jest.fn(), getApprovalPolicies: jest.fn(() => ({})), + supportsApprovals: false, hasApplicableApprovalPolicy: jest.fn(() => false), }; diff --git a/packages/trueforge-core/tests/orchestration/orchestrationApproval.test.ts b/packages/trueforge-core/tests/orchestration/orchestrationApproval.test.ts index f645e598b..b86a9a3e8 100644 --- a/packages/trueforge-core/tests/orchestration/orchestrationApproval.test.ts +++ b/packages/trueforge-core/tests/orchestration/orchestrationApproval.test.ts @@ -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, @@ -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); @@ -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 = { @@ -807,6 +859,7 @@ function makeApprovalHarness(finalReply: string): { toolResponseProcessors: undefined, instructionBuilders: undefined, }, + { systemToolSets: extraSystemToolSets }, ], capabilityState: undefined, tracing: NOOP_AGENT_TRACING, @@ -822,7 +875,7 @@ function makeApprovalHarness(finalReply: string): { pendingTurnEvents: [], mcpServers: [], }); - return { orchestrator, thread, callTool }; + return { orchestrator, thread, callTool, toolSet }; } function makeApprovalHarnessWithUserToolSet( diff --git a/packages/trueforge-core/tests/orchestration/orchestrationMcpAuth.test.ts b/packages/trueforge-core/tests/orchestration/orchestrationMcpAuth.test.ts index e83432e9c..8b082faa0 100644 --- a/packages/trueforge-core/tests/orchestration/orchestrationMcpAuth.test.ts +++ b/packages/trueforge-core/tests/orchestration/orchestrationMcpAuth.test.ts @@ -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 = { diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts index 26c71d79d..3019bc4a6 100644 --- a/packages/trueforge/src/apis/turns.ts +++ b/packages/trueforge/src/apis/turns.ts @@ -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}` }, diff --git a/packages/trueforge/src/mcp/server/trueforge/route.ts b/packages/trueforge/src/mcp/server/trueforge/route.ts index 13aaf031b..c72c01feb 100644 --- a/packages/trueforge/src/mcp/server/trueforge/route.ts +++ b/packages/trueforge/src/mcp/server/trueforge/route.ts @@ -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'; @@ -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, diff --git a/packages/trueforge/src/mcp/server/trueforge/tools/custom.ts b/packages/trueforge/src/mcp/server/trueforge/tools/custom.ts index e758179ca..b27470100 100644 --- a/packages/trueforge/src/mcp/server/trueforge/tools/custom.ts +++ b/packages/trueforge/src/mcp/server/trueforge/tools/custom.ts @@ -64,7 +64,10 @@ function registerCurrentSessionTool(server: McpServer, client: WireApiClient): v runTool(async () => { const sessionId = headerValue(extra.requestInfo?.headers, TRUEFORGE_SESSION_HEADER); if (sessionId === undefined) { - throw new ToolResultError(400, 'No current chat. This tool is only available during a draft session turn.'); + throw new ToolResultError( + 400, + 'Unable to resolve session_id. This tool is only usable from TrueForge sessions', + ); } return client.execute(descriptor, { session_id: sessionId }, extra.signal); }), @@ -74,7 +77,9 @@ function registerCurrentSessionTool(server: McpServer, client: WireApiClient): v function registerUpdateCurrentSessionTool(server: McpServer, client: WireApiClient): void { const descriptor: McpToolDescriptor = { name: 'update_current_session', - description: "Update this session's spec. Send the full spec back with edits; omitted fields are removed.", + description: + "Update this session's spec. Send the full spec back with edits; omitted fields are removed. " + + "Newly attached MCP servers take effect from the user's next message; tell the user to send it.", annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true }, method: 'PATCH', path: '/api/v1/sessions/{session_id}', @@ -94,7 +99,10 @@ function registerUpdateCurrentSessionTool(server: McpServer, client: WireApiClie runTool(async () => { const sessionId = headerValue(extra.requestInfo?.headers, TRUEFORGE_SESSION_HEADER); if (sessionId === undefined) { - throw new ToolResultError(400, 'No current chat. This tool is only available during a draft session turn.'); + throw new ToolResultError( + 400, + 'Unable to resolve session_id. This tool is only usable from TrueForge sessions', + ); } return client.execute(descriptor, { session_id: sessionId, agent: { spec } }, extra.signal); }), diff --git a/packages/trueforge/src/mcp/server/trueforge/wireApiClient.ts b/packages/trueforge/src/mcp/server/trueforge/wireApiClient.ts index 6c8edb356..fbee48dc0 100644 --- a/packages/trueforge/src/mcp/server/trueforge/wireApiClient.ts +++ b/packages/trueforge/src/mcp/server/trueforge/wireApiClient.ts @@ -1,4 +1,3 @@ -import type { TrueForge } from '@truefoundry/trueforge-sdk'; import type { McpToolDescriptor } from './openApiTools'; export class WireApiError extends Error { @@ -42,8 +41,18 @@ export interface WireApiClient { execute(descriptor: McpToolDescriptor, args: Record, signal?: AbortSignal): Promise; } -/** Map generated tool arguments onto raw SDK passthrough requests. */ -export function createWireApiClient(client: Pick, baseUrl: string): WireApiClient { +export interface CreateWireApiClientOptions { + baseUrl: string; + fetch: typeof fetch; + /** Caller credential; when null/undefined, no Authorization header is sent. */ + token?: string | null; + /** Request timeout in seconds. Defaults to 60. */ + timeoutInSeconds?: number; +} + +/** Map generated tool arguments onto authenticated internal HTTP requests. */ +export function createWireApiClient(options: CreateWireApiClientOptions): WireApiClient { + const { baseUrl, fetch: fetchFn, token, timeoutInSeconds = 60 } = options; return { async execute(descriptor, args, signal) { let pathname = descriptor.path; @@ -70,6 +79,9 @@ export function createWireApiClient(client: Pick, baseUrl: s } const headers = new Headers(); + if (token != null && token !== '') { + headers.set('authorization', `Bearer ${token}`); + } const body = descriptor.bodyParameters.length === 0 ? undefined @@ -80,19 +92,16 @@ export function createWireApiClient(client: Pick, baseUrl: s headers.set('content-type', 'application/json'); } - // Resolve an absolute URL here. The SDK passthrough fetch appends a relative path onto the - // base pathname, which percent-encodes `?` and turns the query string into part of the route. - const url = new URL(pathname, baseUrl); + const url = new URL(baseUrl.replace(/\/+$/, '') + pathname); url.search = query.toString(); - const response = await client.fetch( - url, - { - method: descriptor.method, - headers, - ...(body === undefined ? {} : { body: JSON.stringify(body) }), - }, - { maxRetries: 0, ...(signal === undefined ? {} : { abortSignal: signal }) }, - ); + const timeoutSignal = AbortSignal.timeout(timeoutInSeconds * 1000); + const requestSignal = signal === undefined ? timeoutSignal : AbortSignal.any([signal, timeoutSignal]); + const response = await fetchFn(url, { + method: descriptor.method, + headers, + ...(body === undefined ? {} : { body: JSON.stringify(body) }), + signal: requestSignal, + }); const parsed = responseBody(await response.text()); if (!response.ok) { throw new WireApiError(response.status, parsed);