using ModelContextProtocol.Protocol;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization.Metadata;
namespace ModelContextProtocol;
#pragma warning disable CS0618 // Type or member is obsolete
public abstract partial class McpSession : IMcpEndpoint, IAsyncDisposable
#pragma warning restore CS0618 // Type or member is obsolete
{
///
/// Sends a JSON-RPC request and attempts to deserialize the result to .
///
/// The type of the request parameters to serialize from.
/// The type of the result to deserialize to.
/// The JSON-RPC method name to invoke.
/// Object representing the request parameters.
/// The request id for the request.
/// The options governing request serialization.
/// The to monitor for cancellation requests. The default is .
/// A task that represents the asynchronous operation. The task result contains the deserialized result.
public ValueTask SendRequestAsync(
string method,
TParameters parameters,
JsonSerializerOptions? serializerOptions = null,
RequestId requestId = default,
CancellationToken cancellationToken = default)
where TResult : notnull
{
serializerOptions ??= McpJsonUtilities.DefaultOptions;
serializerOptions.MakeReadOnly();
JsonTypeInfo paramsTypeInfo = serializerOptions.GetTypeInfo();
JsonTypeInfo resultTypeInfo = serializerOptions.GetTypeInfo();
return SendRequestAsync(method, parameters, paramsTypeInfo, resultTypeInfo, requestId, cancellationToken);
}
///
/// Sends a JSON-RPC request and attempts to deserialize the result to .
///
/// The type of the request parameters to serialize from.
/// The type of the result to deserialize to.
/// The JSON-RPC method name to invoke.
/// Object representing the request parameters.
/// The type information for request parameter serialization.
/// The type information for request parameter deserialization.
/// The request id for the request.
/// The to monitor for cancellation requests. The default is .
/// A task that represents the asynchronous operation. The task result contains the deserialized result.
internal async ValueTask SendRequestAsync(
string method,
TParameters parameters,
JsonTypeInfo parametersTypeInfo,
JsonTypeInfo resultTypeInfo,
RequestId requestId = default,
CancellationToken cancellationToken = default)
where TResult : notnull
{
Throw.IfNullOrWhiteSpace(method);
Throw.IfNull(parametersTypeInfo);
Throw.IfNull(resultTypeInfo);
JsonRpcRequest jsonRpcRequest = new()
{
Id = requestId,
Method = method,
Params = JsonSerializer.SerializeToNode(parameters, parametersTypeInfo),
};
JsonRpcResponse response = await SendRequestAsync(jsonRpcRequest, cancellationToken).ConfigureAwait(false);
return JsonSerializer.Deserialize(response.Result, resultTypeInfo) ?? throw new JsonException("Unexpected JSON result in response.");
}
///
/// Sends a parameterless notification to the connected session.
///
/// The notification method name.
/// The to monitor for cancellation requests. The default is .
/// A task that represents the asynchronous send operation.
///
///
/// This method sends a notification without any parameters. Notifications are one-way messages
/// that don't expect a response. They are commonly used for events, status updates, or to signal
/// changes in state.
///
///
public Task SendNotificationAsync(string method, CancellationToken cancellationToken = default)
{
Throw.IfNullOrWhiteSpace(method);
return SendMessageAsync(new JsonRpcNotification { Method = method }, cancellationToken);
}
///
/// Sends a notification with parameters to the connected session.
///
/// The type of the notification parameters to serialize.
/// The JSON-RPC method name for the notification.
/// Object representing the notification parameters.
/// The options governing parameter serialization. If null, default options are used.
/// The to monitor for cancellation requests. The default is .
/// A task that represents the asynchronous send operation.
///
///
/// This method sends a notification with parameters to the connected session. Notifications are one-way
/// messages that don't expect a response, commonly used for events, status updates, or signaling changes.
///
///
/// The parameters object is serialized to JSON according to the provided serializer options or the default
/// options if none are specified.
///
///
/// The Model Context Protocol defines several standard notification methods in ,
/// but custom methods can also be used for application-specific notifications.
///
///
public Task SendNotificationAsync(
string method,
TParameters parameters,
JsonSerializerOptions? serializerOptions = null,
CancellationToken cancellationToken = default)
{
serializerOptions ??= McpJsonUtilities.DefaultOptions;
serializerOptions.MakeReadOnly();
JsonTypeInfo parametersTypeInfo = serializerOptions.GetTypeInfo();
return SendNotificationAsync(method, parameters, parametersTypeInfo, cancellationToken);
}
///
/// Sends a notification to the server with parameters.
///
/// The JSON-RPC method name to invoke.
/// Object representing the request parameters.
/// The type information for request parameter serialization.
/// The to monitor for cancellation requests. The default is .
internal Task SendNotificationAsync(
string method,
TParameters parameters,
JsonTypeInfo parametersTypeInfo,
CancellationToken cancellationToken = default)
{
Throw.IfNullOrWhiteSpace(method);
Throw.IfNull(parametersTypeInfo);
JsonNode? parametersJson = JsonSerializer.SerializeToNode(parameters, parametersTypeInfo);
return SendMessageAsync(new JsonRpcNotification { Method = method, Params = parametersJson }, cancellationToken);
}
///
/// Notifies the connected session of progress for a long-running operation.
///
/// The identifying the operation for which progress is being reported.
/// The progress update to send, containing information such as percentage complete or status message.
/// The to monitor for cancellation requests. The default is .
/// A task representing the completion of the notification operation (not the operation being tracked).
/// The current session instance is .
///
///
/// This method sends a progress notification to the connected session using the Model Context Protocol's
/// standardized progress notification format. Progress updates are identified by a
/// that allows the recipient to correlate multiple updates with a specific long-running operation.
///
///
/// Progress notifications are sent asynchronously and don't block the operation from continuing.
///
///
public Task NotifyProgressAsync(
ProgressToken progressToken,
ProgressNotificationValue progress,
CancellationToken cancellationToken = default)
{
return SendNotificationAsync(
NotificationMethods.ProgressNotification,
new ProgressNotificationParams
{
ProgressToken = progressToken,
Progress = progress,
},
McpJsonUtilities.JsonContext.Default.ProgressNotificationParams,
cancellationToken);
}
}