Skip to main content

ToolContext

Struct ToolContext 

Source
pub struct ToolContext {
Show 21 fields pub project_root: String, pub extra_roots: Vec<String>, pub minimal: bool, pub resolved_paths: HashMap<String, String>, pub crp_mode: CrpMode, pub cache: Option<SharedCache>, pub session: Option<Arc<RwLock<SessionState>>>, pub tool_calls: Option<Arc<RwLock<Vec<ToolCallRecord>>>>, pub agent_id: Option<Arc<RwLock<Option<String>>>>, pub workflow: Option<Arc<RwLock<Option<WorkflowRun>>>>, pub ledger: Option<Arc<RwLock<ContextLedger>>>, pub client_name: Option<Arc<RwLock<String>>>, pub client_role: Option<String>, pub shell_access: Option<bool>, pub pipeline_stats: Option<Arc<RwLock<PipelineStats>>>, pub call_count: Option<Arc<AtomicUsize>>, pub autonomy: Option<Arc<AutonomyState>>, pub path_errors: HashMap<String, String>, pub bm25_cache: Option<SharedBm25Cache>, pub progress_sender: Option<SharedProgressSender>, pub cancel: Option<CancellationToken>,
}
Expand description

Context passed to tool handlers. Contains pre-resolved values that many tools need, avoiding repeated async lock acquisition inside handlers. Extended with shared server state for tools that need cache/session access.

Clone exists for per-op delegation (#1088): a batch op with its own path clones the ctx and swaps in that op’s resolved path.

Fields§

§project_root: String§extra_roots: Vec<String>

Session-scoped trusted roots (MCP roots/list, config extra_roots), snapshotted from the session so sync handlers can honor them without an async lock. Empty = single-root jail behaviour (#403).

§minimal: bool§resolved_paths: HashMap<String, String>

Pre-resolved paths keyed by argument name (e.g. “path” -> “/abs/dir”).

§crp_mode: CrpMode

CRP mode for compression-aware tools.

§cache: Option<SharedCache>

Shared cache handle for tools that need read/write access.

§session: Option<Arc<RwLock<SessionState>>>

Shared session handle for tools that need session access.

§tool_calls: Option<Arc<RwLock<Vec<ToolCallRecord>>>>

Tool call records for session-aware tools (e.g. ctx_session status).

§agent_id: Option<Arc<RwLock<Option<String>>>>

Current agent identity for multi-agent tools.

§workflow: Option<Arc<RwLock<Option<WorkflowRun>>>>

Active workflow run state.

§ledger: Option<Arc<RwLock<ContextLedger>>>

Context ledger for handoff operations.

§client_name: Option<Arc<RwLock<String>>>

Client name (cursor, claude, etc.).

§client_role: Option<String>

Optional MCP client role supplied by the session/request context. Absent preserves the backward-compatible permissive default.

§shell_access: Option<bool>

Optional shell permission supplied by the session/request context. None preserves the backward-compatible permissive default.

§pipeline_stats: Option<Arc<RwLock<PipelineStats>>>

Pipeline stats for metrics/proof tools.

§call_count: Option<Arc<AtomicUsize>>

Global call counter for context tools.

§autonomy: Option<Arc<AutonomyState>>

Autonomy state for search repeat detection.

§path_errors: HashMap<String, String>

Errors from path resolution (PathJail rejection, secret path, etc.). Keyed by argument name (e.g. “path” -> “path escapes project root: …”).

§bm25_cache: Option<SharedBm25Cache>

Shared in-memory BM25 index cache for semantic search.

§progress_sender: Option<SharedProgressSender>

MCP progress notification sender for long-running operations.

§cancel: Option<CancellationToken>

Cancellation token for this request, when the transport supplies one (#1781).

A synchronous tool that waits on a child process cannot observe an aborted call without it: the host stops waiting, but the wait loop keeps running and holds its thread until its own cap expires. Tools that block should consult this and return promptly when it fires.

Implementations§

Source§

impl ToolContext

Source

pub fn resolved_path(&self, arg: &str) -> Option<&str>

Source

pub fn path_error(&self, key: &str) -> Option<&str>

Returns the path resolution error for a given key, if any.

Source

pub fn resolve_path_sync(&self, path: &str) -> Result<String, String>

Sync path resolution using project_root + session extra_roots. Thin wrapper over lean_ctx::core::path_resolve::resolve_tool_path_with_roots for sync tool handlers.

Source

pub fn ensure_writable(&self, resolved_path: &str) -> Result<(), String>

Default-deny write gate for the read-only tier (#475). Write-capable tool handlers must call this with an already-resolved absolute path before touching the filesystem; it errors if the path is inside a configured read_only_roots subtree. A no-op (always Ok) when no read-only roots are configured, so non-users pay nothing. Thin wrapper over the single choke point lean_ctx::core::pathjail::enforce_writable — the low-level atomic writers call the same function, so this is the ergonomic, early-error layer, not the only line of defence.

Trait Implementations§

Source§

impl Clone for ToolContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Default for ToolContext

Source§

fn default() -> Self

Minimal context: project_root empty, all shared-state handles None. Single source for the one-shot CLI ctx (cli::call_cmd::oneshot_ctx) and the empty_ctx test helper — adding a field no longer breaks either.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more