Class StdioClientTransport
java.lang.Object
io.modelcontextprotocol.client.transport.StdioClientTransport
- All Implemented Interfaces:
McpClientTransport,McpTransport
Implementation of the MCP Stdio transport that communicates with a server process using
standard input/output streams. Messages are exchanged as newline-delimited JSON-RPC
messages over stdin/stdout, with errors and debug information sent to stderr.
- Author:
- Christian Tzolov, Dariusz Jędrzejczyk
-
Constructor Summary
ConstructorsConstructorDescriptionStdioClientTransport(ServerParameters params) Creates a new StdioClientTransport with the specified parameters and default ObjectMapper.StdioClientTransport(ServerParameters params, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Creates a new StdioClientTransport with the specified parameters and ObjectMapper. -
Method Summary
Modifier and TypeMethodDescriptionvoidWaits for the server process to exit.reactor.core.publisher.Mono<Void>Gracefully closes the transport by destroying the process and disposing of the schedulers.reactor.core.publisher.Mono<Void>connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>, reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler) Starts the server process and initializes the message processing streams.reactor.core.publisher.Sinks.Many<String>protected ProcessBuilderCreates and returns a new ProcessBuilder instance.protected voidhandleOutbound(Function<reactor.core.publisher.Flux<McpSchema.JSONRPCMessage>, reactor.core.publisher.Flux<McpSchema.JSONRPCMessage>> outboundConsumer) reactor.core.publisher.Mono<Void>sendMessage(McpSchema.JSONRPCMessage message) Sends a message to the peer asynchronously.voidsetStdErrorHandler(Consumer<String> errorHandler) Sets the handler for processing transport-level errors.<T> TunmarshalFrom(Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Unmarshals the given data into an object of the specified type.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.modelcontextprotocol.spec.McpClientTransport
setExceptionHandlerMethods inherited from interface io.modelcontextprotocol.spec.McpTransport
close, protocolVersions
-
Constructor Details
-
StdioClientTransport
Creates a new StdioClientTransport with the specified parameters and default ObjectMapper.- Parameters:
params- The parameters for configuring the server process
-
StdioClientTransport
public StdioClientTransport(ServerParameters params, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Creates a new StdioClientTransport with the specified parameters and ObjectMapper.- Parameters:
params- The parameters for configuring the server processobjectMapper- The ObjectMapper to use for JSON serialization/deserialization
-
-
Method Details
-
connect
public reactor.core.publisher.Mono<Void> connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>, reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler) Starts the server process and initializes the message processing streams. This method sets up the process with the configured command, arguments, and environment, then starts the inbound, outbound, and error processing threads.- Specified by:
connectin interfaceMcpClientTransport- Parameters:
handler- a transformer for incoming messages- Returns:
- a
Monothat terminates upon successful client setup. It can mean establishing a connection (which can be later disposed) but it doesn't have to, depending on the transport type. The successful termination of the returnedMonosimply means the client can now be used. An error can be retried according to the application requirements. - Throws:
RuntimeException- if the process fails to start or if the process streams are null
-
getProcessBuilder
Creates and returns a new ProcessBuilder instance. Protected to allow overriding in tests.- Returns:
- A new ProcessBuilder instance
-
setStdErrorHandler
Sets the handler for processing transport-level errors.The provided handler will be called when errors occur during transport operations, such as connection failures or protocol violations.
- Parameters:
errorHandler- a consumer that processes error messages
-
awaitForExit
public void awaitForExit()Waits for the server process to exit.- Throws:
RuntimeException- if the process is interrupted while waiting
-
sendMessage
Description copied from interface:McpTransportSends a message to the peer asynchronously.This method handles the transmission of messages to the server in an asynchronous manner. Messages are sent in JSON-RPC format as specified by the MCP protocol.
- Specified by:
sendMessagein interfaceMcpTransport- Parameters:
message- theMcpSchema.JSONRPCMessageto be sent to the server- Returns:
- a
reactor.core.publisher.Mono<Void>that completes when the message has been sent
-
handleOutbound
protected void handleOutbound(Function<reactor.core.publisher.Flux<McpSchema.JSONRPCMessage>, reactor.core.publisher.Flux<McpSchema.JSONRPCMessage>> outboundConsumer) -
closeGracefully
Gracefully closes the transport by destroying the process and disposing of the schedulers. This method sends a TERM signal to the process and waits for it to exit before cleaning up resources.- Specified by:
closeGracefullyin interfaceMcpTransport- Returns:
- A Mono that completes when the transport is closed
-
getErrorSink
-
unmarshalFrom
Description copied from interface:McpTransportUnmarshals the given data into an object of the specified type.- Specified by:
unmarshalFromin interfaceMcpTransport- Type Parameters:
T- the type of the object to unmarshal- Parameters:
data- the data to unmarshaltypeRef- the type reference for the object to unmarshal- Returns:
- the unmarshalled object
-