pub mod config;
pub mod content;
pub mod runtime;
use std::sync::Arc;
use graphrt_core::tool::ToolRegistry;
pub use config::{HttpConfig, McpServerConfig, McpServers, McpTransport, StdioConfig};
pub use runtime::{McpError, McpToolRuntime};
pub const PROVIDER_PREFIX: &str = "mcp:";
pub fn register(registry: &mut ToolRegistry, runtime: McpToolRuntime) {
let name = runtime.name().to_owned();
let runtime = Arc::new(runtime);
registry.add(format!("{PROVIDER_PREFIX}{name}"), runtime.clone());
registry.add(name, runtime);
}
pub fn try_register(registry: &mut ToolRegistry, runtime: McpToolRuntime) -> Result<(), McpError> {
let name = runtime.name().to_owned();
validate_name(&name)?;
let alias = format!("{PROVIDER_PREFIX}{name}");
for provider in [&name, &alias] {
if registry.get(provider).is_some() {
return Err(McpError::DuplicateProvider(provider.clone()));
}
}
register(registry, runtime);
Ok(())
}
pub fn validate_configs(configs: &[McpServerConfig]) -> Result<(), McpError> {
let mut seen = std::collections::HashSet::new();
for config in configs {
validate_name(&config.name)?;
if !seen.insert(config.name.as_str()) {
return Err(McpError::Config(format!("duplicate MCP server name `{}`", config.name)));
}
}
Ok(())
}
fn validate_name(name: &str) -> Result<(), McpError> {
if name.trim().is_empty() {
return Err(McpError::Config("MCP server name must not be empty".to_owned()));
}
if name.contains('/') {
return Err(McpError::Config(format!("MCP server name `{name}` must not contain `/`")));
}
Ok(())
}
pub async fn connect_all(configs: &[McpServerConfig]) -> Result<ToolRegistry, McpError> {
validate_configs(configs)?;
let connections = futures::future::join_all(configs.iter().map(McpToolRuntime::connect)).await;
let mut registry = ToolRegistry::new();
for connection in connections {
try_register(&mut registry, connection?)?;
}
Ok(registry)
}
#[cfg(test)]
mod tests {
use super::*;
fn cfg(name: &str) -> McpServerConfig {
serde_json::from_value(serde_json::json!({"name": name, "command": "srv"})).unwrap()
}
#[test]
fn config_names_are_validated() {
assert!(validate_configs(&[cfg("a"), cfg("b")]).is_ok());
assert!(matches!(validate_configs(&[cfg("a"), cfg("a")]), Err(McpError::Config(m)) if m.contains("duplicate")));
assert!(validate_configs(&[cfg("")]).is_err());
assert!(validate_configs(&[cfg("a/b")]).is_err());
}
#[tokio::test]
async fn connect_all_rejects_duplicates_before_spawning() {
let err = connect_all(&[cfg("dup"), cfg("dup")]).await.unwrap_err();
assert!(matches!(err, McpError::Config(_)), "{err}");
}
}