Task struct and state machine validation for MCP Tasks.
Tasks represent async operations initiated by tool calls. This module
provides a pure data structure and state transition validation functions.
ExMCP.Tasks and ExMCP.Tasks.Store provide the optional durable lifecycle
boundary; the task struct itself does not own a process.
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.
Transitions a task to input_required with outstanding input requests.
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.
Converts a task to the wire representation for a protocol era or version.
Attempts a state transition.
Checks if a transition from one state to another is valid.
Types
@type state() :: :working | :input_required | :completed | :failed | :cancelled
@type t() :: %ExMCP.Tasks.Task{ arguments: map(), created_at: String.t(), error: map() | nil, id: String.t(), input_requests: map() | nil, last_updated_at: String.t() | nil, metadata: map(), poll_interval: integer() | nil, result: map() | nil, state: state(), status_message: String.t() | nil, tool_name: String.t(), ttl: integer() | nil }
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.
Transitions a task to input_required with outstanding input requests.
@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.
Converts a task to the wire representation for a protocol era or version.
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.