using ModelContextProtocol.Protocol;
namespace ModelContextProtocol.Extensions.Tasks;
///
/// Represents the result of a request that supports task-augmented execution, which may be either
/// the standard result or a indicating asynchronous processing.
///
/// The standard result type for the request (e.g., ).
///
///
/// When a server supports the io.modelcontextprotocol/tasks extension and the client declares
/// the extension capability on its request, the server may return a
/// instead of the standard result. This type represents that polymorphic response.
///
///
/// Use to determine which variant was returned, then access either
/// for the immediate result or for the task handle.
///
///
/// See the SEP-2663
/// specification for details.
///
///
public class ResultOrCreatedTask where TResult : Result
{
private readonly TResult? _result;
private readonly CreateTaskResult? _taskCreated;
///
/// Initializes a new instance of with an immediate result.
///
/// The standard result returned by the server.
public ResultOrCreatedTask(TResult result)
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
_result = result;
}
///
/// Initializes a new instance of with a task handle.
///
/// The task creation result returned by the server.
public ResultOrCreatedTask(CreateTaskResult taskCreated)
{
if (taskCreated is null)
{
throw new ArgumentNullException(nameof(taskCreated));
}
_taskCreated = taskCreated;
}
///
/// Gets a value indicating whether the server created a task instead of returning an immediate result.
///
public bool IsTask => _taskCreated is not null;
///
/// Gets the immediate result, or if the server created a task.
///
public TResult? Result => _result;
///
/// Gets the task creation result, or if the server returned an immediate result.
///
public CreateTaskResult? TaskCreated => _taskCreated;
///
/// Implicitly converts a to a
/// wrapping the immediate result.
///
/// The result to wrap.
public static implicit operator ResultOrCreatedTask(TResult result) => new(result);
///
/// Implicitly converts a to a
/// wrapping the task handle.
///
/// The task creation result to wrap.
public static implicit operator ResultOrCreatedTask(CreateTaskResult taskCreated) => new(taskCreated);
}