Class McpServerSession

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

public class McpServerSession extends Object implements McpSession
Represents a Model Control Protocol (MCP) session on the server side. It manages bidirectional JSON-RPC communication with the client.
  • Constructor Details

  • Method Details

    • getId

      public String getId()
      Retrieve the session id.
      Returns:
      session id
    • init

      public void init(McpSchema.ClientCapabilities clientCapabilities, McpSchema.Implementation clientInfo)
      Called upon successful initialization sequence between the client and the server with the client capabilities and information. Initialization Spec
      Parameters:
      clientCapabilities - the capabilities the connected client provides
      clientInfo - the information about the connected client
    • sendRequest

      public <T> reactor.core.publisher.Mono<T> sendRequest(String method, Object requestParams, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
      Description copied from interface: McpSession
      Sends a request to the model counterparty and expects a response of type T.

      This method handles the request-response pattern where a response is expected from the client or server. The response type is determined by the provided TypeReference.

      Specified by:
      sendRequest in interface McpSession
      Type Parameters:
      T - the type of the expected response
      Parameters:
      method - the name of the method to be called on the counterparty
      requestParams - the parameters to be sent with the request
      typeRef - the TypeReference describing the expected response type
      Returns:
      a Mono that will emit the response when received
    • sendNotification

      public reactor.core.publisher.Mono<Void> sendNotification(String method, Object params)
      Description copied from interface: McpSession
      Sends a notification to the model client or server with parameters.

      Similar to McpSession.sendNotification(String) but allows sending additional parameters with the notification.

      Specified by:
      sendNotification in interface McpSession
      Parameters:
      method - the name of the notification method to be sent to the counterparty
      params - parameters to be sent with the notification
      Returns:
      a Mono that completes when the notification has been sent
    • handle

      public reactor.core.publisher.Mono<Void> handle(McpSchema.JSONRPCMessage message)
      Called by the McpServerTransportProvider once the session is determined. The purpose of this method is to dispatch the message to an appropriate handler as specified by the MCP server implementation (McpAsyncServer or McpSyncServer) via McpServerSession.Factory that the server creates.
      Parameters:
      message - the incoming JSON-RPC message
      Returns:
      a Mono that completes when the message is processed
    • closeGracefully

      public reactor.core.publisher.Mono<Void> closeGracefully()
      Description copied from interface: McpSession
      Closes the session and releases any associated resources asynchronously.
      Specified by:
      closeGracefully in interface McpSession
      Returns:
      a reactor.core.publisher.Mono<Void> that completes when the session has been closed.
    • close

      public void close()
      Description copied from interface: McpSession
      Closes the session and releases any associated resources.
      Specified by:
      close in interface McpSession