Method Initialize
Initialize(CancellationToken)
Synchronously ensures the MCP session is initialized and returns a clone of the
JSON-RPC initialize
response payload.
public JsonDocument Initialize(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenToken to cancel the operation.
Returns
- JsonDocument
A clone of the server’s
initialize
response.
Remarks
This is a blocking convenience wrapper over InitializeAsync(CancellationToken).
If the client is already initialized, a cached clone is returned. Otherwise, the method
performs the handshake (initialize
followed by notifications/initialized
),
captures the session id, and caches the response. Concurrent callers share the same
underlying initialization task.
Prefer InitializeAsync(CancellationToken) in async code paths to avoid blocking.
Exceptions
- ObjectDisposedException
Thrown if the client has been disposed.
- HttpRequestException
Thrown on HTTP failures, JSON-RPC errors, malformed responses, or if the server returns an error.
- OperationCanceledException
Thrown if the operation is canceled.
- See Also