fix(mcp): preserve DML results and returning rows
This commit is contained in:
1 parent
473ef910b6
commit
2e9dcdc26b
8 files changed
+356
-11
No files matched your search
@@ -616,8 +616,10 @@ async fn execute_execute_query(
|
||||
|
||||
/// Format a QueryResult as a Markdown table for LLM consumption.
|
||||
fn format_query_result_as_text(result: &QueryResult, limit: usize) -> Result<String, String> {
|
||||
if result.rows.is_empty() {
|
||||
return Ok("Query returned 0 rows.".to_string());
|
||||
// A result without columns is a command result, not an empty result set.
|
||||
// This is how drivers represent DML that does not use RETURNING.
|
||||
if result.columns.is_empty() {
|
||||
return Ok(format!("Query executed. {} row(s) affected.", result.affected_rows));
|
||||
}
|
||||
|
||||
let mut lines = Vec::new();
|
||||
@@ -1135,6 +1137,50 @@ for line in sys.stdin:
|
||||
assert!(names.contains(&"explain_query"));
|
||||
}
|
||||
|
||||
fn query_result(columns: Vec<&str>, rows: Vec<Vec<serde_json::Value>>, affected_rows: u64) -> QueryResult {
|
||||
QueryResult {
|
||||
columns: columns.into_iter().map(str::to_string).collect(),
|
||||
column_types: Vec::new(),
|
||||
column_sortables: Vec::new(),
|
||||
spatial_columns: Vec::new(),
|
||||
spatial_values: Vec::new(),
|
||||
rows,
|
||||
affected_rows,
|
||||
execution_time_ms: 1,
|
||||
truncated: false,
|
||||
session_id: None,
|
||||
has_more: false,
|
||||
elasticsearch_raw_body: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_result_formatter_reports_dml_affected_rows() {
|
||||
let result = query_result(vec![], vec![], 2);
|
||||
|
||||
assert_eq!(format_query_result_as_text(&result, 50).unwrap(), "Query executed. 2 row(s) affected.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_result_formatter_distinguishes_zero_row_dml_from_an_empty_result_set() {
|
||||
let dml = query_result(vec![], vec![], 0);
|
||||
let returning = query_result(vec!["id", "name"], vec![], 0);
|
||||
|
||||
assert_eq!(format_query_result_as_text(&dml, 50).unwrap(), "Query executed. 0 row(s) affected.");
|
||||
assert_eq!(format_query_result_as_text(&returning, 50).unwrap(), "| id | name |\n|---|---|\n(0 rows, 1ms)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_result_formatter_renders_returning_rows() {
|
||||
let result =
|
||||
query_result(vec!["id", "name"], vec![vec![serde_json::json!(5), serde_json::json!("returning")]], 0);
|
||||
|
||||
assert_eq!(
|
||||
format_query_result_as_text(&result, 50).unwrap(),
|
||||
"| id | name |\n|---|---|\n| 5 | returning |\n(1 rows, 1ms)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_data_sql_uses_database_identifier_and_limit_syntax() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user