Class McpStreamableServerSession

java.lang.Object
io.modelcontextprotocol.spec.McpStreamableServerSession
All Implemented Interfaces:
McpLoggableSession, McpSession

public class McpStreamableServerSession extends Object implements McpLoggableSession
Representation of a Streamable HTTP server session that keeps track of mapping server-initiated requests to the client and mapping arriving responses. It also allows handling incoming notifications. For requests, it provides the default SSE streaming capability without the insight into the transport-specific details of HTTP handling.
Author:
Dariusz Jędrzejczyk
  • Constructor Details

    • McpStreamableServerSession

      public McpStreamableServerSession(String id, McpSchema.ClientCapabilities clientCapabilities, McpSchema.Implementation clientInfo, Duration requestTimeout, Map<String,McpRequestHandler<?>> requestHandlers, Map<String,McpNotificationHandler> notificationHandlers)
      Create an instance of the streamable session.
      Parameters:
      id - session ID
      clientCapabilities - client capabilities
      clientInfo - client info
      requestTimeout - timeout to use for requests
      requestHandlers - the map of MCP request handlers keyed by method name
      notificationHandlers - the map of MCP notification handlers keyed by method name
  • Method Details

    • setMinLoggingLevel

      public void setMinLoggingLevel(McpSchema.LoggingLevel minLoggingLevel)
      Description copied from interface: McpLoggableSession
      Set the minimum logging level for the client. Messages below this level will be filtered out.
      Specified by:
      setMinLoggingLevel in interface McpLoggableSession
      Parameters:
      minLoggingLevel - The minimum logging level
    • isNotificationForLevelAllowed

      public boolean isNotificationForLevelAllowed(McpSchema.LoggingLevel loggingLevel)
      Description copied from interface: McpLoggableSession
      Allows checking whether a particular logging level is allowed.
      Specified by:
      isNotificationForLevelAllowed in interface McpLoggableSession
      Parameters:
      loggingLevel - the level to check
      Returns:
      whether the logging at the specified level is permitted.
    • getId

      public String getId()
      Return the Session ID.
      Returns:
      session ID
    • 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
    • delete

      public reactor.core.publisher.Mono<Void> delete()
    • listeningStream

      Create a listening stream (the generic HTTP GET request without Last-Event-ID header).
      Parameters:
      transport - The dedicated SSE transport stream
      Returns:
      a stream representation
    • replay

      public reactor.core.publisher.Flux<McpSchema.JSONRPCMessage> replay(Object lastEventId)
    • responseStream

      public reactor.core.publisher.Mono<Void> responseStream(McpSchema.JSONRPCRequest jsonrpcRequest, McpStreamableServerTransport transport)
      Provide the SSE stream of MCP messages finalized with a Response.
      Parameters:
      jsonrpcRequest - the MCP request triggering the stream creation
      transport - the SSE transport stream to send messages to
      Returns:
      Mono which completes once the processing is done
    • accept

      public reactor.core.publisher.Mono<Void> accept(McpSchema.JSONRPCNotification notification)
      Handle the MCP notification.
      Parameters:
      notification - MCP notification
      Returns:
      Mono which completes upon succesful handling
    • accept

      public reactor.core.publisher.Mono<Void> accept(McpSchema.JSONRPCResponse response)
      Handle the MCP response.
      Parameters:
      response - MCP response to the server-initiated request
      Returns:
      Mono which completes upon successful processing
    • 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