Skip to main content

ToolPermissionRequest

Struct ToolPermissionRequest 

Source
pub struct ToolPermissionRequest {
    pub tool_name: String,
    pub input: Value,
    pub permission_suggestions: Vec<PermissionSuggestion>,
    pub blocked_path: Option<String>,
    pub decision_reason: Option<String>,
    pub tool_use_id: Option<String>,
}
Expand description

Tool permission request details

This is sent when Claude wants to use a tool. The SDK should evaluate the request and respond with allow/deny using the ergonomic builder methods.

§Example

use claude_codes::{ToolPermissionRequest, ControlResponse};
use serde_json::json;

fn handle_permission(req: &ToolPermissionRequest, request_id: &str) -> ControlResponse {
    // Block dangerous bash commands
    if req.tool_name == "Bash" {
        if let Some(cmd) = req.input.get("command").and_then(|v| v.as_str()) {
            if cmd.contains("rm -rf") {
                return req.deny("Dangerous command blocked", request_id);
            }
        }
    }

    // Allow everything else
    req.allow(request_id)
}

Fields§

§tool_name: String

Name of the tool Claude wants to use (e.g., “Bash”, “Write”, “Read”)

§input: Value

Input parameters for the tool

§permission_suggestions: Vec<PermissionSuggestion>

Suggested permissions that could be granted to avoid repeated prompts

§blocked_path: Option<String>

Path that was blocked (if this is a retry after path-based denial)

§decision_reason: Option<String>

Reason why this tool use requires approval

§tool_use_id: Option<String>

The tool use ID for this request

Implementations§

Source§

impl ToolPermissionRequest

Source

pub fn allow(&self, request_id: &str) -> ControlResponse

Allow the tool to execute with its original input.

§Example
let req = ToolPermissionRequest {
    tool_name: "Read".to_string(),
    input: json!({"file_path": "/tmp/test.txt"}),
    permission_suggestions: vec![],
    blocked_path: None,
    decision_reason: None,
    tool_use_id: None,
};
let response = req.allow("req-123");
Source

pub fn allow_with( &self, modified_input: Value, request_id: &str, ) -> ControlResponse

Allow the tool to execute with modified input.

Use this to sanitize or redirect tool inputs. For example, redirecting file writes to a safe directory.

§Example
let req = ToolPermissionRequest {
    tool_name: "Write".to_string(),
    input: json!({"file_path": "/etc/passwd", "content": "test"}),
    permission_suggestions: vec![],
    blocked_path: None,
    decision_reason: None,
    tool_use_id: None,
};
// Redirect to safe location
let safe_input = json!({"file_path": "/tmp/safe/passwd", "content": "test"});
let response = req.allow_with(safe_input, "req-123");
Source

pub fn allow_with_permissions( &self, modified_input: Value, permissions: Vec<Value>, request_id: &str, ) -> ControlResponse

Allow with updated permissions list (raw JSON Values).

Prefer using allow_and_remember for type safety.

Source

pub fn allow_and_remember( &self, permissions: Vec<Permission>, request_id: &str, ) -> ControlResponse

Allow the tool and grant permissions for “remember this decision”.

This is the ergonomic way to allow a tool while also granting permissions so similar actions won’t require approval in the future.

§Example
use claude_codes::{ToolPermissionRequest, Permission};
use serde_json::json;

let req = ToolPermissionRequest {
    tool_name: "Bash".to_string(),
    input: json!({"command": "npm test"}),
    permission_suggestions: vec![],
    blocked_path: None,
    decision_reason: None,
    tool_use_id: None,
};

// Allow and remember this decision for the session
let response = req.allow_and_remember(
    vec![Permission::allow_tool("Bash", "npm test")],
    "req-123",
);
Source

pub fn allow_with_and_remember( &self, modified_input: Value, permissions: Vec<Permission>, request_id: &str, ) -> ControlResponse

Allow the tool with modified input and grant permissions.

Combines input modification with “remember this decision” functionality.

Source

pub fn allow_and_remember_suggestion( &self, request_id: &str, ) -> Option<ControlResponse>

Allow the tool and remember using the first permission suggestion.

This is a convenience method for the common case of accepting Claude’s first suggested permission (usually the most relevant one).

Returns None if there are no permission suggestions.

§Example
use claude_codes::ToolPermissionRequest;
use serde_json::json;

let req = ToolPermissionRequest {
    tool_name: "Bash".to_string(),
    input: json!({"command": "npm test"}),
    permission_suggestions: vec![],  // Would have suggestions in real use
    blocked_path: None,
    decision_reason: None,
    tool_use_id: None,
};

// Try to allow with first suggestion, or just allow without remembering
let response = req.allow_and_remember_suggestion("req-123")
    .unwrap_or_else(|| req.allow("req-123"));
Source

pub fn deny( &self, message: impl Into<String>, request_id: &str, ) -> ControlResponse

Deny the tool execution.

The message will be shown to Claude, who may try a different approach.

§Example
let req = ToolPermissionRequest {
    tool_name: "Bash".to_string(),
    input: json!({"command": "sudo rm -rf /"}),
    permission_suggestions: vec![],
    blocked_path: None,
    decision_reason: None,
    tool_use_id: None,
};
let response = req.deny("Dangerous command blocked by policy", "req-123");
Source

pub fn deny_and_stop( &self, message: impl Into<String>, request_id: &str, ) -> ControlResponse

Deny the tool execution and stop the entire session.

Use this for severe policy violations that should halt all processing.

Trait Implementations§

Source§

impl Clone for ToolPermissionRequest

Source§

fn clone(&self) -> ToolPermissionRequest

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ToolPermissionRequest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ToolPermissionRequest

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for ToolPermissionRequest

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,