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

Daniel54b18ef1c652
diff --git a/kernel/go.mod b/kernel/go.mod
index 655da20133..8190d7e709 100644
--- a/kernel/go.mod
+++ b/kernel/go.mod
@@ -7,7 +7,7 @@ require (
github.com/88250/clipboard v0.1.5
github.com/88250/epub v0.0.0-20230830085737-c19055cd1f48
github.com/88250/go-humanize v0.0.0-20240424102817-4f78fac47ea7
- github.com/88250/gulu v1.2.3-0.20260409163331-8c1dab1828ba
+ github.com/88250/gulu v1.2.3-0.20260609080815-b53113e66541
github.com/88250/lute v1.7.7-0.20260609015101-c1d6e480ca97
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1
github.com/ClarkThan/ahocorasick v0.0.0-20231011042242-30d1ef1347f4
diff --git a/kernel/go.sum b/kernel/go.sum
index c756f8f96d..d2434fa1d4 100644
--- a/kernel/go.sum
+++ b/kernel/go.sum
@@ -12,8 +12,8 @@ github.com/88250/go-humanize v0.0.0-20240424102817-4f78fac47ea7 h1:MafIFwSS0x6A4
github.com/88250/go-humanize v0.0.0-20240424102817-4f78fac47ea7/go.mod h1:HrKCCTin3YNDSLBD02K0AOljjV6eNwc3/zyEI+xyV1I=
github.com/88250/go-sqlite3 v1.14.13-0.20231214121541-e7f54c482950 h1:Pa5hMiBceTVVqrYaDlLio2QSKbXMUmAZPbzCwT5eNCw=
github.com/88250/go-sqlite3 v1.14.13-0.20231214121541-e7f54c482950/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
-github.com/88250/gulu v1.2.3-0.20260409163331-8c1dab1828ba h1:F9nZLeLTSPUIyYrQqprqElKFef4xVI7fzjxkLXRb2Bo=
-github.com/88250/gulu v1.2.3-0.20260409163331-8c1dab1828ba/go.mod h1:D+Db16m0N7r9MLZCMcj1a0ZsEGQAxDZkadOn79Gh0vI=
+github.com/88250/gulu v1.2.3-0.20260609080815-b53113e66541 h1:u5cF1u1/rle/sVCR7/EqhV4y826It+SQoeyiH+k5IRU=
+github.com/88250/gulu v1.2.3-0.20260609080815-b53113e66541/go.mod h1:D+Db16m0N7r9MLZCMcj1a0ZsEGQAxDZkadOn79Gh0vI=
github.com/88250/lute v1.7.7-0.20260609015101-c1d6e480ca97 h1:IqPqrGrv9D8a2ES79Z1sZoKCFwwZXhkHc9qmTWTB1M8=
github.com/88250/lute v1.7.7-0.20260609015101-c1d6e480ca97/go.mod h1:WYyUw//5yVw9BJnoVjx7rI/3szsISxNZCYGOqTIrV0o=
github.com/88250/pdfcpu v0.3.14-0.20250424122812-f10e8d9d8d46 h1:Bq1JsDfVbHKUxNL/B2JXd8cC/1h6aFjrlXpGycnh0Hk=
diff --git a/kernel/mcp/tools/file.go b/kernel/mcp/tools/file.go
index 291d4cdf8a..05f8251c14 100644
--- a/kernel/mcp/tools/file.go
+++ b/kernel/mcp/tools/file.go
@@ -29,19 +29,21 @@ 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.",
+ 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 (file glob filter, e.g. \"*.go\", \"*.{ts,tsx}\").",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
- "action": {Type: "string", Description: "Operation", Enum: []string{"list", "read", "write", "delete", "rename", "copy"}},
- "path": {Type: "string", Description: "Relative path within workspace (for list, read, write, delete)"},
- "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."},
- "old": {Type: "string", Description: "Source path (for rename)"},
- "new": {Type: "string", Description: "Destination path (for rename)"},
- "src": {Type: "string", Description: "Source path (for copy)"},
- "dst": {Type: "string", Description: "Destination path (for copy)"},
+ "action": {Type: "string", Description: "Operation", Enum: []string{"list", "read", "write", "delete", "rename", "copy", "grep"}},
+ "path": {Type: "string", Description: "Relative path within workspace (for list, read, write, delete, grep)"},
+ "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."},
+ "old": {Type: "string", Description: "Source path (for rename)"},
+ "new": {Type: "string", Description: "Destination path (for rename)"},
+ "src": {Type: "string", Description: "Source path (for copy)"},
+ "dst": {Type: "string", Description: "Destination path (for copy)"},
+ "pattern": {Type: "string", Description: "Regex pattern to search for (for grep)"},
+ "include": {Type: "string", Description: "File glob pattern to filter files (for grep, e.g. \"*.go\", \"*.{ts,tsx}\")"},
},
Required: []string{"action"},
},
@@ -67,9 +69,11 @@ func fileHandler(args map[string]interface{}) (CallToolResult, error) {
return fileRename(args)
case "copy":
return fileCopy(args)
+ case "grep":
+ return fileGrep(args)
}
return CallToolResult{
- Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [list, read, write, delete, rename, copy]"}},
+ Content: []ContentItem{{Type: "text", Text: "unknown action '" + action + "', expected one of: [list, read, write, delete, rename, copy, grep]"}},
IsError: true,
}, nil
}
@@ -307,3 +311,39 @@ func typeLabel(isDir bool) string {
}
return "FILE"
}
+
+func fileGrep(args map[string]interface{}) (CallToolResult, error) {
+ pattern, _ := args["pattern"].(string)
+ if pattern == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "pattern is required"}}, IsError: true}, nil
+ }
+
+ 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
+ }
+
+ include, _ := args["include"].(string)
+
+ results, err := gulu.File.Grep(abs, include, pattern, 0)
+ if err != nil {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "grep failed: " + err.Error()}}, IsError: true}, nil
+ }
+
+ var sb strings.Builder
+ sb.WriteString(fmt.Sprintf("Found %d matches:\n\n", len(results)))
+ for _, r := range results {
+ rel, relErr := filepath.Rel(util.WorkspaceDir, r.File)
+ if relErr != nil {
+ rel = r.File
+ }
+ sb.WriteString(fmt.Sprintf("%s:%d: %s\n", rel, r.Line, r.Text))
+ }
+
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
+}