Skip to main content

TaskManager

Struct TaskManager 

Source
pub struct TaskManager { /* private fields */ }
Expand description

Background task manager.

Manages the lifecycle of background tasks including submission, status tracking, and cancellation.

Implementations§

Source§

impl TaskManager

Source

pub fn new() -> Self

Creates a new task manager.

Source

pub fn with_list_changed_notifications() -> Self

Creates a new task manager with list change notifications enabled.

Source

pub fn new_for_testing() -> Self

Creates a task manager configured for deterministic tests.

Tasks are not executed automatically; tests can drive state manually.

Source

pub fn into_shared(self) -> SharedTaskManager

Converts this manager into a shared handle.

Source

pub fn has_list_changed_notifications(&self) -> bool

Returns whether list change notifications are enabled.

Source

pub fn set_notification_sender( &self, sender: Arc<dyn Fn(JsonRpcRequest) + Send + Sync>, )

Sets the notification sender for task status updates.

Source

pub fn register_handler<F, Fut>(&self, task_type: impl Into<String>, handler: F)
where F: Fn(&Cx, Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = McpResult<Value>> + Send + 'static,

Registers a task handler for a specific task type.

The handler will be invoked when a task of this type is submitted.

Source

pub fn submit( &self, _cx: &Cx, task_type: impl Into<String>, params: Option<Value>, ) -> McpResult<TaskId>

Submits a new background task.

Returns the task ID for tracking. The task runs asynchronously in the background region.

Source

pub fn start_task(&self, task_id: &TaskId) -> McpResult<()>

Starts execution of a pending task.

This is called internally to transition a task from Pending to Running.

Source

pub fn update_progress( &self, task_id: &TaskId, progress: f64, message: Option<String>, )

Updates progress for a running task.

Source

pub fn complete_task(&self, task_id: &TaskId, data: Value)

Completes a task with a successful result.

Source

pub fn fail_task(&self, task_id: &TaskId, error: impl Into<String>)

Fails a task with an error.

Source

pub fn get_info(&self, task_id: &TaskId) -> Option<TaskInfo>

Gets information about a task.

Source

pub fn get_result(&self, task_id: &TaskId) -> Option<TaskResult>

Gets the result of a completed task.

Source

pub fn list_tasks(&self, status_filter: Option<TaskStatus>) -> Vec<TaskInfo>

Lists all tasks, optionally filtered by status.

Source

pub fn cancel( &self, task_id: &TaskId, reason: Option<String>, ) -> McpResult<TaskInfo>

Requests cancellation of a task.

Returns true if the task exists and cancellation was requested. The task may still be running until it checks for cancellation.

Source

pub fn is_cancel_requested(&self, task_id: &TaskId) -> bool

Checks if cancellation has been requested for a task.

Source

pub fn active_count(&self) -> usize

Returns the number of active (non-terminal) tasks.

Source

pub fn total_count(&self) -> usize

Returns the total number of tasks.

Source

pub fn cleanup_completed(&self, max_age: Duration)

Removes completed tasks older than the specified duration.

This is useful for preventing unbounded memory growth from completed tasks.

Trait Implementations§

Source§

impl Debug for TaskManager

Source§

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

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

impl Default for TaskManager

Source§

fn default() -> Self

Returns the “default value” for a type. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> Same for T

Source§

type Output = T

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