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§
Sourcefn 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_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,
Sourcefn 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 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,
Sourcefn 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 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
Provided Methods§
Sourcefn 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 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.
Sourcefn 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 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.
Sourcefn 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 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
Sourcefn 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 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,
Sourcefn is_connected(&self) -> bool
fn is_connected(&self) -> bool
Check if the transport is connected
§Returns
True if the transport is connected and ready for communication
Sourcefn connection_info(&self) -> String
fn connection_info(&self) -> String
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".