java.lang.Object
io.modelcontextprotocol.spec.McpServerSession
- All Implemented Interfaces:
McpSession
Represents a Model Control Protocol (MCP) session on the server side. It manages
bidirectional JSON-RPC communication with the client.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFactory for creating server sessions which delegate to a provided 1:1 transport with a connected client.static interfaceNotification handler for the initialization notification from the client.static interfaceRequest handler for the initialization request.static interfaceA handler for client-initiated notifications.static interfaceA handler for client-initiated requests. -
Constructor Summary
ConstructorsConstructorDescriptionMcpServerSession(String id, Duration requestTimeout, McpServerTransport transport, McpServerSession.InitRequestHandler initHandler, McpServerSession.InitNotificationHandler initNotificationHandler, Map<String, McpServerSession.RequestHandler<?>> requestHandlers, Map<String, McpServerSession.NotificationHandler> notificationHandlers) Creates a new server session with the given parameters and the transport to use. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the session and releases any associated resources.reactor.core.publisher.Mono<Void>Closes the session and releases any associated resources asynchronously.getId()Retrieve the session id.reactor.core.publisher.Mono<Void>handle(McpSchema.JSONRPCMessage message) Called by theMcpServerTransportProvideronce the session is determined.voidinit(McpSchema.ClientCapabilities clientCapabilities, McpSchema.Implementation clientInfo) Called upon successful initialization sequence between the client and the server with the client capabilities and information.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.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
-
McpServerSession
public McpServerSession(String id, Duration requestTimeout, McpServerTransport transport, McpServerSession.InitRequestHandler initHandler, McpServerSession.InitNotificationHandler initNotificationHandler, Map<String, McpServerSession.RequestHandler<?>> requestHandlers, Map<String, McpServerSession.NotificationHandler> notificationHandlers) Creates a new server session with the given parameters and the transport to use.- Parameters:
id- session idtransport- the transport to useinitHandler- called when aMcpSchema.InitializeRequestis received by the serverinitNotificationHandler- called when aMcpSchema.METHOD_NOTIFICATION_INITIALIZEDis received.requestHandlers- map of request handlers to usenotificationHandlers- map of notification handlers to use
-
-
Method Details
-
getId
Retrieve the session id.- Returns:
- session id
-
init
public void init(McpSchema.ClientCapabilities clientCapabilities, McpSchema.Implementation clientInfo) Called upon successful initialization sequence between the client and the server with the client capabilities and information. Initialization Spec- Parameters:
clientCapabilities- the capabilities the connected client providesclientInfo- the information about the connected client
-
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
-
handle
Called by theMcpServerTransportProvideronce the session is determined. The purpose of this method is to dispatch the message to an appropriate handler as specified by the MCP server implementation (McpAsyncServerorMcpSyncServer) viaMcpServerSession.Factorythat the server creates.- Parameters:
message- the incoming JSON-RPC message- Returns:
- a Mono that completes when the message is processed
-
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
-