Class McpJson

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

public final class McpJson extends Object
Bridges between JSON text and Jackson's tree model while both the deprecated JsonNode-based transport API and its replacement coexist.
  • Method Details

    • deserialize

      public static <T> T deserialize(String message, Class<T> type)
      Deserializes an MCP message into a protocol type.

      The message is taken as it arrived on the wire, so this is a single parse. Reading it into a tree first and then into a type costs two more passes and, for high-precision numbers, is not guaranteed to round-trip.

    • deserialize

      @Deprecated(since="1.20.0", forRemoval=true) public static <T> T deserialize(com.fasterxml.jackson.databind.JsonNode message, Class<T> type)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use deserialize(String, Class), which does not expose Jackson types and avoids re-serializing the tree in order to read it.
      Deserializes an MCP message held as a Jackson 2 tree.
    • toValue

      public static Object toValue(String json)
      Reads any JSON value as plain JDK types: a Map, a List, a boxed primitive, or null for a JSON null.
      Throws:
      IllegalArgumentException - if the text is not valid JSON, or JsonException from a codec that reports the typed exceptions.
    • toMap

      public static Map<String,Object> toMap(String json)
      Reads a JSON object as plain values.
    • convert

      public static <T> T convert(Object value, Class<T> type)
      Re-reads an already-decoded JSON value as the given type.
    • convertList

      public static <T> List<T> convertList(Object value, Class<T> elementType)
      Re-reads an already-decoded JSON array as a list of the given element type.
    • serialize

      public static String serialize(Object message)
      Serializes an outbound MCP message. A null message renders as null rather than as the JSON null literal, so that an absent payload stays absent.
    • parse

      public static com.fasterxml.jackson.databind.JsonNode parse(String json)
    • map

      public static <T,R> CompletableFuture<R> map(CompletableFuture<T> source, Function<T,R> mapper)
      Maps a future's value while keeping cancellation linked to the source. A plain thenApply would not propagate cancellation upstream, which would leave the transport's response stream open after the client cancels the operation.