Multi Round-Trip Requests (MRTR) (#1458)
This commit is contained in:
parent
8202bcc932
commit
f6dbe4372f
1 changed files with 5832 additions and 103 deletions
50
src/ModelContextProtocol.Core/Server/MrtrContinuation.cs
Normal file
50
src/ModelContextProtocol.Core/Server/MrtrContinuation.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace ModelContextProtocol.Server;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the lifecycle state for an MRTR handler invocation across retries.
|
||||
/// Created when the handler starts and stored in <c>_mrtrContinuations</c> when
|
||||
/// the handler suspends waiting for client input.
|
||||
/// </summary>
|
||||
internal sealed class MrtrContinuation
|
||||
{
|
||||
private readonly CancellationTokenSource _handlerCts;
|
||||
|
||||
public MrtrContinuation(CancellationTokenSource handlerCts, Task<JsonNode?> handlerTask, MrtrContext mrtrContext)
|
||||
{
|
||||
_handlerCts = handlerCts;
|
||||
HandlerTask = handlerTask;
|
||||
MrtrContext = mrtrContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a token that cancels when the handler should be aborted.
|
||||
/// Passed to the handler at creation and remains valid across retries.
|
||||
/// </summary>
|
||||
public CancellationToken HandlerToken => _handlerCts.Token;
|
||||
|
||||
/// <summary>
|
||||
/// The handler task that is suspended awaiting input.
|
||||
/// </summary>
|
||||
public Task<JsonNode?> HandlerTask { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The MRTR context for the handler's async flow.
|
||||
/// </summary>
|
||||
public MrtrContext MrtrContext { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The exchange that is awaiting a response from the client.
|
||||
/// Set each time the handler suspends on a new exchange.
|
||||
/// </summary>
|
||||
public MrtrExchange? PendingExchange { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cancels the handler. Safe to call multiple times and concurrently -
|
||||
/// <see cref="CancellationTokenSource.Cancel()"/> is thread-safe with itself.
|
||||
/// The CTS is intentionally never disposed to avoid deadlock risks from
|
||||
/// calling Cancel/Dispose inside synchronization primitives.
|
||||
/// </summary>
|
||||
public void CancelHandler() => _handlerCts.Cancel();
|
||||
}
|
||||
Loading…
Reference in a new issue