use rustango::mcp::{McpContext, McpError};
mod tools_read;
mod tools_templates;
mod tools_write;
pub const MCP_PREFIX: &str = "/cms-admin/mcp";
#[must_use]
pub fn router(settings: &rustango::config::McpSettings) -> axum::Router {
axum::Router::new().nest(
MCP_PREFIX,
rustango::mcp::secure_tenant_router_from_settings(settings),
)
}
pub(crate) const FORBIDDEN: i64 = -32003;
pub struct ToolActor {
pub id: i64,
pub username: String,
pub is_superuser: bool,
}
impl ToolActor {
pub(crate) fn as_page_edit_actor(&self) -> crate::admin::PageEditActor {
crate::admin::PageEditActor {
id: self.id,
username: self.username.clone(),
is_superuser: self.is_superuser,
}
}
}
pub async fn require_actor(ctx: &McpContext) -> Result<ToolActor, McpError> {
use rustango::core::Column as _;
use rustango::tenancy::auth::User;
let Some(uid) = ctx.agent.user_id else {
return Err(McpError::new(
FORBIDDEN,
"CMS tools require a user-owned key (create one under \
Account → MCP keys); standalone machine agents are not \
accepted",
));
};
let user = User::objects()
.where_(User::id.eq(uid))
.first(&ctx.pool)
.await
.map_err(|e| McpError::internal(format!("actor lookup: {e}")))?
.filter(|u| u.active)
.ok_or_else(|| McpError::new(FORBIDDEN, "key owner is missing or deactivated"))?;
Ok(ToolActor {
id: uid,
username: user.username,
is_superuser: user.is_superuser,
})
}
pub async fn require_codename(
pool: &rustango::sql::Pool,
actor: &ToolActor,
codename: &str,
) -> Result<(), McpError> {
require_any_codename(pool, actor, &[codename]).await
}
pub async fn require_any_codename(
pool: &rustango::sql::Pool,
actor: &ToolActor,
codenames: &[&str],
) -> Result<(), McpError> {
if actor.is_superuser {
return Ok(());
}
let names = crate::permissions::user_codenames(pool, actor.id)
.await
.map_err(|e| McpError::internal(format!("permission lookup: {e}")))?;
if codenames.iter().any(|c| names.contains(*c)) {
Ok(())
} else {
Err(McpError::new(
FORBIDDEN,
format!("permission `{}` required", codenames.join("` or `")),
))
}
}
pub(crate) async fn require_page_action(
pool: &rustango::sql::Pool,
actor: &ToolActor,
page_id: i64,
action: crate::permissions::Action,
) -> Result<(), McpError> {
let allowed = crate::permissions::user_can(pool, actor.id, page_id, action)
.await
.map_err(|e| McpError::internal(format!("permission lookup: {e}")))?;
if allowed {
Ok(())
} else {
Err(McpError::new(
FORBIDDEN,
format!("action `{}` on page {page_id} denied", action.as_str()),
))
}
}
pub(crate) fn mint_uuid4() -> String {
let mut b = [0u8; 16];
let _ = getrandom::fill(&mut b);
b[6] = (b[6] & 0x0f) | 0x40;
b[8] = (b[8] & 0x3f) | 0x80;
let h = |r: std::ops::Range<usize>| {
b[r].iter().fold(String::new(), |mut acc, x| {
use std::fmt::Write as _;
let _ = write!(acc, "{x:02x}");
acc
})
};
format!(
"{}-{}-{}-{}-{}",
h(0..4),
h(4..6),
h(6..8),
h(8..10),
h(10..16)
)
}
pub(crate) fn value_to_form_string(v: &serde_json::Value) -> Option<String> {
match v {
serde_json::Value::Null => None,
serde_json::Value::Bool(true) => Some("on".to_owned()),
serde_json::Value::Bool(false) => None,
serde_json::Value::String(s) => Some(s.clone()),
serde_json::Value::Number(n) => Some(n.to_string()),
arr_or_obj => serde_json::to_string(arr_or_obj).ok(),
}
}