Skip to content

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

:bug: Fix kernel plugin MCP tool registration https://github.com/siyuan-note/siyuan/issues/13795

Danielf1748c60f855
diff --git a/kernel/plugin/api_mcp.go b/kernel/plugin/api_mcp.go
index 20ce07d0ce..738d8e47f8 100644
--- a/kernel/plugin/api_mcp.go
+++ b/kernel/plugin/api_mcp.go
@@ -92,7 +92,7 @@ func injectMcp(p *KernelPlugin, rt *goja.Runtime, siyuan *goja.Object) (err erro
return
}
}
- if readOnlyValue := configObj.Get("readOnly"); !goja.IsUndefined(readOnlyValue) && !goja.IsNull(readOnlyValue) {
+ if readOnlyValue := configObj.Get("readOnly"); isJsValueNotNull(readOnlyValue) {
var ok bool
if readOnlyHint, ok = readOnlyValue.Export().(bool); !ok {
err = fmt.Errorf("config.readOnly must be a boolean")
diff --git a/kernel/plugin/api_mcp_test.go b/kernel/plugin/api_mcp_test.go
new file mode 100644
index 0000000000..7a3db2d7ea
--- /dev/null
+++ b/kernel/plugin/api_mcp_test.go
@@ -0,0 +1,76 @@
+// 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 plugin
+
+import (
+ "testing"
+
+ "github.com/dop251/goja"
+ "github.com/dop251/goja_nodejs/eventloop"
+ "github.com/siyuan-note/siyuan/kernel/mcp/tools"
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+func TestRegisterMcpToolOptionalReadOnly(t *testing.T) {
+ p := &KernelPlugin{Petal: &model.Petal{Name: "test-plugin-optional-read-only"}}
+ loop := eventloop.NewEventLoop()
+ p.worker.Start(loop)
+ t.Cleanup(p.Clear)
+
+ var scriptErr error
+ loop.Run(func(rt *goja.Runtime) {
+ siyuan := rt.NewObject()
+ if err := injectMcp(p, rt, siyuan); err != nil {
+ scriptErr = err
+ return
+ }
+ if err := rt.Set("siyuan", siyuan); err != nil {
+ scriptErr = err
+ return
+ }
+ _, scriptErr = rt.RunString(`
+ siyuan.mcp.registerTool("default", {
+ description: "Default mutability",
+ inputSchema: {type: "object"}
+ }, (input) => input);
+ siyuan.mcp.registerTool("read-only", {
+ description: "Read-only tool",
+ inputSchema: {type: "object"},
+ readOnly: true
+ }, (input) => input);
+ `)
+ })
+ if scriptErr != nil {
+ t.Fatalf("register MCP tools failed: %v", scriptErr)
+ }
+
+ defaultTool := tools.GetTool(pluginToolName(p.Name, "default"))
+ if defaultTool == nil {
+ t.Fatal("tool without readOnly was not registered")
+ }
+ if defaultTool.ReadOnlyHint {
+ t.Fatal("tool without readOnly must default to writable")
+ }
+
+ readOnlyTool := tools.GetTool(pluginToolName(p.Name, "read-only"))
+ if readOnlyTool == nil {
+ t.Fatal("tool with readOnly was not registered")
+ }
+ if !readOnlyTool.ReadOnlyHint {
+ t.Fatal("tool with readOnly=true must be marked read-only")
+ }
+}
diff --git a/kernel/plugin/worker.go b/kernel/plugin/worker.go
index 991babcb9e..d3f9d7d863 100644
--- a/kernel/plugin/worker.go
+++ b/kernel/plugin/worker.go
@@ -18,6 +18,7 @@ package plugin
import (
"fmt"
+ "runtime/debug"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/eventloop"
@@ -51,14 +52,15 @@ func (w *Worker) Run(executor TaskExecutor, callback TaskCallback) error {
defer func() {
defer func() {
- // catch panic from callback
+ // 捕获回调中的 panic 并保留原始调用栈,便于定位 Promise 处理异常。
if r := recover(); r != nil {
- logging.LogErrorf("task callback panicked: %v\n", r)
+ logging.LogErrorf("task callback panicked: %v\n%s", r, debug.Stack())
}
}()
- // catch panic from executor
+ // 捕获执行器中的 panic 并保留原始调用栈,同时将错误传给回调。
if r := recover(); r != nil {
+ logging.LogErrorf("task executor panicked: %v\n%s", r, debug.Stack())
err = fmt.Errorf("task executor panicked: %v", r)
}
if callback != nil {