Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
fix(mcp): onto_lint and onto_diff read RDF/XML and relative-IRI Turtle like the CLI does
Vishal Habibc6b76f3a8d7b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 28a447e6..32294a37 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -34,6 +34,16 @@ All notable changes to Open Ontologies are documented here.`python/tests/test_conftest_collection_skip.py` holds it, with a control runwithout the variable.+- **MCP `onto_lint` and `onto_diff`, and CLI `diff`, read RDF/XML and+ relative-IRI Turtle.** They passed the raw file to the Turtle parser, so an+ RDF/XML `.owl` file failed with "Invalid IRI code point" and `<#Dog> a+ owl:Class .` failed with "No scheme found in an absolute IRI", while CLI+ `lint`, `onto_load` and `onto_validate` accepted the same files. They now+ read through `GraphStore::read_as_turtle`, as CLI `lint` does. A diff+ resolves both files against the old file's location, so `<#Dog>` is one IRI+ in both versions and adding a class reports `added: 1, removed: 0`, not+ every triple. A file CLI `diff` cannot read is reported as JSON naming it+ (`Cannot read <path>: …`), as `onto_diff` does.- **`owl-dl` classifies Pizza exactly as HermiT does, and says when it is notcomplete.** On the canonical `pizza.owl` it returned no subsumptions and 143undetermined classes; it now returns HermiT 1.4.3's 500 subsumptions and itsdiff --git a/src/main.rs b/src/main.rsindex 0f166502..189f4816 100644--- a/src/main.rs+++ b/src/main.rs@@ -2754,8 +2754,17 @@ async fn async_main() -> anyhow::Result<()> {}Commands::Diff { old_path, new_path } => {use open_ontologies::ontology::OntologyService;- let old = std::fs::read_to_string(&old_path)?;- let new = std::fs::read_to_string(&new_path)?;+ // Read both sides as lint does: an RDF/XML file is converted rather+ // than handed to the Turtle parser, and errors stay JSON.+ let read = |path: &str| match GraphStore::read_as_turtle(path) {+ Ok(c) => c,+ Err(e) => {+ output_json(&serde_json::json!({"error": e.to_string()}), cli.pretty);+ std::process::exit(1);+ }+ };+ let old = read(&old_path);+ let new = read(&new_path);let result = OntologyService::diff(&old, &new).unwrap_or_else(|e| format!(r#"{{"error":"{}"}}"#, e));output_result_checked(&result, cli.pretty);diff --git a/src/server.rs b/src/server.rsindex 74aa315f..740b3343 100644--- a/src/server.rs+++ b/src/server.rs@@ -550,13 +550,15 @@ impl OpenOntologiesServer {}#[tool(name = "onto_diff", description = "Compare two ontology files and show added/removed triples")]- async fn onto_diff(&self, Parameters(input): Parameters<OntoDiffInput>) -> String {+ pub async fn onto_diff(&self, Parameters(input): Parameters<OntoDiffInput>) -> String {use crate::ontology::OntologyService;- let old = match std::fs::read_to_string(&input.old_path) {+ // Same reading as `onto_load` and CLI `lint`: sniff the format, convert+ // RDF/XML and friends, and give Turtle the file's base IRI.+ let old = match GraphStore::read_as_turtle(&input.old_path) {Ok(c) => c,Err(e) => return Self::err_json(format!("Cannot read {}: {}", input.old_path, e)),};- let new = match std::fs::read_to_string(&input.new_path) {+ let new = match GraphStore::read_as_turtle(&input.new_path) {Ok(c) => c,Err(e) => return Self::err_json(format!("Cannot read {}: {}", input.new_path, e)),};@@ -564,12 +566,14 @@ impl OpenOntologiesServer {}#[tool(name = "onto_lint", description = "Check an ontology for quality issues: missing labels, comments, domains, ranges")]- async fn onto_lint(&self, Parameters(input): Parameters<OntoLintInput>) -> String {+ pub async fn onto_lint(&self, Parameters(input): Parameters<OntoLintInput>) -> String {use crate::ontology::OntologyService;let content = if input.inline.unwrap_or(false) {input.input.clone()} else {- match std::fs::read_to_string(&input.input) {+ // As CLI `lint` does: an RDF/XML file is converted, not handed to+ // the Turtle parser, and relative IRIs resolve against the file.+ match GraphStore::read_as_turtle(&input.input) {Ok(c) => c,Err(e) => return Self::err_json(e),}diff --git a/tests/mcp_lint_diff_formats_test.rs b/tests/mcp_lint_diff_formats_test.rsnew file mode 100644index 00000000..c961eadc--- /dev/null+++ b/tests/mcp_lint_diff_formats_test.rs@@ -0,0 +1,118 @@+//! The MCP `onto_lint` and `onto_diff` handlers must read a file the way+//! `onto_load` and CLI `lint` do.+//!+//! They used to pass the raw file to the Turtle parser, so an RDF/XML `.owl`+//! file failed with "Invalid IRI code point" and Turtle with relative IRIs+//! failed with "No scheme found in an absolute IRI", while CLI `lint` and+//! `onto_validate` accepted the same files.++use open_ontologies::graph::GraphStore;+use open_ontologies::inputs::{OntoDiffInput, OntoLintInput};+use open_ontologies::server::OpenOntologiesServer;+use open_ontologies::state::StateDb;+use rmcp::handler::server::wrapper::Parameters;+use std::sync::Arc;++const TURTLE: &str = r#"+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+ @prefix ex: <http://ex.org/> .+ ex:Animal a owl:Class ; rdfs:label "Animal" .+ ex:Dog a owl:Class ; rdfs:subClassOf ex:Animal .+"#;++/// The same two classes as `TURTLE`, serialised as RDF/XML.+const RDF_XML: &str = r#"<?xml version="1.0" encoding="UTF-8"?>+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"+ xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">+ <rdf:Description rdf:about="http://ex.org/Animal">+ <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>+ <rdfs:label>Animal</rdfs:label>+ </rdf:Description>+ <rdf:Description rdf:about="http://ex.org/Dog">+ <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>+ <rdfs:subClassOf rdf:resource="http://ex.org/Animal"/>+ </rdf:Description>+</rdf:RDF>+"#;++fn server() -> (tempfile::TempDir, OpenOntologiesServer) {+ let tmp = tempfile::tempdir().unwrap();+ let db = StateDb::open(&tmp.path().join("state.db")).unwrap();+ let server = OpenOntologiesServer::new_with_graph(db, Arc::new(GraphStore::new()));+ (tmp, server)+}++fn write(dir: &tempfile::TempDir, name: &str, content: &str) -> String {+ let path = dir.path().join(name);+ std::fs::write(&path, content).unwrap();+ path.to_str().unwrap().to_string()+}++async fn lint(server: &OpenOntologiesServer, path: &str) -> serde_json::Value {+ let out = server+ .onto_lint(Parameters(OntoLintInput {+ input: path.to_string(),+ inline: None,+ }))+ .await;+ serde_json::from_str(&out).unwrap()+}++#[tokio::test]+async fn onto_lint_reads_rdf_xml_like_turtle() {+ let (tmp, server) = server();+ let ttl = write(&tmp, "a.ttl", TURTLE);+ let owl = write(&tmp, "a.owl", RDF_XML);++ let from_ttl = lint(&server, &ttl).await;+ let from_owl = lint(&server, &owl).await;+ assert!(+ from_owl.get("error").is_none(),+ "onto_lint failed on RDF/XML: {from_owl}"+ );+ assert_eq!(+ from_owl["issue_count"], from_ttl["issue_count"],+ "{from_owl}"+ );+}++#[tokio::test]+async fn onto_lint_resolves_relative_iris_against_the_file() {+ let (tmp, server) = server();+ let rel = write(+ &tmp,+ "rel.ttl",+ "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n<#Dog> a owl:Class .\n",+ );++ let out = lint(&server, &rel).await;+ assert!(+ out.get("error").is_none(),+ "onto_lint failed on relative IRIs: {out}"+ );+ assert!(out["issue_count"].as_u64().unwrap() > 0, "{out}");+}++#[tokio::test]+async fn onto_diff_reads_rdf_xml_like_turtle() {+ let (tmp, server) = server();+ let ttl = write(&tmp, "a.ttl", TURTLE);+ let owl = write(&tmp, "a.owl", RDF_XML);++ let out: serde_json::Value = serde_json::from_str(+ &server+ .onto_diff(Parameters(OntoDiffInput {+ old_path: owl,+ new_path: ttl,+ }))+ .await,+ )+ .unwrap();+ assert!(+ out.get("error").is_none(),+ "onto_diff failed on RDF/XML: {out}"+ );+ assert_eq!(out["added"], 0, "{out}");+ assert_eq!(out["removed"], 0, "{out}");+}