Class BaseTransport

java.lang.Object
com.tnsai.mcp.client.BaseTransport
All Implemented Interfaces:
McpTransport

public abstract class BaseTransport extends Object implements McpTransport
Shared scaffolding for McpTransport implementations.

Stdio, SSE, Streamable-HTTP, and HTTP transports all hand-rolled the same lifecycle boilerplate: a connected AtomicBoolean, a Consumer<String> for inbound messages, a Consumer<Throwable> for errors, identical setters for both, and identical null-checks on the setters. This class owns that boilerplate once so future transports only implement the transport-specific I/O.

Lifecycle contract

  • connect() delegates to doConnect() then flips connected to true only if doConnect() returns without throwing.
  • disconnect() flips connected to false first, then delegates to doDisconnect() — this way a subclass's disconnect I/O cannot see an already-false state change.
  • McpTransport.send(String) is transport-specific; subclasses must enforce their own connected-check with ensureConnected() or the raw connected atomic.

Incoming-message + error dispatch

Subclass threads that receive inbound data call dispatchMessage(String) or dispatchError(Throwable) — these call the registered consumer and swallow-and-log any consumer exception so a bad callback can't take down the transport's read loop.

Subclass template


 public class MyTransport extends BaseTransport {
     @Override protected void doConnect() throws IOException {
         // open socket / start read thread / etc.
     }
     @Override public void send(String message) throws IOException {
         ensureConnected();
         // transport-specific write
     }
     @Override protected void doDisconnect() throws IOException {
         // close socket / stop read thread
     }
 }
 
Since:
0.3.0
  • Constructor Details

    • BaseTransport

      public BaseTransport()
  • Method Details

    • connect

      public final void connect() throws IOException
      Description copied from interface: McpTransport
      Connects to the MCP server.
      Specified by:
      connect in interface McpTransport
      Throws:
      IOException - if connection fails
    • disconnect

      public final void disconnect() throws IOException
      Description copied from interface: McpTransport
      Disconnects from the MCP server.
      Specified by:
      disconnect in interface McpTransport
      Throws:
      IOException - if disconnection fails
    • isConnected

      public final boolean isConnected()
      Description copied from interface: McpTransport
      Checks if the transport is currently connected.
      Specified by:
      isConnected in interface McpTransport
      Returns:
      true if connected, false otherwise
    • setOnMessage

      public final void setOnMessage(Consumer<String> messageConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for incoming messages from the MCP server.
      Specified by:
      setOnMessage in interface McpTransport
      Parameters:
      messageConsumer - the consumer for incoming messages (JSON-RPC response/notification)
    • setOnError

      public final void setOnError(Consumer<Throwable> errorConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for transport errors.
      Specified by:
      setOnError in interface McpTransport
      Parameters:
      errorConsumer - the consumer for transport errors