Anubis.Server.Task (anubis_mcp v1.6.0)

Copy Markdown

Represents an MCP task — a durable state machine wrapping a long-running request.

Spec reference: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/tasks.

Tasks are receiver-owned: when a server accepts a task-augmented request (e.g. tools/call with a task field), it generates a task id, runs the work asynchronously, and exposes the lifecycle through the tasks/get, tasks/result, and tasks/cancel operations.

Summary

Functions

Generates a cryptographically-strong task id.

Builds a fresh task in :working status.

Returns true when the task is in a terminal status.

Wraps the task projection inside the CreateTaskResult envelope returned to the requestor at task creation time.

Builds the wire-format Task projection used by tasks/* responses and the notifications/tasks/status notification. Excludes the underlying result.

Transitions the task into a new status. The caller is responsible for enforcing the FSM (working ↔ input_required → terminal).

Types

status()

@type status() :: :working | :input_required | :completed | :failed | :cancelled

t()

@type t() :: %Anubis.Server.Task{
  created_at: DateTime.t(),
  error: Anubis.MCP.Error.t() | nil,
  id: String.t(),
  last_updated_at: DateTime.t(),
  method: String.t(),
  original_params: map() | nil,
  poll_interval: pos_integer() | nil,
  request_id: String.t() | integer(),
  result: term() | nil,
  session_id: String.t(),
  status: status(),
  status_message: String.t() | nil,
  ttl: pos_integer() | nil
}

Functions

generate_id()

@spec generate_id() :: String.t()

Generates a cryptographically-strong task id.

Per spec: receivers MUST use enough entropy to prevent guessing when no authorization context is bound to the task.

new(opts)

@spec new(keyword()) :: t()

Builds a fresh task in :working status.

terminal?(status)

@spec terminal?(t() | status()) :: boolean()

Returns true when the task is in a terminal status.

to_create_result(task)

@spec to_create_result(t()) :: map()

Wraps the task projection inside the CreateTaskResult envelope returned to the requestor at task creation time.

to_protocol(task)

@spec to_protocol(t()) :: map()

Builds the wire-format Task projection used by tasks/* responses and the notifications/tasks/status notification. Excludes the underlying result.

transition(task, status, opts \\ [])

@spec transition(t(), status(), keyword()) :: t()

Transitions the task into a new status. The caller is responsible for enforcing the FSM (working ↔ input_required → terminal).