Skip to content

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

:bug: Correct MCP document search results https://github.com/siyuan-note/siyuan/issues/19105

Danielacef624771d7
diff --git a/kernel/mcp/tools/document.go b/kernel/mcp/tools/document.go
index 171159a8d6..bbdd3d1c49 100644
--- a/kernel/mcp/tools/document.go
+++ b/kernel/mcp/tools/document.go
@@ -18,6 +18,7 @@ package tools
import (
"fmt"
+ "path"
"strings"
"github.com/88250/lute/ast"
@@ -271,13 +272,26 @@ func documentSearchDocs(args map[string]any) (CallToolResult, error) {
}
var sb strings.Builder
- sb.WriteString(fmt.Sprintf("Documents matching '%s' (%d):\n\n", keyword, len(docs)))
+ sb.WriteString(fmt.Sprintf("Search results matching '%s' (%d):\n\n", keyword, len(docs)))
for _, d := range docs {
- sb.WriteString(fmt.Sprintf("- %s (id: %s, hPath: %s)\n", d["name"], d["id"], d["hPath"]))
+ typ, id, name := documentSearchDisplayFields(d)
+ if typ == "NOTEBOOK" {
+ sb.WriteString(fmt.Sprintf("- [%s] %s (id: %s, hPath: %s)\n", typ, name, id, d["hPath"]))
+ } else {
+ sb.WriteString(fmt.Sprintf("- [%s] %s (id: %s, notebook: %s, hPath: %s)\n", typ, name, id, d["box"], d["hPath"]))
+ }
}
return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
}
+func documentSearchDisplayFields(doc map[string]string) (typ, id, name string) {
+ hPath := strings.TrimSuffix(doc["hPath"], "/")
+ if doc["path"] == "/" {
+ return "NOTEBOOK", doc["box"], path.Base(hPath)
+ }
+ return "DOCUMENT", strings.TrimSuffix(path.Base(doc["path"]), ".sy"), path.Base(hPath)
+}
+
func documentInfo(args map[string]any) (CallToolResult, error) {
id, _ := args["id"].(string)
if id == "" {
diff --git a/kernel/mcp/tools/document_test.go b/kernel/mcp/tools/document_test.go
new file mode 100644
index 0000000000..8bbc235d51
--- /dev/null
+++ b/kernel/mcp/tools/document_test.go
@@ -0,0 +1,62 @@
+// 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 TestDocumentSearchDisplayFields(t *testing.T) {
+ tests := []struct {
+ name string
+ doc map[string]string
+ wantType string
+ wantID string
+ wantName string
+ }{
+ {
+ name: "document",
+ doc: map[string]string{
+ "path": "/20260903120000-parent1/20260903123000-child01.sy",
+ "hPath": "Notebook/Parent/Document",
+ "box": "20260903110000-boxid01",
+ },
+ wantType: "DOCUMENT",
+ wantID: "20260903123000-child01",
+ wantName: "Document",
+ },
+ {
+ name: "notebook",
+ doc: map[string]string{
+ "path": "/",
+ "hPath": "Notebook/",
+ "box": "20260903110000-boxid01",
+ },
+ wantType: "NOTEBOOK",
+ wantID: "20260903110000-boxid01",
+ wantName: "Notebook",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ typ, id, name := documentSearchDisplayFields(test.doc)
+ if typ != test.wantType || id != test.wantID || name != test.wantName {
+ t.Fatalf("documentSearchDisplayFields() = (%q, %q, %q), want (%q, %q, %q)",
+ typ, id, name, test.wantType, test.wantID, test.wantName)
+ }
+ })
+ }
+}