Extension API: Opt-in Tool Approval
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.ktsto 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 |
Kotlin host code ( |
Full |
Programmatic policy from the host app |
Gate 2 |
|
|
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 |
|---|---|
|
Global โ applies to every Book session on this machine |
|
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:
-
Extracts a human-readable
summaryfrom the tool arguments (usingcommand,path,file_path, orpatternkeys, truncated to 60 characters). -
Calls
ctx.ui.approveToolCall(toolName, summary)to show an interactive APPROVE / DENY / ALWAYS_APPROVE prompt. -
Maps the response to a
ToolCallResult:-
APPROVEโ allow execution -
DENYornullโ 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:
-
System.console() == nullโ stdout is piped to another process; no interactive prompt is possible. -
Unrecognised input โ the user typed something other than
y,n, orain 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? |
|---|---|---|
|
Canonical, production-ready scripts |
โ
Yes โ |
|
Illustrative examples for documentation |
โ No โ for reference only |
Always install scripts from :extensions/src/scripts/, not from samples/.