Class McpEndpointExtensions
- Namespace
- ModelContextProtocol
- Assembly
- ModelContextProtocol.dll
Provides extension methods for interacting with an IMcpEndpoint.
public static class McpEndpointExtensions
- Inheritance
-
McpEndpointExtensions
- Inherited Members
Remarks
This class provides strongly-typed methods for working with the Model Context Protocol (MCP) endpoints, simplifying JSON-RPC communication by handling serialization and deserialization of parameters and results.
These extension methods are designed to be used with both client (IMcpClient) and server (IMcpServer) implementations of the IMcpEndpoint interface.
Methods
NotifyProgressAsync(IMcpEndpoint, ProgressToken, ProgressNotificationValue, CancellationToken)
Notifies the connected endpoint of progress for a long-running operation.
public static Task NotifyProgressAsync(this IMcpEndpoint endpoint, ProgressToken progressToken, ProgressNotificationValue progress, CancellationToken cancellationToken = default)
Parameters
endpoint
IMcpEndpointThe endpoint issuing the notification.
progressToken
ProgressTokenThe ProgressToken identifying the operation for which progress is being reported.
progress
ProgressNotificationValueThe progress update to send, containing information such as percentage complete or status message.
cancellationToken
CancellationTokenThe 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).
Remarks
This method sends a progress notification to the connected endpoint using the Model Context Protocol's standardized progress notification format. Progress updates are identified by a ProgressToken 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.
Exceptions
- ArgumentNullException
endpoint
is null.
SendNotificationAsync(IMcpEndpoint, string, CancellationToken)
Sends a parameterless notification to the connected endpoint.
public static Task SendNotificationAsync(this IMcpEndpoint client, string method, CancellationToken cancellationToken = default)
Parameters
client
IMcpEndpointThe MCP client or server instance.
method
stringThe notification method name.
cancellationToken
CancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.
Returns
- Task
A task that represents the asynchronous send operation.
Remarks
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.
SendNotificationAsync<TParameters>(IMcpEndpoint, string, TParameters, JsonSerializerOptions?, CancellationToken)
Sends a notification with parameters to the connected endpoint.
public static Task SendNotificationAsync<TParameters>(this IMcpEndpoint endpoint, string method, TParameters parameters, JsonSerializerOptions? serializerOptions = null, CancellationToken cancellationToken = default)
Parameters
endpoint
IMcpEndpointThe MCP client or server instance.
method
stringThe JSON-RPC method name for the notification.
parameters
TParametersObject representing the notification parameters.
serializerOptions
JsonSerializerOptionsThe options governing parameter serialization. If null, default options are used.
cancellationToken
CancellationTokenThe 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.
Remarks
This method sends a notification with parameters to the connected endpoint. 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 NotificationMethods, but custom methods can also be used for application-specific notifications.
SendRequestAsync<TParameters, TResult>(IMcpEndpoint, string, TParameters, JsonSerializerOptions?, RequestId?, CancellationToken)
Sends a JSON-RPC request and attempts to deserialize the result to TResult
.
public static Task<TResult> SendRequestAsync<TParameters, TResult>(this IMcpEndpoint endpoint, string method, TParameters parameters, JsonSerializerOptions? serializerOptions = null, RequestId? requestId = null, CancellationToken cancellationToken = default) where TResult : notnull
Parameters
endpoint
IMcpEndpointThe MCP client or server instance.
method
stringThe JSON-RPC method name to invoke.
parameters
TParametersObject representing the request parameters.
serializerOptions
JsonSerializerOptionsThe options governing request serialization.
requestId
RequestId?The request id for the request.
cancellationToken
CancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.
Returns
- Task<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.