barrel_mcp_tasks (barrel_mcp v2.0.2)

View Source

Long-running operation registry (MCP tasks).

Tools registered with long_running => true return immediately with a taskId instead of synchronously producing a result. The worker continues in the background; clients poll via tasks/get, enumerate via tasks/list, and abort via tasks/cancel. State transitions emit notifications/tasks/status on the session's SSE channel.

Tasks live in a protected ETS table keyed by {SessionId, TaskId}. A periodic sweep evicts terminal tasks (success / error / cancelled) older than ?TASK_TTL.

Summary

Functions

Mark a task as cancelled and notify the client. Sends {cancel, RequestId} to the worker pid (if recorded) so cooperative arity-2 handlers can abort.

Create a new running task. Returns the task id.

Record failure: store the error and emit notification.

Record success: store the result and emit notifications/tasks/status.

Record the worker pid (and optional originating request id) on a running task so a later tasks/cancel can stop it.

Functions

cancel(SessionId, TaskId)

-spec cancel(binary() | undefined, binary()) -> ok | {error, not_found}.

Mark a task as cancelled and notify the client. Sends {cancel, RequestId} to the worker pid (if recorded) so cooperative arity-2 handlers can abort.

create(SessionId, Method, Opts)

-spec create(SessionId :: binary() | undefined, Method :: binary(), Opts :: map()) -> {ok, binary()}.

Create a new running task. Returns the task id.

fail(SessionId, TaskId, Reason)

-spec fail(binary() | undefined, binary(), term()) -> ok | {error, not_found}.

Record failure: store the error and emit notification.

finish(SessionId, TaskId, Result)

-spec finish(binary() | undefined, binary(), term()) -> ok | {error, not_found}.

Record success: store the result and emit notifications/tasks/status.

get(SessionId, TaskId)

-spec get(SessionId :: binary() | undefined, TaskId :: binary()) -> {ok, map()} | {error, not_found}.

handle_call(_, From, State)

handle_cast(Msg, State)

handle_info(_, State)

init(_)

list(SessionId, Opts)

-spec list(SessionId :: binary() | undefined, map()) -> {ok, [map()]}.

set_worker(SessionId, TaskId, Info)

-spec set_worker(binary() | undefined, binary(), #{worker := pid(), request_id => integer() | binary()}) ->
                    ok | {error, not_found}.

Record the worker pid (and optional originating request id) on a running task so a later tasks/cancel can stop it.

start_link()

terminate(Reason, State)