Class McpSession | MCP C# SDK

Namespace
ModelContextProtocol
Assembly
ModelContextProtocol.Core.dll

Represents a client or server Model Context Protocol (MCP) session.

public abstract class McpSession : IAsyncDisposable
Inheritance

McpSession

Implements
Derived
Inherited Members

Properties

NegotiatedProtocolVersion

Gets the negotiated protocol version for the current MCP session.

public abstract string? NegotiatedProtocolVersion { get; }

Property Value

string

SessionId

Gets an identifier associated with the current MCP session.

public abstract string? SessionId { get; }

Property Value

string

Methods

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public abstract ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

NotifyProgressAsync(ProgressNotificationParams, CancellationToken)

Notifies the connected session of progress for a long-running operation.

public Task NotifyProgressAsync(ProgressNotificationParams requestParams, CancellationToken cancellationToken = default)

Parameters

requestParams ProgressNotificationParams

The request parameters to send in the request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task representing the completion of the notification operation (not the operation being tracked).

Exceptions

ArgumentNullException

requestParams is null.

NotifyProgressAsync(ProgressToken, ProgressNotificationValue, RequestOptions?, CancellationToken)

Notifies the connected session of progress for a long-running operation.

public Task NotifyProgressAsync(ProgressToken progressToken, ProgressNotificationValue progress, RequestOptions? options = null, CancellationToken cancellationToken = default)

Parameters

progressToken ProgressToken

The token that identifies the operation for which progress is being reported.

progress ProgressNotificationValue

The progress update to send, containing information such as percentage complete or status message.

options RequestOptions

Optional request options including metadata, serialization settings, and progress tracking.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task representing the completion of the notification operation (not the operation being tracked).

Exceptions

ArgumentNullException

progress is null.

RegisterNotificationHandler(string, Func<JsonRpcNotification, CancellationToken, ValueTask>)

Registers a handler to be invoked when a notification for the specified method is received.

public abstract IAsyncDisposable RegisterNotificationHandler(string method, Func<JsonRpcNotification, CancellationToken, ValueTask> handler)

Parameters

method string

The notification method.

handler Func<JsonRpcNotification, CancellationToken, ValueTask>

The handler to be invoked.

Returns

IAsyncDisposable

An IAsyncDisposable that will remove the registered handler when disposed.

Exceptions

ArgumentNullException

method or handler is null.

ArgumentException

method is empty or composed entirely of whitespace.

SendMessageAsync(JsonRpcMessage, CancellationToken)

Sends a JSON-RPC message to the connected session.

public abstract Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)

Parameters

message JsonRpcMessage

The JSON-RPC message to send. This can be any type that implements JsonRpcMessage, such as JsonRpcRequest, JsonRpcResponse, JsonRpcNotification, or JsonRpcError.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task that represents the asynchronous send operation.

Exceptions

InvalidOperationException

The transport is not connected.

ArgumentNullException

message is null.

SendNotificationAsync(string, CancellationToken)

Sends a parameterless notification to the connected session.

public Task SendNotificationAsync(string method, CancellationToken cancellationToken = default)

Parameters

method string

The notification method name.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task that represents the asynchronous send operation.

Exceptions

ArgumentNullException

method is null.

ArgumentException

method is empty or composed entirely of whitespace.

SendNotificationAsync<TParameters>(string, TParameters, JsonSerializerOptions?, CancellationToken)

Sends a notification with parameters to the connected session.

public Task SendNotificationAsync<TParameters>(string method, TParameters parameters, JsonSerializerOptions? serializerOptions = null, CancellationToken cancellationToken = default)

Parameters

method string

The JSON-RPC method name for the notification.

parameters TParameters

The notification parameters.

serializerOptions JsonSerializerOptions

The options governing parameter serialization. If null, default options are used.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task that represents the asynchronous send operation.

Type Parameters

TParameters

The type of the notification parameters to serialize.

Exceptions

ArgumentNullException

method is null.

ArgumentException

method is empty or composed entirely of whitespace.

SendRequestAsync(JsonRpcRequest, CancellationToken)

Sends a JSON-RPC request to the connected session and waits for a response.

public abstract Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, CancellationToken cancellationToken = default)

Parameters

request JsonRpcRequest

The JSON-RPC request to send.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task<JsonRpcResponse>

A task containing the session's response.

Exceptions

InvalidOperationException

The transport is not connected, or another error occurred during request processing.

McpException

An error occurred during request processing.

SendRequestAsync<TParameters, TResult>(string, TParameters, JsonSerializerOptions?, RequestId, CancellationToken)

Sends a JSON-RPC request and attempts to deserialize the result to TResult.

public ValueTask<TResult> SendRequestAsync<TParameters, TResult>(string method, TParameters parameters, JsonSerializerOptions? serializerOptions = null, RequestId requestId = default, CancellationToken cancellationToken = default) where TResult : notnull

Parameters

method string

The JSON-RPC method name to invoke.

parameters TParameters

The request parameters.

serializerOptions JsonSerializerOptions

The options governing request serialization.

requestId RequestId

The request ID for the request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TResult>

A task that represents the asynchronous operation. The task result contains the deserialized result.

Type Parameters

TParameters

The type of the request parameters to serialize from.

TResult

The type of the result to deserialize to.

Exceptions

ArgumentNullException

method is null.

ArgumentException

method is empty or composed entirely of whitespace.

McpException

The request failed or the server returned an error response.