Class WebSocketMcpTransport

java.lang.Object
dev.langchain4j.mcp.client.transport.websocket.WebSocketMcpTransport
All Implemented Interfaces:
McpTransport, Closeable, AutoCloseable

public class WebSocketMcpTransport extends Object implements McpTransport
  • Constructor Details

  • Method Details

    • start

      public void start(McpOperationHandler operationHandler)
      Description copied from interface: McpTransport
      Creates a connection to the MCP server (runs the server as a subprocess if needed). This does NOT yet send the "initialize" message to negotiate capabilities.
      Specified by:
      start in interface McpTransport
    • sendInitializeRequest

      public CompletableFuture<String> sendInitializeRequest(McpInitializeRequest operation)
      Description copied from interface: McpTransport
      Sends the MCP InitializeRequest and returns the server's response as unparsed JSON text, so that a transport does not need a JSON library. Only used with the legacy MCP protocol (versions up to 2025-11-25); the modern protocol uses server/discover.
      Specified by:
      sendInitializeRequest in interface McpTransport
    • sendRequest

      public CompletableFuture<String> sendRequest(McpClientMessage request)
      Description copied from interface: McpTransport
      Sends a JSON-RPC request and returns the server's response as unparsed JSON text.
      Specified by:
      sendRequest in interface McpTransport
    • sendRequest

      public CompletableFuture<String> sendRequest(McpCallContext context)
      Description copied from interface: McpTransport
      Sends a JSON-RPC request and returns the server's response as unparsed JSON text.
      Specified by:
      sendRequest in interface McpTransport
    • sendMessage

      public void sendMessage(McpClientMessage request)
      Description copied from interface: McpTransport
      Sends a message that does not expect a reply - in MCP terms a notification, or a response to a server-initiated request.
      Specified by:
      sendMessage in interface McpTransport
    • sendMessage

      public void sendMessage(McpCallContext context)
      Description copied from interface: McpTransport
      Sends a message that does not expect a reply - in MCP terms a notification, or a response to a server-initiated request.
      Specified by:
      sendMessage in interface McpTransport
    • checkHealth

      public void checkHealth()
      Description copied from interface: McpTransport
      Performs transport-specific health checks, if applicable. This is called by `McpClient.checkHealth()` as the first check before performing a check by sending a 'ping' over the MCP protocol. The purpose is that the transport may have some specific and faster ways to detect that it is broken, like for example, the STDIO transport can fail the check if it detects that the server subprocess isn't alive anymore.
      Specified by:
      checkHealth in interface McpTransport
    • requiresCancellationNotification

      public boolean requiresCancellationNotification()
      Description copied from interface: McpTransport
      Returns whether this transport requires an explicit notifications/cancelled message to cancel an in-progress request. Transports that use per-request SSE streams (like Streamable HTTP) cancel by closing the stream and should return false. Transports that share a single channel (like stdio) must return true so the server knows which request to cancel.

      Note: when using the legacy protocol (2025-11-25), the client always sends notifications/cancelled regardless of this value, because the legacy spec states that disconnection should not be interpreted as cancellation.

      Defaults to true, which is the safe answer for a transport that shares a single channel; per-request-stream transports override it.

      Specified by:
      requiresCancellationNotification in interface McpTransport
    • onFailure

      public void onFailure(Runnable actionOnFailure)
      Specified by:
      onFailure in interface McpTransport
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • reloadSslContext

      public void reloadSslContext(SSLContext sslContext)
      Reloads the SSL context used by the transport. From this point on, new websocket connections will use the updated SSL context. This does NOT cancel or restart the existing connection, so this takes effect only after the existing connection stops working.
    • builder

      public static WebSocketMcpTransport.Builder builder()
    • initialize

      @Deprecated(since="1.20.0", forRemoval=true) public CompletableFuture<com.fasterxml.jackson.databind.JsonNode> initialize(McpInitializeRequest request)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use sendInitializeRequest(McpInitializeRequest) instead, which does not expose Jackson types.
      Description copied from interface: McpTransport
      Sends the "initialize" message to the MCP server to negotiate capabilities, supported protocol version etc. When this method returns successfully, the transport is fully initialized and ready to be used. This has to be called AFTER the "start" method. Only used with the legacy MCP protocol (versions up to 2025-11-25). Modern protocol uses server/discover instead.
      Specified by:
      initialize in interface McpTransport
    • executeOperationWithResponse

      @Deprecated(since="1.20.0", forRemoval=true) public CompletableFuture<com.fasterxml.jackson.databind.JsonNode> executeOperationWithResponse(McpClientMessage request)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use sendRequest(McpClientMessage) instead, which does not expose Jackson types.
      Description copied from interface: McpTransport
      Executes an operation that expects a response from the server.
      Specified by:
      executeOperationWithResponse in interface McpTransport
    • executeOperationWithResponse

      @Deprecated(since="1.20.0", forRemoval=true) public CompletableFuture<com.fasterxml.jackson.databind.JsonNode> executeOperationWithResponse(McpCallContext context)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use sendRequest(McpCallContext) instead, which does not expose Jackson types.
      Description copied from interface: McpTransport
      Executes an operation that expects a response from the server.
      Specified by:
      executeOperationWithResponse in interface McpTransport
    • executeOperationWithoutResponse

      @Deprecated(since="1.20.0", forRemoval=true) public void executeOperationWithoutResponse(McpClientMessage request)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use sendMessage(McpClientMessage) instead, which does not expose Jackson types.
      Description copied from interface: McpTransport
      Sends a message that does not expect a reply.
      Specified by:
      executeOperationWithoutResponse in interface McpTransport
    • executeOperationWithoutResponse

      @Deprecated(since="1.20.0", forRemoval=true) public void executeOperationWithoutResponse(McpCallContext context)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use sendMessage(McpCallContext) instead, which does not expose Jackson types.
      Description copied from interface: McpTransport
      Sends a message that does not expect a reply.
      Specified by:
      executeOperationWithoutResponse in interface McpTransport