import { randomUUID } from 'node:crypto' import { z, type ZodTypeAny } from 'zod' import { checkAgentPolicy } from './agent-policy' import { getAgent, loadAgentRegistry } from './agent-registry' import { runAiAgentObject } from './agent-runtime' import { AgentPolicyError } from './agent-tools' import type { AiAgentRunChannel } from './agent-call-contract' import { isAiAgentDispatchDeniedError, resolveAiAgentRunDispatcher, resolveDispatchOwner, type AiAgentRunDispatchCaller, type AiAgentRunDispatcher, } from './agent-run-dispatcher' import { AgentToolError } from './agent-tool-error' import { hasRequiredFeatures } from './auth' import type { AiToolDefinition, McpToolContext } from './types' export const AGENT_TOOL_DEFAULT_TIMEOUT_MS = 120_000 export type AgentToolResult = { result: TOutput agentId: string runId: string | null agentRunId: string | null pendingActionIds?: string[] } export type DefineAgentToolSpec = { name: string description: string agentId: string inputSchema: z.ZodType outputSchema?: z.ZodType toPrompt?: (input: TInput) => string requiredFeatures?: string[] timeoutMs?: number } export function isAgentToolFeatureSuperset( toolFeatures: readonly string[], agentFeatures: readonly string[] | undefined, ): boolean { if (!agentFeatures || agentFeatures.length === 0) return true return hasRequiredFeatures([...agentFeatures], [...toolFeatures], false) } export function buildAgentToolOutputSchema(resultSchema?: ZodTypeAny): ZodTypeAny { return z.object({ result: resultSchema ?? z.unknown(), agentId: z.string(), runId: z.string().nullable(), agentRunId: z.string().nullable(), pendingActionIds: z.array(z.string()).optional(), }) } export function toAgentRunChannel(channel: McpToolContext['channel']): AiAgentRunChannel { if (channel === 'mcp') return 'mcp' if (channel === 'tools-api') return 'tools-api' return 'chat' } function defaultToPrompt(input: unknown): string { if (typeof input === 'string') return input if (input && typeof input === 'object' && !Array.isArray(input)) { const candidate = (input as Record).input if (typeof candidate === 'string') return candidate } return JSON.stringify(input) } function resolveCorrelationId(context: McpToolContext): string { return context.correlationId ?? context.auditCorrelation?.correlationId ?? randomUUID() } function resolveApiKeyId(context: McpToolContext): string | null { return context.apiKeyId ?? context.auditCorrelation?.apiKeyId ?? null } function buildDispatchCaller(agentId: string, context: McpToolContext): AiAgentRunDispatchCaller { if (!context.tenantId || !context.organizationId) { throw new AgentToolError('AGENT_POLICY_DENIED', agentId, 'agent_principal_missing') } const apiKeyId = resolveApiKeyId(context) return { tenantId: context.tenantId, organizationId: context.organizationId, userId: apiKeyId ? (context.requesterUserId ?? context.userId) : context.userId, apiKeyId, features: context.userFeatures, isSuperAdmin: context.isSuperAdmin, } } async function withTimeout( toolName: string, timeoutMs: number, run: (signal: AbortSignal) => Promise, ): Promise { const controller = new AbortController() let timer: ReturnType | undefined const timeout = new Promise((_resolve, reject) => { timer = setTimeout(() => { controller.abort() reject(new Error(`[internal] Agent tool "${toolName}" timed out after ${timeoutMs}ms`)) }, timeoutMs) }) try { return await Promise.race([run(controller.signal), timeout]) } finally { if (timer) clearTimeout(timer) } } function validateAgainst(schema: ZodTypeAny | undefined, value: unknown, toolName: string): unknown { if (!schema || typeof schema.safeParse !== 'function') return value const parsed = schema.safeParse(value) if (!parsed.success) { throw new Error(`[internal] Agent tool "${toolName}" received an object that does not match its output schema`) } return parsed.data } type LocalAgentDefinition = NonNullable> function assertLocalAgentPolicy( spec: DefineAgentToolSpec, agent: LocalAgentDefinition, context: McpToolContext, ): void { if (spec.requiredFeatures && !isAgentToolFeatureSuperset(spec.requiredFeatures, agent.requiredFeatures)) { throw new Error( `[internal] Agent tool "${spec.name}" requiredFeatures must cover every feature agent "${spec.agentId}" requires`, ) } const decision = checkAgentPolicy({ agentId: spec.agentId, authContext: { userFeatures: context.userFeatures, isSuperAdmin: context.isSuperAdmin }, requestedExecutionMode: 'object', }) if (!decision.ok) { throw new AgentToolError('AGENT_POLICY_DENIED', spec.agentId, decision.code) } } async function runThroughDispatcher( dispatcher: AiAgentRunDispatcher, spec: DefineAgentToolSpec, input: TInput, context: McpToolContext, correlationId: string, timeoutMs: number, ): Promise> { await loadAgentRegistry() const localAgent = getAgent(spec.agentId) if (localAgent) assertLocalAgentPolicy(spec, localAgent, context) if (!(await dispatcher.isAvailable(spec.agentId))) { throw new AgentToolError('AGENT_DISPATCHER_UNAVAILABLE', spec.agentId) } const caller = buildDispatchCaller(spec.agentId, context) try { const dispatched = await withTimeout(spec.name, timeoutMs, (signal) => dispatcher.runObject({ agentId: spec.agentId, input, caller, channel: toAgentRunChannel(context.channel), via: 'tool', correlationId, timeoutMs, signal, }), ) return { result: validateAgainst(spec.outputSchema, dispatched.result, spec.name), agentId: spec.agentId, runId: null, agentRunId: dispatched.agentRunId, } } catch (error) { if (isAiAgentDispatchDeniedError(error)) { throw new AgentToolError('AGENT_POLICY_DENIED', spec.agentId, error.code) } throw error } } async function runThroughAiAssistant( spec: DefineAgentToolSpec, input: TInput, context: McpToolContext, correlationId: string, timeoutMs: number, ): Promise> { await loadAgentRegistry() const agent = getAgent(spec.agentId) if (!agent) { throw new AgentToolError('AGENT_DISPATCHER_UNAVAILABLE', spec.agentId) } assertLocalAgentPolicy(spec, agent, context) const userId = context.userId if (!userId) { throw new AgentToolError('AGENT_POLICY_DENIED', spec.agentId, 'agent_principal_missing') } const prompt = spec.toPrompt ? spec.toPrompt(input) : defaultToPrompt(input) const channel = context.channel ?? 'agent' try { const outcome = await withTimeout(spec.name, timeoutMs, async (signal) => { const run = await runAiAgentObject({ agentId: spec.agentId, input: prompt, authContext: { tenantId: context.tenantId, organizationId: context.organizationId, userId, features: context.userFeatures, isSuperAdmin: context.isSuperAdmin, }, container: context.container, enableTools: true, sessionId: correlationId, channel, mutationMode: context.mutationMode, apiKeyId: resolveApiKeyId(context), correlationId, trace: { channel: toAgentRunChannel(context.channel), correlationId }, abortSignal: signal, }) if (run.mode === 'generate') { return { object: run.object, runId: run.runId, pendingActionIds: run.pendingActionIds } } return { object: await run.object, runId: run.runId, pendingActionIds: undefined } }) const agentValidated = validateAgainst(agent.output?.schema, outcome.object, spec.name) const pendingActionIds = outcome.pendingActionIds ?? [] return { result: validateAgainst(spec.outputSchema, agentValidated, spec.name), agentId: spec.agentId, runId: outcome.runId ?? null, agentRunId: null, ...(pendingActionIds.length > 0 ? { pendingActionIds: [...pendingActionIds] } : {}), } } catch (error) { if (error instanceof AgentPolicyError) { throw new AgentToolError('AGENT_POLICY_DENIED', spec.agentId, error.code) } throw error } } async function isOwnedByDispatcher(dispatcher: AiAgentRunDispatcher, agentId: string): Promise { if (dispatcher.owns(agentId)) return true await loadAgentRegistry() return resolveDispatchOwner(dispatcher, agentId, { knownLocally: Boolean(getAgent(agentId)) }) } export function defineAgentTool( spec: DefineAgentToolSpec, ): AiToolDefinition> { const timeoutMs = spec.timeoutMs ?? AGENT_TOOL_DEFAULT_TIMEOUT_MS const explicitFeatures = spec.requiredFeatures ? [...spec.requiredFeatures] : undefined const handler = async (input: TInput, context: McpToolContext): Promise> => { const correlationId = resolveCorrelationId(context) const genericSpec = spec as DefineAgentToolSpec const dispatcher = resolveAiAgentRunDispatcher(context.container) const owner = dispatcher && (await isOwnedByDispatcher(dispatcher, spec.agentId)) ? dispatcher : null const outcome = owner ? await runThroughDispatcher(owner, genericSpec, input, context, correlationId, timeoutMs) : await runThroughAiAssistant(genericSpec, input, context, correlationId, timeoutMs) return outcome as AgentToolResult } const tool: AiToolDefinition> = { name: spec.name, description: spec.description, inputSchema: spec.inputSchema, isMutation: false, tags: ['agent'], agentRef: { agentId: spec.agentId, engine: 'ai-assistant' }, mcp: { outputSchema: buildAgentToolOutputSchema(spec.outputSchema) }, handler, } if (explicitFeatures) tool.requiredFeatures = explicitFeatures return tool }