Interface IMcpEndpoint
- Namespace
- ModelContextProtocol
- Assembly
- ModelContextProtocol.dll
Represents a client or server Model Context Protocol (MCP) endpoint.
public interface IMcpEndpoint : IAsyncDisposable
- Inherited Members
- Extension Methods
Remarks
The MCP endpoint provides the core communication functionality used by both clients and servers:
- Sending JSON-RPC requests and receiving responses.
- Sending notifications to the connected endpoint.
- Registering handlers for receiving notifications.
IMcpEndpoint serves as the base interface for both IMcpClient and IMcpServer interfaces, providing the common functionality needed for MCP protocol communication. Most applications will use these more specific interfaces rather than working with IMcpEndpoint directly.
All MCP endpoints should be properly disposed after use as they implement IAsyncDisposable.
Methods
RegisterNotificationHandler(string, Func<JsonRpcNotification, CancellationToken, ValueTask>)
Registers a handler to be invoked when a notification for the specified method is received.
IAsyncDisposable RegisterNotificationHandler(string method, Func<JsonRpcNotification, CancellationToken, ValueTask> handler)
Parameters
method
stringThe notification method.
handler
Func<JsonRpcNotification, CancellationToken, ValueTask>The handler to be invoked.
Returns
- IAsyncDisposable
An IDisposable that will remove the registered handler when disposed.
SendMessageAsync(JsonRpcMessage, CancellationToken)
Sends a JSON-RPC message to the connected endpoint.
Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)
Parameters
message
JsonRpcMessageThe JSON-RPC message to send. This can be any type that implements JsonRpcMessage, such as JsonRpcRequest, JsonRpcResponse, JsonRpcNotification, or JsonRpcError.
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 provides low-level access to send any JSON-RPC message. For specific message types, consider using the higher-level methods such as SendRequestAsync(JsonRpcRequest, CancellationToken) or extension methods like SendNotificationAsync(IMcpEndpoint, string, CancellationToken), which provide a simpler API.
The method will serialize the message and transmit it using the underlying transport mechanism.
Exceptions
- McpException
The transport is not connected.
- ArgumentNullException
message
is null.
SendRequestAsync(JsonRpcRequest, CancellationToken)
Sends a JSON-RPC request to the connected endpoint and waits for a response.
Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, CancellationToken cancellationToken = default)
Parameters
request
JsonRpcRequestThe JSON-RPC request to send.
cancellationToken
CancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.
Returns
- Task<JsonRpcResponse>
A task containing the endpoint's response.
Remarks
This method provides low-level access to send raw JSON-RPC requests. For most use cases, consider using the strongly-typed extension methods that provide a more convenient API.
Exceptions
- McpException
The transport is not connected, or another error occurs during request processing.