ExMCP.Tasks.Task (ex_mcp v0.9.2)
View SourceTask struct and state machine validation for MCP Tasks (2025-11-25).
Tasks represent async operations initiated by tool calls. This module provides a pure data structure and state transition validation functions. It does NOT include any GenServer or process management - users implement task lifecycle management themselves via handler callbacks.
State Machine
Valid states: :working, :input_required, :completed, :failed, :cancelled
Valid transitions:
:working->:input_required|:completed|:failed|:cancelled:input_required->:working|:cancelled:completed-> (terminal state):failed-> (terminal state):cancelled-> (terminal state)
Usage
task = ExMCP.Tasks.Task.new("my-tool", %{"arg" => "value"})
{:ok, task} = ExMCP.Tasks.Task.transition(task, :completed)
Summary
Functions
Transitions and sets the result (for completed tasks).
Transitions to failed state with error info.
Creates a new task in the :working state.
Parses a state string to a state atom.
Returns all valid states.
Checks if the task is in a terminal state.
Returns all terminal states.
Converts a task to a map suitable for protocol serialization.
Attempts a state transition.
Checks if a transition from one state to another is valid.
Types
@type state() :: :working | :input_required | :completed | :failed | :cancelled
Functions
Transitions and sets the result (for completed tasks).
Transitions to failed state with error info.
Creates a new task in the :working state.
Parameters
tool_name- Name of the tool this task is executingarguments- Tool argumentsopts- Optional fields::id,:ttl,:metadata
Parses a state string to a state atom.
@spec states() :: [state()]
Returns all valid states.
Checks if the task is in a terminal state.
@spec terminal_states() :: [state()]
Returns all terminal states.
Converts a task to a map suitable for protocol serialization.
Attempts a state transition.
Returns {:ok, updated_task} if the transition is valid,
{:error, reason} if invalid.
Checks if a transition from one state to another is valid.