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

Danielb3b2b12a0967
diff --git a/kernel/mcp/tools/attr.go b/kernel/mcp/tools/attr.go
index fa6cbc1915..be6c502610 100644
--- a/kernel/mcp/tools/attr.go
+++ b/kernel/mcp/tools/attr.go
@@ -26,12 +26,13 @@ import (
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\"}).",
+ 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\"}).\n- batch-get: Batch get attributes for multiple blocks. Requires: ids (comma-separated block IDs).",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"get", "set"}},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"get", "set", "batch-get"}},
"id": {Type: "string", Description: "Block ID"},
+ "ids": {Type: "string", Description: "Comma-separated block IDs (for batch-get)"},
"attrs": {Type: "object", Description: "Attribute key-value pairs (for set)"},
},
Required: []string{"action"},
@@ -50,6 +51,8 @@ func attrHandler(args map[string]interface{}) (CallToolResult, error) {
return attrGet(args)
case "set":
return attrSet(args)
+ case "batch-get":
+ return attrBatchGet(args)
}
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
@@ -98,3 +101,29 @@ func attrSet(args map[string]interface{}) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "attributes set for: " + id}}}, nil
}
+
+func attrBatchGet(args map[string]interface{}) (CallToolResult, error) {
+ idsStr, _ := args["ids"].(string)
+ if idsStr == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "ids (comma-separated) is required"}}, IsError: true}, nil
+ }
+ idList := strings.Split(idsStr, ",")
+ for i := range idList {
+ idList[i] = strings.TrimSpace(idList[i])
+ }
+
+ attrs := sql.BatchGetBlockAttrs(idList)
+ if len(attrs) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no attributes found"}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Batch attributes (%d blocks):\n\n", len(attrs)))
+ for id, kv := range attrs {
+ sb.WriteString(fmt.Sprintf("--- %s ---\n", id))
+ for k, v := range kv {
+ sb.WriteString(fmt.Sprintf("- %s: %s\n", k, v))
+ }
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
diff --git a/kernel/mcp/tools/block.go b/kernel/mcp/tools/block.go
index 0391166072..0cb3f7d15b 100644
--- a/kernel/mcp/tools/block.go
+++ b/kernel/mcp/tools/block.go
@@ -20,17 +20,18 @@ import (
"fmt"
"strings"
+ "github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/util"
)
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.\n- move: Move a block. Requires: id, parentID. Optional: previousID.\n- breadcrumb: Get block breadcrumb path. Requires: id.",
+ Description: "Block operations for SiYuan.\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- tree_stat: Get tree statistics (word count, character count, block count, etc.) by document ID. Requires: id.\n- dom: Get block DOM by 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", "move", "breadcrumb"}},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"get", "get_kramdown", "get_children", "tree_stat", "dom", "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"}},
@@ -56,6 +57,10 @@ func blockHandler(args map[string]interface{}) (CallToolResult, error) {
return blockGetKramdown(args)
case "get_children":
return blockGetChildren(args)
+ case "tree_stat":
+ return blockTreeStat(args)
+ case "dom":
+ return blockDom(args)
case "insert":
return blockInsert(args)
case "append":
@@ -348,3 +353,29 @@ func blockBreadcrumb(args map[string]interface{}) (CallToolResult, error) {
}
return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
}
+
+func blockTreeStat(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
+ }
+ stat := filesys.StatTree(id)
+ if stat == nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "document not found or empty"}}, IsError: true}, nil
+ }
+ text := fmt.Sprintf("Document statistics:\n- Characters: %d\n- Words: %d\n- Blocks: %d\n- Links: %d\n- Images: %d\n- Refs: %d",
+ stat.RuneCount, stat.WordCount, stat.BlockCount, stat.LinkCount, stat.ImageCount, stat.RefCount)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: text}}}, nil
+}
+
+func blockDom(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
+ }
+ dom := model.GetBlockDOM(id)
+ if dom == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "block not found or empty: " + id}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: dom}}}, nil
+}
diff --git a/kernel/mcp/tools/database.go b/kernel/mcp/tools/database.go
index 9bf3eae745..7ba5853cac 100644
--- a/kernel/mcp/tools/database.go
+++ b/kernel/mcp/tools/database.go
@@ -17,26 +17,43 @@
package tools
import (
+ "encoding/json"
"fmt"
"strings"
+ "github.com/88250/lute/ast"
"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).",
+ 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).\n- keys: List all keys (fields) of a database. Requires: id.\n- key_add: Add a key (field) to a database. Requires: id, name, type. Optional: icon, prev.\n- key_remove: Remove a key from a database. Requires: id, keyID. Optional: removeRelationDest.\n- item_add: Add a row to a database. Requires: id. Optional: blockID, content, viewID, groupID, previousID, detached, ignoreDefaultFill.\n- item_remove: Remove rows from a database. Requires: id, itemIDs (comma-separated).\n- item_update: Update a cell value. Requires: id, keyID, itemID, value (JSON string).\n- unused: List unused databases.\n- clean: Clean unused databases. Optional: id (single database ID to clean).",
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)"},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"search", "get", "render", "keys", "key_add", "key_remove", "item_add", "item_remove", "item_update", "unused", "clean"}},
+ "keyword": {Type: "string", Description: "Search keyword (for search)"},
+ "id": {Type: "string", Description: "Attribute view ID (for get, render, keys, key_add, key_remove, item_add, item_remove, item_update, clean)"},
+ "viewID": {Type: "string", Description: "View ID (for render, item_add)"},
+ "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)"},
+ "name": {Type: "string", Description: "Key name (for key_add)"},
+ "type": {Type: "string", Description: "Key type: block/text/number/date/select/mSelect/url/email/phone/mAsset/template/created/updated/checkbox/relation/rollup/lineNumber (for key_add)"},
+ "icon": {Type: "string", Description: "Key icon (for key_add, optional)"},
+ "prev": {Type: "string", Description: "Previous key ID for ordering (for key_add, optional)"},
+ "keyID": {Type: "string", Description: "Key ID (for key_remove, item_update)"},
+ "removeRelationDest": {Type: "boolean", Description: "Also remove related data in linked databases (for key_remove, optional)"},
+ "blockID": {Type: "string", Description: "Block ID to bind (for item_add, optional)"},
+ "content": {Type: "string", Description: "Block column text content (for item_add, optional)"},
+ "groupID": {Type: "string", Description: "Group ID for positioning (for item_add, optional)"},
+ "previousID": {Type: "string", Description: "Previous item ID for positioning (for item_add, optional)"},
+ "detached": {Type: "boolean", Description: "Create detached row (for item_add, optional)"},
+ "ignoreDefaultFill": {Type: "boolean", Description: "Skip filling default values (for item_add, optional)"},
+ "itemID": {Type: "string", Description: "Item ID (for item_update)"},
+ "itemIDs": {Type: "string", Description: "Comma-separated item IDs (for item_remove)"},
+ "value": {Type: "string", Description: "JSON value for the cell (for item_update)"},
},
Required: []string{"action"},
},
@@ -56,6 +73,22 @@ func databaseHandler(args map[string]interface{}) (CallToolResult, error) {
return databaseGet(args)
case "render":
return databaseRender(args)
+ case "keys":
+ return databaseKeys(args)
+ case "key_add":
+ return databaseKeyAdd(args)
+ case "key_remove":
+ return databaseKeyRemove(args)
+ case "item_add":
+ return databaseItemAdd(args)
+ case "item_remove":
+ return databaseItemRemove(args)
+ case "item_update":
+ return databaseItemUpdate(args)
+ case "unused":
+ return databaseUnused(args)
+ case "clean":
+ return databaseClean(args)
}
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
@@ -143,3 +176,149 @@ func databaseRender(args map[string]interface{}) (CallToolResult, error) {
}
return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
}
+
+func databaseKeys(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("Keys for %s (%s):\n\n", id, attrView.Name))
+ for _, kv := range attrView.KeyValues {
+ sb.WriteString(fmt.Sprintf("- %s (%s) [%s]\n", kv.Key.ID, kv.Key.Name, kv.Key.Type))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func databaseKeyAdd(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ name, _ := args["name"].(string)
+ keyType, _ := args["type"].(string)
+ if id == "" || name == "" || keyType == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id, name and type are required"}}, IsError: true}, nil
+ }
+ icon, _ := args["icon"].(string)
+ prev, _ := args["prev"].(string)
+ keyID := ast.NewNodeID()
+ if err := model.AddAttributeViewKey(id, keyID, name, keyType, icon, prev); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "add key failed: " + err.Error()}}, IsError: true}, nil
+ }
+ model.AppendPushReloadAttrViewEntry(id)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("key added: %s (%s)", keyID, name)}}}, nil
+}
+
+func databaseKeyRemove(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ keyID, _ := args["keyID"].(string)
+ if id == "" || keyID == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id and keyID are required"}}, IsError: true}, nil
+ }
+ removeRelation := false
+ if v, ok := args["removeRelationDest"].(bool); ok {
+ removeRelation = v
+ }
+ if err := model.RemoveAttributeViewKey(id, keyID, removeRelation); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "remove key failed: " + err.Error()}}, IsError: true}, nil
+ }
+ model.AppendPushReloadAttrViewEntry(id)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "key removed: " + keyID}}}, nil
+}
+
+func databaseItemAdd(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
+ }
+ isDetached := false
+ if v, ok := args["detached"].(bool); ok {
+ isDetached = v
+ }
+ blockID, _ := args["blockID"].(string)
+ if !isDetached && blockID == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "blockID is required for non-detached rows"}}, IsError: true}, nil
+ }
+ content, _ := args["content"].(string)
+ viewID, _ := args["viewID"].(string)
+ groupID, _ := args["groupID"].(string)
+ previousID, _ := args["previousID"].(string)
+ ignoreFill := false
+ if v, ok := args["ignoreDefaultFill"].(bool); ok {
+ ignoreFill = v
+ }
+ src := map[string]any{"isDetached": isDetached}
+ if blockID != "" {
+ src["id"] = blockID
+ }
+ if content != "" {
+ src["content"] = content
+ }
+ srcs := []map[string]any{src}
+ if err := model.AddAttributeViewBlock(nil, srcs, id, blockID, viewID, groupID, previousID, ignoreFill); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "add item failed: " + err.Error()}}, IsError: true}, nil
+ }
+ model.AppendPushReloadAttrViewEntry(id)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "item added"}}}, nil
+}
+
+func databaseItemRemove(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ itemIDsStr, _ := args["itemIDs"].(string)
+ if id == "" || itemIDsStr == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id and itemIDs are required"}}, IsError: true}, nil
+ }
+ itemIDs := strings.Split(itemIDsStr, ",")
+ for i := range itemIDs {
+ itemIDs[i] = strings.TrimSpace(itemIDs[i])
+ }
+ if err := model.RemoveAttributeViewBlock(itemIDs, id); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "remove items failed: " + err.Error()}}, IsError: true}, nil
+ }
+ model.AppendPushReloadAttrViewEntry(id)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("%d item(s) removed", len(itemIDs))}}}, nil
+}
+
+func databaseItemUpdate(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ keyID, _ := args["keyID"].(string)
+ itemID, _ := args["itemID"].(string)
+ valueStr, _ := args["value"].(string)
+ if id == "" || keyID == "" || itemID == "" || valueStr == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id, keyID, itemID and value are required"}}, IsError: true}, nil
+ }
+ var valueData map[string]any
+ if err := json.Unmarshal([]byte(valueStr), &valueData); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "invalid JSON value: " + err.Error()}}, IsError: true}, nil
+ }
+ if _, err := model.UpdateAttributeViewCell(nil, id, keyID, itemID, valueData); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "update cell failed: " + err.Error()}}, IsError: true}, nil
+ }
+ model.AppendPushReloadAttrViewEntry(id)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "cell updated"}}}, nil
+}
+
+func databaseUnused(args map[string]interface{}) (CallToolResult, error) {
+ items := model.UnusedAttributeViews(true)
+ if len(items) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no unused databases found"}}}, nil
+ }
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Unused databases (%d):\n\n", len(items)))
+ for _, item := range items {
+ sb.WriteString(fmt.Sprintf("- %s (%s)\n", item.Item, item.Name))
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func databaseClean(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ if id != "" {
+ model.RemoveUnusedAttributeView(id)
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "unused database cleaned: " + id}}}, nil
+ }
+ removed := model.RemoveUnusedAttributeViews()
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("%d unused database(s) cleaned", len(removed))}}}, nil
+}
diff --git a/kernel/mcp/tools/document.go b/kernel/mcp/tools/document.go
index 07c4ad63ef..f0795cab1e 100644
--- a/kernel/mcp/tools/document.go
+++ b/kernel/mcp/tools/document.go
@@ -27,11 +27,11 @@ 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, 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.",
+ 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- duplicate: Duplicate a document by ID. Requires: id.\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", "move", "search_docs"}},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"get", "create", "list", "delete", "rename", "move", "duplicate", "search_docs"}},
"id": {Type: "string", Description: "Document block ID"},
"title": {Type: "string", Description: "Document title (for create, rename)"},
"path": {Type: "string", Description: "Document path like /folder/doc (for create, list, move)"},
@@ -63,6 +63,8 @@ func documentHandler(args map[string]interface{}) (CallToolResult, error) {
return documentRename(args)
case "move":
return documentMove(args)
+ case "duplicate":
+ return documentDuplicate(args)
case "search_docs":
return documentSearchDocs(args)
}
@@ -200,6 +202,22 @@ func documentMove(args map[string]interface{}) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("document moved: %s -> %s%s", id, notebook, path)}}}, nil
}
+func documentDuplicate(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
+ }
+
+ 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
+ }
+
+ model.DuplicateDoc(tree)
+ model.AppendPushReloadFiletreeEntry()
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "document duplicated: " + id}}}, nil
+}
+
func documentSearchDocs(args map[string]interface{}) (CallToolResult, error) {
keyword, _ := args["keyword"].(string)
if keyword == "" {
diff --git a/kernel/mcp/tools/export.go b/kernel/mcp/tools/export.go
index 9f786aa10e..db2a8d0b11 100644
--- a/kernel/mcp/tools/export.go
+++ b/kernel/mcp/tools/export.go
@@ -24,14 +24,15 @@ import (
var ExportTool = &Tool{
Name: "export",
- Description: "Export operations for SiYuan.\n- md: Export a document as Markdown. Requires: id.",
+ Description: "Export operations for SiYuan.\n- md: Export a document as Markdown. Requires: id.\n- html: Export a document as HTML. Requires: id.\n- preview: Export a document as preview HTML. Requires: id.\n- docx: Export a document as Word (.docx). Requires: id, output (output file path).\n- sy: Export a document as .sy.zip. Requires: id.\n- md-zip: Export a document as Markdown zip. Requires: id.\n- data: Export full workspace data backup.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"md"}},
- "id": {Type: "string", Description: "Document block ID"},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"md", "html", "preview", "docx", "sy", "md-zip", "data"}},
+ "id": {Type: "string", Description: "Document block ID (for md, html, preview, docx, sy, md-zip)"},
+ "output": {Type: "string", Description: "Output file path (required for docx, optional for others)"},
},
- Required: []string{"action", "id"},
+ Required: []string{"action"},
},
Handler: exportHandler,
}
@@ -45,6 +46,18 @@ func exportHandler(args map[string]interface{}) (CallToolResult, error) {
switch action {
case "md":
return exportMd(args)
+ case "html":
+ return exportHtml(args)
+ case "preview":
+ return exportPreview(args)
+ case "docx":
+ return exportDocx(args)
+ case "sy":
+ return exportSy(args)
+ case "md-zip":
+ return exportMdZip(args)
+ case "data":
+ return exportData(args)
}
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
@@ -65,3 +78,75 @@ func exportMd(args map[string]interface{}) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("# %s\n\n%s", hPath, content)}}}, nil
}
+
+func exportHtml(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
+ }
+ _, dom, _ := model.ExportHTML(id, "", false, false, false)
+ if dom == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export failed or empty"}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: dom}}}, nil
+}
+
+func exportPreview(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
+ }
+ html := model.ExportPreview(id, false)
+ if html == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export preview failed or empty"}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: html}}}, nil
+}
+
+func exportDocx(args map[string]interface{}) (CallToolResult, error) {
+ id, _ := args["id"].(string)
+ output, _ := args["output"].(string)
+ if id == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
+ }
+ if output == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "output file path is required for docx"}}, IsError: true}, nil
+ }
+ fullPath, err := model.ExportDocx(id, output, false, false)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export docx failed: " + err.Error()}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("exported docx to: %s", fullPath)}}}, nil
+}
+
+func exportSy(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
+ }
+ _, zipPath := model.ExportPandocConvertZip([]string{id}, "", ".sy")
+ if zipPath == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export sy failed"}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("exported sy.zip to: %s", zipPath)}}}, nil
+}
+
+func exportMdZip(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
+ }
+ _, zipPath := model.ExportPandocConvertZip([]string{id}, "", ".md")
+ if zipPath == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export md-zip failed"}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("exported md zip to: %s", zipPath)}}}, nil
+}
+
+func exportData(args map[string]interface{}) (CallToolResult, error) {
+ zipPath, err := model.ExportData()
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export data failed: " + err.Error()}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("exported data backup to: %s", zipPath)}}}, nil
+}
diff --git a/kernel/mcp/tools/history.go b/kernel/mcp/tools/history.go
index 3d5e2a9978..b036ef1bb9 100644
--- a/kernel/mcp/tools/history.go
+++ b/kernel/mcp/tools/history.go
@@ -21,21 +21,23 @@ import (
"strings"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
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.",
+ 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- search: Search document history by keyword. Requires: query. Optional: notebook, op, type, page.\n- get: Get historical content of a document by history path. Requires: path.\n- rollback: Rollback a document to a historical version. Requires: path.\n- clear: Clear all history.",
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)"},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"list", "search", "get", "rollback", "clear"}},
+ "query": {Type: "string", Description: "Search query (for list, search)"},
+ "notebook": {Type: "string", Description: "Notebook ID filter (for list, search)"},
+ "op": {Type: "string", Description: "Operation filter: delete/update/create (for list, search)"},
"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)"},
+ "path": {Type: "string", Description: "History path (for get, rollback)"},
},
Required: []string{"action"},
},
@@ -51,8 +53,14 @@ func historyHandler(args map[string]interface{}) (CallToolResult, error) {
switch action {
case "list":
return historyList(args)
+ case "search":
+ return historySearch(args)
case "get":
return historyGet(args)
+ case "rollback":
+ return historyRollback(args)
+ case "clear":
+ return historyClear(args)
}
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: "unknown action: " + action}},
@@ -106,3 +114,59 @@ func historyGet(args map[string]interface{}) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: content}}}, nil
}
+
+func historySearch(args map[string]interface{}) (CallToolResult, error) {
+ query, _ := args["query"].(string)
+ if query == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "query is required"}}, IsError: true}, nil
+ }
+ box, _ := 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)
+ }
+
+ timestamps, pageCount, totalCount := model.FullTextSearchHistory(query, box, op, typ, page)
+ if len(timestamps) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("no history found for '%s'", query)}}}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Search '%s' (%d total, page %d/%d):\n\n", query, totalCount, page, pageCount))
+ for _, ts := range timestamps {
+ items := model.FullTextSearchHistoryItems(ts, query, box, 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 historyRollback(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
+ }
+ if err := model.RollbackDocHistory(path); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "rollback failed: " + err.Error()}}, IsError: true}, nil
+ }
+ docID := util.GetTreeID(path)
+ if bt := treenode.GetBlockTree(docID); bt != nil {
+ model.AppendPushReloadProtyleEntry(bt.RootID)
+ }
+ model.AppendPushReloadFiletreeEntry()
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "history rolled back: " + path}}}, nil
+}
+
+func historyClear(args map[string]interface{}) (CallToolResult, error) {
+ if err := model.ClearWorkspaceHistory(); err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "clear history failed: " + err.Error()}}, IsError: true}, nil
+ }
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "all history cleared"}}}, nil
+}
diff --git a/kernel/mcp/tools/search.go b/kernel/mcp/tools/search.go
index 1e2869f2ad..cb4b5759b3 100644
--- a/kernel/mcp/tools/search.go
+++ b/kernel/mcp/tools/search.go
@@ -25,7 +25,7 @@ import (
var SearchTool = &Tool{
Name: "search",
- Description: "Full-text search in SiYuan notes.\n- fulltext: Search blocks by keywords. Required: query. Optional: page (default 1), pageSize (default 20).",
+ Description: "Full-text search in SiYuan.\n- fulltext: Search blocks by keywords. Required: query. Optional: page (default 1), pageSize (default 20).",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{