Class WebSocketMcpTransport
java.lang.Object
dev.langchain4j.mcp.client.transport.websocket.WebSocketMcpTransport
- All Implemented Interfaces:
McpTransport, Closeable, AutoCloseable
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuilder()voidPerforms transport-specific health checks, if applicable.voidclose()voidvoidreloadSslContext(SSLContext sslContext) Reloads the SSL context used by the transport.booleanReturns whether this transport requires an explicitnotifications/cancelledmessage to cancel an in-progress request.sendInitializeRequest(McpInitializeRequest operation) Sends the MCPInitializeRequestand returns the server's response as unparsed JSON text, so that a transport does not need a JSON library.voidsendMessage(McpCallContext context) Sends a message that does not expect a reply - in MCP terms a notification, or a response to a server-initiated request.sendRequest(McpCallContext context) Sends a JSON-RPC request and returns the server's response as unparsed JSON text.voidstart(McpOperationHandler operationHandler) Creates a connection to the MCP server (runs the server as a subprocess if needed).Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface McpTransport
sendMessage, sendRequest, setModernProtocol, setProtocolVersion
-
Constructor Details
-
WebSocketMcpTransport
-
-
Method Details
-
start
Description copied from interface:McpTransportCreates 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:
startin interfaceMcpTransport
-
sendInitializeRequest
Description copied from interface:McpTransportSends the MCPInitializeRequestand returns the server's response as unparsed JSON text, so that a transport does not need a JSON library. This has to be called AFTER the "start" method; when it returns successfully, the transport is ready to be used. Only used with the legacy MCP protocol (versions up to 2025-11-25); the modern protocol usesserver/discover.- Specified by:
sendInitializeRequestin interfaceMcpTransport
-
sendRequest
Description copied from interface:McpTransportSends a JSON-RPC request and returns the server's response as unparsed JSON text. The context carries the message and its per-call headers and_meta, which the transport applies.- Specified by:
sendRequestin interfaceMcpTransport
-
sendMessage
Description copied from interface:McpTransportSends a message that does not expect a reply - in MCP terms a notification, or a response to a server-initiated request. The context carries the message and its per-call headers and_meta, which the transport applies.- Specified by:
sendMessagein interfaceMcpTransport
-
checkHealth
public void checkHealth()Description copied from interface:McpTransportPerforms 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:
checkHealthin interfaceMcpTransport
-
requiresCancellationNotification
public boolean requiresCancellationNotification()Description copied from interface:McpTransportReturns whether this transport requires an explicitnotifications/cancelledmessage to cancel an in-progress request. Transports that use per-request SSE streams (like Streamable HTTP) cancel by closing the stream and should returnfalse. Transports that share a single channel (like stdio) must returntrueso the server knows which request to cancel.Note: when using the legacy protocol (2025-11-25), the client always sends
notifications/cancelledregardless 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:
requiresCancellationNotificationin interfaceMcpTransport
-
onFailure
- Specified by:
onFailurein interfaceMcpTransport
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
reloadSslContext
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
-