java.lang.Object
io.modelcontextprotocol.spec.McpClientSession
- All Implemented Interfaces:
McpSession
Default implementation of the MCP (Model Context Protocol) session that manages
bidirectional JSON-RPC communication between clients and servers. This implementation
follows the MCP specification for message exchange and transport handling.
The session manages:
- Request/response handling with unique message IDs
- Notification processing
- Message timeout management
- Transport layer abstraction
- Author:
- Christian Tzolov, Dariusz Jędrzejczyk
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for handling incoming JSON-RPC notifications.static interfaceFunctional interface for handling incoming JSON-RPC requests. -
Constructor Summary
ConstructorsConstructorDescriptionMcpClientSession(Duration requestTimeout, McpClientTransport transport, Map<String, McpClientSession.RequestHandler<?>> requestHandlers, Map<String, McpClientSession.NotificationHandler> notificationHandlers) Creates a new McpClientSession with the specified configuration and handlers. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the session immediately, potentially interrupting pending operations.reactor.core.publisher.Mono<Void>Closes the session gracefully, allowing pending operations to complete.static io.modelcontextprotocol.spec.McpClientSession.MethodNotFoundErrorgetMethodNotFoundError(String method) reactor.core.publisher.Mono<Void>sendNotification(String method, Object params) Sends a JSON-RPC notification.<T> reactor.core.publisher.Mono<T>sendRequest(String method, Object requestParams, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Sends a JSON-RPC request and returns the response.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
-
McpClientSession
public McpClientSession(Duration requestTimeout, McpClientTransport transport, Map<String, McpClientSession.RequestHandler<?>> requestHandlers, Map<String, McpClientSession.NotificationHandler> notificationHandlers) Creates a new McpClientSession with the specified configuration and handlers.- Parameters:
requestTimeout- Duration to wait for responsestransport- Transport implementation for message exchangerequestHandlers- Map of method names to request handlersnotificationHandlers- Map of method names to notification handlers
-
-
Method Details
-
getMethodNotFoundError
public static io.modelcontextprotocol.spec.McpClientSession.MethodNotFoundError getMethodNotFoundError(String method) -
sendRequest
public <T> reactor.core.publisher.Mono<T> sendRequest(String method, Object requestParams, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Sends a JSON-RPC request and returns the response.- Specified by:
sendRequestin interfaceMcpSession- Type Parameters:
T- The expected response type- Parameters:
method- The method name to callrequestParams- The request parameterstypeRef- Type reference for response deserialization- Returns:
- A Mono containing the response
-
sendNotification
Sends a JSON-RPC notification.- Specified by:
sendNotificationin interfaceMcpSession- Parameters:
method- The method name for the notificationparams- The notification parameters- Returns:
- A Mono that completes when the notification is sent
-
closeGracefully
Closes the session gracefully, allowing pending operations to complete.- Specified by:
closeGracefullyin interfaceMcpSession- Returns:
- A Mono that completes when the session is closed
-
close
public void close()Closes the session immediately, potentially interrupting pending operations.- Specified by:
closein interfaceMcpSession
-