Package com.tnsai.mcp.client
Class BaseTransport
java.lang.Object
com.tnsai.mcp.client.BaseTransport
- All Implemented Interfaces:
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 todoConnect()then flipsconnectedtotrueonly ifdoConnect()returns without throwing.disconnect()flipsconnectedtofalsefirst, then delegates todoDisconnect()— 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 withensureConnected()or the rawconnectedatomic.
Incoming-message + error dispatch
Subclass threads that receive inbound data calldispatchMessage(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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidconnect()Connects to the MCP server.final voidDisconnects from the MCP server.final booleanChecks if the transport is currently connected.final voidsetOnError(Consumer<Throwable> errorConsumer) Sets a consumer for transport errors.final voidsetOnMessage(Consumer<String> messageConsumer) Sets a consumer for incoming messages from the MCP server.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.mcp.client.McpTransport
send
-
Constructor Details
-
BaseTransport
public BaseTransport()
-
-
Method Details
-
connect
Description copied from interface:McpTransportConnects to the MCP server.- Specified by:
connectin interfaceMcpTransport- Throws:
IOException- if connection fails
-
disconnect
Description copied from interface:McpTransportDisconnects from the MCP server.- Specified by:
disconnectin interfaceMcpTransport- Throws:
IOException- if disconnection fails
-
isConnected
public final boolean isConnected()Description copied from interface:McpTransportChecks if the transport is currently connected.- Specified by:
isConnectedin interfaceMcpTransport- Returns:
- true if connected, false otherwise
-
setOnMessage
Description copied from interface:McpTransportSets a consumer for incoming messages from the MCP server.- Specified by:
setOnMessagein interfaceMcpTransport- Parameters:
messageConsumer- the consumer for incoming messages (JSON-RPC response/notification)
-
setOnError
Description copied from interface:McpTransportSets a consumer for transport errors.- Specified by:
setOnErrorin interfaceMcpTransport- Parameters:
errorConsumer- the consumer for transport errors
-