csharp-sdk/src/ModelContextProtocol.Core/Server/TaskExecutionContext.cs
Eirik Tsarpalis 10e4f5b20f
Implement MCP task support (SEP-1686) (#1170)
Co-authored-by: Stephen Halter <halter73@gmail.com>
2026-01-27 19:21:02 -08:00

47 lines
1.5 KiB
C#

namespace ModelContextProtocol.Server;
/// <summary>
/// Represents the execution context for a task being executed by the server.
/// This context flows with async execution and enables automatic task status updates.
/// </summary>
internal sealed class TaskExecutionContext
{
/// <summary>
/// Gets the AsyncLocal instance used to track the current task execution context.
/// </summary>
private static readonly AsyncLocal<TaskExecutionContext?> s_current = new();
/// <summary>
/// Gets or sets the current task execution context for the executing async flow.
/// </summary>
public static TaskExecutionContext? Current
{
get => s_current.Value;
set => s_current.Value = value;
}
/// <summary>
/// Gets the task ID of the currently executing task.
/// </summary>
public required string TaskId { get; init; }
/// <summary>
/// Gets the session ID associated with the task.
/// </summary>
public string? SessionId { get; init; }
/// <summary>
/// Gets the task store used to persist task state.
/// </summary>
public required IMcpTaskStore TaskStore { get; init; }
/// <summary>
/// Gets whether task status notifications should be sent.
/// </summary>
public bool SendNotifications { get; init; }
/// <summary>
/// Gets or sets the function to call when sending a task status notification.
/// </summary>
public Func<Protocol.McpTask, CancellationToken, Task>? NotifyTaskStatusFunc { get; init; }
}