Class McpSessionManager

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

public class McpSessionManager extends Object
Manages MCP HTTP sessions with automatic expiration.

Sessions are identified by a UUID and tracked in a concurrent map. A background cleanup task periodically removes expired sessions.

Example usage:


 McpSessionManager manager = new McpSessionManager(Duration.ofMinutes(30));
 McpSession session = manager.createSession(requestHandler);
 // ... use session ...
 manager.terminateSession(session.getSessionId());
 manager.shutdown();
 
  • Constructor Details

    • McpSessionManager

      public McpSessionManager(Duration sessionTtl)
      Creates a new session manager with the given TTL.
      Parameters:
      sessionTtl - the time-to-live for sessions before they expire
  • Method Details

    • createSession

      public McpSession createSession(McpRequestHandler requestHandler)
      Creates a new session.
      Parameters:
      requestHandler - the request handler for this session
      Returns:
      the newly created session
    • getSession

      public McpSession getSession(String id)
      Retrieves a session by ID.
      Parameters:
      id - the session ID
      Returns:
      the session, or null if not found or expired
    • terminateSession

      public boolean terminateSession(String id)
      Terminates a session and closes its SSE stream if active.
      Parameters:
      id - the session ID
      Returns:
      true if the session was found and terminated
    • getSessionCount

      public int getSessionCount()
      Returns the number of active sessions.
      Returns:
      the session count
    • broadcast

      public void broadcast(String notification)
      Sends a notification to all active sessions.
      Parameters:
      notification - the JSON-RPC notification string
    • shutdown

      public void shutdown()
      Shuts down the session manager and terminates all sessions.