Skip to main content

rust_mcp_transport/
transport.rs

1use crate::{error::TransportResult, message_dispatcher::MessageDispatcher};
2use crate::{schema::RequestId, SessionId};
3use async_trait::async_trait;
4use std::{pin::Pin, sync::Arc, time::Duration};
5use tokio::{
6    sync::oneshot::{self, Sender},
7    task::JoinHandle,
8};
9
10/// Default Timeout in milliseconds
11const DEFAULT_TIMEOUT_MSEC: u64 = 60_000;
12
13/// Enum representing a stream that can either be readable or writable.
14/// This allows the reuse of the same traits for both MCP Server and MCP Client,
15/// where the data direction is reversed.
16///
17/// It encapsulates two types of I/O streams:
18/// - `Readable`: A stream that implements the `AsyncRead` trait for reading data asynchronously.
19/// - `Writable`: A stream that implements the `AsyncWrite` trait for writing data asynchronously.
20///
21pub enum IoStream {
22    Readable(Pin<Box<dyn tokio::io::AsyncRead + Send + Sync>>),
23    Writable(Pin<Box<dyn tokio::io::AsyncWrite + Send + Sync>>),
24}
25
26/// Maximum size (in bytes) of a single newline-delimited incoming message
27/// for all transports (stdio, SSE, streamable HTTP). Messages exceeding
28/// this limit are dropped silently; increase this value if you expect
29/// large tool results or responses.
30pub const DEFAULT_MAX_LINE_LENGTH: usize = 16 * 1024 * 1024;
31
32/// Configuration for the transport layer
33#[derive(Debug, Clone)]
34pub struct TransportOptions {
35    /// The timeout in milliseconds for requests.
36    ///
37    /// This value defines the maximum amount of time to wait for a response before
38    /// considering the request as timed out.
39    pub timeout: Duration,
40    /// Maximum size (in bytes) of a single newline-delimited incoming message.
41    ///
42    /// Messages exceeding this limit are dropped and logged with a warning;
43    /// the stream stays alive and resumes on the next line. If you expect
44    /// large tool results or responses, increase this value.
45    /// Default: 16 MiB.
46    pub max_line_length: usize,
47
48    /// Capacity of the incoming-message channel buffer.
49    ///
50    /// A larger value smooths out head-of-line jitter under bursty traffic at
51    /// the cost of more buffered memory. Defaults to 36.
52    pub channel_capacity: usize,
53}
54impl Default for TransportOptions {
55    fn default() -> Self {
56        Self {
57            timeout: Duration::from_millis(DEFAULT_TIMEOUT_MSEC),
58            max_line_length: DEFAULT_MAX_LINE_LENGTH,
59            channel_capacity: crate::mcp_stream::DEFAULT_MESSAGE_CHANNEL_CAPACITY,
60        }
61    }
62}
63
64/// A trait for dispatching MCP (Message Communication Protocol) messages.
65///
66/// This trait is designed to be implemented by components such as clients, servers, or transports
67/// that send and receive messages in the MCP protocol. It defines the interface for transmitting messages,
68/// optionally awaiting responses, writing raw payloads, and handling batch communication.
69///
70/// # Associated Types
71///
72/// - `R`: The response type expected from a message. This must implement deserialization and be safe
73///   for concurrent use in async contexts.
74/// - `S`: The type of the outgoing message sent directly to the wire. Must be serializable.
75/// - `M`: The internal message type used for responses received from a remote peer.
76/// - `OM`: The outgoing message type submitted to the dispatcher. This is the higher-level form of `S`
77///   used by clients or services submitting requests.
78///
79#[async_trait]
80pub trait McpDispatch<R, S, M, OM>: Send + Sync + 'static
81where
82    R: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
83    S: Clone + Send + Sync + serde::Serialize + 'static,
84    M: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
85    OM: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
86{
87    /// Sends a raw message represented by type `S` and optionally includes a `request_id`.
88    /// The `request_id` is used when sending a message in response to an MCP request.
89    /// It should match the `request_id` of the original request.
90    async fn send_message(
91        &self,
92        message: S,
93        request_timeout: Option<Duration>,
94    ) -> TransportResult<Option<R>>;
95
96    async fn send(&self, message: OM, timeout: Option<Duration>) -> TransportResult<Option<M>>;
97
98    /// Writes a string payload to the underlying asynchronous writable stream,
99    /// appending a newline character and flushing the stream afterward.
100    ///
101    async fn write_str(&self, payload: &str, skip_store: bool) -> TransportResult<()>;
102}
103
104/// A trait representing the transport layer for the MCP (Message Communication Protocol).
105///
106/// This trait abstracts the transport layer functionality required to send and receive messages
107/// within an MCP-based system. It provides methods to initialize the transport, send and receive
108/// messages, handle errors, manage pending requests, and implement keep-alive functionality.
109///
110/// # Associated Types
111///
112/// - `R`: The type of message expected to be received from the transport layer. Must be deserializable.
113/// - `S`: The type of message to be sent over the transport layer. Must be serializable.
114/// - `M`: The internal message type used by the dispatcher. Typically this wraps or transforms `R`.
115/// - `OR`: The outbound response type expected to be produced by the dispatcher when handling incoming messages.
116/// - `OM`: The outbound message type that the dispatcher expects to send as a reply to received messages.
117///
118#[async_trait]
119pub trait Transport<R, S, M, OR, OM>: Send + Sync + 'static
120where
121    R: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
122    S: Clone + Send + Sync + serde::Serialize + 'static,
123    M: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
124    OR: Clone + Send + Sync + serde::Serialize + 'static,
125    OM: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
126{
127    async fn start(&self) -> TransportResult<tokio_stream::wrappers::ReceiverStream<R>>
128    where
129        MessageDispatcher<M>: McpDispatch<R, OR, M, OM>;
130    fn message_sender(&self) -> Arc<tokio::sync::RwLock<Option<MessageDispatcher<M>>>>;
131    fn error_stream(&self) -> &tokio::sync::RwLock<Option<IoStream>>;
132    async fn shut_down(&self) -> TransportResult<()>;
133    async fn is_shut_down(&self) -> bool;
134    async fn consume_string_payload(&self, payload: &str) -> TransportResult<()>;
135    async fn pending_request_tx(&self, request_id: &RequestId) -> Option<Sender<M>>;
136    async fn keep_alive(
137        &self,
138        interval: Duration,
139        disconnect_tx: oneshot::Sender<()>,
140    ) -> TransportResult<JoinHandle<()>>;
141    async fn session_id(&self) -> Option<SessionId> {
142        None
143    }
144}
145
146/// A composite trait that combines both transport and dispatch capabilities for the MCP protocol.
147///
148/// `TransportDispatcher` unifies the functionality of [`Transport`] and [`McpDispatch`], allowing implementors
149/// to both manage the transport layer and handle message dispatch logic in a single abstraction.
150///
151/// This trait applies to components responsible for the following operations:
152/// - Handle low-level I/O (stream management, payload parsing, lifecycle control)
153/// - Dispatch and route messages, potentially awaiting or sending responses
154///
155/// # Supertraits
156///
157/// - [`Transport<R, S, M, OR, OM>`]: Provides the transport-level operations (starting, shutting down,
158///   receiving messages, etc.).
159/// - [`McpDispatch<R, OR, M, OM>`]: Provides message-sending and dispatching capabilities.
160///
161/// # Associated Types
162///
163/// - `R`: The raw message type expected to be received. Must be deserializable.
164/// - `S`: The message type sent over the transport (often serialized directly to wire).
165/// - `M`: The internal message type used within the dispatcher.
166/// - `OR`: The outbound response type returned from processing a received message.
167/// - `OM`: The outbound message type submitted by clients or application code.
168///
169pub trait TransportDispatcher<R, S, M, OR, OM>:
170    Transport<R, S, M, OR, OM> + McpDispatch<R, OR, M, OM>
171where
172    R: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
173    S: Clone + Send + Sync + serde::Serialize + 'static,
174    M: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
175    OR: Clone + Send + Sync + serde::Serialize + 'static,
176    OM: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
177{
178}
179
180#[cfg(test)]
181mod tests {
182    use super::*;
183
184    #[test]
185    fn default_channel_capacity_matches_constant() {
186        assert_eq!(
187            TransportOptions::default().channel_capacity,
188            crate::mcp_stream::DEFAULT_MESSAGE_CHANNEL_CAPACITY
189        );
190    }
191
192    #[test]
193    fn channel_capacity_is_overridable() {
194        let options = TransportOptions {
195            channel_capacity: 256,
196            ..Default::default()
197        };
198        assert_eq!(options.channel_capacity, 256);
199    }
200}