|
| 1 | +using System.Text.Json.Serialization.Metadata; |
| 2 | + |
| 3 | +namespace ModelContextProtocol.Protocol; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Represents the result of a request that may return either the standard result or an alternate |
| 7 | +/// <see cref="Result"/> subtype for scenarios like asynchronous task execution. |
| 8 | +/// </summary> |
| 9 | +/// <typeparam name="TResult">The standard result type for the request (e.g., <see cref="CallToolResult"/>).</typeparam> |
| 10 | +/// <remarks> |
| 11 | +/// <para> |
| 12 | +/// Extensions that augment request handling (such as the Tasks extension) use this type to indicate |
| 13 | +/// that the server returned an alternate result instead of the normal one. The alternate carries its |
| 14 | +/// own <see cref="JsonTypeInfo"/> so the transport layer can serialize it without compile-time knowledge |
| 15 | +/// of the concrete type. |
| 16 | +/// </para> |
| 17 | +/// <para> |
| 18 | +/// Use <see cref="IsAlternate"/> to determine which variant was returned, then access either |
| 19 | +/// <see cref="Result"/> for the immediate result or <see cref="Alternate"/> for the alternate. |
| 20 | +/// </para> |
| 21 | +/// </remarks> |
| 22 | +public class ResultOrAlternate<TResult> where TResult : Result |
| 23 | +{ |
| 24 | + private readonly TResult? _result; |
| 25 | + private readonly Result? _alternate; |
| 26 | + private readonly JsonTypeInfo? _alternateTypeInfo; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of <see cref="ResultOrAlternate{TResult}"/> with an immediate result. |
| 30 | + /// </summary> |
| 31 | + /// <param name="result">The standard result returned by the server.</param> |
| 32 | + public ResultOrAlternate(TResult result) |
| 33 | + { |
| 34 | + Throw.IfNull(result); |
| 35 | + _result = result; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of <see cref="ResultOrAlternate{TResult}"/> with an alternate result. |
| 40 | + /// </summary> |
| 41 | + /// <param name="alternate">The alternate result.</param> |
| 42 | + /// <param name="alternateTypeInfo">The <see cref="JsonTypeInfo"/> used to serialize the alternate result.</param> |
| 43 | + public ResultOrAlternate(Result alternate, JsonTypeInfo alternateTypeInfo) |
| 44 | + { |
| 45 | + Throw.IfNull(alternate); |
| 46 | + Throw.IfNull(alternateTypeInfo); |
| 47 | + _alternate = alternate; |
| 48 | + _alternateTypeInfo = alternateTypeInfo; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets a value indicating whether the server returned an alternate result instead of the standard result. |
| 53 | + /// </summary> |
| 54 | + public bool IsAlternate => _alternate is not null; |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets the immediate result, or <see langword="null"/> if the server returned an alternate. |
| 58 | + /// </summary> |
| 59 | + public TResult? Result => _result; |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets the alternate result, or <see langword="null"/> if the server returned the standard result. |
| 63 | + /// </summary> |
| 64 | + public Result? Alternate => _alternate; |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Gets the <see cref="JsonTypeInfo"/> for serializing the alternate result, or <see langword="null"/> |
| 68 | + /// if the server returned the standard result. |
| 69 | + /// </summary> |
| 70 | + public JsonTypeInfo? AlternateTypeInfo => _alternateTypeInfo; |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Implicitly converts a <typeparamref name="TResult"/> to a <see cref="ResultOrAlternate{TResult}"/> |
| 74 | + /// wrapping the immediate result. |
| 75 | + /// </summary> |
| 76 | + /// <param name="result">The result to wrap.</param> |
| 77 | + public static implicit operator ResultOrAlternate<TResult>(TResult result) => new(result); |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Implicitly converts a <see cref="CreateTaskResult"/> to a <see cref="ResultOrAlternate{TResult}"/> |
| 81 | + /// wrapping the task handle as an alternate result using the default serialization context. |
| 82 | + /// </summary> |
| 83 | + /// <param name="taskCreated">The task creation result to wrap.</param> |
| 84 | + public static implicit operator ResultOrAlternate<TResult>(CreateTaskResult taskCreated) => |
| 85 | + new(taskCreated, McpJsonUtilities.JsonContext.Default.CreateTaskResult); |
| 86 | +} |
0 commit comments