Struct StdioTransport

Source
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

Source

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 name
  • stdin - The child process’s stdin handle
  • stdout - The child process’s stdout handle
§Returns

A new StdioTransport instance

Source

pub fn name(&self) -> &str

Gets the name of the server associated with this transport.

§Returns

A string slice containing the server name.

Source

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

Source

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

Source

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

Source

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

Source

pub async fn call_tool( &self, name: impl AsRef<str> + Debug, args: Value, ) -> Result<Value>

Calls a tool provided by the MCP server.

This method is instrumented with tracing.

§Arguments
  • name - The name of the tool to call
  • args - The arguments to pass to the tool
§Returns

A Result<Value> containing the tool’s response if successful

Source

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

Source

pub async fn get_resource(&self, uri: impl AsRef<str> + Debug) -> Result<Value>

Retrieves a specific resource from the MCP server.

This method is instrumented with tracing.

§Arguments
  • uri - The URI of the resource to retrieve
§Returns

A Result<Value> containing the resource data if successful

Source

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

Source§

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,

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,

This method is instrumented with tracing.

Source§

fn list_resources<'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 get_resource<'life0, 'life1, 'async_trait>( &'life0 self, uri: &'life1 str, ) -> 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.

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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,

Source§

impl<T> MaybeSendSync for T