mcp: add systemd://unit/{name} and docker://container/{name} resource templates

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
ariandClaude Sonnet 5.5 committed 2026-10-16 09:49:11 -04:00
1 parent 74802efd0e
commit 28b3a8b572
6 files changed
+115 -7

No files matched your search

+46 -2
View File
@@ -1,12 +1,17 @@
//! MCP resources. Resolved live on every read; not subscribable.
use rmcp::model::{ReadResourceResult, Resource, ResourceContents};
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 UNIT_URI_PREFIX: &str = "systemd://unit/";
pub const CONTAINER_URI_PREFIX: &str = "docker://container/";
/// Resources advertised by `resources/list`.
pub fn list() -> Vec<Resource> {
@@ -22,6 +27,34 @@ pub fn list() -> Vec<Resource> {
]
}
/// 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()))
}
/// Read a resource by URI, querying the source on each call.
pub async fn read(state: &AppState, uri: &str) -> Result<ReadResourceResult, AppError> {
let json = match uri {
@@ -33,7 +66,18 @@ pub async fn read(state: &AppState, uri: &str) -> Result<ReadResourceResult, App
let cs = state.docker.ps(true).await?;
serde_json::to_string_pretty(&cs)
}
other => return Err(AppError::NotFound(format!("unknown resource '{other}'"))),
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(ReadResourceResult::new(vec![