Skip to content

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

:art: Improve MCP SQL row limit visibility https://github.com/siyuan-note/siyuan/issues/19108

Daniel1e9580315e09
diff --git a/kernel/mcp/tools/sql.go b/kernel/mcp/tools/sql.go
index 53a28f2c12..d7a37d081a 100644
--- a/kernel/mcp/tools/sql.go
+++ b/kernel/mcp/tools/sql.go
@@ -24,14 +24,16 @@ import (
"github.com/siyuan-note/siyuan/kernel/sql"
)
+const sqlQueryDefaultLimit = 100
+
var SQLTool = &Tool{
Name: "sql",
- Description: "Read-only SQL on SiYuan's database. Action: query(stmt) — SELECT only.",
+ Description: "Read-only SQL on SiYuan's database. Action: query(stmt) — SELECT only. Results default to at most 100 rows; use explicit LIMIT and OFFSET clauses for pagination.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
"action": {Type: "string", Description: "Operation", Enum: []string{"query"}},
- "stmt": {Type: "string", Description: "SQL SELECT statement"},
+ "stmt": {Type: "string", Description: "SQL SELECT statement. Results default to at most 100 rows; use LIMIT and OFFSET for pagination"},
"notebook": {Type: "string", Description: "Optional notebook ID used to query an encrypted notebook"},
},
Required: []string{"action", "stmt"},
@@ -84,13 +86,16 @@ func sqlQuery(args map[string]any) (CallToolResult, error) {
}
var rows []map[string]any
+ var possiblyTruncated bool
var err error
if boxID == "" {
- rows, err = sql.Query(stmt, 100)
+ rows, err = sql.Query(stmt, sqlQueryDefaultLimit)
+ possiblyTruncated = len(rows) == sqlQueryDefaultLimit
} else {
rows, err = sql.QueryNoLimitInBox(stmt, boxID)
- if len(rows) > 100 {
- rows = rows[:100]
+ if len(rows) > sqlQueryDefaultLimit {
+ possiblyTruncated = true
+ rows = rows[:sqlQueryDefaultLimit]
}
}
if err != nil {
@@ -101,15 +106,19 @@ 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
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: formatSQLRows(rows, possiblyTruncated)}}}, nil
}
-func formatSQLRows(rows []map[string]any) string {
+func formatSQLRows(rows []map[string]any, possiblyTruncated bool) string {
columns := keysOf(rows[0])
sort.Strings(columns)
var sb strings.Builder
- sb.WriteString(fmt.Sprintf("Query results (%d rows):\n\n", len(rows)))
+ if possiblyTruncated {
+ sb.WriteString(fmt.Sprintf("Query results (%d rows; the %d-row limit may have truncated the result. Use an explicit LIMIT with OFFSET to paginate):\n\n", len(rows), sqlQueryDefaultLimit))
+ } else {
+ sb.WriteString(fmt.Sprintf("Query results (%d rows):\n\n", len(rows)))
+ }
sb.WriteString("| " + strings.Join(columns, " | ") + " |\n")
sb.WriteString("|" + strings.Repeat("---|", len(columns)) + "\n")
for _, row := range rows {
diff --git a/kernel/mcp/tools/sql_test.go b/kernel/mcp/tools/sql_test.go
index aa5357485d..1be78a61c3 100644
--- a/kernel/mcp/tools/sql_test.go
+++ b/kernel/mcp/tools/sql_test.go
@@ -16,7 +16,10 @@
package tools
-import "testing"
+import (
+ "strings"
+ "testing"
+)
func TestFormatSQLRowsKeepsValuesAlignedWithColumns(t *testing.T) {
rows := []map[string]any{
@@ -31,7 +34,21 @@ func TestFormatSQLRowsKeepsValuesAlignedWithColumns(t *testing.T) {
"| row-2 | beta | 202 |\n" +
"| row-3 | gamma | 303 |\n"
- if got := formatSQLRows(rows); got != want {
+ if got := formatSQLRows(rows, false); got != want {
t.Fatalf("unexpected SQL rows:\n%s\nwant:\n%s", got, want)
}
}
+
+func TestFormatSQLRowsReportsPossibleTruncation(t *testing.T) {
+ rows := make([]map[string]any, sqlQueryDefaultLimit)
+ for i := range rows {
+ rows[i] = map[string]any{"id": i}
+ }
+
+ got := formatSQLRows(rows, true)
+ wantPrefix := "Query results (100 rows; the 100-row limit may have truncated the result. " +
+ "Use an explicit LIMIT with OFFSET to paginate):\n\n"
+ if !strings.HasPrefix(got, wantPrefix) {
+ t.Fatalf("missing SQL result truncation warning:\n%s", got)
+ }
+}