using System.Text.Json; using System.Text.Json.Serialization; namespace ModelContextProtocol.Protocol; /// /// Represents the client's response to an elicitation request. /// public sealed class ElicitResult : Result { /// /// Gets or sets the user action in response to the elicitation. /// /// /// Defaults to "cancel" if not explicitly set. /// /// /// /// /// "accept" /// User submitted the form/confirmed the action /// /// /// "decline" /// User explicitly declined the action /// /// /// "cancel" /// User dismissed without making an explicit choice (default) /// /// /// [JsonPropertyName("action")] public string Action { get; set; } = "cancel"; /// /// Gets a value that indicates whether the elicitation was accepted by the user. /// /// /// If , it indicates that the elicitation request completed successfully and the value of has been populated with a value. /// [JsonIgnore] public bool IsAccepted => string.Equals(Action, "accept", StringComparison.OrdinalIgnoreCase); /// /// Gets or sets the submitted form data. /// /// /// /// This value is typically omitted if the action is "cancel" or "decline". /// /// /// Values in the dictionary should be of types , , /// , , or (for multi-select enums). /// /// [JsonPropertyName("content")] public IDictionary? Content { get; set; } /// /// Applies default values from the elicitation request schema to any missing fields in the result content. /// internal static ElicitResult WithDefaults(ElicitRequestParams? requestParams, ElicitResult result) { if (result.IsAccepted && requestParams?.RequestedSchema?.Properties is { } properties) { Dictionary? newContent = null; foreach (KeyValuePair kvp in properties) { if ((result.Content is null || !result.Content.ContainsKey(kvp.Key)) && kvp.Value.GetDefaultAsJsonElement() is { } element) { newContent ??= result.Content is not null ? new Dictionary(result.Content) : []; newContent[kvp.Key] = element; } } if (newContent is not null) { return new ElicitResult { Action = result.Action, Content = newContent, Meta = result.Meta }; } } return result; } } /// /// Represents the client's response to an elicitation request, with typed content payload. /// /// The type of the expected content payload. public sealed class ElicitResult : Result { /// /// Gets or sets the user action in response to the elicitation. /// /// /// Defaults to "cancel" if not explicitly set. /// /// /// /// /// "accept" /// User submitted the form/confirmed the action /// /// /// "decline" /// User explicitly declined the action /// /// /// "cancel" /// User dismissed without making an explicit choice (default) /// /// /// public string Action { get; set; } = "cancel"; /// /// Gets a value that indicates whether the elicitation was accepted by the user. /// /// /// If , it indicates that the elicitation request completed successfully and the value of has been populated with a value. /// public bool IsAccepted => string.Equals(Action, "accept", StringComparison.OrdinalIgnoreCase); /// /// Gets or sets the submitted form data as a typed value. /// public T? Content { get; set; } }