@open-mercato/ai-assistant 0.11.1-develop.8305.1.6a9c271838 → 0.11.1-develop.8317.1.dde3d38b64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -189,10 +189,18 @@ function formatToolResult(result: unknown): string {
189
189
  }
190
190
  }
191
191
 
192
+ interface ToolHandlerChannelContext {
193
+ channel: AiPendingActionChannel
194
+ mutationMode?: McpMutationMode
195
+ apiKeyId: string | null
196
+ correlationId: string | null
197
+ }
198
+
192
199
  function buildToolHandlerContext(
193
200
  ctx: AiChatRequestContext,
194
201
  container?: AwilixContainer,
195
202
  tool?: AiToolDefinition,
203
+ channelContext?: ToolHandlerChannelContext,
196
204
  ): McpToolContext {
197
205
  return {
198
206
  tenantId: ctx.tenantId,
@@ -202,6 +210,14 @@ function buildToolHandlerContext(
202
210
  userFeatures: ctx.features,
203
211
  isSuperAdmin: ctx.isSuperAdmin,
204
212
  ...(tool ? { tool } : {}),
213
+ ...(channelContext
214
+ ? {
215
+ channel: channelContext.channel,
216
+ ...(channelContext.mutationMode ? { mutationMode: channelContext.mutationMode } : {}),
217
+ apiKeyId: channelContext.apiKeyId,
218
+ correlationId: channelContext.correlationId,
219
+ }
220
+ : {}),
205
221
  }
206
222
  }
207
223
 
@@ -292,9 +308,10 @@ function adaptToolToAiSdk(
292
308
  container: AwilixContainer | undefined,
293
309
  effectiveMutationPolicy: 'read-only' | 'confirm-required' | 'destructive-confirm-required' | undefined,
294
310
  toolCallTimings: Map<string, ToolCallTiming>,
311
+ channelContext?: ToolHandlerChannelContext,
295
312
  ): Tool<unknown, unknown> {
296
313
  const safeSchema = toSafeZodSchema(tool.inputSchema as ZodType)
297
- const handlerContext = buildToolHandlerContext(ctx, container, tool)
314
+ const handlerContext = buildToolHandlerContext(ctx, container, tool, channelContext)
298
315
  const runToolCall = async (args: unknown, toolCallId: string | null): Promise<string> => {
299
316
  const replayDecision = resolveToolReplayDecision({
300
317
  toolName: tool.name,
@@ -313,8 +330,11 @@ function adaptToolToAiSdk(
313
330
  // Per-call gating decision. Under `destructive-confirm-required`,
314
331
  // tools with a predicate `isDestructive(args)` may flip per call —
315
332
  // e.g. comment delete gates while comment create runs direct.
333
+ // On `mcp` / `tools-api` a non-null `mutation` means approval mode,
334
+ // which never runs the handler directly, so every write is gated.
316
335
  const requiresApprovalNow = (() => {
317
336
  if (!mutation) return false
337
+ if (mutation.channel !== 'agent') return true
318
338
  if (effectiveMutationPolicy === 'confirm-required') return true
319
339
  if (effectiveMutationPolicy === 'destructive-confirm-required') {
320
340
  return resolveIsDestructive(mutation.tool.isDestructive, args)
@@ -535,6 +555,12 @@ export async function resolveAiAgentTools(
535
555
  input.container,
536
556
  effectiveMutationPolicy,
537
557
  toolCallTimings,
558
+ {
559
+ channel,
560
+ mutationMode: input.mutationMode,
561
+ apiKeyId: input.apiKeyId ?? null,
562
+ correlationId: input.correlationId ?? null,
563
+ },
538
564
  )
539
565
  } catch (error) {
540
566
  logger.error('Failed to adapt tool for agent', { toolName, agentId: agent.id, err: error })