using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; namespace ModelContextProtocol.Protocol; /// /// Represents an MCP task, which is a durable state machine carrying information /// about the underlying execution state of a request. /// /// /// /// Tasks are useful for representing expensive computations and batch processing requests. /// Each task is uniquely identifiable by a receiver-generated task ID. /// /// /// Tasks follow a defined lifecycle through the property. They begin /// in the status and may transition through various states /// before reaching a terminal status (, , /// or ). /// /// /// See the tasks specification for details. /// /// [DebuggerDisplay("{DebuggerDisplay,nq}")] [Experimental(Experimentals.Tasks_DiagnosticId, UrlFormat = Experimentals.Tasks_Url)] public sealed class McpTask { /// /// Gets or sets the unique identifier for the task. /// /// /// Task IDs are generated by the receiver when creating a task and must be unique /// among all tasks controlled by that receiver. /// [JsonPropertyName("taskId")] public required string TaskId { get; set; } /// /// Gets or sets the current state of the task execution. /// [JsonPropertyName("status")] public required McpTaskStatus Status { get; set; } /// /// Gets or sets an optional human-readable message describing the current state. /// /// /// This message can be present for any status, including error details for failed tasks. /// [JsonPropertyName("statusMessage")] public string? StatusMessage { get; set; } /// /// Gets or sets the ISO 8601 timestamp when the task was created. /// /// /// Receivers must include this timestamp in all task responses to indicate when /// the task was created. /// [JsonPropertyName("createdAt")] public required DateTimeOffset CreatedAt { get; set; } /// /// Gets or sets the ISO 8601 timestamp when the task status was last updated. /// /// /// Receivers must include this timestamp in all task responses to indicate when /// the task was last updated. /// [JsonPropertyName("lastUpdatedAt")] public required DateTimeOffset LastUpdatedAt { get; set; } /// /// Gets or sets the time to live (retention duration) from creation before the task may be deleted. /// /// /// /// A null value indicates unlimited lifetime. After a task's TTL lifetime has elapsed, /// receivers may delete the task and its results, regardless of the task status. /// /// /// Receivers may override the requested TTL duration and must include the actual TTL /// duration (or null for unlimited) in task responses. /// /// [JsonPropertyName("ttl")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] public TimeSpan? TimeToLive { get; set; } /// /// Gets or sets the suggested time between status checks. /// /// /// Requestors should respect this value when provided to avoid excessive polling. /// This value is optional and may not be present in all task responses. /// [JsonPropertyName("pollInterval")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] public TimeSpan? PollInterval { get; set; } private string DebuggerDisplay => $"Task {TaskId}: {Status}" + (StatusMessage != null ? $" - {StatusMessage}" : ""); }