Class McpOperationHandler
java.lang.Object
dev.langchain4j.mcp.client.transport.McpOperationHandler
Handles incoming messages from the MCP server.
A transport calls onMessage(String) for every message it receives, and
expectResponse(Long, java.util.concurrent.CompletableFuture) before sending a request,
to register the id whose response it is waiting for.
-
Constructor Summary
ConstructorsConstructorDescriptionMcpOperationHandler(Map<Long, CompletableFuture<String>> pendingOperations, Supplier<List<McpRoot>> roots, McpTransport transport, Consumer<McpLogMessage> logMessageConsumer, Runnable onToolListUpdate, Runnable onResourceListUpdate, Runnable onPromptListUpdate, Consumer<String> onResourceUpdate, McpProgressHandler progressHandler, Runnable onServerPing, Runnable onServerRootsList, BiConsumer<Long, String> onServerCancelled, BiConsumer<Long, Map<String, Object>> onSubscriptionAcknowledged) McpOperationHandler(Map<Long, CompletableFuture<String>> pendingOperations, Supplier<List<McpRoot>> roots, McpTransport transport, Consumer<McpLogMessage> logMessageConsumer, Runnable onToolListUpdate, Runnable onResourceListUpdate, Runnable onPromptListUpdate, Consumer<String> onResourceUpdate, McpProgressHandler progressHandler, Runnable onServerPing, Runnable onServerRootsList, BiConsumer<Long, String> onServerCancelled, BiConsumer<Long, Map<String, Object>> onSubscriptionAcknowledged, @Nullable Consumer<McpServerMessageContext> onServerMessage) -
Method Summary
Modifier and TypeMethodDescriptionvoidcancelAllPendingOperations(String reason) voidexpectResponse(Long id, CompletableFuture<String> future) Registers a client-initiated request whose response is awaited; the future is completed with the response as unparsed JSON text.voidHandles an inbound message from the server, taken exactly as it arrived on the wire, so that a transport does not have to parse it first.
-
Constructor Details
-
McpOperationHandler
public McpOperationHandler(Map<Long, CompletableFuture<String>> pendingOperations, Supplier<List<McpRoot>> roots, McpTransport transport, Consumer<McpLogMessage> logMessageConsumer, Runnable onToolListUpdate, Runnable onResourceListUpdate, Runnable onPromptListUpdate, Consumer<String> onResourceUpdate, McpProgressHandler progressHandler, Runnable onServerPing, Runnable onServerRootsList, BiConsumer<Long, String> onServerCancelled, BiConsumer<Long, Map<String, Object>> onSubscriptionAcknowledged) -
McpOperationHandler
public McpOperationHandler(Map<Long, CompletableFuture<String>> pendingOperations, Supplier<List<McpRoot>> roots, McpTransport transport, Consumer<McpLogMessage> logMessageConsumer, Runnable onToolListUpdate, Runnable onResourceListUpdate, Runnable onPromptListUpdate, Consumer<String> onResourceUpdate, McpProgressHandler progressHandler, Runnable onServerPing, Runnable onServerRootsList, BiConsumer<Long, String> onServerCancelled, BiConsumer<Long, Map<String, Object>> onSubscriptionAcknowledged, @Nullable Consumer<McpServerMessageContext> onServerMessage) - Parameters:
onServerMessage- called with every request and notification from the server, before it's handled
-
-
Method Details
-
onMessage
Handles an inbound message from the server, taken exactly as it arrived on the wire, so that a transport does not have to parse it first.Valid JSON that is not a JSON-RPC object - a batch array, a scalar, a bare null - is ignored. Text that is not valid JSON at all throws, so that the transport can decide: fail the pending operation, or log and keep reading.
- Throws:
IllegalArgumentException- if the message is not valid JSON, orJsonExceptionfrom a codec that reports the typed exceptions.
-
expectResponse
Registers a client-initiated request whose response is awaited; the future is completed with the response as unparsed JSON text. Cancelling the future removes its registration. -
cancelAllPendingOperations
-