Skip to main content

Transport

Trait Transport 

Source
pub trait Transport: Send + Sync {
    // Required methods
    fn send_request<'life0, 'async_trait>(
        &'life0 mut self,
        request: JsonRpcRequest,
    ) -> Pin<Box<dyn Future<Output = McpResult<JsonRpcResponse>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn send_notification<'life0, 'async_trait>(
        &'life0 mut self,
        notification: JsonRpcNotification,
    ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn receive_notification<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = McpResult<Option<JsonRpcNotification>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn close<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn open_subscription<'life0, 'async_trait>(
        &'life0 mut self,
        _request: JsonRpcRequest,
    ) -> Pin<Box<dyn Future<Output = McpResult<ClientSubscription>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn cancel_subscription<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _request_id: &'life1 Value,
    ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn handle_incoming_request<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = McpResult<Option<JsonRpcRequest>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn send_response<'life0, 'async_trait>(
        &'life0 mut self,
        _response: JsonRpcResponse,
    ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn is_connected(&self) -> bool { ... }
    fn connection_info(&self) -> String { ... }
}
Expand description

Transport trait for MCP clients

Note: This trait is primarily for internal use and advanced custom transport implementations. Most users should use the provided transport implementations (StdioTransport, HttpTransport, WebSocketTransport) rather than implementing this trait directly.

§When to use Transport directly:

  • Implementing a custom transport protocol (e.g., IPC, named pipes, custom network protocol)
  • Creating mock transports for testing
  • Building transport middleware or decorators

§Example Custom Transport:

use prism_mcp_rs::transport::traits::Transport;
use prism_mcp_rs::protocol::{JsonRpcRequest, JsonRpcResponse, JsonRpcNotification};
use prism_mcp_rs::core::error::McpResult;
use async_trait::async_trait;

struct CustomTransport {
    // Your transport implementation
}

#[async_trait]
impl Transport for CustomTransport {
    async fn send_request(&mut self, request: JsonRpcRequest) -> McpResult<JsonRpcResponse> {
        // Implementation
    }
     
    async fn send_notification(&mut self, notification: JsonRpcNotification) -> McpResult<()> {
        // Implementation
    }
     
    async fn receive_notification(&mut self) -> McpResult<Option<JsonRpcNotification>> {
        // Implementation
    }
     
    async fn close(&mut self) -> McpResult<()> {
        // Close implementation
        Ok(())
    }
}

Trait defines the interface for sending requests and receiving responses in a client-side MCP connection.

Required Methods§

Source

fn send_request<'life0, 'async_trait>( &'life0 mut self, request: JsonRpcRequest, ) -> Pin<Box<dyn Future<Output = McpResult<JsonRpcResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send a JSON-RPC request and wait for a response

§Arguments
  • request - The JSON-RPC request to send
§Returns

Result containing the JSON-RPC response or an error

Source

fn send_notification<'life0, 'async_trait>( &'life0 mut self, notification: JsonRpcNotification, ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send a JSON-RPC notification (no response expected)

§Arguments
  • notification - The JSON-RPC notification to send
§Returns

Result indicating success or an error

Source

fn receive_notification<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = McpResult<Option<JsonRpcNotification>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Receive a notification from the server (non-blocking)

§Returns

Result containing an optional notification or an error

Source

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

Close the transport connection

§Returns

Result indicating success or an error

Provided Methods§

Source

fn open_subscription<'life0, 'async_trait>( &'life0 mut self, _request: JsonRpcRequest, ) -> Pin<Box<dyn Future<Output = McpResult<ClientSubscription>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Open a modern long-lived subscription stream.

Source

fn cancel_subscription<'life0, 'life1, 'async_trait>( &'life0 mut self, _request_id: &'life1 Value, ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Cancel an open subscription. HTTP implementations close the response stream; STDIO implementations send notifications/cancelled.

Source

fn handle_incoming_request<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = McpResult<Option<JsonRpcRequest>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Handle incoming server request (for bidirectional communication)

Method is called to check for incoming requests from the server. It should be non-blocking and return None if no request is available.

§Returns

Result containing an optional server request or an error

Source

fn send_response<'life0, 'async_trait>( &'life0 mut self, _response: JsonRpcResponse, ) -> Pin<Box<dyn Future<Output = McpResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send response to server request (for bidirectional communication)

Method sends a response back to the server for a server-initiated request.

§Arguments
  • response - The JSON-RPC response to send
§Returns

Result indicating success or an error

Source

fn is_connected(&self) -> bool

Check if the transport is connected

§Returns

True if the transport is connected and ready for communication

Source

fn connection_info(&self) -> String

Get connection information for debugging

§Returns

String describing the connection

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§