Method ShutdownAsync
ShutdownAsync(CancellationToken)
Asynchronously shuts down the MCP client, clearing all cached state and preparing for potential reuse.
public Task ShutdownAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to cancel the shutdown operation.
Returns
- Task
A task that represents the asynchronous shutdown operation.
Examples
// Long-running application with periodic resets
var client = new McpClient("https://api.example.com/mcp");
while (running)
{
await ProcessBatch(client);
// Reset every hour to ensure fresh connections
await client.ShutdownAsync();
await Task.Delay(TimeSpan.FromHours(1));
}
Remarks
Shutdown performs the following cleanup operations:
- Cancels all pending HTTP requests
- Closes the SSE connection (if active)
- Clears cached tools, resources, prompts, and session state
- Recreates the internal HTTP client (if owned) with fresh connection pools
After shutdown, the client can be reused by calling any operation method, which will trigger automatic re-initialization. This is useful when you need to:
- Reset the connection after a prolonged period of inactivity
- Force a fresh handshake with the server
- Clear all cached data and start over
It's safe to call this method multiple times. If you're done with the client permanently, use Dispose() instead.
Exceptions
- OperationCanceledException
Thrown if the operation is canceled via
cancellationToken.