# `Anubis.Server.Task`

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.

# `status`

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

# `t`

```elixir
@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
}
```

# `generate_id`

```elixir
@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`

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

Builds a fresh task in `:working` status.

# `terminal?`

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

Returns true when the task is in a terminal status.

# `to_create_result`

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

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

# `to_protocol`

```elixir
@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`

```elixir
@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`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
