Skip to content

World's first open-source enterprise world model.

Report get_document read failures as tool errors (#727)

mayakid127d83b8608c
diff --git a/crates/utopia-server/src/api/mcp_tests.rs b/crates/utopia-server/src/api/mcp_tests.rs
index 44afda72..a5eaab71 100644
--- a/crates/utopia-server/src/api/mcp_tests.rs
+++ b/crates/utopia-server/src/api/mcp_tests.rs
@@ -676,6 +676,11 @@ async fn failed_reads_do_not_become_successful_empty_results() -> anyhow::Result
question: None,
};
for (name, args, text) in [
+ (
+ "get_document",
+ json!({"document_id":f.document}),
+ "Could not read the document.",
+ ),
(
"find_entities",
json!({"name":"Alice"}),
@@ -750,6 +755,113 @@ async fn failed_reads_do_not_become_successful_empty_results() -> anyhow::Result
f.clean().await
}
+#[tokio::test]
+async fn document_reads_preserve_text_empty_and_unavailable_results() -> anyhow::Result<()> {
+ let Some(f) = Fixture::new().await? else {
+ return Ok(());
+ };
+ let read = f
+ .call("get_document", json!({"document_id":f.document}))
+ .await?;
+ assert_eq!(read["isError"], false);
+ let text = read["content"][0]["text"].as_str().unwrap();
+ assert!(text.contains("orchard.md"));
+ assert!(text.contains("2 section(s)"));
+ assert!(text.contains("Alice works for Acme."));
+ assert!(read.get("structuredContent").is_none());
+
+ let foreign = Uuid::now_v7();
+ let empty = Uuid::now_v7();
+ for (id, kb) in [(foreign, f.other_kb), (empty, f.kb)] {
+ sqlx::query(
+ "INSERT INTO documents(id,kb_id,filename,sha256) VALUES ($1,$2,'empty.md',repeat('1',64))",
+ )
+ .bind(id)
+ .bind(kb)
+ .execute(&f.state.pool)
+ .await?;
+ }
+ let read = f.call("get_document", json!({"document_id":empty})).await?;
+ assert_eq!(read["isError"], false);
+ assert!(read["content"][0]["text"]
+ .as_str()
+ .unwrap()
+ .contains("0 section(s):\n(no text)"));
+
+ sqlx::query("UPDATE documents SET deleted_at=now() WHERE id=$1")
+ .bind(f.document)
+ .execute(&f.state.pool)
+ .await?;
+ // Missing, foreign and deleted IDs remain indistinguishable; a read failure
+ // must not change that boundary or turn a genuinely empty document into an error.
+ for id in [Uuid::now_v7(), foreign, f.document] {
+ let result = f.call("get_document", json!({"document_id":id})).await?;
+ assert_eq!(result["isError"], false);
+ assert_eq!(
+ result["content"][0]["text"],
+ "No document with that id in this knowledge base."
+ );
+ assert!(result.get("structuredContent").is_none());
+ }
+ f.clean().await
+}
+
+#[tokio::test]
+async fn failed_document_chunks_do_not_become_a_successful_empty_document() -> anyhow::Result<()> {
+ let Some(f) = Fixture::new().await? else {
+ return Ok(());
+ };
+ let schema = format!("mcp_chunks_failure_{}", Uuid::now_v7().simple());
+ // Shadow chunks only for this connection: the document lookup succeeds but
+ // its subsequent chunk query fails, without altering tables used by other tests.
+ sqlx::raw_sql(&format!(
+ "CREATE SCHEMA {schema}; CREATE VIEW {schema}.chunks AS SELECT NULL::uuid AS id;"
+ ))
+ .execute(&f.state.pool)
+ .await?;
+ let options: sqlx::postgres::PgConnectOptions =
+ utopia_store::test_db::url().unwrap().parse()?;
+ let pool = sqlx::postgres::PgPoolOptions::new()
+ .max_connections(1)
+ .connect_with(options.options([("search_path", format!("{schema},public"))]))
+ .await?;
+ assert!(utopia_store::documents::find_in_kb(&pool, f.kb, f.document)
+ .await?
+ .is_some());
+ let mut state = f.state.clone();
+ state.pool = pool.clone();
+ let ctx = ToolCtx {
+ state: &state,
+ kb_id: f.kb,
+ workspace_id: f.ws,
+ mounted_sources: &[],
+ can_write: false,
+ actor: None,
+ via_token: None,
+ question: None,
+ };
+ let mut sink = ToolSink::default();
+ let result = tool_result(
+ tools::dispatch(
+ &ctx,
+ &mut sink,
+ "get_document",
+ &json!({"document_id":f.document}),
+ )
+ .await,
+ );
+ pool.close().await;
+ sqlx::raw_sql(&format!("DROP SCHEMA {schema} CASCADE"))
+ .execute(&f.state.pool)
+ .await?;
+ f.clean().await?;
+ assert_eq!(result["isError"], true);
+ assert_eq!(result["content"][0]["text"], "Could not read the document.");
+ assert!(result.get("structuredContent").is_none());
+ assert!(sink.sources.is_empty());
+ Ok(())
+}
+
#[test]
fn text_only_results_do_not_acquire_a_structured_payload() {
let result = tool_result(ToolResult::new("existing text".into(), json!({})));
diff --git a/crates/utopia-server/src/api/tools.rs b/crates/utopia-server/src/api/tools.rs
index f34b83fd..aba544d5 100644
--- a/crates/utopia-server/src/api/tools.rs
+++ b/crates/utopia-server/src/api/tools.rs
@@ -240,13 +240,30 @@ pub async fn get_document(
return refuse("invalid id");
};
// 本库之外的 id 一律当作不存在——分不出「没有」和「不给你看」才是对的
- let Ok(Some(doc)) = utopia_store::documents::find_in_kb(&ctx.state.pool, ctx.kb_id, id).await
- else {
- return refuse("not found");
+ let doc = match utopia_store::documents::find_in_kb(&ctx.state.pool, ctx.kb_id, id).await {
+ Ok(Some(doc)) => doc,
+ Ok(None) => return refuse("not found"),
+ Err(e) => {
+ tracing::warn!(error = %e, "MCP document lookup failed");
+ return ToolResult::new(
+ "Could not read the document.".into(),
+ json!({"kind": "document", "label": "?", "detail": "failed"}),
+ )
+ .error();
+ }
};
- let chunks = utopia_store::documents::chunks_in_document(&ctx.state.pool, ctx.kb_id, id)
- .await
- .unwrap_or_default();
+ let chunks =
+ match utopia_store::documents::chunks_in_document(&ctx.state.pool, ctx.kb_id, id).await {
+ Ok(chunks) => chunks,
+ Err(e) => {
+ tracing::warn!(error = %e, "MCP document chunk read failed");
+ return ToolResult::new(
+ "Could not read the document.".into(),
+ json!({"kind": "document", "label": doc.filename, "detail": "failed"}),
+ )
+ .error();
+ }
+ };
let mut lines = Vec::new();
let mut used = 0usize;