Table of Contents

Method Initialize

Namespace
LMKit.Mcp.Client
Assembly
LM-Kit.NET.dll

Initialize(CancellationToken)

Synchronously initializes the MCP session and returns the server's initialization response.

public JsonDocument Initialize(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A cancellation token to cancel the initialization operation.

Returns

JsonDocument

A JsonDocument containing a copy of the server's JSON-RPC initialize response 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:

  1. Sends an initialize request to the server
  2. Receives and processes the server's response (including capabilities and protocol version)
  3. Sends a notifications/initialized message to confirm readiness
  4. 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