Server instructions and the eight tool descriptions mention peers and the untrusted-data rule only while a registry is configured. peers://status returns the list_peers JSON and is advertised only then. diagnose_unit and container_health_report take an optional validated peer argument.
115 lines
4.6 KiB
Rust
115 lines
4.6 KiB
Rust
//! MCP resources. Resolved live on every read; not subscribable.
|
|
|
|
use percent_encoding::percent_decode_str;
|
|
use rmcp::model::{ReadResourceResult, Resource, ResourceContents, ResourceTemplate};
|
|
|
|
use crate::error::AppError;
|
|
use crate::sources::docker::normalize_name;
|
|
use crate::sources::systemd::validate_unit_name;
|
|
use crate::state::AppState;
|
|
|
|
pub const FAILED_UNITS_URI: &str = "systemd://units/failed";
|
|
pub const CONTAINERS_URI: &str = "docker://containers";
|
|
pub const PEERS_STATUS_URI: &str = "peers://status";
|
|
pub const UNIT_URI_PREFIX: &str = "systemd://unit/";
|
|
pub const CONTAINER_URI_PREFIX: &str = "docker://container/";
|
|
|
|
/// Resources advertised by `resources/list`. `peers://status` only exists
|
|
/// while peers are configured.
|
|
pub fn list(peers: bool) -> Vec<Resource> {
|
|
let mut v = vec![
|
|
Resource::new(FAILED_UNITS_URI, "failed-units")
|
|
.with_title("Failed systemd units")
|
|
.with_description("Units currently in the failed active state.")
|
|
.with_mime_type("application/json"),
|
|
Resource::new(CONTAINERS_URI, "containers")
|
|
.with_title("Docker containers")
|
|
.with_description("All containers (running and stopped) with state and health.")
|
|
.with_mime_type("application/json"),
|
|
];
|
|
if peers {
|
|
v.push(
|
|
Resource::new(PEERS_STATUS_URI, "peer-status")
|
|
.with_title("Peer status")
|
|
.with_description(
|
|
"Health of each configured peer: server.up (TCP connect), api.up \
|
|
(authenticated ping) with auth state, latency and last change. Same JSON \
|
|
as the list_peers tool.",
|
|
)
|
|
.with_mime_type("application/json"),
|
|
);
|
|
}
|
|
v
|
|
}
|
|
|
|
/// Templates advertised by `resources/templates/list`. They return the same
|
|
/// JSON as the `get_systemd_unit` and `get_container` tools.
|
|
pub fn templates() -> Vec<ResourceTemplate> {
|
|
vec![
|
|
ResourceTemplate::new("systemd://unit/{name}", "systemd-unit")
|
|
.with_title("systemd unit status")
|
|
.with_description(
|
|
"Status of one loaded systemd unit, e.g. systemd://unit/nginx.service. \
|
|
Escape unusual characters (such as backslash) with percent-encoding.",
|
|
)
|
|
.with_mime_type("application/json"),
|
|
ResourceTemplate::new("docker://container/{name}", "docker-container")
|
|
.with_title("Docker container status")
|
|
.with_description(
|
|
"Status of one container by name or id, e.g. docker://container/web-1.",
|
|
)
|
|
.with_mime_type("application/json"),
|
|
]
|
|
}
|
|
|
|
/// Percent-decode the `{name}` part of a template URI.
|
|
fn decode_name(raw: &str) -> Result<String, AppError> {
|
|
percent_decode_str(raw)
|
|
.decode_utf8()
|
|
.map(|c| c.into_owned())
|
|
.map_err(|_| AppError::BadRequest("resource name is not valid UTF-8".into()))
|
|
}
|
|
|
|
fn json_resource(uri: &str, json: String) -> ReadResourceResult {
|
|
ReadResourceResult::new(vec![ResourceContents::TextResourceContents {
|
|
uri: uri.to_owned(),
|
|
mime_type: Some("application/json".into()),
|
|
text: json,
|
|
meta: None,
|
|
}])
|
|
}
|
|
|
|
/// Read a resource by URI, querying the source on each call.
|
|
pub async fn read(state: &AppState, uri: &str) -> Result<ReadResourceResult, AppError> {
|
|
if uri == PEERS_STATUS_URI {
|
|
let reg = super::peers::registry(state)?;
|
|
let json = serde_json::to_string(&super::peers::statuses(reg))
|
|
.map_err(|e| AppError::Internal(e.to_string()))?;
|
|
return Ok(json_resource(uri, json));
|
|
}
|
|
let json = match uri {
|
|
FAILED_UNITS_URI => {
|
|
let units = state.systemd.list_units(Some("failed")).await?;
|
|
serde_json::to_string_pretty(&units)
|
|
}
|
|
CONTAINERS_URI => {
|
|
let cs = state.docker.ps(true).await?;
|
|
serde_json::to_string_pretty(&cs)
|
|
}
|
|
other => {
|
|
if let Some(raw) = other.strip_prefix(UNIT_URI_PREFIX) {
|
|
let name = decode_name(raw)?;
|
|
validate_unit_name(&name)?;
|
|
serde_json::to_string_pretty(&state.systemd.unit_status(&name).await?)
|
|
} else if let Some(raw) = other.strip_prefix(CONTAINER_URI_PREFIX) {
|
|
let name = normalize_name(&decode_name(raw)?)?;
|
|
serde_json::to_string_pretty(&state.docker.container_status(&name).await?)
|
|
} else {
|
|
return Err(AppError::NotFound(format!("unknown resource '{other}'")));
|
|
}
|
|
}
|
|
}
|
|
.map_err(|e| AppError::Internal(e.to_string()))?;
|
|
Ok(json_resource(uri, json))
|
|
}
|