Class McpClientSession

java.lang.Object
io.modelcontextprotocol.spec.McpClientSession
All Implemented Interfaces:
McpSession

public class McpClientSession extends Object implements McpSession
Default implementation of the MCP (Model Context Protocol) session that manages bidirectional JSON-RPC communication between clients and servers. This implementation follows the MCP specification for message exchange and transport handling.

The session manages:

  • Request/response handling with unique message IDs
  • Notification processing
  • Message timeout management
  • Transport layer abstraction
Author:
Christian Tzolov, Dariusz Jędrzejczyk
  • Constructor Details

    • McpClientSession

      public McpClientSession(Duration requestTimeout, McpClientTransport transport, Map<String,McpClientSession.RequestHandler<?>> requestHandlers, Map<String,McpClientSession.NotificationHandler> notificationHandlers)
      Creates a new McpClientSession with the specified configuration and handlers.
      Parameters:
      requestTimeout - Duration to wait for responses
      transport - Transport implementation for message exchange
      requestHandlers - Map of method names to request handlers
      notificationHandlers - Map of method names to notification handlers
  • Method Details

    • getMethodNotFoundError

      public static io.modelcontextprotocol.spec.McpClientSession.MethodNotFoundError getMethodNotFoundError(String method)
    • sendRequest

      public <T> reactor.core.publisher.Mono<T> sendRequest(String method, Object requestParams, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
      Sends a JSON-RPC request and returns the response.
      Specified by:
      sendRequest in interface McpSession
      Type Parameters:
      T - The expected response type
      Parameters:
      method - The method name to call
      requestParams - The request parameters
      typeRef - Type reference for response deserialization
      Returns:
      A Mono containing the response
    • sendNotification

      public reactor.core.publisher.Mono<Void> sendNotification(String method, Object params)
      Sends a JSON-RPC notification.
      Specified by:
      sendNotification in interface McpSession
      Parameters:
      method - The method name for the notification
      params - The notification parameters
      Returns:
      A Mono that completes when the notification is sent
    • closeGracefully

      public reactor.core.publisher.Mono<Void> closeGracefully()
      Closes the session gracefully, allowing pending operations to complete.
      Specified by:
      closeGracefully in interface McpSession
      Returns:
      A Mono that completes when the session is closed
    • close

      public void close()
      Closes the session immediately, potentially interrupting pending operations.
      Specified by:
      close in interface McpSession