Package com.tnsai.mcp.server
Class McpSession
java.lang.Object
com.tnsai.mcp.server.McpSession
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 Summary
Modifier and TypeMethodDescriptionvoidenqueueNotification(String notification) Enqueues a notification to be delivered via the SSE stream or polled later.Returns the active SSE output stream for the GET notification endpoint.Returns when this session was created.Returns when this session was last accessed.Returns the queue of pending notifications for this session.Returns the request handler associated with this session.Returns the session ID.booleanChecks if this session has expired based on the given TTL.booleanReturns whether this session has been initialized via the MCP handshake.voidsetActiveSseStream(OutputStream stream) Sets the active SSE output stream for server-initiated notifications.voidtouch()Updates the last access timestamp to now.
-
Method Details
-
getSessionId
Returns the session ID.- Returns:
- the session ID
-
getCreatedAt
Returns when this session was created.- Returns:
- the creation time
-
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
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
Returns the queue of pending notifications for this session.- Returns:
- the pending notifications queue
-
enqueueNotification
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
Returns the active SSE output stream for the GET notification endpoint.- Returns:
- the active SSE stream, or null if none
-
setActiveSseStream
Sets the active SSE output stream for server-initiated notifications.- Parameters:
stream- the output stream, or null to clear
-
getRequestHandler
Returns the request handler associated with this session.- Returns:
- the request handler
-