pub mod config;
pub mod spawn;
pub mod tool;
pub mod trust;
#[cfg(test)]
mod tests;
use std::path::Path;
use std::sync::Arc;
use crate::tools::registry::ToolRegistry;
use crate::tools::traits::ToolExecutor;
pub use config::{McpIssue, McpTier, ProjectOverrides, ResolvedMcp, ServerStatus};
pub use tool::{McpBridgeTool, composed_name};
pub const REMOTE_UNSUPPORTED: &str = "remote transports not supported in this slice";
#[derive(Debug, Clone, Default)]
pub struct McpDiagnostics {
pub statuses: Vec<ServerStatus>,
pub issues: Vec<McpIssue>,
pub tools: Vec<String>,
}
impl McpDiagnostics {
pub fn usable_count(&self) -> usize {
self.statuses.iter().filter(|s| s.usable).count()
}
pub fn log(&self) {
tracing::info!(
servers = self.statuses.len(),
usable = self.usable_count(),
tools = self.tools.len(),
"mcp: configured servers loaded",
);
for status in self.statuses.iter().filter(|s| !s.usable) {
tracing::warn!(
server = %status.name,
tier = status.tier.as_str(),
detail = status.detail.as_deref().unwrap_or(""),
"mcp: server not usable",
);
}
for issue in &self.issues {
tracing::warn!(
tier = issue.tier.as_str(),
path = %issue.path.display(),
detail = %issue.detail,
remedy = %issue.remedy,
"mcp: configuration problem",
);
}
}
}
#[derive(Default)]
pub struct McpToolSet {
tools: Vec<Arc<dyn ToolExecutor>>,
diagnostics: McpDiagnostics,
}
impl McpToolSet {
pub async fn load(project_root: &Path) -> Self {
if trusty_common::running_under_test_harness() {
tracing::debug!("mcp: test harness detected; not reading the operator's server file");
return Self::default();
}
match trusty_mcp::config::default_path() {
Ok(path) => Self::load_at(&path, project_root).await,
Err(e) => {
tracing::warn!(error = %e, "mcp: no shared MCP server file path resolves; no servers configured");
Self::default()
}
}
}
pub async fn load_at(global_path: &Path, project_root: &Path) -> Self {
Self::load_within(global_path, project_root, spawn::HANDSHAKE_TIMEOUT).await
}
pub async fn load_within(
global_path: &Path,
project_root: &Path,
budget: std::time::Duration,
) -> Self {
let resolved = config::resolve_at(global_path, project_root);
let mut set = Self {
tools: Vec::new(),
diagnostics: McpDiagnostics {
statuses: resolved.statuses,
issues: resolved.issues,
tools: Vec::new(),
},
};
let attempts: Vec<(usize, &trusty_mcp::config::McpServerConfig)> = resolved
.servers
.iter()
.enumerate()
.filter(|(index, _)| set.diagnostics.statuses[*index].usable)
.collect();
let outcomes = futures_util::future::join_all(
attempts
.iter()
.map(|(_, server)| spawn::connect_within(server, budget)),
)
.await;
for ((index, server), outcome) in attempts.into_iter().zip(outcomes) {
match outcome {
Ok(connected) => set.absorb(&server.name, connected),
Err(reason) => {
tracing::warn!(
server = %server.name,
reason = %reason,
"mcp: server unavailable; the rest of this run is unaffected",
);
let status = &mut set.diagnostics.statuses[index];
status.usable = false;
status.detail = Some(reason);
}
}
}
set
}
fn absorb(&mut self, server: &str, connected: spawn::Connected) {
for advertised in &connected.tools {
let name = composed_name(server, &advertised.name);
if self.diagnostics.tools.contains(&name) {
self.diagnostics.issues.push(McpIssue {
tier: McpTier::Global,
path: std::path::PathBuf::from(server),
detail: format!("the tool {name:?} is advertised more than once"),
remedy: "Only the first was registered. Check the server for a duplicate \
tool name."
.to_string(),
});
continue;
}
match McpBridgeTool::new(server, advertised, Arc::clone(&connected.client)) {
Ok(bridge) => {
self.diagnostics.tools.push(name);
self.tools.push(Arc::new(bridge));
}
Err(reason) => self.diagnostics.issues.push(McpIssue {
tier: McpTier::Global,
path: std::path::PathBuf::from(server),
detail: format!("the tool {name:?} was not registered because {reason}"),
remedy: "Ask the server's author to shrink the advertised inputSchema."
.to_string(),
}),
}
}
}
pub fn diagnostics(&self) -> &McpDiagnostics {
&self.diagnostics
}
pub fn is_empty(&self) -> bool {
self.tools.is_empty()
}
#[cfg(test)]
pub(crate) fn tool_names(&self) -> Vec<&str> {
self.tools.iter().map(|t| t.name()).collect()
}
}
pub fn register_configured_tools(registry: &mut ToolRegistry, set: &McpToolSet) -> McpDiagnostics {
let mut diagnostics = set.diagnostics.clone();
for tool in &set.tools {
let name = tool.name().to_string();
if registry.contains(&name) {
tracing::warn!(tool = %name, "mcp: a tool of this name is already registered; skipping");
diagnostics.tools.retain(|t| t != &name);
diagnostics.issues.push(McpIssue {
tier: McpTier::Global,
path: std::path::PathBuf::from(&name),
detail: format!("the tool {name:?} collides with one already registered"),
remedy: "Rename the server in ~/.trusty-tools/mcp/servers.toml so its tools \
compose to a free name."
.to_string(),
});
continue;
}
registry.register(Arc::clone(tool));
}
diagnostics
}