From 05f702a9816ff670d64ae44ab37047d0ea6159ff Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Tue, 10 Nov 2026 01:55:57 -0500 Subject: [PATCH] Sanity: read datasets listed as elements named after them Sanity's server answers list_datasets with a sentence and XML in which each dataset is an element named after it, with no name field. Every project's datasets read as none, so the picker said Sanity listed no datasets. The names are now read from those elements, and a query answer in Sanity's documents format is covered by a test. --- .../src/knowledge/sources/sanity_mcp.rs | 71 ++++++++++++++++++- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/services/common/src/knowledge/sources/sanity_mcp.rs b/services/common/src/knowledge/sources/sanity_mcp.rs index 76f45a05a..224637041 100644 --- a/services/common/src/knowledge/sources/sanity_mcp.rs +++ b/services/common/src/knowledge/sources/sanity_mcp.rs @@ -661,10 +661,23 @@ fn dataset_names(v: &Value) -> Vec { }) { push(m["name"].as_str().unwrap_or("")); } - if let Some(a) = find_key(v, "datasets").and_then(Value::as_array) { - for n in a.iter().filter_map(Value::as_str) { - push(n); + match find_key(v, "datasets") { + Some(Value::Array(a)) => { + for n in a.iter().filter_map(Value::as_str) { + push(n); + } + } + // Sanity's server answers in XML with each dataset as an element + // named after it (`public + // ...`): the names are the keys. + Some(Value::Object(m)) => { + for (name, d) in m { + if d.get("datasetProfile").and_then(Value::as_str) != Some("comments") { + push(name); + } + } } + _ => {} } out.sort(); out @@ -829,6 +842,58 @@ mod tests { } } + /// Sanity's server, as it answered a real project: a sentence, then XML + /// with each dataset as an element named after it. + #[tokio::test] + async fn datasets_named_by_their_elements_are_listed() { + let reply = "Here are the datasets: \n\n \n public\n 2024-01-08T21:23:57.066Z\n \n \n private\n 2024-02-01T10:00:00.000Z\n \n \n private\n comments\n \n"; + let v = super::super::mcp::data_in(reply).expect("the XML is read"); + assert_eq!(dataset_names(&v), vec!["production", "staging"]); + + let mcp = server(move |tool, _| match tool { + "list_datasets" => Ok(json!({"content": [{"type": "text", "text": reply}]})), + _ => Ok(text_result(&json!([]))), + }); + let c = SanityMcp { + tools: mcp, + now: 0, + hints: Vec::new(), + }; + let nodes = c + .browse(Some("project:abc123"), "") + .await + .expect("the datasets are listed"); + let titles: Vec<&str> = nodes.iter().map(|n| n.title.as_str()).collect(); + assert_eq!(titles, vec!["production", "staging"]); + } + + /// A query answered the way Sanity's server formats it: a sentence, then + /// `` holding the JSON of the one result. + #[tokio::test] + async fn a_query_answered_in_sanitys_xml_is_read() { + let mcp = server(|tool, _| match tool { + "query_documents" => Ok(json!({"content": [{"type": "text", "text": + "Query executed successfully. Found 1 total document, returning 1 (40 tokens): \n\n {\n \"chTypes\": [\n \"post\",\n \"venue\"\n ]\n}\n\n1\n1\n"}]})), + _ => Ok(text_result(&json!([]))), + }); + let c = SanityMcp { + tools: mcp, + now: 0, + hints: Vec::new(), + }; + let v = c + .query( + "abc123", + "production", + "chTypes", + "array::unique(*[]._type)", + json!({}), + ) + .await + .expect("the answer is read"); + assert_eq!(v, json!(["post", "venue"])); + } + fn doc(id: &str, rev: &str, at: &str) -> Value { json!({"_id": id, "_type": "post", "_rev": rev, "_updatedAt": at, "title": format!("Post {id}")}) }