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

Danielc420c2972753
diff --git a/kernel/mcp/tools/asset.go b/kernel/mcp/tools/asset.go
index 1ff7aca27e..35196936b7 100644
--- a/kernel/mcp/tools/asset.go
+++ b/kernel/mcp/tools/asset.go
@@ -18,19 +18,21 @@ package tools
import (
"fmt"
+ "os"
"path/filepath"
"strings"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
var AssetTool = &Tool{
Name: "asset",
- Description: "Asset management for SiYuan.\n- upload: Upload local files as workspace assets. Requires: id, files (comma-separated absolute paths).\n- unused: List unused assets.\n- clean: Clean unused assets. Optional: path (single unused asset path to remove).",
+ Description: "Asset management for SiYuan.\n- upload: Upload local files as workspace assets. Requires: id, files (comma-separated absolute paths).\n- unused: List unused assets.\n- clean: Clean unused assets. Optional: path (single unused asset path to remove).\n- stat: Get asset file metadata (size, modTime). Requires: path.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"upload", "unused", "clean"}},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"upload", "unused", "clean", "stat"}},
"id": {Type: "string", Description: "Document block ID (for upload)"},
"files": {Type: "string", Description: "Comma-separated absolute file paths (for upload)"},
"path": {Type: "string", Description: "Single unused asset path to remove, relative to data directory (for clean, optional). Use as returned by the unused action, e.g. assets/image/xxx.png."},
@@ -53,9 +55,11 @@ func assetHandler(args map[string]interface{}) (CallToolResult, error) {
return assetUnused(args)
case "clean":
return assetClean(args)
+ case "stat":
+ return assetStat(args)
}
return CallToolResult{
- Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [upload, unused, clean]"}},
+ Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [upload, unused, clean, stat]"}},
IsError: true,
}, nil
}
@@ -121,3 +125,21 @@ func assetClean(args map[string]interface{}) (CallToolResult, error) {
}
return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
}
+
+func assetStat(args map[string]interface{}) (CallToolResult, error) {
+ p, _ := args["path"].(string)
+ if p == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "path is required"}}, IsError: true}, nil
+ }
+
+ abs := filepath.Join(util.DataDir, p)
+ info, err := os.Stat(abs)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "stat failed: " + err.Error()}}, IsError: true}, nil
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf(
+ "Path: %s\nSize: %d\nIsDir: %v\nModTime: %s",
+ p, info.Size(), info.IsDir(), info.ModTime().Format("2006-01-02 15:04:05"),
+ )}}}, nil
+}
diff --git a/kernel/mcp/tools/block.go b/kernel/mcp/tools/block.go
index f39268baec..25f14499d0 100644
--- a/kernel/mcp/tools/block.go
+++ b/kernel/mcp/tools/block.go
@@ -27,12 +27,13 @@ import (
var BlockTool = &Tool{
Name: "block",
- 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.",
+ 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.\n- batch_get: Batch get block info by IDs. Requires: ids (comma-separated block IDs).\n- batch_kramdown: Batch get block kramdown by IDs. Requires: ids (comma-separated block IDs).",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"get", "get_kramdown", "get_children", "tree_stat", "dom", "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", "batch_get", "batch_kramdown"}},
"id": {Type: "string", Description: "Block ID"},
+ "ids": {Type: "string", Description: "Comma-separated block IDs (for batch_get, batch_kramdown)"},
"data": {Type: "string", Description: "Content (markdown or dom)"},
"dataType": {Type: "string", Description: "Content type: markdown or dom", Enum: []string{"markdown", "dom"}},
"parentID": {Type: "string", Description: "Parent block ID"},
@@ -75,9 +76,13 @@ func blockHandler(args map[string]interface{}) (CallToolResult, error) {
return blockMove(args)
case "breadcrumb":
return blockBreadcrumb(args)
+ case "batch_get":
+ return blockBatchGet(args)
+ case "batch_kramdown":
+ return blockBatchKramdown(args)
}
return CallToolResult{
- Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [get, get_kramdown, get_children, tree_stat, dom, insert, append, prepend, update, delete, move, breadcrumb]"}},
+ Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [get, get_kramdown, get_children, tree_stat, dom, insert, append, prepend, update, delete, move, breadcrumb, batch_get, batch_kramdown]"}},
IsError: true,
}, nil
}
@@ -385,3 +390,71 @@ func blockDom(args map[string]interface{}) (CallToolResult, error) {
}
return CallToolResult{Content: []ContentItem{{Type: "text", Text: dom}}}, nil
}
+
+func blockBatchGet(args map[string]interface{}) (CallToolResult, error) {
+ idsStr, _ := args["ids"].(string)
+ if idsStr == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "ids is required (comma-separated)"}}, IsError: true}, nil
+ }
+
+ ids := strings.Split(idsStr, ",")
+ for i := range ids {
+ ids[i] = strings.TrimSpace(ids[i])
+ }
+
+ if len(ids) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no valid IDs provided"}}, IsError: true}, nil
+ }
+
+ infos := model.GetDocsInfo(ids, false, false)
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Batch get %d blocks (found %d):\n\n", len(ids), len(infos)))
+ for _, info := range infos {
+ sb.WriteString(fmt.Sprintf("- %s: %s (rootID: %s, refCount: %d)\n", info.ID, info.Name, info.RootID, info.RefCount))
+ }
+ for _, id := range ids {
+ found := false
+ for _, info := range infos {
+ if info.ID == id {
+ found = true
+ break
+ }
+ }
+ if !found {
+ sb.WriteString(fmt.Sprintf("- %s: not found\n", id))
+ }
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}
+
+func blockBatchKramdown(args map[string]interface{}) (CallToolResult, error) {
+ idsStr, _ := args["ids"].(string)
+ if idsStr == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "ids is required (comma-separated)"}}, IsError: true}, nil
+ }
+
+ ids := strings.Split(idsStr, ",")
+ for i := range ids {
+ ids[i] = strings.TrimSpace(ids[i])
+ }
+
+ if len(ids) == 0 {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no valid IDs provided"}}, IsError: true}, nil
+ }
+
+ kramdowns := model.GetBlockKramdowns(ids, "md")
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Batch kramdown %d blocks (found %d):\n\n", len(ids), len(kramdowns)))
+ for _, id := range ids {
+ if kd, ok := kramdowns[id]; ok {
+ sb.WriteString(fmt.Sprintf("--- %s ---\n%s\n\n", id, kd))
+ } else {
+ sb.WriteString(fmt.Sprintf("--- %s ---\n(not found)\n\n", id))
+ }
+ }
+
+ 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 2bbb2bc514..5b56a03ac6 100644
--- a/kernel/mcp/tools/document.go
+++ b/kernel/mcp/tools/document.go
@@ -286,8 +286,29 @@ func documentInfo(args map[string]interface{}) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("get doc info failed: %s", err)}}, IsError: true}, nil
}
- return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf(
- "ID: %s\nRootID: %s\nName: %s\nRefCount: %d\nSubFileCount: %d",
- info.ID, info.RootID, info.Name, info.RefCount, info.SubFileCount,
- )}}}, nil
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf(
+ "ID: %s\nRootID: %s\nName: %s\nRefCount: %d\nSubFileCount: %d\nIcon: %s",
+ info.ID, info.RootID, info.Name, info.RefCount, info.SubFileCount, info.Icon,
+ ))
+ if len(info.RefIDs) > 0 {
+ sb.WriteString(fmt.Sprintf("\nRefIDs: %s", strings.Join(info.RefIDs, ", ")))
+ }
+ if len(info.AttrViews) > 0 {
+ sb.WriteString("\nAttrViews:")
+ for _, av := range info.AttrViews {
+ sb.WriteString(fmt.Sprintf("\n - %s: %s", av.ID, av.Name))
+ }
+ }
+ if len(info.IAL) > 0 {
+ sb.WriteString("\nIAL:")
+ for k, v := range info.IAL {
+ if len(v) > 100 {
+ v = v[:100] + "..."
+ }
+ sb.WriteString(fmt.Sprintf("\n %s: %s", k, v))
+ }
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
}
diff --git a/kernel/mcp/tools/file.go b/kernel/mcp/tools/file.go
index 517da8209a..109883826c 100644
--- a/kernel/mcp/tools/file.go
+++ b/kernel/mcp/tools/file.go
@@ -29,12 +29,12 @@ import (
var FileTool = &Tool{
Name: "file",
- Description: "Workspace file operations for SiYuan (paths are relative to workspace).\n- list: List directory contents. Requires: path.\n- read: Read file content. Requires: path.\n- write: Write file content. Requires: path, data.\n- delete: Delete file or directory. Requires: path.\n- rename: Rename or move file. Requires: old, new.\n- copy: Copy file or directory. Requires: src, dst.\n- grep: Search file contents using regex pattern. Requires: pattern, path. Optional: include, context.\n- find: Find files by glob pattern recursively. Requires: path. Optional: include (e.g. \"*.go\").",
+ Description: "Workspace file operations for SiYuan (paths are relative to workspace).\n- list: List directory contents. Requires: path.\n- read: Read file content. Requires: path.\n- write: Write file content. Requires: path, data.\n- delete: Delete file or directory. Requires: path.\n- rename: Rename or move file. Requires: old, new.\n- copy: Copy file or directory. Requires: src, dst.\n- grep: Search file contents using regex pattern. Requires: pattern, path. Optional: include, context.\n- find: Find files by glob pattern recursively. Requires: path. Optional: include (e.g. \"*.go\").\n- stat: Get file metadata (size, isDir, modTime). Requires: path.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"list", "read", "write", "delete", "rename", "copy", "grep", "find"}},
- "path": {Type: "string", Description: "Relative path within workspace (for list, read, write, delete, grep, find)"},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"list", "read", "write", "delete", "rename", "copy", "grep", "find", "stat"}},
+ "path": {Type: "string", Description: "Relative path within workspace (for list, read, write, delete, grep, find, stat)"},
"data": {Type: "string", Description: "File content (for write)"},
"offset": {Type: "number", Description: "Line number to start reading from (for read, 1-based). Negative means N lines from the end. Default: 0 (read from beginning)."},
"limit": {Type: "number", Description: "Maximum lines to read (for read). Default: all lines."},
@@ -74,9 +74,11 @@ func fileHandler(args map[string]interface{}) (CallToolResult, error) {
return fileGrep(args)
case "find":
return fileFind(args)
+ case "stat":
+ return fileStat(args)
}
return CallToolResult{
- Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [list, read, write, delete, rename, copy, grep, find]"}},
+ Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [list, read, write, delete, rename, copy, grep, find, stat]"}},
IsError: true,
}, nil
}
@@ -441,3 +443,25 @@ func expandGlobBrace(pattern string) []string {
}
return result
}
+
+func fileStat(args map[string]interface{}) (CallToolResult, error) {
+ p, _ := args["path"].(string)
+ if p == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "path is required"}}, IsError: true}, nil
+ }
+
+ abs, err := resolvePath(p)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: err.Error()}}, IsError: true}, nil
+ }
+
+ info, err := os.Stat(abs)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "stat failed: " + err.Error()}}, IsError: true}, nil
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf(
+ "Path: %s\nSize: %d\nIsDir: %v\nModTime: %s",
+ p, info.Size(), info.IsDir(), info.ModTime().Format("2006-01-02 15:04:05"),
+ )}}}, nil
+}