Package com.tnsai.mcp.server
Class McpSessionManager
java.lang.Object
com.tnsai.mcp.server.McpSessionManager
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 Summary
ConstructorsConstructorDescriptionMcpSessionManager(Duration sessionTtl) Creates a new session manager with the given TTL. -
Method Summary
Modifier and TypeMethodDescriptionvoidSends a notification to all active sessions.createSession(McpRequestHandler requestHandler) Creates a new session.getSession(String id) Retrieves a session by ID.intReturns the number of active sessions.voidshutdown()Shuts down the session manager and terminates all sessions.booleanTerminates a session and closes its SSE stream if active.
-
Constructor Details
-
McpSessionManager
Creates a new session manager with the given TTL.- Parameters:
sessionTtl- the time-to-live for sessions before they expire
-
-
Method Details
-
createSession
Creates a new session.- Parameters:
requestHandler- the request handler for this session- Returns:
- the newly created session
-
getSession
Retrieves a session by ID.- Parameters:
id- the session ID- Returns:
- the session, or null if not found or expired
-
terminateSession
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
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.
-