Class McpOperationHandler

java.lang.Object
dev.langchain4j.mcp.client.transport.McpOperationHandler

public class McpOperationHandler extends Object
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.

The handle and startOperation methods do the same through Jackson's JsonNode and are deprecated.

  • Constructor Details

  • Method Details

    • handle

      @Deprecated(since="1.20.0", forRemoval=true) public void handle(com.fasterxml.jackson.databind.JsonNode message)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use onMessage(String) instead, which does not expose Jackson types.
      Handles an inbound message from the server.
    • onMessage

      public void onMessage(String json)
      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, or JsonException from a codec that reports the typed exceptions.
    • startOperation

      @Deprecated(since="1.20.0", forRemoval=true) public void startOperation(Long id, CompletableFuture<com.fasterxml.jackson.databind.JsonNode> future)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use expectResponse(Long, CompletableFuture) instead, which does not expose Jackson types.
      Registers a client-initiated request whose response is awaited.
    • expectResponse

      public void expectResponse(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.
    • cancelAllPendingOperations

      public void cancelAllPendingOperations(String reason)