Skip to content

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

:sparkles: Support Model Context Protocol (MCP) https://github.com/siyuan-note/siyuan/issues/13795

Danielaea4ffb22107
diff --git a/kernel/mcp/tools/attr.go b/kernel/mcp/tools/attr.go
new file mode 100644
index 0000000000..fa6cbc1915
--- /dev/null
+++ b/kernel/mcp/tools/attr.go
@@ -0,0 +1,100 @@
+// SiYuan - Refactor your thinking
+// 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 (
+ "fmt"
+ "strings"
+
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/sql"
+)
+
+var AttrTool = &Tool{
+ Name: "attr",
+ Description: "Block attribute operations for SiYuan.\n- get: Get all custom attributes of a block. Requires: id.\n- set: Set custom attributes on a block. Requires: id, attrs (object like {\"key\":\"value\"}).",
+ InputSchema: ToolSchema{
+ Type: "object",
+ Properties: map[string]Property{
+ "action": {Type: "string", Description: "Operation", Enum: []string{"get", "set"}},
+ "id": {Type: "string", Description: "Block ID"},
+ "attrs": {Type: "object", Description: "Attribute key-value pairs (for set)"},
+ },
+ Required: []string{"action"},
+ },
+ Handler: attrHandler,
+}
+
+func init() {
+ register(AttrTool)
+}
+
+func attrHandler(args map[string]interface{}) (CallToolResult, error) {
+ action, _ := args["action"].(string)
+ switch action {
+ case "get":
+ return attrGet(args)
+ case "set":
+ return attrSet(args)
+ }
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
+ IsError: true,
+ }, nil
+}
+
+func attrGet(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+
+ attrs := sql.GetBlockAttrs(id)
+ if len(attrs) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no custom attributes"}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Attributes for %s:\n\n", id))
+ for k, v := range attrs {
+ sb.WriteString(fmt.Sprintf("- %s: %s\n", k, v))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func attrSet(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+
+ attrsArg, ok := args["attrs"].(map[string]interface{})
+ if !ok || len(attrsArg) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "attrs (key-value object) is required"}}, IsError: true}, nil
+ }
+
+ nameValues := make(map[string]string, len(attrsArg))
+ for k, v := range attrsArg {
+ nameValues[k] = fmt.Sprintf("%v", v)
+ }
+
+ if err := model.SetBlockAttrs(id, nameValues); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "set attrs failed: " + err.Error()}}, IsError: true}, nil
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "attributes set for: " + id}}}, nil
+}
diff --git a/kernel/mcp/tools/block.go b/kernel/mcp/tools/block.go
index 7b9184406e..0391166072 100644
--- a/kernel/mcp/tools/block.go
+++ b/kernel/mcp/tools/block.go
@@ -26,11 +26,11 @@ import (
var BlockTool = &Tool{
Name: "block",
- Description: "Block operations for SiYuan notes.\n- get: Get block info by ID. Requires: id.\n- get_kramdown: Get block kramdown by ID. Requires: id.\n- get_children: Get child blocks by parent ID. Requires: id.\n- insert: Insert a new block. Requires: data, dataType (markdown or dom). Optional: parentID, nextID, previousID.\n- append: Append a child block. Requires: data, dataType, parentID.\n- prepend: Prepend a child block. Requires: data, dataType, parentID.\n- update: Update a block. Requires: id, data, dataType.\n- delete: Delete a block. Requires: id.",
+ Description: "Block operations for SiYuan notes.\n- get: Get block info by ID. Requires: id.\n- get_kramdown: Get block kramdown by ID. Requires: id.\n- get_children: Get child blocks by parent ID. Requires: id.\n- insert: Insert a new block. Requires: data, dataType (markdown or dom). Optional: parentID, nextID, previousID.\n- append: Append a child block. Requires: data, dataType, parentID.\n- prepend: Prepend a child block. Requires: data, dataType, parentID.\n- update: Update a block. Requires: id, data, dataType.\n- delete: Delete a block. Requires: id.\n- move: Move a block. Requires: id, parentID. Optional: previousID.\n- breadcrumb: Get block breadcrumb path. Requires: id.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"get", "get_kramdown", "get_children", "insert", "append", "prepend", "update", "delete"}},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"get", "get_kramdown", "get_children", "insert", "append", "prepend", "update", "delete", "move", "breadcrumb"}},
"id": {Type: "string", Description: "Block ID"},
"data": {Type: "string", Description: "Content (markdown or dom)"},
"dataType": {Type: "string", Description: "Content type: markdown or dom", Enum: []string{"markdown", "dom"}},
@@ -66,6 +66,10 @@ func blockHandler(args map[string]interface{}) (CallToolResult, error) {
return blockUpdate(args)
case "delete":
return blockDelete(args)
+ case "move":
+ return blockMove(args)
+ case "breadcrumb":
+ return blockBreadcrumb(args)
}
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
@@ -301,3 +305,46 @@ func markdownToBlockDOM(md string) (string, error) {
}
return result, nil
}
+
+func blockMove(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+ parentID, _ := args["parentID"].(string)
+ if parentID == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "parentID is required"}}, IsError: true}, nil
+ }
+ previousID, _ := args["previousID"].(string)
+
+ transactions := []*model.Transaction{{
+ DoOperations: []*model.Operation{{
+ Action: "move",
+ ID: id,
+ ParentID: parentID,
+ PreviousID: previousID,
+ }},
+ }}
+
+ model.PerformTransactions(&transactions)
+ model.FlushTxQueue()
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "block moved: " + id}}}, nil
+}
+
+func blockBreadcrumb(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+
+ paths, err := model.BuildBlockBreadcrumb(id, nil)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "breadcrumb failed: " + err.Error()}}, IsError: true}, nil
+ }
+
+ var sb strings.Builder
+ for _, p := range paths {
+ sb.WriteString(fmt.Sprintf("%s/%s (%s)\n", p.Type, p.Name, p.ID))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
diff --git a/kernel/mcp/tools/bookmark.go b/kernel/mcp/tools/bookmark.go
new file mode 100644
index 0000000000..8bf24d87f0
--- /dev/null
+++ b/kernel/mcp/tools/bookmark.go
@@ -0,0 +1,108 @@
+// SiYuan - Refactor your thinking
+// 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 (
+ "fmt"
+ "strings"
+
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+var BookmarkTool = &Tool{
+ Name: "bookmark",
+ Description: "Bookmark management for SiYuan.\n- list: List all bookmarks with block counts.\n- labels: List all bookmark labels.\n- remove: Remove a bookmark. Requires: label.\n- rename: Rename a bookmark. Requires: old, new.",
+ InputSchema: ToolSchema{
+ Type: "object",
+ Properties: map[string]Property{
+ "action": {Type: "string", Description: "Operation", Enum: []string{"list", "labels", "remove", "rename"}},
+ "label": {Type: "string", Description: "Bookmark label (for remove)"},
+ "old": {Type: "string", Description: "Old label (for rename)"},
+ "new": {Type: "string", Description: "New label (for rename)"},
+ },
+ Required: []string{"action"},
+ },
+ Handler: bookmarkHandler,
+}
+
+func init() {
+ register(BookmarkTool)
+}
+
+func bookmarkHandler(args map[string]interface{}) (CallToolResult, error) {
+ action, _ := args["action"].(string)
+ switch action {
+ case "list":
+ return bookmarkList(args)
+ case "labels":
+ return bookmarkLabels(args)
+ case "remove":
+ return bookmarkRemove(args)
+ case "rename":
+ return bookmarkRename(args)
+ }
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
+ IsError: true,
+ }, nil
+}
+
+func bookmarkList(args map[string]interface{}) (CallToolResult, error) {
+ bookmarks := model.BuildBookmark()
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Bookmarks (%d):\n\n", len(*bookmarks)))
+ for _, b := range *bookmarks {
+ sb.WriteString(fmt.Sprintf("- %s: %d blocks\n", b.Name, b.Count))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func bookmarkLabels(args map[string]interface{}) (CallToolResult, error) {
+ labels := model.BookmarkLabels()
+ if len(labels) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no bookmark labels found"}}}, nil
+ }
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Bookmark labels (%d):\n\n", len(labels)))
+ for _, l := range labels {
+ sb.WriteString(fmt.Sprintf("- %s\n", l))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func bookmarkRemove(args map[string]interface{}) (CallToolResult, error) {
+ label, _ := args["label"].(string)
+ if label == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "label is required"}}, IsError: true}, nil
+ }
+ if err := model.RemoveBookmark(label); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "remove bookmark failed: " + err.Error()}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "bookmark removed: " + label}}}, nil
+}
+
+func bookmarkRename(args map[string]interface{}) (CallToolResult, error) {
+ oldLabel, _ := args["old"].(string)
+ newLabel, _ := args["new"].(string)
+ if oldLabel == "" || newLabel == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "old and new are required"}}, IsError: true}, nil
+ }
+ if err := model.RenameBookmark(oldLabel, newLabel); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "rename bookmark failed: " + err.Error()}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "bookmark renamed: " + oldLabel + " -> " + newLabel}}}, nil
+}
diff --git a/kernel/mcp/tools/database.go b/kernel/mcp/tools/database.go
new file mode 100644
index 0000000000..9bf3eae745
--- /dev/null
+++ b/kernel/mcp/tools/database.go
@@ -0,0 +1,145 @@
+// SiYuan - Refactor your thinking
+// 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 (
+ "fmt"
+ "strings"
+
+ "github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+var DatabaseTool = &Tool{
+ Name: "database",
+ Description: "Attribute view (database) operations for SiYuan.\n- search: Search for attribute views by keyword. Requires: keyword.\n- get: Get attribute view details by ID. Requires: id.\n- render: Render attribute view table. Requires: id. Optional: viewID, query, page (default 1), pageSize (default 50).",
+ InputSchema: ToolSchema{
+ Type: "object",
+ Properties: map[string]Property{
+ "action": {Type: "string", Description: "Operation", Enum: []string{"search", "get", "render"}},
+ "keyword": {Type: "string", Description: "Search keyword (for search)"},
+ "id": {Type: "string", Description: "Attribute view ID (for get, render)"},
+ "viewID": {Type: "string", Description: "View ID (for render)"},
+ "query": {Type: "string", Description: "Filter query (for render)"},
+ "page": {Type: "number", Description: "Page number (default 1)"},
+ "pageSize": {Type: "number", Description: "Results per page (default 50)"},
+ },
+ Required: []string{"action"},
+ },
+ Handler: databaseHandler,
+}
+
+func init() {
+ register(DatabaseTool)
+}
+
+func databaseHandler(args map[string]interface{}) (CallToolResult, error) {
+ action, _ := args["action"].(string)
+ switch action {
+ case "search":
+ return databaseSearch(args)
+ case "get":
+ return databaseGet(args)
+ case "render":
+ return databaseRender(args)
+ }
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
+ IsError: true,
+ }, nil
+}
+
+func databaseSearch(args map[string]interface{}) (CallToolResult, error) {
+ keyword, _ := args["keyword"].(string)
+ if keyword == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "keyword is required"}}, IsError: true}, nil
+ }
+
+ results := model.SearchAttributeView(keyword, nil)
+ if len(results) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no attribute views found"}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Attribute views matching '%s' (%d):\n\n", keyword, len(results)))
+ for _, r := range results {
+ sb.WriteString(fmt.Sprintf("- %s (id: %s, hPath: %s)\n", r.AvName, r.AvID, r.HPath))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func databaseGet(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+
+ attrView := model.GetAttributeView(id)
+ if attrView == nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "attribute view not found: " + id}}, IsError: true}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Attribute View: %s\n\n", id))
+ sb.WriteString(fmt.Sprintf("Name: %s\n", attrView.Name))
+ sb.WriteString(fmt.Sprintf("Keys (%d):\n", len(attrView.KeyValues)))
+ for _, kv := range attrView.KeyValues {
+ sb.WriteString(fmt.Sprintf("- %s (%s): %s\n", kv.Key.Name, kv.Key.Type, kv.Key.Icon))
+ }
+ sb.WriteString(fmt.Sprintf("\nViews (%d):\n", len(attrView.Views)))
+ for _, v := range attrView.Views {
+ sb.WriteString(fmt.Sprintf("- %s (%s, pageSize: %d)\n", v.Name, v.LayoutType, v.PageSize))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func databaseRender(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+ viewID, _ := args["viewID"].(string)
+ query, _ := args["query"].(string)
+ page := 1
+ if v, ok := args["page"].(float64); ok {
+ page = int(v)
+ }
+ pageSize := 50
+ if v, ok := args["pageSize"].(float64); ok {
+ pageSize = int(v)
+ }
+
+ viewable, _, err := model.RenderAttributeView("", id, viewID, query, page, pageSize, nil, false)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "render failed: " + err.Error()}}, IsError: true}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Attribute View Render (page %d):\n\n", page))
+ if viewable != nil {
+ if table, ok := viewable.(*av.Table); ok {
+ for _, row := range table.Rows {
+ vals := make([]string, 0, len(row.Cells))
+ for _, cell := range row.Cells {
+ vals = append(vals, fmt.Sprintf("%v", cell.Value))
+ }
+ sb.WriteString(strings.Join(vals, " | ") + "\n")
+ }
+ }
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
diff --git a/kernel/mcp/tools/document.go b/kernel/mcp/tools/document.go
index efbdfc8dcd..a4b46141ed 100644
--- a/kernel/mcp/tools/document.go
+++ b/kernel/mcp/tools/document.go
@@ -25,16 +25,17 @@ import (
var DocumentTool = &Tool{
Name: "document",
- Description: "Document operations for SiYuan notebooks.\n- get: Get document content by ID. Requires: id.\n- create: Create a new document. Requires: notebook, title, path (e.g. /folder/doc). Optional: markdown.\n- list: List documents in a notebook path. Requires: notebook. Optional: path (default /).\n- delete: Delete a document by ID. Requires: id.\n- rename: Rename a document by ID. Requires: id, title.",
+ Description: "Document operations for SiYuan notebooks.\n- get: Get document content by ID. Requires: id.\n- create: Create a new document. Requires: notebook, path (e.g. /folder/doc), title. Optional: markdown.\n- list: List documents in a notebook path. Requires: notebook. Optional: path (default /).\n- delete: Delete a document by ID. Requires: id.\n- rename: Rename a document by ID. Requires: id, title.\n- move: Move a document to a different notebook/path. Requires: id, notebook, path.\n- search_docs: Search documents by keyword. Requires: keyword.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"get", "create", "list", "delete", "rename"}},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"get", "create", "list", "delete", "rename", "move", "search_docs"}},
"id": {Type: "string", Description: "Document block ID"},
- "notebook": {Type: "string", Description: "Notebook ID (required for create, list)"},
"title": {Type: "string", Description: "Document title (for create, rename)"},
- "path": {Type: "string", Description: "Document path like /folder/doc (for create, list)"},
+ "path": {Type: "string", Description: "Document path like /folder/doc (for create, list, move)"},
"markdown": {Type: "string", Description: "Initial markdown content (for create)"},
+ "keyword": {Type: "string", Description: "Search keyword (for search_docs)"},
+ "notebook": {Type: "string", Description: "Notebook ID (required for create, list, move)"},
},
Required: []string{"action"},
},
@@ -58,6 +59,10 @@ func documentHandler(args map[string]interface{}) (CallToolResult, error) {
return documentDelete(args)
case "rename":
return documentRename(args)
+ case "move":
+ return documentMove(args)
+ case "search_docs":
+ return documentSearchDocs(args)
}
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
@@ -170,3 +175,42 @@ func documentRename(args map[string]interface{}) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("document renamed: %s -> %s", id, title)}}}, nil
}
+
+func documentMove(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ notebook, _ := args["notebook"].(string)
+ path, _ := args["path"].(string)
+ if id == "" || notebook == "" || path == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id, notebook and path are required"}}, IsError: true}, nil
+ }
+
+ tree, err := model.LoadTreeByBlockID(id)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("load doc failed: %s", err)}}, IsError: true}, nil
+ }
+
+ if err := model.MoveDocs([]string{tree.Path}, notebook, path, nil); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("move doc failed: %s", err)}}, IsError: true}, nil
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("document moved: %s -> %s%s", id, notebook, path)}}}, nil
+}
+
+func documentSearchDocs(args map[string]interface{}) (CallToolResult, error) {
+ keyword, _ := args["keyword"].(string)
+ if keyword == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "keyword is required"}}, IsError: true}, nil
+ }
+
+ docs := model.SearchDocs(keyword, false, nil)
+ if len(docs) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no documents found"}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Documents 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"]))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
diff --git a/kernel/mcp/tools/export.go b/kernel/mcp/tools/export.go
new file mode 100644
index 0000000000..9f786aa10e
--- /dev/null
+++ b/kernel/mcp/tools/export.go
@@ -0,0 +1,67 @@
+// SiYuan - Refactor your thinking
+// 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 (
+ "fmt"
+
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+var ExportTool = &Tool{
+ Name: "export",
+ Description: "Export operations for SiYuan.\n- md: Export a document as Markdown. Requires: id.",
+ InputSchema: ToolSchema{
+ Type: "object",
+ Properties: map[string]Property{
+ "action": {Type: "string", Description: "Operation", Enum: []string{"md"}},
+ "id": {Type: "string", Description: "Document block ID"},
+ },
+ Required: []string{"action", "id"},
+ },
+ Handler: exportHandler,
+}
+
+func init() {
+ register(ExportTool)
+}
+
+func exportHandler(args map[string]interface{}) (CallToolResult, error) {
+ action, _ := args["action"].(string)
+ switch action {
+ case "md":
+ return exportMd(args)
+ }
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
+ IsError: true,
+ }, nil
+}
+
+func exportMd(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+
+ hPath, content := model.ExportMarkdownContent(id, 4, 0, true, false, false, false, false)
+ if content == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export failed or empty"}}, IsError: true}, nil
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("# %s\n\n%s", hPath, content)}}}, nil
+}
diff --git a/kernel/mcp/tools/history.go b/kernel/mcp/tools/history.go
new file mode 100644
index 0000000000..3d5e2a9978
--- /dev/null
+++ b/kernel/mcp/tools/history.go
@@ -0,0 +1,108 @@
+// SiYuan - Refactor your thinking
+// 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 (
+ "fmt"
+ "strings"
+
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+var HistoryTool = &Tool{
+ Name: "history",
+ Description: "Document history operations for SiYuan.\n- list: Search document history. Optional: query, notebook, op (delete/update/create), type (0=name,1=content,2=asset,3=docID,4=database), page.\n- get: Get historical content of a document by history path. Requires: path.",
+ InputSchema: ToolSchema{
+ Type: "object",
+ Properties: map[string]Property{
+ "action": {Type: "string", Description: "Operation", Enum: []string{"list", "get"}},
+ "query": {Type: "string", Description: "Search query (for list)"},
+ "notebook": {Type: "string", Description: "Notebook ID filter (for list)"},
+ "op": {Type: "string", Description: "Operation filter: delete/update/create (for list)"},
+ "type": {Type: "number", Description: "Search type: 0=name,1=content,2=asset,3=docID,4=database (default 1)"},
+ "page": {Type: "number", Description: "Page number (default 1)"},
+ "path": {Type: "string", Description: "History path (for get)"},
+ },
+ Required: []string{"action"},
+ },
+ Handler: historyHandler,
+}
+
+func init() {
+ register(HistoryTool)
+}
+
+func historyHandler(args map[string]interface{}) (CallToolResult, error) {
+ action, _ := args["action"].(string)
+ switch action {
+ case "list":
+ return historyList(args)
+ case "get":
+ return historyGet(args)
+ }
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
+ IsError: true,
+ }, nil
+}
+
+func historyList(args map[string]interface{}) (CallToolResult, error) {
+ query, _ := args["query"].(string)
+ notebook, _ := args["notebook"].(string)
+ op, _ := args["op"].(string)
+ typ := 1
+ if v, ok := args["type"].(float64); ok {
+ typ = int(v)
+ }
+ page := 1
+ if v, ok := args["page"].(float64); ok {
+ page = int(v)
+ }
+
+ results, pageCount, totalCount := model.FullTextSearchHistory(query, notebook, op, typ, page)
+ if len(results) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no history found"}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("History (%d total, page %d/%d):\n\n", totalCount, page, pageCount))
+ for _, ts := range results {
+ items := model.FullTextSearchHistoryItems(ts, query, notebook, op, typ)
+ sb.WriteString(fmt.Sprintf("--- %s (%d items) ---\n", ts, len(items)))
+ for _, item := range items {
+ sb.WriteString(fmt.Sprintf(" - [%s] %s (path: %s)\n", item.Op, item.Title, item.Path))
+ }
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func historyGet(args map[string]interface{}) (CallToolResult, error) {
+ path, _ := args["path"].(string)
+ if path == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "path is required"}}, IsError: true}, nil
+ }
+
+ _, _, content, _, err := model.GetDocHistoryContent(path, "", false)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "get history failed: " + err.Error()}}, IsError: true}, nil
+ }
+ if content == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no content"}}, IsError: true}, nil
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: content}}}, nil
+}
diff --git a/kernel/mcp/tools/ref.go b/kernel/mcp/tools/ref.go
new file mode 100644
index 0000000000..193f2f5c13
--- /dev/null
+++ b/kernel/mcp/tools/ref.go
@@ -0,0 +1,99 @@
+// SiYuan - Refactor your thinking
+// 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 (
+ "fmt"
+ "strings"
+
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+var RefTool = &Tool{
+ Name: "ref",
+ Description: "Reference and backlink operations for SiYuan.\n- backlinks: Get backlinks for a block/document. Requires: id. Optional: keyword.\n- mentions: Get mentions of a block/document. Requires: id. Optional: keyword.\n- refresh: Refresh backlink index for a block/document. Requires: id.",
+ InputSchema: ToolSchema{
+ Type: "object",
+ Properties: map[string]Property{
+ "action": {Type: "string", Description: "Operation", Enum: []string{"backlinks", "mentions", "refresh"}},
+ "id": {Type: "string", Description: "Block ID"},
+ "keyword": {Type: "string", Description: "Filter by keyword"},
+ },
+ Required: []string{"action", "id"},
+ },
+ Handler: refHandler,
+}
+
+func init() {
+ register(RefTool)
+}
+
+func refHandler(args map[string]interface{}) (CallToolResult, error) {
+ action, _ := args["action"].(string)
+ switch action {
+ case "backlinks":
+ return refBacklinks(args)
+ case "mentions":
+ return refMentions(args)
+ case "refresh":
+ return refRefresh(args)
+ }
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
+ IsError: true,
+ }, nil
+}
+
+func refBacklinks(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ keyword, _ := args["keyword"].(string)
+
+ _, backlinks, _, _, _ := model.GetBacklink2(id, keyword, "", 0, 0, false)
+ if len(backlinks) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no backlinks found"}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Backlinks (%d):\n\n", len(backlinks)))
+ for _, p := range backlinks {
+ sb.WriteString(fmt.Sprintf("- [%s] %s (id: %s)\n", p.NodeType, p.HPath, p.ID))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func refMentions(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ keyword, _ := args["keyword"].(string)
+
+ _, _, mentions, _, _ := model.GetBacklink2(id, "", keyword, 0, 0, false)
+ if len(mentions) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no mentions found"}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Mentions (%d):\n\n", len(mentions)))
+ for _, p := range mentions {
+ sb.WriteString(fmt.Sprintf("- [%s] %s (id: %s)\n", p.NodeType, p.HPath, p.ID))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func refRefresh(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ model.RefreshBacklink(id)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "backlink refreshed for: " + id}}}, nil
+}