pub struct StdioTransport { /* private fields */ }Expand description
StdioTransport provides communication with an MCP server via standard I/O.
This implementation uses JSON-RPC over standard input/output to communicate with
an MCP server. It handles concurrent requests using a background task for reading
responses and dispatches them to the appropriate handler.
Most public methods are instrumented with tracing spans.
§Example
use mcp_runner::transport::StdioTransport;
use mcp_runner::error::Result;
use async_process::{Child, Command};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<()> {
// Start a child process
let mut child = Command::new("mcp-server")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.expect("Failed to start MCP server");
// Take ownership of stdin/stdout
let stdin = child.stdin.take().expect("Failed to get stdin");
let stdout = child.stdout.take().expect("Failed to get stdout");
// Create transport
let transport = StdioTransport::new("example-server".to_string(), stdin, stdout);
// Initialize the transport
transport.initialize().await?;
// List available tools
let tools = transport.list_tools().await?;
println!("Available tools: {:?}", tools);
Ok(())
}Implementations§
Source§impl StdioTransport
impl StdioTransport
Sourcepub fn new(name: String, stdin: ChildStdin, stdout: ChildStdout) -> Self
pub fn new(name: String, stdin: ChildStdin, stdout: ChildStdout) -> Self
Creates a new StdioTransport instance.
This constructor takes ownership of the child process’s stdin and stdout,
and sets up a background task to process incoming JSON-RPC messages.
This method is instrumented with tracing.
§Arguments
name- A name for this transport, typically the server namestdin- The child process’s stdin handlestdout- The child process’s stdout handle
§Returns
A new StdioTransport instance
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Gets the name of the server associated with this transport.
§Returns
A string slice containing the server name.
Sourcepub async fn send_request(
&self,
request: JsonRpcRequest,
) -> Result<JsonRpcResponse>
pub async fn send_request( &self, request: JsonRpcRequest, ) -> Result<JsonRpcResponse>
Sends a JSON-RPC request and waits for a response.
This method handles the details of sending a request, registering a response
handler, and waiting for the response to arrive.
This method is instrumented with tracing.
§Arguments
request- The JSON-RPC request to send
§Returns
A Result<JsonRpcResponse> containing the response if successful
Sourcepub async fn send_notification(&self, notification: Value) -> Result<()>
pub async fn send_notification(&self, notification: Value) -> Result<()>
Sends a JSON-RPC notification (no response expected).
Unlike requests, notifications don’t expect a response, so this method
just sends the message without setting up a response handler.
This method is instrumented with tracing.
§Arguments
notification- The JSON-RPC notification to send
§Returns
A Result<()> indicating success or failure
Sourcepub async fn initialize(&self) -> Result<()>
pub async fn initialize(&self) -> Result<()>
Initializes the MCP server.
Sends the notifications/initialized notification to the server,
indicating that the client is ready to communicate.
This method is instrumented with tracing.
§Returns
A Result<()> indicating success or failure
Sourcepub async fn list_tools(&self) -> Result<Vec<Value>>
pub async fn list_tools(&self) -> Result<Vec<Value>>
Lists available tools provided by the MCP server.
This method is instrumented with tracing.
§Returns
A Result<Vec<Value>> containing the list of tools if successful
Sourcepub async fn list_resources(&self) -> Result<Vec<Value>>
pub async fn list_resources(&self) -> Result<Vec<Value>>
Lists available resources provided by the MCP server.
This method is instrumented with tracing.
§Returns
A Result<Vec<Value>> containing the list of resources if successful
Sourcepub async fn close(&mut self) -> Result<()>
pub async fn close(&mut self) -> Result<()>
Closes the transport and cleans up resources.
This method should be called when the transport is no longer needed
to ensure proper cleanup of background tasks and resources.
This method is instrumented with tracing.
§Returns
A Result<()> indicating success or failure
Trait Implementations§
Source§impl Transport for StdioTransport
impl Transport for StdioTransport
Source§fn initialize<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn initialize<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
This method is instrumented with tracing.
Source§fn list_tools<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn list_tools<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
This method is instrumented with tracing.
Source§fn call_tool<'life0, 'life1, 'async_trait>(
&'life0 self,
name: &'life1 str,
args: Value,
) -> Pin<Box<dyn Future<Output = Result<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn call_tool<'life0, 'life1, 'async_trait>(
&'life0 self,
name: &'life1 str,
args: Value,
) -> Pin<Box<dyn Future<Output = Result<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
This method is instrumented with tracing.