use serde_json::{json, Value};
use std::sync::OnceLock;
pub const EXTENSION: &str = "io.modelcontextprotocol/skills";
pub struct Skill { pub name: &'static str, pub description: &'static str, pub version: &'static str, pub text: &'static str }
pub const SKILLS: &[Skill] = &[
Skill { name: "inspect-model", version: "2.0.0", description: "Inspect BREP documents, units, solid validity, bounds and volume before choosing an edit.", text: include_str!("skills/inspect-model/SKILL.md") },
Skill { name: "safe-parametric-edit", version: "2.0.0", description: "Edit a BREP feature using document identity, revision checks and post-edit verification.", text: include_str!("skills/safe-parametric-edit/SKILL.md") },
Skill { name: "import-step", version: "2.0.0", description: "Import a STEP artifact into BREP and qualify the resulting solids and operation outcome.", text: include_str!("skills/import-step/SKILL.md") },
Skill { name: "recover-execution", version: "2.0.0", description: "Recover a lost BREP script response or reconcile failed and cancelled CAD work without duplicating changes.", text: include_str!("skills/recover-execution/SKILL.md") },
Skill { name: "edit-assembly", version: "2.0.0", description: "Edit BREP assembly components and constraints while preserving work-part context and component identity.", text: include_str!("skills/edit-assembly/SKILL.md") },
Skill { name: "repair-feature", version: "2.0.0", description: "Diagnose and repair a refused BREP feature using its actual schema, geometry references and refusal details.", text: include_str!("skills/repair-feature/SKILL.md") },
Skill { name: "export-model", version: "2.0.0", description: "Validate a BREP model and export it to an authorized format and destination.", text: include_str!("skills/export-model/SKILL.md") },
Skill { name: "design-part", version: "2.0.0", description: "Design a BREP part or assembly well: modelling order, expressions, stable references, checkpoints, and what the kernel refuses with the workaround for each.", text: include_str!("skills/design-part/SKILL.md") },
];
pub fn header(name: &str, description: &str, version: Option<&str>) -> String {
match version {
Some(version) => format!("---\nname: {name}\ndescription: {description}\nmetadata:\n version: \"{version}\"\n---\n"),
None => format!("---\nname: {name}\ndescription: {description}\n---\n"),
}
}
pub fn valid_version(version: &str) -> bool {
!version.is_empty() && version.len() <= 32 && version.split('.').all(|part| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit()))
}
impl Skill {
pub fn uri(&self) -> String { format!("skill://{}/SKILL.md", self.name) }
pub fn entry(&self) -> Value {
let mut frontmatter = json!({"name":self.name,"description":self.description});
if !self.version.is_empty() { frontmatter["metadata"] = json!({"version":self.version}); }
json!({"uri":self.uri(), "frontmatter":frontmatter,
"resources":[{"uri":self.uri(),"size":self.text.len(),"digest":format!("sha256:{}",crate::cad::canon::sha256_hex(self.text.as_bytes()))}]})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Source { Embedded, Installed }
pub struct Catalogue {
pub skills: Vec<&'static Skill>,
pub source: Source,
pub bundle_version: String,
pub bundle_sha256: String,
}
impl Catalogue {
pub fn embedded() -> &'static Catalogue {
static EMBEDDED: OnceLock<Catalogue> = OnceLock::new();
EMBEDDED.get_or_init(|| Catalogue {
skills: SKILLS.iter().collect(),
source: Source::Embedded,
bundle_version: env!("CARGO_PKG_VERSION").into(),
bundle_sha256: crate::skill_distribution::sha256(&crate::skill_distribution::bundle()),
})
}
pub fn get(&self, uri: &str) -> Option<&'static Skill> { self.skills.iter().copied().find(|s| s.uri() == uri) }
pub fn named(&self, name: &str) -> Option<&'static Skill> { self.skills.iter().copied().find(|s| s.name == name) }
fn meta(&self) -> Value {
json!({"io.brep/skills":{"source":match self.source { Source::Embedded => "embedded", Source::Installed => "installed" },
"bundleVersion":self.bundle_version,"bundleSha256":self.bundle_sha256}})
}
pub fn request(&self, method: &str, params: &Value) -> Result<Value, String> {
let mut result = json!({"resultType":"complete","ttlMs":300000,"cacheScope":"public"});
match method {
"skills/list" => {
if params.get("cursor").is_some() { return Err("no cursor is valid for this complete skill catalogue".into()); }
result["skills"] = self.skills.iter().map(|s| s.entry()).collect();
}
"skills/get" => result["skill"] = self.get(params["uri"].as_str().unwrap_or("")).ok_or("unknown skill URI")?.entry(),
_ => return Err("unknown skills method".into()),
}
result["_meta"] = self.meta();
Ok(result)
}
}
pub fn get(uri: &str) -> Option<&'static Skill> { Catalogue::embedded().get(uri) }
pub fn request(method: &str, params: &Value) -> Result<Value, String> { Catalogue::embedded().request(method, params) }