MCPcopy
hub / github.com/codeaashu/claude-code / executePermissionRequestHooksForSDK

Function executePermissionRequestHooksForSDK

src/cli/structuredIO.ts:787–859  ·  view source on GitHub ↗

* Execute PermissionRequest hooks and return a decision if one is made. * Returns undefined if no hook made a decision.

(
  toolName: string,
  toolUseID: string,
  input: Record<string, unknown>,
  toolUseContext: ToolUseContext,
  suggestions: PermissionUpdate[] | undefined,
)

Source from the content-addressed store, hash-verified

785 * Returns undefined if no hook made a decision.
786 */
787async function executePermissionRequestHooksForSDK(
788 toolName: string,
789 toolUseID: string,
790 input: Record<string, unknown>,
791 toolUseContext: ToolUseContext,
792 suggestions: PermissionUpdate[] | undefined,
793): Promise<PermissionDecision | undefined> {
794 const appState = toolUseContext.getAppState()
795 const permissionMode = appState.toolPermissionContext.mode
796
797 // Iterate directly over the generator instead of using `all`
798 const hookGenerator = executePermissionRequestHooks(
799 toolName,
800 toolUseID,
801 input,
802 toolUseContext,
803 permissionMode,
804 suggestions,
805 toolUseContext.abortController.signal,
806 )
807
808 for await (const hookResult of hookGenerator) {
809 if (
810 hookResult.permissionRequestResult &&
811 (hookResult.permissionRequestResult.behavior === 'allow' ||
812 hookResult.permissionRequestResult.behavior === 'deny')
813 ) {
814 const decision = hookResult.permissionRequestResult
815 if (decision.behavior === 'allow') {
816 const finalInput = decision.updatedInput || input
817
818 // Apply permission updates if provided by hook ("always allow")
819 const permissionUpdates = decision.updatedPermissions ?? []
820 if (permissionUpdates.length > 0) {
821 persistPermissionUpdates(permissionUpdates)
822 const currentAppState = toolUseContext.getAppState()
823 const updatedContext = applyPermissionUpdates(
824 currentAppState.toolPermissionContext,
825 permissionUpdates,
826 )
827 // Update permission context via setAppState
828 toolUseContext.setAppState(prev => {
829 if (prev.toolPermissionContext === updatedContext) return prev
830 return { ...prev, toolPermissionContext: updatedContext }
831 })
832 }
833
834 return {
835 behavior: 'allow',
836 updatedInput: finalInput,
837 userModified: false,
838 decisionReason: {
839 type: 'hook',
840 hookName: 'PermissionRequest',
841 },
842 }
843 } else {
844 // Hook denied the permission

Callers 1

createCanUseToolMethod · 0.85

Calls 3

persistPermissionUpdatesFunction · 0.85
applyPermissionUpdatesFunction · 0.85

Tested by

no test coverage detected