public class McpClientSession extends Object implements McpSession
The session manages:
| Modifier and Type | Class and Description |
|---|---|
static class |
McpClientSession.MethodNotFoundError |
static interface |
McpClientSession.NotificationHandler
Functional interface for handling incoming JSON-RPC notifications.
|
static interface |
McpClientSession.RequestHandler<T>
Functional interface for handling incoming JSON-RPC requests.
|
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the session immediately, potentially interrupting pending operations.
|
reactor.core.publisher.Mono<Void> |
closeGracefully()
Closes the session gracefully, allowing pending operations to complete.
|
String |
getId()
Retrieve the session id.
|
static McpClientSession.MethodNotFoundError |
getMethodNotFoundError(String method) |
reactor.core.publisher.Mono<Void> |
handle(McpSchema.JSONRPCMessage message)
Called by the
McpServerTransportProvider once the session is determined. |
void |
openSSE()
The client may issue an HTTP GET to the MCP endpoint.
|
reactor.core.publisher.Mono<Void> |
sendNotification(String method)
Sends a notification to the model client or server without parameters.
|
reactor.core.publisher.Mono<Void> |
sendNotification(String method,
Map<String,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.
|
public McpClientSession(Duration requestTimeout, McpClientTransport transport, Map<String,McpClientSession.RequestHandler<?>> requestHandlers, Map<String,McpClientSession.NotificationHandler> notificationHandlers)
requestTimeout - Duration to wait for responsestransport - Transport implementation for message exchangerequestHandlers - Map of method names to request handlersnotificationHandlers - Map of method names to notification handlerspublic void openSSE()
public static McpClientSession.MethodNotFoundError getMethodNotFoundError(String method)
public String getId()
McpSessiongetId in interface McpSessionpublic reactor.core.publisher.Mono<Void> handle(McpSchema.JSONRPCMessage message)
McpSessionMcpServerTransportProvider once 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
(McpAsyncServer or
McpSyncServer) via
McpServerSession.Factory that the server creates.handle in interface McpSessionmessage - the incoming JSON-RPC messagepublic <T> reactor.core.publisher.Mono<T> sendRequest(String method, Object requestParams, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
sendRequest in interface McpSessionT - The expected response typemethod - The method name to callrequestParams - The request parameterstypeRef - Type reference for response deserializationpublic reactor.core.publisher.Mono<Void> sendNotification(String method)
McpSessionThis method implements the notification pattern where no response is expected from the counterparty. It's useful for fire-and-forget scenarios.
sendNotification in interface McpSessionmethod - the name of the notification method to be called on the serverpublic reactor.core.publisher.Mono<Void> sendNotification(String method, Map<String,Object> params)
sendNotification in interface McpSessionmethod - The method name for the notificationparams - The notification parameterspublic reactor.core.publisher.Mono<Void> closeGracefully()
closeGracefully in interface McpSessionpublic void close()
close in interface McpSessionCopyright © 2025. All rights reserved.