pub use mcp_sse_proxy::is_sse_with_headers;
use crate::model::McpProtocol;
use anyhow::Result;
use log::info;
use std::collections::HashMap;
pub async fn detect_mcp_protocol(url: &str) -> Result<McpProtocol> {
detect_mcp_protocol_with_headers(url, None).await
}
pub async fn detect_mcp_protocol_with_headers(
url: &str,
headers: Option<&HashMap<String, String>>,
) -> Result<McpProtocol> {
info!(
"Start automatically detecting MCP service protocol: {}",
url
);
if is_sse_with_headers(url, headers).await {
info!("SSE protocol detected: {}", url);
return Ok(McpProtocol::Sse);
}
info!("Default uses Streamable HTTP protocol: {}", url);
Ok(McpProtocol::Stream)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_detect_invalid_url() {
let result = detect_mcp_protocol("not-a-url").await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), McpProtocol::Stream);
}
#[tokio::test]
async fn test_detect_nonexistent_server() {
let result = detect_mcp_protocol("http://localhost:99999/mcp").await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), McpProtocol::Stream);
}
#[tokio::test]
async fn test_detect_with_headers_nonexistent_server() {
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), "Bearer test-token".to_string());
let result =
detect_mcp_protocol_with_headers("http://localhost:99999/mcp", Some(&headers)).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), McpProtocol::Stream);
}
}