- Namespace
- ModelContextProtocol
- Assembly
- ModelContextProtocol.Core.dll
Provides an in-memory implementation of IMcpTaskStore for development and testing.
[Experimental("MCPEXP001", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcpexp001")]
public sealed class InMemoryMcpTaskStore : IMcpTaskStore, IDisposable
- Inheritance
-
InMemoryMcpTaskStore
- Implements
- Inherited Members
Constructors
InMemoryMcpTaskStore(TimeSpan?, TimeSpan?, TimeSpan?, TimeSpan?, int, int?, int?)
public InMemoryMcpTaskStore(TimeSpan? defaultTtl = null, TimeSpan? maxTtl = null, TimeSpan? pollInterval = null, TimeSpan? cleanupInterval = null, int pageSize = 100, int? maxTasks = null, int? maxTasksPerSession = null)
Parameters
defaultTtlTimeSpan?Default TTL to use when task creation does not specify a TTL. Null means unlimited.
maxTtlTimeSpan?Maximum TTL allowed. If a task requests a longer TTL, it will be capped to this value. Null means no maximum limit.
pollIntervalTimeSpan?Advertised polling interval for tasks. Default is 1 second. This value is used when creating new tasks to indicate how frequently clients should poll for updates.
cleanupIntervalTimeSpan?Interval for running background cleanup of expired tasks. Default is 1 minute. Pass InfiniteTimeSpan to disable automatic cleanup.
pageSizeintMaximum number of tasks to return per page in ListTasksAsync(string?, string?, CancellationToken). Default is 100.
maxTasksint?Maximum number of tasks allowed in the store globally. Null means unlimited. When the limit is reached, CreateTaskAsync(McpTaskMetadata, RequestId, JsonRpcRequest, string?, CancellationToken) will throw InvalidOperationException.
maxTasksPerSessionint?Maximum number of tasks allowed per session. Null means unlimited. When the limit is reached for a session, CreateTaskAsync(McpTaskMetadata, RequestId, JsonRpcRequest, string?, CancellationToken) will throw InvalidOperationException.
Methods
CancelTaskAsync(string, string?, CancellationToken)
Attempts to cancel a task, transitioning it to Cancelled status.
public Task<McpTask> CancelTaskAsync(string taskId, string? sessionId = null, CancellationToken cancellationToken = default)
Parameters
taskIdstringThe unique identifier of the task to cancel.
sessionIdstringOptional session identifier for access control.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- Task<McpTask>
The updated McpTask. If the task is already in a terminal state (Completed, Failed, or Cancelled), the task is returned unchanged.
CreateTaskAsync(McpTaskMetadata, RequestId, JsonRpcRequest, string?, CancellationToken)
Creates a new task for tracking an asynchronous operation.
public Task<McpTask> CreateTaskAsync(McpTaskMetadata taskParams, RequestId requestId, JsonRpcRequest request, string? sessionId = null, CancellationToken cancellationToken = default)
Parameters
taskParamsMcpTaskMetadataMetadata for the task, including requested TTL.
requestIdRequestIdThe JSON-RPC request ID that initiated this task.
requestJsonRpcRequestThe original JSON-RPC request that triggered task creation.
sessionIdstringOptional session identifier for multi-session isolation.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- Task<McpTask>
A new McpTask with a unique task ID, initial status of Working, and the actual TTL that will be used (which may differ from the requested TTL).
Dispose()
Disposes the task store and stops background cleanup.
GetTaskAsync(string, string?, CancellationToken)
Retrieves a task by its unique identifier.
public Task<McpTask?> GetTaskAsync(string taskId, string? sessionId = null, CancellationToken cancellationToken = default)
Parameters
taskIdstringThe unique identifier of the task to retrieve.
sessionIdstringOptional session identifier for access control.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
GetTaskResultAsync(string, string?, CancellationToken)
Retrieves the stored result of a completed or failed task.
public Task<JsonElement> GetTaskResultAsync(string taskId, string? sessionId = null, CancellationToken cancellationToken = default)
Parameters
taskIdstringThe unique identifier of the task.
sessionIdstringOptional session identifier for access control.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- Task<JsonElement>
The stored operation result as a JSON element.
ListTasksAsync(string?, string?, CancellationToken)
Lists tasks with pagination support.
public Task<ListTasksResult> ListTasksAsync(string? cursor = null, string? sessionId = null, CancellationToken cancellationToken = default)
Parameters
cursorstringOptional cursor for pagination, from a previous call's nextCursor value.
sessionIdstringOptional session identifier for filtering tasks by session.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- Task<ListTasksResult>
A ListTasksResult containing the tasks and an optional cursor for the next page.
StoreTaskResultAsync(string, McpTaskStatus, JsonElement, string?, CancellationToken)
Stores the final result of a task that has reached a terminal status.
public Task<McpTask> StoreTaskResultAsync(string taskId, McpTaskStatus status, JsonElement result, string? sessionId = null, CancellationToken cancellationToken = default)
Parameters
taskIdstringThe unique identifier of the task.
statusMcpTaskStatusresultJsonElementThe operation result to store as a JSON element.
sessionIdstringOptional session identifier for access control.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
UpdateTaskStatusAsync(string, McpTaskStatus, string?, string?, CancellationToken)
Updates the status and optional status message of a task.
public Task<McpTask> UpdateTaskStatusAsync(string taskId, McpTaskStatus status, string? statusMessage, string? sessionId = null, CancellationToken cancellationToken = default)
Parameters
taskIdstringThe unique identifier of the task.
statusMcpTaskStatusThe new status to set.
statusMessagestringOptional diagnostic message describing the status change.
sessionIdstringOptional session identifier for access control.
cancellationTokenCancellationTokenCancellation token for the operation.