mcp_sdk_rs/transport/
mod.rs

1use async_trait::async_trait;
2use futures::Stream;
3use serde::{Deserialize, Serialize};
4use std::pin::Pin;
5
6use crate::protocol::{Notification, Request, Response};
7use crate::Error;
8
9/// A message that can be sent over a transport
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(untagged)]
12pub enum Message {
13    #[serde(rename = "request")]
14    Request(Request),
15    #[serde(rename = "response")]
16    Response(Response),
17    #[serde(rename = "notification")]
18    Notification(Notification),
19}
20
21/// Trait for implementing MCP transports
22#[async_trait]
23pub trait Transport: Send + Sync + 'static {
24    /// Send a message over the transport
25    async fn send(&self, message: Message) -> Result<(), Error>;
26
27    /// Receive messages from the transport
28    fn receive(&self) -> Pin<Box<dyn Stream<Item = Result<Message, Error>> + Send>>;
29
30    /// Close the transport
31    async fn close(&self) -> Result<(), Error>;
32}
33
34pub mod stdio;
35pub mod websocket;