Part of the Extension API Guide. Interactive APPROVE/DENY gating for tool calls via tool_approval.bk.kts.

Opt-in Tool Approval

Default: All tools execute freely โ€” no approval is required ("yolo mode"). Opt-in: Install tool_approval.bk.kts to enable interactive approval prompts.

Overview

There are two independent gates before any tool executes:

Gate Who implements it What it has access to When to use

Gate 1 AgentLoopConfig.beforeToolCall()

Kotlin host code (:cli, :domain)

Full AgentContext โ€” messages history, system prompt, all tools

Programmatic policy from the host app

Gate 2 ToolCall extension interceptor

.bk.kts scripts

toolName, toolCallId, input args + ExtensionContext (isHeadless, ui, etc.)

Operator-installed scripts for interactive or custom policy

Gate 1 fires first. If it blocks, Gate 2 never runs. If it allows (or returns null, the default), Gate 2 runs next. Multiple scripts can register ToolCall interceptors โ€” they fire in registration order and the first one that returns a result wins; subsequent interceptors for the same call are skipped.

Tool approval via tool_approval.bk.kts operates at Gate 2. The script is not baked into Book’s runtime. You enable it by copying (or symlinking) it to a script discovery directory.

Install Locations

Location Scope

~/.bk/tools/tool_approval.bk.kts

Global โ€” applies to every Book session on this machine

.bk/tools/tool_approval.bk.kts

Project-local โ€” applies only when Book is run in this directory tree

What the Script Does

The script registers a ToolCall interceptor that guards a configurable set of tool names. For each intercepted tool call, it:

  1. Extracts a human-readable summary from the tool arguments (using command, path, file_path, or pattern keys, truncated to 60 characters).

  2. Calls ctx.ui.approveToolCall(toolName, summary) to show an interactive APPROVE / DENY / ALWAYS_APPROVE prompt.

  3. Maps the response to a ToolCallResult:

    • APPROVE โ†’ allow execution

    • DENY or null โ†’ block execution with a "User denied" reason

    • ALWAYS_APPROVE โ†’ allow execution and suppress future prompts for this tool in the session

Customising the Guarded Tool Set

Edit the requireApproval set at the top of the script:

// In tool_approval.bk.kts โ€” edit this set to match your risk model
val requireApproval = setOf(
    "bash",
    "write_file",
    "execute_python",
    // add or remove tool names here
)

Non-Interactive Mode Behaviour

ctx.isHeadless is true whenever Book is running outside the interactive TUI โ€” this covers both headless (-p / --prompt) and ACP server mode. The script detects this and bypasses all prompts, allowing every tool to execute freely.

# Headless (CI / piped) โ€” isHeadless = true
bk -p "refactor the auth module" src/

# ACP server mode (e.g. Xcode integration) โ€” isHeadless = true
bk serve

This is the correct behaviour for all non-interactive contexts: there is no terminal to display a prompt in either case, and blocking on user input would stall the session permanently.

Null Decision: Piped Stdout or Unrecognised Input

ctx.ui.approveToolCall() returns null in two cases:

  1. System.console() == null โ€” stdout is piped to another process; no interactive prompt is possible.

  2. Unrecognised input โ€” the user typed something other than y, n, or a in terminal (non-TUI) mode.

In TUI mode (alt-screen) the response is key-driven (y/n/a/Escape) and always returns a concrete ApprovalDecision. The script’s when (null) branch covers both piped-stdout and unrecognised-input cases โ€” both are treated as DENY, blocking the tool.

ApprovalDecision Enum

// Available without import (in defaultImports)
enum class ApprovalDecision {
    APPROVE,           // Allow this call once
    DENY,              // Block this call
    ALWAYS_APPROVE     // Allow this call and all future calls to the same tool in this session
}

Curated Scripts vs. Sample Scripts

Location Purpose CI-compiled?

:extensions module (extensions/src/scripts/)

Canonical, production-ready scripts

โœ… Yes โ€” ./gradlew :extensions:check

samples/

Illustrative examples for documentation

โŒ No โ€” for reference only

Always install scripts from :extensions/src/scripts/, not from samples/.