use std::path::{Path, PathBuf};
use serde::Deserialize;
use trusty_mcp::config::{McpConfigFile, McpServerConfig, McpTransport};
use crate::paths::{EntryKind, resolve_project_entry};
pub const PROJECT_MCP_FILE: &str = "mcp.toml";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum McpTier {
Global,
Project,
}
impl McpTier {
pub fn as_str(self) -> &'static str {
match self {
Self::Global => "global",
Self::Project => "project",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct McpIssue {
pub tier: McpTier,
pub path: PathBuf,
pub detail: String,
pub remedy: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerStatus {
pub name: String,
pub tier: McpTier,
pub enabled: bool,
pub usable: bool,
pub detail: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProjectOverrides {
#[serde(default)]
pub servers: Vec<McpServerConfig>,
#[serde(default, alias = "disable")]
pub disabled: Vec<String>,
}
impl ProjectOverrides {
pub fn is_empty(&self) -> bool {
self.servers.is_empty() && self.disabled.is_empty()
}
}
#[derive(Debug, Clone, Default)]
pub struct ResolvedMcp {
pub global: Vec<McpServerConfig>,
pub overrides: ProjectOverrides,
pub servers: Vec<McpServerConfig>,
pub statuses: Vec<ServerStatus>,
pub issues: Vec<McpIssue>,
}
impl ResolvedMcp {
pub fn usable(&self) -> Vec<&McpServerConfig> {
self.servers
.iter()
.zip(&self.statuses)
.filter(|(_, status)| status.usable)
.map(|(server, _)| server)
.collect()
}
}
pub fn load_global(path: &Path) -> (Vec<McpServerConfig>, Vec<McpIssue>) {
match McpConfigFile::load_or_default(path) {
Ok(file) => (file.servers, Vec::new()),
Err(e) => {
tracing::warn!(
path = %path.display(),
error = %e,
"mcp: the shared MCP server file could not be read; starting with ZERO MCP servers",
);
(
Vec::new(),
vec![McpIssue {
tier: McpTier::Global,
path: path.to_path_buf(),
detail: format!("the shared MCP server file could not be read: {e}"),
remedy: "Fix the TOML in that file, or delete it to start from no configured \
MCP servers. Until then this run connects to none."
.to_string(),
}],
)
}
}
}
pub fn read_project_overrides(project_root: &Path) -> (ProjectOverrides, Vec<McpIssue>) {
let resolved = resolve_project_entry(project_root, PROJECT_MCP_FILE, EntryKind::File);
let path = resolved.path;
let Ok(raw) = std::fs::read_to_string(&path) else {
return (ProjectOverrides::default(), Vec::new());
};
match toml::from_str::<ProjectOverrides>(&raw) {
Ok(overrides) => (overrides, Vec::new()),
Err(e) => {
tracing::warn!(
path = %path.display(),
error = %e,
"mcp: the project MCP override file could not be read; using the global set alone",
);
(
ProjectOverrides::default(),
vec![McpIssue {
tier: McpTier::Project,
path,
detail: format!("the project MCP override file could not be read: {e}"),
remedy: "Fix the TOML in that file. Until then this project's MCP overrides \
are ignored and the global servers are used unchanged."
.to_string(),
}],
)
}
}
}
pub fn resolve_at(global_path: &Path, project_root: &Path) -> ResolvedMcp {
let (global, mut issues) = load_global(global_path);
let (overrides, project_issues) = read_project_overrides(project_root);
issues.extend(project_issues);
let project_path = resolve_project_entry(project_root, PROJECT_MCP_FILE, EntryKind::File).path;
let (accepted, refused) = super::trust::gate(&global, &overrides, &project_path);
issues.extend(refused);
let overridden: Vec<&str> = accepted.iter().map(|o| o.name()).collect();
let servers = trusty_mcp::config::resolve(&global, &accepted);
let statuses = servers
.iter()
.map(|server| grade(server, &overridden))
.collect();
ResolvedMcp {
global,
overrides,
servers,
statuses,
issues,
}
}
fn grade(server: &McpServerConfig, overridden: &[&str]) -> ServerStatus {
let tier = if overridden.contains(&server.name.as_str()) {
McpTier::Project
} else {
McpTier::Global
};
let status = |usable: bool, detail: Option<&str>| ServerStatus {
name: server.name.clone(),
tier,
enabled: server.enabled,
usable,
detail: detail.map(str::to_string),
};
if !server.enabled {
return status(false, Some("disabled in configuration"));
}
match server.transport {
McpTransport::Stdio { .. } => status(true, None),
_ => status(false, Some(super::REMOTE_UNSUPPORTED)),
}
}