use std::collections::HashMap;
use std::future::Future;
use bitrouter_core::api::mcp::types::McpGatewayError;
use bitrouter_core::api::mcp::types::{
InitializeResult, McpGetPromptResult, McpPrompt, McpResource, McpResourceContent,
McpResourceTemplate, McpTool, McpToolCallResult,
};
pub trait McpTransport: Send + Sync {
fn initialize(&self) -> impl Future<Output = Result<InitializeResult, McpGatewayError>> + Send;
fn terminate(&self) -> impl Future<Output = ()> + Send;
fn list_tools(&self) -> impl Future<Output = Result<Vec<McpTool>, McpGatewayError>> + Send;
fn call_tool(
&self,
name: &str,
arguments: Option<serde_json::Map<String, serde_json::Value>>,
) -> impl Future<Output = Result<McpToolCallResult, McpGatewayError>> + Send;
fn list_resources(
&self,
) -> impl Future<Output = Result<Vec<McpResource>, McpGatewayError>> + Send;
fn read_resource(
&self,
uri: &str,
) -> impl Future<Output = Result<Vec<McpResourceContent>, McpGatewayError>> + Send;
fn list_resource_templates(
&self,
) -> impl Future<Output = Result<Vec<McpResourceTemplate>, McpGatewayError>> + Send;
fn list_prompts(&self) -> impl Future<Output = Result<Vec<McpPrompt>, McpGatewayError>> + Send;
fn get_prompt(
&self,
name: &str,
arguments: Option<HashMap<String, String>>,
) -> impl Future<Output = Result<McpGetPromptResult, McpGatewayError>> + Send;
}
pub mod http;
pub(crate) enum TransportKind {
Http(http::McpHttpClient),
}
impl McpTransport for TransportKind {
async fn initialize(&self) -> Result<InitializeResult, McpGatewayError> {
match self {
Self::Http(c) => c.initialize().await,
}
}
async fn terminate(&self) {
match self {
Self::Http(c) => c.terminate().await,
}
}
async fn list_tools(&self) -> Result<Vec<McpTool>, McpGatewayError> {
match self {
Self::Http(c) => c.list_tools().await,
}
}
async fn call_tool(
&self,
name: &str,
arguments: Option<serde_json::Map<String, serde_json::Value>>,
) -> Result<McpToolCallResult, McpGatewayError> {
match self {
Self::Http(c) => c.call_tool(name, arguments).await,
}
}
async fn list_resources(&self) -> Result<Vec<McpResource>, McpGatewayError> {
match self {
Self::Http(c) => c.list_resources().await,
}
}
async fn read_resource(&self, uri: &str) -> Result<Vec<McpResourceContent>, McpGatewayError> {
match self {
Self::Http(c) => c.read_resource(uri).await,
}
}
async fn list_resource_templates(&self) -> Result<Vec<McpResourceTemplate>, McpGatewayError> {
match self {
Self::Http(c) => c.list_resource_templates().await,
}
}
async fn list_prompts(&self) -> Result<Vec<McpPrompt>, McpGatewayError> {
match self {
Self::Http(c) => c.list_prompts().await,
}
}
async fn get_prompt(
&self,
name: &str,
arguments: Option<HashMap<String, String>>,
) -> Result<McpGetPromptResult, McpGatewayError> {
match self {
Self::Http(c) => c.get_prompt(name, arguments).await,
}
}
}