yapi_core/mcp/
transport.rs1use serde_json::Value;
4
5use super::http::HttpTransport;
6use super::jsonrpc::McpError;
7use super::stdio::StdioTransport;
8
9pub const MAX_MESSAGE_BYTES: usize = 16 * 1024 * 1024;
11
12#[derive(Debug)]
14pub enum Event {
15 Message(Value),
17 Error(String),
19 Closed,
21}
22
23pub type Events = tokio::sync::mpsc::UnboundedSender<Event>;
25
26pub enum Transport {
28 Stdio(Box<StdioTransport>),
30 Http(HttpTransport),
32}
33
34impl Transport {
35 pub async fn start(&self, events: Events) -> Result<(), McpError> {
37 match self {
38 Transport::Stdio(stdio) => stdio.start(events).await,
39 Transport::Http(http) => http.start(events),
40 }
41 }
42
43 pub async fn send(&self, message: &Value) -> Result<(), McpError> {
45 match self {
46 Transport::Stdio(stdio) => stdio.send(message).await,
47 Transport::Http(http) => http.send(message).await,
48 }
49 }
50
51 pub async fn close(&self) {
53 match self {
54 Transport::Stdio(stdio) => stdio.close().await,
55 Transport::Http(http) => http.close().await,
56 }
57 }
58
59 pub fn set_protocol_version(&self, version: &str) {
61 if let Transport::Http(http) = self {
62 http.set_protocol_version(version);
63 }
64 }
65
66 pub fn stderr(&self) -> Option<String> {
68 match self {
69 Transport::Stdio(stdio) => Some(stdio.stderr()),
70 Transport::Http(_) => None,
71 }
72 }
73}