using ModelContextProtocol.Protocol; using System.Diagnostics.CodeAnalysis; using System.Text.Json; namespace ModelContextProtocol; /// /// Provides an interface for pluggable task storage implementations in MCP servers. /// /// /// /// The task store is responsible for managing the lifecycle of tasks, including creation, /// status updates, result storage, and retrieval. Implementations must be thread-safe and /// may support session-based isolation for multi-session scenarios. /// /// /// TTL (Time To Live) Management: Implementations may override the requested TTL value in /// to enforce resource limits. The actual TTL /// used is returned in the property. A null TTL indicates /// unlimited lifetime. Tasks may be deleted after their TTL expires, regardless of status. /// /// [Experimental(Experimentals.Tasks_DiagnosticId, UrlFormat = Experimentals.Tasks_Url)] public interface IMcpTaskStore { /// /// Creates a new task for tracking an asynchronous operation. /// /// Metadata for the task, including requested TTL. /// The JSON-RPC request ID that initiated this task. /// The original JSON-RPC request that triggered task creation. /// Optional session identifier for multi-session isolation. /// Cancellation token for the operation. /// /// A new with a unique task ID, initial status of , /// and the actual TTL that will be used (which may differ from the requested TTL). /// /// /// Implementations must generate a unique task ID and set the /// and timestamps. The implementation may override the /// requested TTL to enforce storage limits. /// Task CreateTaskAsync( McpTaskMetadata taskParams, RequestId requestId, JsonRpcRequest request, string? sessionId = null, CancellationToken cancellationToken = default); /// /// Retrieves a task by its unique identifier. /// /// The unique identifier of the task to retrieve. /// Optional session identifier for access control. /// Cancellation token for the operation. /// /// The if found and accessible, otherwise . /// /// /// Returns null if the task does not exist or if session-based access control denies access. /// Task GetTaskAsync(string taskId, string? sessionId = null, CancellationToken cancellationToken = default); /// /// Stores the final result of a task that has reached a terminal status. /// /// The unique identifier of the task. /// The terminal status: or . /// The operation result to store as a JSON element. /// Optional session identifier for access control. /// Cancellation token for the operation. /// A task representing the asynchronous operation. /// /// /// The must be either or /// . This method updates the task status and stores /// the result for later retrieval via . /// /// /// Implementations should throw if called on a task /// that is already in a terminal state, to prevent result overwrites. /// /// /// The updated with the new status and result stored. Task StoreTaskResultAsync( string taskId, McpTaskStatus status, JsonElement result, string? sessionId = null, CancellationToken cancellationToken = default); /// /// Retrieves the stored result of a completed or failed task. /// /// The unique identifier of the task. /// Optional session identifier for access control. /// Cancellation token for the operation. /// The stored operation result as a JSON element. /// /// This method should only be called on tasks in terminal states ( /// or ). The result contains the JSON representation of the /// original operation result (e.g., for tools/call). /// Task GetTaskResultAsync(string taskId, string? sessionId = null, CancellationToken cancellationToken = default); /// /// Updates the status and optional status message of a task. /// /// The unique identifier of the task. /// The new status to set. /// Optional diagnostic message describing the status change. /// Optional session identifier for access control. /// Cancellation token for the operation. /// A task representing the asynchronous operation. /// /// This method updates the task's , , /// and properties. Common uses include transitioning to /// , , or updating /// progress messages while in status. /// /// The updated with the new status applied. Task UpdateTaskStatusAsync( string taskId, McpTaskStatus status, string? statusMessage, string? sessionId = null, CancellationToken cancellationToken = default); /// /// Lists tasks with pagination support. /// /// Optional cursor for pagination, from a previous call's nextCursor value. /// Optional session identifier for filtering tasks by session. /// Cancellation token for the operation. /// A containing the tasks and an optional cursor for the next page. /// /// When is provided, implementations should filter to only return /// tasks associated with that session. The cursor format is implementation-specific. /// Task ListTasksAsync( string? cursor = null, string? sessionId = null, CancellationToken cancellationToken = default); /// /// Attempts to cancel a task, transitioning it to status. /// /// The unique identifier of the task to cancel. /// Optional session identifier for access control. /// Cancellation token for the operation. /// /// The updated . If the task is already in a terminal state /// (, , or /// ), the task is returned unchanged. /// /// /// /// This method must be idempotent. If called on a task that is already in a terminal state, /// it returns the current task without error. This behavior differs from the MCP specification /// but ensures idempotency and avoids race conditions between cancellation and task completion. /// /// /// For tasks not in a terminal state, the implementation should attempt to stop the underlying /// operation and transition the task to status before returning. /// /// Task CancelTaskAsync(string taskId, string? sessionId = null, CancellationToken cancellationToken = default); }