Package com.tnsai.mcp.client
Class StdioTransport
java.lang.Object
com.tnsai.mcp.client.StdioTransport
- All Implemented Interfaces:
McpTransport
Stdio-based MCP transport that communicates with an external process.
Features:
- Automatic reconnection with exponential backoff
- Connection health monitoring
- Configurable timeouts and retry parameters
- Process lifecycle management
- Thread-safe message handling
Example usage:
StdioTransport transport = StdioTransport.builder()
.command("npx", "-y", "@modelcontextprotocol/server-filesystem", "/path")
.maxRetries(5)
.initialRetryDelay(Duration.ofMillis(500))
.maxRetryDelay(Duration.ofSeconds(30))
.healthCheckInterval(Duration.ofSeconds(30))
.processStartTimeout(Duration.ofSeconds(10))
.build();
transport.setOnMessage(message -> System.out.println("Received: " + message));
transport.setOnError(error -> System.err.println("Error: " + error));
transport.connect();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating StdioTransport instances with custom configuration.static final recordReason for a subprocess disconnection. -
Method Summary
Modifier and TypeMethodDescriptionstatic StdioTransport.Builderbuilder()Creates a new builder for StdioTransport.voidconnect()Connects to the MCP server.static StdioTransportCreates a simple StdioTransport with default settings.voidDisconnects from the MCP server.String[]Returns the command used to start the process.intReturns the process exit code if available.longReturns the time since last activity in milliseconds.intReturns the number of reconnection attempts made.booleanChecks if the transport is currently connected.voidSends a message to the MCP server.voidsetOnCrashDetected(Consumer<StdioTransport.DisconnectReason> onCrashDetected) Sets a callback for subprocess crash detection with structured reason.voidsetOnDisconnect(Runnable onDisconnect) Sets a callback to be invoked when disconnection is detected.voidsetOnError(Consumer<Throwable> errorConsumer) Sets a consumer for transport errors.voidsetOnMessage(Consumer<String> messageConsumer) Sets a consumer for incoming messages from the MCP server.voidsetOnReconnect(Runnable onReconnect) Sets a callback to be invoked when reconnection succeeds.
-
Method Details
-
builder
Creates a new builder for StdioTransport.- Returns:
- a new Builder instance
-
create
Creates a simple StdioTransport with default settings.- Parameters:
command- the command and arguments to execute- Returns:
- a new StdioTransport instance
-
connect
Description copied from interface:McpTransportConnects to the MCP server.- Specified by:
connectin interfaceMcpTransport- Throws:
IOException- if connection fails
-
send
Description copied from interface:McpTransportSends a message to the MCP server.- Specified by:
sendin interfaceMcpTransport- Parameters:
message- the message to send (JSON-RPC request)- Throws:
IOException- if sending fails
-
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
-
setOnReconnect
Sets a callback to be invoked when reconnection succeeds.- Parameters:
onReconnect- the callback to invoke on successful reconnection
-
setOnDisconnect
Sets a callback to be invoked when disconnection is detected.- Parameters:
onDisconnect- the callback to invoke on disconnection
-
setOnCrashDetected
Sets a callback for subprocess crash detection with structured reason.Unlike
setOnDisconnect(java.lang.Runnable), this callback receives aStdioTransport.DisconnectReasonwith exit code and crash details.- Parameters:
onCrashDetected- the callback to invoke when a crash is detected
-
disconnect
Description copied from interface:McpTransportDisconnects from the MCP server.- Specified by:
disconnectin interfaceMcpTransport- Throws:
IOException- if disconnection fails
-
isConnected
public boolean isConnected()Description copied from interface:McpTransportChecks if the transport is currently connected.- Specified by:
isConnectedin interfaceMcpTransport- Returns:
- true if connected, false otherwise
-
getReconnectAttempts
public int getReconnectAttempts()Returns the number of reconnection attempts made.- Returns:
- the current reconnection attempt count
-
getIdleTimeMillis
public long getIdleTimeMillis()Returns the time since last activity in milliseconds.- Returns:
- milliseconds since last message was sent or received
-
getCommand
Returns the command used to start the process.- Returns:
- a copy of the command array
-
getExitCode
public int getExitCode()Returns the process exit code if available.- Returns:
- the exit code, or -1 if the process is still running or hasn't been started
-