Package io.modelcontextprotocol.spec
Class McpStreamableServerSession
java.lang.Object
io.modelcontextprotocol.spec.McpStreamableServerSession
- All Implemented Interfaces:
McpLoggableSession,McpSession
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFactory for new Streamable HTTP MCP sessions.static interfaceRequest handler for the initialization request.static final recordComposite holding theMcpStreamableServerSessionand the initialization resultfinal classAn individual SSE stream within a Streamable HTTP context. -
Constructor Summary
ConstructorsConstructorDescriptionMcpStreamableServerSession(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. -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Void>accept(McpSchema.JSONRPCNotification notification) Handle the MCP notification.reactor.core.publisher.Mono<Void>accept(McpSchema.JSONRPCResponse response) Handle the MCP response.voidclose()Closes the session and releases any associated resources.reactor.core.publisher.Mono<Void>Closes the session and releases any associated resources asynchronously.reactor.core.publisher.Mono<Void>delete()getId()Return the Session ID.booleanisNotificationForLevelAllowed(McpSchema.LoggingLevel loggingLevel) Allows checking whether a particular logging level is allowed.listeningStream(McpStreamableServerTransport transport) Create a listening stream (the generic HTTP GET request without Last-Event-ID header).reactor.core.publisher.Flux<McpSchema.JSONRPCMessage>reactor.core.publisher.Mono<Void>responseStream(McpSchema.JSONRPCRequest jsonrpcRequest, McpStreamableServerTransport transport) Provide the SSE stream of MCP messages finalized with a Response.reactor.core.publisher.Mono<Void>sendNotification(String method, Object params) Sends a notification to the model client or server with parameters.<T> reactor.core.publisher.Mono<T>sendRequest(String method, Object requestParams, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Sends a request to the model counterparty and expects a response of type T.voidsetMinLoggingLevel(McpSchema.LoggingLevel minLoggingLevel) Set the minimum logging level for the client.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.modelcontextprotocol.spec.McpSession
sendNotification
-
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 IDclientCapabilities- client capabilitiesclientInfo- client inforequestTimeout- timeout to use for requestsrequestHandlers- the map of MCP request handlers keyed by method namenotificationHandlers- the map of MCP notification handlers keyed by method name
-
-
Method Details
-
setMinLoggingLevel
Description copied from interface:McpLoggableSessionSet the minimum logging level for the client. Messages below this level will be filtered out.- Specified by:
setMinLoggingLevelin interfaceMcpLoggableSession- Parameters:
minLoggingLevel- The minimum logging level
-
isNotificationForLevelAllowed
Description copied from interface:McpLoggableSessionAllows checking whether a particular logging level is allowed.- Specified by:
isNotificationForLevelAllowedin interfaceMcpLoggableSession- Parameters:
loggingLevel- the level to check- Returns:
- whether the logging at the specified level is permitted.
-
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:McpSessionSends 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:
sendRequestin interfaceMcpSession- Type Parameters:
T- the type of the expected response- Parameters:
method- the name of the method to be called on the counterpartyrequestParams- the parameters to be sent with the requesttypeRef- the TypeReference describing the expected response type- Returns:
- a Mono that will emit the response when received
-
sendNotification
Description copied from interface:McpSessionSends 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:
sendNotificationin interfaceMcpSession- Parameters:
method- the name of the notification method to be sent to the counterpartyparams- parameters to be sent with the notification- Returns:
- a Mono that completes when the notification has been sent
-
delete
-
listeningStream
public McpStreamableServerSession.McpStreamableServerSessionStream listeningStream(McpStreamableServerTransport transport) 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
-
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 creationtransport- the SSE transport stream to send messages to- Returns:
- Mono which completes once the processing is done
-
accept
Handle the MCP notification.- Parameters:
notification- MCP notification- Returns:
- Mono which completes upon succesful handling
-
accept
Handle the MCP response.- Parameters:
response- MCP response to the server-initiated request- Returns:
- Mono which completes upon successful processing
-
closeGracefully
Description copied from interface:McpSessionCloses the session and releases any associated resources asynchronously.- Specified by:
closeGracefullyin interfaceMcpSession- Returns:
- a
reactor.core.publisher.Mono<Void>that completes when the session has been closed.
-
close
public void close()Description copied from interface:McpSessionCloses the session and releases any associated resources.- Specified by:
closein interfaceMcpSession
-