Class McpSession

java.lang.Object
com.tnsai.mcp.server.McpSession

public class McpSession extends Object
Represents an MCP HTTP session.

Each session corresponds to a single client connection, identified by the Mcp-Session-Id header. A session tracks initialization state, pending server-initiated notifications, and an optional SSE output stream for the GET notification endpoint.

  • Method Details

    • getSessionId

      public String getSessionId()
      Returns the session ID.
      Returns:
      the session ID
    • getCreatedAt

      public Instant getCreatedAt()
      Returns when this session was created.
      Returns:
      the creation time
    • getLastAccessedAt

      public Instant getLastAccessedAt()
      Returns when this session was last accessed.
      Returns:
      the last access time
    • isInitialized

      public boolean isInitialized()
      Returns whether this session has been initialized via the MCP handshake.
      Returns:
      true if initialized
    • touch

      public void touch()
      Updates the last access timestamp to now.
    • isExpired

      public boolean isExpired(Duration ttl)
      Checks if this session has expired based on the given TTL.
      Parameters:
      ttl - the time-to-live duration
      Returns:
      true if the session has expired
    • getPendingNotifications

      public Queue<String> getPendingNotifications()
      Returns the queue of pending notifications for this session.
      Returns:
      the pending notifications queue
    • enqueueNotification

      public void enqueueNotification(String notification)
      Enqueues a notification to be delivered via the SSE stream or polled later. If an active SSE stream exists, also writes directly to it.
      Parameters:
      notification - the JSON-RPC notification string
    • getActiveSseStream

      public OutputStream getActiveSseStream()
      Returns the active SSE output stream for the GET notification endpoint.
      Returns:
      the active SSE stream, or null if none
    • setActiveSseStream

      public void setActiveSseStream(OutputStream stream)
      Sets the active SSE output stream for server-initiated notifications.
      Parameters:
      stream - the output stream, or null to clear
    • getRequestHandler

      public McpRequestHandler getRequestHandler()
      Returns the request handler associated with this session.
      Returns:
      the request handler