Struct StdioTransport

Source
pub struct StdioTransport<R>
where R: Clone + Send + Sync + DeserializeOwned + 'static,
{ /* private fields */ }
Expand description

Implements a standard I/O transport for MCP communication.

This module provides the StdioTransport struct, which serves as a transport layer for the Model Context Protocol (MCP) using standard input/output (stdio). It supports both client-side and server-side communication by optionally launching a subprocess or using the current process’s stdio streams. The transport handles message streaming, dispatching, and shutdown operations, integrating with the MCP runtime ecosystem.

Implementations§

Source§

impl<R> StdioTransport<R>
where R: Clone + Send + Sync + DeserializeOwned + 'static,

Source

pub fn new( options: TransportOptions, ) -> Result<StdioTransport<R>, TransportError>

Creates a new StdioTransport instance for MCP Server.

This constructor configures the transport to use the current process’s stdio streams,

§Arguments
  • options - Configuration options for the transport, including timeout settings.
§Returns

A TransportResult containing the initialized StdioTransport instance.

§Errors

Currently, this method does not fail, but it returns a TransportResult for API consistency.

Source

pub fn create_with_server_launch<C>( command: C, args: Vec<String>, env: Option<HashMap<String, String>>, options: TransportOptions, ) -> Result<StdioTransport<R>, TransportError>
where C: Into<String>,

Creates a new StdioTransport instance with a subprocess for MCP Client use.

This constructor configures the transport to launch a MCP Server with a specified command arguments and optional environment variables

§Arguments
  • command - The command to execute (e.g., “rust-mcp-filesystem”).
  • args - Arguments to pass to the command. (e.g., “~/Documents”).
  • env - Optional environment variables for the subprocess.
  • options - Configuration options for the transport, including timeout settings.
§Returns

A TransportResult containing the initialized StdioTransport instance, ready to launch the MCP server on start.

Trait Implementations§

Source§

impl McpDispatch<ClientMessages, ServerMessages, ClientMessage, ServerMessage> for StdioTransport<ClientMessage>

Source§

fn send_message<'life0, 'async_trait>( &'life0 self, message: ServerMessages, request_timeout: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<Option<ClientMessages>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, StdioTransport<ClientMessage>: 'async_trait,

Sends a raw message represented by type S and optionally includes a request_id. The request_id is used when sending a message in response to an MCP request. It should match the request_id of the original request.
Source§

fn send<'life0, 'async_trait>( &'life0 self, message: ServerMessage, request_timeout: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<Option<ClientMessage>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, StdioTransport<ClientMessage>: 'async_trait,

Source§

fn send_batch<'life0, 'async_trait>( &'life0 self, message: Vec<ServerMessage>, request_timeout: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<ClientMessage>>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, StdioTransport<ClientMessage>: 'async_trait,

Source§

fn write_str<'life0, 'life1, 'async_trait>( &'life0 self, payload: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, StdioTransport<ClientMessage>: 'async_trait,

Writes a string payload to the underlying asynchronous writable stream, appending a newline character and flushing the stream afterward.
Source§

impl<R, S, M, OR, OM> Transport<R, S, M, OR, OM> for StdioTransport<M>
where R: Clone + Send + Sync + DeserializeOwned + 'static, S: Clone + Send + Sync + Serialize + 'static, M: Clone + Send + Sync + DeserializeOwned + 'static, OR: Clone + Send + Sync + Serialize + 'static, OM: Clone + Send + Sync + DeserializeOwned + 'static,

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<ReceiverStream<R>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, MessageDispatcher<M>: McpDispatch<R, OR, M, OM>, StdioTransport<M>: 'async_trait,

Starts the transport, initializing streams and the message dispatcher.

If configured with a command (MCP Client), launches the MCP server and connects its stdio streams. Otherwise, uses the current process’s stdio for server-side communication.

§Returns

A TransportResult containing:

  • A pinned stream of incoming messages.
  • A MessageDispatcher<R> for sending messages.
  • An IoStream for stderr (readable) or stdout (writable) depending on the mode.
§Errors

Returns a TransportError if the subprocess fails to spawn or stdio streams cannot be accessed.

Source§

fn is_shut_down<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where 'life0: 'async_trait, StdioTransport<M>: 'async_trait,

Checks if the transport has been shut down.

Source§

fn shut_down<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, StdioTransport<M>: 'async_trait,

Sends a shutdown signal via the watch channel and kills the subprocess if present.

§Returns

A TransportResult indicating success or failure.

§Errors

Returns a TransportError if the shutdown signal fails or the process cannot be killed.

Source§

fn pending_request_tx<'life0, 'life1, 'async_trait>( &'life0 self, request_id: &'life1 RequestId, ) -> Pin<Box<dyn Future<Output = Option<Sender<M>>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, StdioTransport<M>: 'async_trait,

Source§

fn message_sender(&self) -> Arc<RwLock<Option<MessageDispatcher<M>>>>

Source§

fn error_stream(&self) -> &RwLock<Option<IoStream>>

Source§

fn consume_string_payload<'life0, 'life1, 'async_trait>( &'life0 self, _payload: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, StdioTransport<M>: 'async_trait,

Source§

fn keep_alive<'life0, 'async_trait>( &'life0 self, _interval: Duration, _disconnect_tx: Sender<()>, ) -> Pin<Box<dyn Future<Output = Result<JoinHandle<()>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, StdioTransport<M>: 'async_trait,

Source§

impl TransportDispatcher<ClientMessages, MessageFromServer, ClientMessage, ServerMessages, ServerMessage> for StdioTransport<ClientMessage>

Auto Trait Implementations§

§

impl<R> !Freeze for StdioTransport<R>

§

impl<R> !RefUnwindSafe for StdioTransport<R>

§

impl<R> Send for StdioTransport<R>

§

impl<R> Sync for StdioTransport<R>

§

impl<R> Unpin for StdioTransport<R>

§

impl<R> !UnwindSafe for StdioTransport<R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,