MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / createCanUseTool

Method createCanUseTool

source code/cli/structuredIO.ts:535–661  ·  view source on GitHub ↗
(
    onPermissionPrompt?: (details: RequiresActionDetails) => void,
  )

Source from the content-addressed store, hash-verified

533 }
534
535 createCanUseTool(
536 onPermissionPrompt?: (details: RequiresActionDetails) => void,
537 ): CanUseToolFn {
538 return async (
539 tool: Tool,
540 input: { [key: string]: unknown },
541 toolUseContext: ToolUseContext,
542 assistantMessage: AssistantMessage,
543 toolUseID: string,
544 forceDecision?: PermissionDecision,
545 ): Promise<PermissionDecision> => {
546 const mainPermissionResult =
547 forceDecision ??
548 (await hasPermissionsToUseTool(
549 tool,
550 input,
551 toolUseContext,
552 assistantMessage,
553 toolUseID,
554 ))
555 // If the tool is allowed or denied, return the result
556 if (
557 mainPermissionResult.behavior === 'allow' ||
558 mainPermissionResult.behavior === 'deny'
559 ) {
560 return mainPermissionResult
561 }
562
563 // Run PermissionRequest hooks in parallel with the SDK permission
564 // prompt. In the terminal CLI, hooks race against the interactive
565 // prompt so that e.g. a hook with --delay 20 doesn't block the UI.
566 // We need the same behavior here: the SDK host (VS Code, etc.) shows
567 // its permission dialog immediately while hooks run in the background.
568 // Whichever resolves first wins; the loser is cancelled/ignored.
569
570 // AbortController used to cancel the SDK request if a hook decides first
571 const hookAbortController = new AbortController()
572 const parentSignal = toolUseContext.abortController.signal
573 // Forward parent abort to our local controller
574 const onParentAbort = () => hookAbortController.abort()
575 parentSignal.addEventListener('abort', onParentAbort, { once: true })
576
577 try {
578 // Start the hook evaluation (runs in background)
579 const hookPromise = executePermissionRequestHooksForSDK(
580 tool.name,
581 toolUseID,
582 input,
583 toolUseContext,
584 mainPermissionResult.suggestions,
585 ).then(decision => ({ source: 'hook' as const, decision }))
586
587 // Start the SDK permission prompt immediately (don't wait for hooks)
588 const requestId = randomUUID()
589 onPermissionPrompt?.(
590 buildRequiresActionDetails(tool, input, toolUseID, requestId),
591 )
592 const sdkPromise = this.sendRequest<PermissionToolOutput>(

Callers 1

getCanUseToolFnFunction · 0.80

Tested by

no test coverage detected