pub struct ServerStdioTransport { /* private fields */ }
Expand description
Server transport that communicates with MCP clients over standard I/O.
The ServerStdioTransport
uses standard input and output streams (stdin/stdout)
to send and receive MCP messages. This transport is ideal for command-line
applications, where the server needs to communicate with a client that launched
it as a child process.
Use cases include:
- CLI tools that implement MCP
- Embedding MCP in existing command-line applications
- Testing and development scenarios
§Example
use mcp_core::{protocol::Protocol, transport::{ServerStdioTransport, Transport}};
async fn example() {
let protocol = Protocol::builder().build();
let transport = ServerStdioTransport::new(protocol);
// Start handling messages
transport.open().await.expect("Failed to start stdio server");
}
Implementations§
Trait Implementations§
Source§impl Clone for ServerStdioTransport
impl Clone for ServerStdioTransport
Source§fn clone(&self) -> ServerStdioTransport
fn clone(&self) -> ServerStdioTransport
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Transport for ServerStdioTransport
impl Transport for ServerStdioTransport
Source§fn open<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn open<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Opens the transport and starts processing messages.
This method enters a loop that:
- Polls for incoming messages from stdin
- Processes each message according to its type (request, notification, response)
- Sends responses as needed
- Continues until EOF is received on stdin
§Returns
A Result
indicating success or failure
Source§fn close<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn close<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Closes the transport.
This is a no-op for the stdio transport as standard I/O streams are managed by the OS.
§Returns
A Result
indicating success
Source§fn poll_message<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn poll_message<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Polls for incoming messages from stdin.
This method reads a line from stdin and parses it as a JSON-RPC message.
§Returns
A Result
containing an Option<Message>
. None
indicates EOF.
Source§fn request(
&self,
method: &str,
params: Option<Value>,
options: RequestOptions,
) -> Pin<Box<dyn Future<Output = Result<JsonRpcResponse>> + Send + Sync>>
fn request( &self, method: &str, params: Option<Value>, options: RequestOptions, ) -> Pin<Box<dyn Future<Output = Result<JsonRpcResponse>> + Send + Sync>>
Sends a request to the client and waits for a response.
This method:
- Creates a new request ID
- Constructs a JSON-RPC request
- Sends it to stdout
- Waits for a response with the same ID, with a timeout
§Arguments
method
- The method name for the requestparams
- Optional parameters for the requestoptions
- Request options (like timeout)
§Returns
A Future
that resolves to a Result
containing the response
Source§fn send_notification<'life0, 'life1, 'async_trait>(
&'life0 self,
method: &'life1 str,
params: Option<Value>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn send_notification<'life0, 'life1, 'async_trait>(
&'life0 self,
method: &'life1 str,
params: Option<Value>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sends a notification to the client.
This method constructs a JSON-RPC notification and writes it to stdout. Unlike requests, notifications do not expect a response.
§Arguments
method
- The method name for the notificationparams
- Optional parameters for the notification
§Returns
A Result
indicating success or failure