Refactor transports to help enable graceful shutdown (#142)

- This also paves the way for better multi-session support
- We should definitely rethink names for the transport API
- For now, I kept the names similar as possible, so we can
  focus on the API shape

Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
Stephen Halter 2025-03-30 17:10:14 -07:00 • committed by GitHub
commit 2ab9db0878
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,11 +8,6 @@ namespace ModelContextProtocol.Server;
/// </summary>
public interface IMcpServer : IAsyncDisposable
{
/// <summary>
/// Gets a value indicating whether the server has been initialized.
/// </summary>
bool IsInitialized { get; }
/// <summary>
/// Gets the capabilities supported by the client.
/// </summary>
@ -48,23 +43,23 @@ public interface IMcpServer : IAsyncDisposable
void AddNotificationHandler(string method, Func<JsonRpcNotification, Task> handler);
/// <summary>
/// Starts the server and begins listening for client requests.
/// Runs the server, listening for and handling client requests.
/// </summary>
Task StartAsync(CancellationToken cancellationToken = default);
Task RunAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Sends a generic JSON-RPC request to the client.
/// NB! This is a temporary method that is available to send not yet implemented feature messages.
/// NB! This is a temporary method that is available to send not yet implemented feature messages.
/// Once all MCP features are implemented this will be made private, as it is purely a convenience for those who wish to implement features ahead of the library.
/// </summary>
/// <typeparam name="T">The expected response type.</typeparam>
/// <typeparam name="TResult">The expected response type.</typeparam>
/// <param name="request">The JSON-RPC request to send.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task containing the client's response.</returns>
Task<T> SendRequestAsync<T>(JsonRpcRequest request, CancellationToken cancellationToken) where T : class;
Task<TResult> SendRequestAsync<TResult>(JsonRpcRequest request, CancellationToken cancellationToken = default) where TResult : class;
/// <summary>
/// Sends a message to the server.
/// Sends a message to the client.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>