use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use anyhow::Result;
use openmoat_hosts::{Host, McpCommandForm};
use serde::Serialize;
use serde_json::Value;
use super::{Review, review, servers, set_up_hosts, wrap};
use crate::home::Home;
use crate::integrity::Lock;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ServerStatus {
pub host: &'static str,
#[serde(skip)]
pub agent: &'static str,
pub name: String,
pub file: PathBuf,
pub protected: bool,
pub why: String,
pub local: bool,
pub fails_requirement: bool,
}
impl ServerStatus {
pub fn describe(&self) -> String {
let state = if self.protected {
"protected"
} else {
"unprotected"
};
format!(
"{} ({}, {}): {state}: {}",
crate::render::Named(&self.name),
self.agent,
self.file.display(),
self.why
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Shape {
Wrapped {
sandboxed: bool,
},
Local {
cannot_wrap: Option<&'static str>,
},
Remote,
InHost,
Unknown,
}
fn shape_of(entry: &Value, form: McpCommandForm) -> Shape {
if let Some(sandboxed) = wrap::sandboxed(entry, form) {
return Shape::Wrapped { sandboxed };
}
if wrap::starts_process(entry) {
return Shape::Local {
cannot_wrap: wrap::not_wrappable(entry, form),
};
}
let declared_remote = entry
.get("type")
.and_then(Value::as_str)
.is_some_and(|t| !matches!(t, "stdio" | "local"));
let has_address = ["url", "httpUrl", "serverUrl", "uri"]
.iter()
.any(|key| entry.get(*key).is_some());
if declared_remote || has_address {
Shape::Remote
} else {
Shape::Unknown
}
}
fn judged(shape: Shape, name: &str, accepted: bool, set_up: bool, agent: &str) -> (bool, String) {
if !set_up {
return (
false,
format!("OpenMoat's hook is not set up for {agent}, so nothing checks this server"),
);
}
let accept = format!("`openmoat mcp accept {name}` shows it and asks");
match (shape, accepted) {
(Shape::Wrapped { sandboxed: true }, true) => (
true,
"starts through `openmoat mcp proxy` in the OS sandbox, and its definition is accepted"
.to_owned(),
),
(Shape::Wrapped { sandboxed: false }, true) => (
true,
"starts through `openmoat mcp proxy`, and its definition is accepted; it runs with \
your permissions, outside the OS sandbox"
.to_owned(),
),
(Shape::Wrapped { .. }, false) => (
false,
format!("its definition is new or changed, so it does not start; {accept}"),
),
(Shape::Local { cannot_wrap }, accepted) => (
false,
format!(
"starts outside OpenMoat with your permissions; {}{}",
cannot_wrap.map_or_else(
|| format!("`openmoat mcp wrap {name}` checks it before it starts"),
wrap::cannot_wrap_advice
),
if accepted {
String::new()
} else {
format!(", and its definition is new or changed: {accept}")
}
),
),
(Shape::Remote, true) => (
true,
"runs nothing on this machine, and its definition is accepted, so its tool calls are \
checked"
.to_owned(),
),
(Shape::InHost, true) => (
true,
format!(
"built into {agent}; runs inside {agent}'s process, not as a separate server \
OpenMoat can wrap, and its definition is accepted, so its tool calls are checked"
),
),
(Shape::Remote | Shape::InHost, false) => (
false,
format!("its definition is new or changed, so its tool calls are refused; {accept}"),
),
(Shape::Unknown, _) => (
false,
"OpenMoat cannot tell how the agent starts this server, so nothing checks it"
.to_owned(),
),
}
}
#[derive(Debug, Default)]
pub struct Report {
pub servers: Vec<ServerStatus>,
pub unreadable: Vec<(PathBuf, String)>,
}
pub fn report(home: &Home, binary: &Path, cwd: Option<&Path>) -> Result<Report> {
let set_up: Vec<Host> = set_up_hosts(home, binary);
let held = Lock::load(&home.lock_path())
.and_then(|lock| review(home, &lock, &set_up, cwd))
.unwrap_or_default();
let mut out = Report::default();
for host in Host::ALL.into_iter().filter(|h| h.spec().mcp.is_some()) {
let scan = servers::scan(&servers::sources(home, host, cwd)?);
let is_set_up = set_up.contains(&host);
out.unreadable.extend(scan.unreadable);
for server in scan.servers {
let accepted = accepted(&held, &server.id);
out.servers
.push(status_of(host, server, accepted, is_set_up));
}
}
Ok(out)
}
fn accepted(review: &Review, id: &str) -> bool {
let waiting: BTreeSet<&str> = review
.changes
.iter()
.filter(|c| c.blocks())
.map(|c| c.id.as_str())
.collect();
review.pinned && !waiting.contains(id)
}
fn status_of(host: Host, server: servers::Server, accepted: bool, set_up: bool) -> ServerStatus {
let shape = if server.runs_in_host {
Shape::InHost
} else {
shape_of(&server.entry, server.form)
};
let agent = host.display_name();
let (protected, why) = judged(shape, &server.name, accepted, set_up, agent);
let local = matches!(
shape,
Shape::Wrapped { .. } | Shape::Local { .. } | Shape::Unknown
);
ServerStatus {
host: host.id(),
agent,
name: server.name,
file: server.file,
protected,
why,
local,
fails_requirement: local && !protected && set_up,
}
}
pub fn failed_requirement(home: &Home, binary: &Path, cwd: Option<&Path>) -> Vec<String> {
match report(home, binary, cwd) {
Ok(found) => found
.servers
.iter()
.filter(|s| s.fails_requirement)
.map(ServerStatus::describe)
.chain(
found
.unreadable
.iter()
.map(|(path, why)| format!("{} cannot be read ({why})", path.display())),
)
.collect(),
Err(e) => vec![format!("the MCP servers cannot be listed: {e:#}")],
}
}
#[cfg(test)]
mod tests;