Method Initialize
Initialize(CancellationToken)
Synchronously initializes the MCP session and returns the server's initialization response.
public JsonDocument Initialize(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to cancel the initialization operation.
Returns
- JsonDocument
A JsonDocument containing a copy of the server's JSON-RPC
initializeresponse payload. Each call returns a fresh clone, so you can safely dispose of it without affecting the cached version.
Remarks
This method blocks the calling thread. In asynchronous code paths, prefer using InitializeAsync(CancellationToken) to avoid blocking.
If the client is already initialized, this method returns immediately with a cached copy of the initialization response. Otherwise, it performs the complete MCP handshake:
- Sends an
initializerequest to the server - Receives and processes the server's response (including capabilities and protocol version)
- Sends a
notifications/initializedmessage to confirm readiness - Starts listening for server notifications via SSE (if supported)
You typically don't need to call this method explicitly, as the client initializes automatically on the first operation (e.g., listing tools or calling a tool). However, you may want to call it explicitly to:
- Validate connectivity before performing operations
- Pre-warm the connection
- Access server metadata from the initialization response
Multiple concurrent callers share the same initialization task, ensuring the handshake only happens once even under high concurrency.
Exceptions
- ObjectDisposedException
Thrown if the client has been disposed.
- HttpRequestException
Thrown on HTTP failures, JSON-RPC errors, or malformed responses from the server.
- OperationCanceledException
Thrown if the operation is canceled via
cancellationToken.
- See Also