java.lang.Object
io.modelcontextprotocol.client.transport.StdioClientTransport
All Implemented Interfaces:
McpClientTransport, McpTransport

public class StdioClientTransport extends Object implements McpClientTransport
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 Details

    • StdioClientTransport

      public StdioClientTransport(ServerParameters params)
      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 process
      objectMapper - 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:
      connect in interface McpClientTransport
      Throws:
      RuntimeException - if the process fails to start or if the process streams are null
    • getProcessBuilder

      protected ProcessBuilder getProcessBuilder()
      Creates and returns a new ProcessBuilder instance. Protected to allow overriding in tests.
      Returns:
      A new ProcessBuilder instance
    • setStdErrorHandler

      public void setStdErrorHandler(Consumer<String> errorHandler)
      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

      public reactor.core.publisher.Mono<Void> sendMessage(McpSchema.JSONRPCMessage message)
      Description copied from interface: McpTransport
      Sends 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:
      sendMessage in interface McpTransport
      Parameters:
      message - the McpSchema.JSONRPCMessage to 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

      public reactor.core.publisher.Mono<Void> 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:
      closeGracefully in interface McpTransport
      Returns:
      A Mono that completes when the transport is closed
    • getErrorSink

      public reactor.core.publisher.Sinks.Many<String> getErrorSink()
    • unmarshalFrom

      public <T> T unmarshalFrom(Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
      Description copied from interface: McpTransport
      Unmarshals the given data into an object of the specified type.
      Specified by:
      unmarshalFrom in interface McpTransport
      Type Parameters:
      T - the type of the object to unmarshal
      Parameters:
      data - the data to unmarshal
      typeRef - the type reference for the object to unmarshal
      Returns:
      the unmarshalled object