using System.Text.Json.Nodes; namespace ModelContextProtocol.Server; /// /// Represents the lifecycle state for an MRTR handler invocation across retries. /// Created when the handler starts and stored in _mrtrContinuations when /// the handler suspends waiting for client input. /// internal sealed class MrtrContinuation { private readonly CancellationTokenSource _handlerCts; public MrtrContinuation(CancellationTokenSource handlerCts, Task handlerTask, MrtrContext mrtrContext) { _handlerCts = handlerCts; HandlerTask = handlerTask; MrtrContext = mrtrContext; } /// /// Gets a token that cancels when the handler should be aborted. /// Passed to the handler at creation and remains valid across retries. /// public CancellationToken HandlerToken => _handlerCts.Token; /// /// The handler task that is suspended awaiting input. /// public Task HandlerTask { get; } /// /// The MRTR context for the handler's async flow. /// public MrtrContext MrtrContext { get; } /// /// The exchange that is awaiting a response from the client. /// Set each time the handler suspends on a new exchange. /// public MrtrExchange? PendingExchange { get; set; } /// /// Cancels the handler. Safe to call multiple times and concurrently - /// is thread-safe with itself. /// The CTS is intentionally never disposed to avoid deadlock risks from /// calling Cancel/Dispose inside synchronization primitives. /// public void CancelHandler() => _handlerCts.Cancel(); }