Skip to content

World's first open-source enterprise world model.

Reject wrong types before executing required string tool arguments (#814)

Maya Kid7a3bfb3a43b4
diff --git a/crates/utopia-server/src/api/chat.rs b/crates/utopia-server/src/api/chat.rs
index f8d6cea4..2bc7a4f5 100644
--- a/crates/utopia-server/src/api/chat.rs
+++ b/crates/utopia-server/src/api/chat.rs
@@ -208,6 +208,17 @@ pub(super) fn check_call(
),
));
}
+ let is_string =
+ function.is_some_and(|f| f["parameters"]["properties"][key]["type"] == "string");
+ if is_string && !args[key].is_string() {
+ return Err(refuse(
+ &format!("invalid {key}"),
+ format!(
+ "{name} needs `{key}`, which must be a string, so the call was not run. \
+ Call it again with `{key}` set to a string."
+ ),
+ ));
+ }
}
Ok(args)
}
@@ -1158,6 +1169,55 @@ mod tests {
// --- check_call ---------------------------------------------------------
+ #[test]
+ fn required_strings_reject_other_json_types_without_coercion() {
+ let tools = tools_schema(true, &[]);
+ for (name, key) in [("search_chunks", "query"), ("remember", "text")] {
+ for value in [
+ json!(123),
+ json!(false),
+ json!(["pressure"]),
+ json!({"text":"pressure"}),
+ ] {
+ let args = json!({key: value});
+ let err = check_call(&tools, name, &args.to_string())
+ .expect_err("a required string must be a string");
+ assert_eq!(err.1["detail"], format!("invalid {key}"));
+ assert!(err.0.contains("must be a string"));
+ }
+ }
+ for text in ["pressure", " 压力 "] {
+ let args = json!({"query":text,"limit":5});
+ assert_eq!(
+ check_call(&tools, "search_chunks", &args.to_string()).unwrap(),
+ args
+ );
+ }
+ // Keep the existing missing/UUID error precedence, even for a wrong type.
+ assert!(check_call(&tools, "get_document", r#"{"document_id":123}"#)
+ .unwrap_err()
+ .0
+ .contains("uuid"));
+ assert_eq!(
+ check_call(&tools, "search_chunks", r#"{"query":null}"#)
+ .unwrap_err()
+ .1["detail"],
+ "missing query"
+ );
+ let custom = json!([{"function":{"name":"typed_control","parameters":{
+ "required":["count","items"],"properties":{"count":{"type":"integer"},"items":{"type":"array"},"optional":{"type":"string"}}
+ }}}]);
+ let args = json!({"count":3,"items":["a"],"optional":false});
+ assert_eq!(
+ check_call(&custom, "typed_control", &args.to_string()).unwrap(),
+ args
+ );
+ assert_eq!(
+ check_call(&tools, "unknown_tool", &args.to_string()).unwrap(),
+ args
+ );
+ }
+
/// 参数在半路断掉——模型撞上 token 上限时就长这样。
///
/// **从前这里回落成空对象,然后 `search_chunks` 拿用户那句原话去检索。**
diff --git a/crates/utopia-server/src/api/mcp_tests.rs b/crates/utopia-server/src/api/mcp_tests.rs
index 7c029f37..e82b8b6b 100644
--- a/crates/utopia-server/src/api/mcp_tests.rs
+++ b/crates/utopia-server/src/api/mcp_tests.rs
@@ -280,6 +280,68 @@ async fn find_entities_returns_ranked_ids_and_keeps_text() -> anyhow::Result<()>
f.clean().await
}
+#[tokio::test]
+async fn wrong_string_types_are_refused_and_audited_without_writing_memory() -> anyhow::Result<()> {
+ let Some(mut f) = Fixture::new().await? else {
+ return Ok(());
+ };
+ let auth = utopia_store::tokens::authenticate(&f.state.pool, &f.token).await?;
+ sqlx::query("UPDATE kb_members SET role='editor' WHERE kb_id=$1 AND user_id=$2")
+ .bind(f.kb)
+ .bind(auth.user_id)
+ .execute(&f.state.pool)
+ .await?;
+ f.token = utopia_store::tokens::issue(
+ &f.state.pool,
+ auth.user_id,
+ "argument types",
+ "write",
+ Some(&[f.kb]),
+ None,
+ )
+ .await?
+ .1;
+ let mut calls = 0_i64;
+ for (name, key) in [("search_chunks", "query"), ("remember", "text")] {
+ for value in [
+ json!(123),
+ json!(false),
+ json!(["pressure"]),
+ json!({"text":"pressure"}),
+ ] {
+ let response = f.call(name, json!({key:value})).await?;
+ assert_eq!(response["isError"], true, "{response}");
+ assert!(response["content"][0]["text"]
+ .as_str()
+ .unwrap()
+ .contains("must be a string"));
+ assert!(response.get("structuredContent").is_none());
+ calls += 1;
+ let audited: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM audit_events WHERE kb_id=$1 AND action='mcp.tool_called'",
+ )
+ .bind(f.kb)
+ .fetch_one(&f.state.pool)
+ .await?;
+ assert_eq!(audited, calls);
+ }
+ }
+ let memories: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM documents WHERE kb_id=$1 AND external_key='memory:log'",
+ )
+ .bind(f.kb)
+ .fetch_one(&f.state.pool)
+ .await?;
+ assert_eq!(memories, 0);
+ let good = f.call("search_chunks", json!({"query":"orchard"})).await?;
+ assert_eq!(good["isError"], false);
+ assert!(!good["structuredContent"]["chunks"]
+ .as_array()
+ .unwrap()
+ .is_empty());
+ f.clean().await
+}
+
#[tokio::test]
async fn search_chunks_returns_chunk_and_document_ids_with_the_same_excerpt() -> anyhow::Result<()>
{