Transport

Trait Transport 

Source
pub trait Transport: Send + Sync {
    // Required methods
    fn initialize<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + 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;
    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 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;
    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;
}
Expand description

Transport is the core trait for communication with MCP servers.

This trait defines the interface for interacting with Model Context Protocol (MCP) servers through various transport mechanisms. Implementations of this trait handle the low-level communication details, allowing clients to focus on high-level interactions.

§Examples

Using a transport to list available tools:

use mcp_runner::transport::Transport;
use mcp_runner::error::Result;
use serde_json::Value;

async fn example<T: Transport>(transport: &T) -> Result<()> {
    // Initialize the transport
    transport.initialize().await?;
     
    // List available tools
    let tools = transport.list_tools().await?;
    println!("Available tools: {:?}", tools);
     
    Ok(())
}

Required Methods§

Source

fn initialize<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Initializes the transport connection to the MCP server.

This method should be called before any other methods to ensure the transport is ready for communication.

§Returns

A Result<()> that is:

  • Ok(()) if initialization was successful
  • Err(Error) if initialization failed
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,

Lists all available tools provided by the MCP server.

§Returns

A Result<Vec<Value>> that is:

  • Ok(Vec<Value>) containing a list of tool definitions if successful
  • Err(Error) if the request failed
§Tool Definition Format

Each tool definition is a JSON object with at least:

  • name: A string identifier for the tool
  • description: A human-readable description of the tool
  • Additional fields as specified by the MCP server
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,

Calls a specific tool on the MCP server with the provided arguments.

§Arguments
  • name - The name of the tool to call
  • args - Arguments to pass to the tool as a JSON value
§Returns

A Result<Value> that is:

  • Ok(Value) containing the tool’s response if successful
  • Err(Error) if the tool call failed
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,

Lists all available resources provided by the MCP server.

Resources can include model metadata, usage information, or other static or dynamic data exposed by the server.

§Returns

A Result<Vec<Value>> that is:

  • Ok(Vec<Value>) containing a list of resource definitions if successful
  • Err(Error) if the request failed
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,

Retrieves a specific resource from the MCP server.

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

A Result<Value> that is:

  • Ok(Value) containing the resource data if successful
  • Err(Error) if the resource retrieval failed

Implementors§