using ModelContextProtocol.Protocol; using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization.Metadata; namespace ModelContextProtocol; public abstract partial class McpSession : IAsyncDisposable { /// /// 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. /// 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. /// is . /// is empty or composed entirely of whitespace. /// The request failed or the server returned an error response. public ValueTask SendRequestAsync( string method, TParameters parameters, JsonSerializerOptions? serializerOptions = null, RequestId requestId = default, CancellationToken cancellationToken = default) where TResult : notnull { serializerOptions ??= McpJsonUtilities.DefaultOptions; serializerOptions.MakeReadOnly(); return SendRequestAsync( method, parameters, serializerOptions.GetTypeInfo(), serializerOptions.GetTypeInfo(), 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. /// The request parameters. /// The type information for request parameter serialization. /// The type information for result 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. /// is . /// is empty or composed entirely of whitespace. /// /// /// 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. /// 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. /// is . /// is empty or composed entirely of whitespace. /// /// /// 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 connected session with parameters. /// /// The JSON-RPC method name to invoke. /// 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 token that identifies the operation for which progress is being reported. /// The progress update to send, containing information such as percentage complete or status message. /// Optional request options including metadata, serialization settings, and progress tracking. /// The to monitor for cancellation requests. The default is . /// A task representing the completion of the notification operation (not the operation being tracked). /// 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, RequestOptions? options = null, CancellationToken cancellationToken = default) { Throw.IfNull(progress); return NotifyProgressAsync( new ProgressNotificationParams { ProgressToken = progressToken, Progress = progress, Meta = options?.GetMetaForRequest(), }, cancellationToken); } /// /// Notifies the connected session of progress for a long-running operation. /// /// The request parameters to send in the request. /// The to monitor for cancellation requests. The default is . /// A task representing the completion of the notification operation (not the operation being tracked). /// 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( ProgressNotificationParams requestParams, CancellationToken cancellationToken = default) { Throw.IfNull(requestParams); return SendNotificationAsync( NotificationMethods.ProgressNotification, requestParams, McpJsonUtilities.JsonContext.Default.ProgressNotificationParams, cancellationToken); } }