Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Align MCP SQL result columns with headers https://github.com/siyuan-note/siyuan/issues/19107

Daniel0b539108871a
diff --git a/kernel/mcp/tools/sql.go b/kernel/mcp/tools/sql.go
index 91b6d2b58d..53a28f2c12 100644
--- a/kernel/mcp/tools/sql.go
+++ b/kernel/mcp/tools/sql.go
@@ -18,6 +18,7 @@ package tools
import (
"fmt"
+ "sort"
"strings"
"github.com/siyuan-note/siyuan/kernel/sql"
@@ -100,18 +101,25 @@ func sqlQuery(args map[string]any) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no results"}}}, nil
}
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: formatSQLRows(rows)}}}, nil
+}
+
+func formatSQLRows(rows []map[string]any) string {
+ columns := keysOf(rows[0])
+ sort.Strings(columns)
+
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Query results (%d rows):\n\n", len(rows)))
- sb.WriteString("| " + strings.Join(keysOf(rows[0]), " | ") + " |\n")
- sb.WriteString("|" + strings.Repeat("---|", len(rows[0])) + "\n")
+ sb.WriteString("| " + strings.Join(columns, " | ") + " |\n")
+ sb.WriteString("|" + strings.Repeat("---|", len(columns)) + "\n")
for _, row := range rows {
- vals := make([]string, 0, len(row))
- for _, k := range keysOf(row) {
+ vals := make([]string, 0, len(columns))
+ for _, k := range columns {
vals = append(vals, fmt.Sprintf("%v", row[k]))
}
sb.WriteString("| " + strings.Join(vals, " | ") + " |\n")
}
- return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+ return sb.String()
}
func keysOf(m map[string]any) []string {
diff --git a/kernel/mcp/tools/sql_test.go b/kernel/mcp/tools/sql_test.go
new file mode 100644
index 0000000000..aa5357485d
--- /dev/null
+++ b/kernel/mcp/tools/sql_test.go
@@ -0,0 +1,37 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package tools
+
+import "testing"
+
+func TestFormatSQLRowsKeepsValuesAlignedWithColumns(t *testing.T) {
+ rows := []map[string]any{
+ {"id": "row-1", "label": "alpha", "quantity": 101},
+ {"id": "row-2", "label": "beta", "quantity": 202},
+ {"id": "row-3", "label": "gamma", "quantity": 303},
+ }
+ want := "Query results (3 rows):\n\n" +
+ "| id | label | quantity |\n" +
+ "|---|---|---|\n" +
+ "| row-1 | alpha | 101 |\n" +
+ "| row-2 | beta | 202 |\n" +
+ "| row-3 | gamma | 303 |\n"
+
+ if got := formatSQLRows(rows); got != want {
+ t.Fatalf("unexpected SQL rows:\n%s\nwant:\n%s", got, want)
+ }
+}