Skip to content

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

:bug: Hide Agent session tools from external MCP https://github.com/siyuan-note/siyuan/issues/18918

Daniel6caf1343c5a3
diff --git a/app/src/config/tabs/ai/aiCapabilityUi.ts b/app/src/config/tabs/ai/aiCapabilityUi.ts
index 2d0773f407..380154965f 100644
--- a/app/src/config/tabs/ai/aiCapabilityUi.ts
+++ b/app/src/config/tabs/ai/aiCapabilityUi.ts
@@ -35,6 +35,7 @@ interface ICapabilityInfo {
ownerId?: string;
ownerName?: string;
runtime: "kernel" | "plugin-worker" | "mcp" | "browser";
+ agentOnly?: boolean;
available: boolean;
actions?: ICapabilityActionInfo[];
}
@@ -80,7 +81,8 @@ const getManifestActions = (capability: ReturnType<typeof listCapabilityManifest
const getAllCapabilities = (backend: ICapabilityInfo[], scope: CapabilityScope): ICapabilityInfo[] => {
if (scope === "mcp") {
- return backend.filter((capability) => capability.source !== "mcp" && capability.runtime !== "mcp").sort((a, b) => {
+ return backend.filter((capability) => !capability.agentOnly && capability.source !== "mcp" &&
+ capability.runtime !== "mcp").sort((a, b) => {
const group = getGroupLabel(a).localeCompare(getGroupLabel(b));
return group || a.id.localeCompare(b.id);
});
diff --git a/kernel/mcp/server.go b/kernel/mcp/server.go
index 4da204370a..87313c3120 100644
--- a/kernel/mcp/server.go
+++ b/kernel/mcp/server.go
@@ -140,7 +140,7 @@ func RefreshToolExposure() {
}
func externalMCPToolAllowed(tool *tools.Tool) bool {
- if tool == nil || tool.Source == "mcp" || tool.Runtime == "mcp" {
+ if tool == nil || tool.AgentOnly || tool.Source == "mcp" || tool.Runtime == "mcp" {
return false
}
if model.Conf == nil || model.Conf.AI == nil || model.Conf.AI.MCP == nil {
diff --git a/kernel/mcp/server_test.go b/kernel/mcp/server_test.go
index b042b0f22d..725216052e 100644
--- a/kernel/mcp/server_test.go
+++ b/kernel/mcp/server_test.go
@@ -201,6 +201,17 @@ func TestExternalMCPToolsAreNotReexposed(t *testing.T) {
}
}
+func TestAgentOnlyToolsAreNotExposed(t *testing.T) {
+ for _, tool := range []*tools.Tool{tools.QuestionTool, tools.TodoWriteTool} {
+ if !tool.AgentOnly {
+ t.Fatalf("Agent session tool was not marked as Agent-only: %s", tool.Name)
+ }
+ if externalMCPToolAllowed(tool) {
+ t.Fatalf("Agent session tool was exposed through the SiYuan MCP server: %s", tool.Name)
+ }
+ }
+}
+
func toolListContains(toolList []*mcpsdk.Tool, name string) bool {
for _, tool := range toolList {
if tool.Name == name {
diff --git a/kernel/mcp/tools/capability.go b/kernel/mcp/tools/capability.go
index ceaac602d2..091ac74798 100644
--- a/kernel/mcp/tools/capability.go
+++ b/kernel/mcp/tools/capability.go
@@ -28,6 +28,7 @@ type CapabilityManifest struct {
OwnerID string `json:"ownerId,omitempty"`
OwnerName string `json:"ownerName,omitempty"`
Runtime string `json:"runtime"`
+ AgentOnly bool `json:"agentOnly,omitempty"`
Effects ToolEffects `json:"effects,omitempty"`
Available bool `json:"available"`
Actions []CapabilityActionManifest `json:"actions,omitempty"`
@@ -78,6 +79,7 @@ func ListCapabilityManifests() []CapabilityManifest {
OwnerID: tool.OwnerID,
OwnerName: tool.OwnerName,
Runtime: runtime,
+ AgentOnly: tool.AgentOnly,
Effects: effects,
Available: true,
Actions: capabilityActionsForTool(tool),
diff --git a/kernel/mcp/tools/capability_test.go b/kernel/mcp/tools/capability_test.go
index 5d3c5f5fd0..fba3e1094a 100644
--- a/kernel/mcp/tools/capability_test.go
+++ b/kernel/mcp/tools/capability_test.go
@@ -47,3 +47,18 @@ func TestCapabilityActionsForTool(t *testing.T) {
t.Fatalf("capability action effects were not resolved: %#v", actions)
}
}
+
+func TestCapabilityManifestIncludesAgentOnly(t *testing.T) {
+ manifests := ListCapabilityManifests()
+ found := map[string]bool{}
+ for _, manifest := range manifests {
+ if manifest.Name == QuestionTool.Name || manifest.Name == TodoWriteTool.Name {
+ found[manifest.Name] = manifest.AgentOnly
+ }
+ }
+ for _, name := range []string{QuestionTool.Name, TodoWriteTool.Name} {
+ if !found[name] {
+ t.Fatalf("Agent-only capability metadata is missing: %s", name)
+ }
+ }
+}
diff --git a/kernel/mcp/tools/question.go b/kernel/mcp/tools/question.go
index 9cf94b2faa..747817d7a0 100644
--- a/kernel/mcp/tools/question.go
+++ b/kernel/mcp/tools/question.go
@@ -19,6 +19,7 @@ package tools
var QuestionTool = &Tool{
Name: "question",
Description: "Ask the user questions to clarify needs/preferences (do NOT use for plain-text option lists). questions[]: each {header (short label), question, options[] {label, description}, multiple?, custom?}.",
+ AgentOnly: true,
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
diff --git a/kernel/mcp/tools/todo.go b/kernel/mcp/tools/todo.go
index 57d82779c5..6bd9937334 100644
--- a/kernel/mcp/tools/todo.go
+++ b/kernel/mcp/tools/todo.go
@@ -26,6 +26,7 @@ import (
var TodoWriteTool = &Tool{
Name: "todo_write",
Description: "Maintain a session task list (each call replaces the list). todos[]: each {content, status: pending|in_progress|completed|cancelled}.",
+ AgentOnly: true,
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
@@ -84,8 +85,14 @@ func todoWriteHandler(args map[string]any) (CallToolResult, error) {
todos = append(todos, model.AgentTodoItem{Content: content, Status: status})
}
- // Save todos for this session
- if err := model.SaveAgentTodos(args["_sessionID"].(string), todos); err != nil {
+ sessionID, ok := args["_sessionID"].(string)
+ if !ok || sessionID == "" {
+ return CallToolResult{
+ Content: []ContentItem{{Type: "text", Text: "todo_write error: missing Agent session context"}},
+ IsError: true,
+ }, nil
+ }
+ if err := model.SaveAgentTodos(sessionID, todos); err != nil {
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: "todo_write error: " + err.Error()}},
IsError: true,
diff --git a/kernel/mcp/tools/todo_test.go b/kernel/mcp/tools/todo_test.go
new file mode 100644
index 0000000000..b08fd4b44f
--- /dev/null
+++ b/kernel/mcp/tools/todo_test.go
@@ -0,0 +1,46 @@
+// SiYuan - From thought to insight, with agents
+// 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 "testing"
+
+func TestTodoWriteRejectsInvalidSessionContext(t *testing.T) {
+ tests := []struct {
+ name string
+ sessionID any
+ include bool
+ }{
+ {name: "missing"},
+ {name: "wrong type", sessionID: 1, include: true},
+ {name: "invalid ID", sessionID: "..", include: true},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ args := map[string]any{"todos": []any{}}
+ if test.include {
+ args["_sessionID"] = test.sessionID
+ }
+ result, err := todoWriteHandler(args)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !result.IsError {
+ t.Fatalf("invalid Agent session context was accepted: %#v", test.sessionID)
+ }
+ })
+ }
+}
diff --git a/kernel/mcp/tools/types.go b/kernel/mcp/tools/types.go
index 0a8384568d..a9329c796b 100644
--- a/kernel/mcp/tools/types.go
+++ b/kernel/mcp/tools/types.go
@@ -55,6 +55,8 @@ type Tool struct {
OwnerName string `json:"ownerName,omitempty"`
// Runtime 描述执行位置:kernel、plugin-worker 或 mcp。
Runtime string `json:"runtime,omitempty"`
+ // AgentOnly 标记能力仅供应用内 Agent 使用,不得投影到外部 MCP 服务。
+ AgentOnly bool `json:"agentOnly,omitempty"`
// ReadOnlyHint 仅在外部工具明确声明只读时为 true;未声明时按可能写入处理并要求确认。
ReadOnlyHint bool `json:"readOnlyHint,omitempty"`
// EffectScope 描述写操作影响范围,用于判断本地数据仓库快照是否具有回滚价值。
diff --git a/kernel/model/todo.go b/kernel/model/todo.go
index fcb54d6d4e..c2ffa01483 100644
--- a/kernel/model/todo.go
+++ b/kernel/model/todo.go
@@ -18,9 +18,11 @@ package model
import (
"encoding/json"
+ "errors"
"os"
"path/filepath"
+ "github.com/88250/lute/ast"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -40,6 +42,9 @@ func agentTodosPath(sessionID string) string {
}
func SaveAgentTodos(sessionID string, todos []AgentTodoItem) error {
+ if !ast.IsNodeIDPattern(sessionID) {
+ return errors.New("invalid Agent session ID")
+ }
dir := filepath.Join(util.DataDir, "storage", "ai", "agent", "sessions", sessionID)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
@@ -59,6 +64,9 @@ func SaveAgentTodos(sessionID string, todos []AgentTodoItem) error {
}
func LoadAgentTodos(sessionID string) ([]AgentTodoItem, error) {
+ if !ast.IsNodeIDPattern(sessionID) {
+ return nil, errors.New("invalid Agent session ID")
+ }
path := agentTodosPath(sessionID)
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, nil
diff --git a/kernel/model/todo_test.go b/kernel/model/todo_test.go
new file mode 100644
index 0000000000..6db4bccb09
--- /dev/null
+++ b/kernel/model/todo_test.go
@@ -0,0 +1,28 @@
+// SiYuan - From thought to insight, with agents
+// 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 model
+
+import "testing"
+
+func TestAgentTodosRejectInvalidSessionID(t *testing.T) {
+ if err := SaveAgentTodos("..", nil); err == nil {
+ t.Fatal("invalid Agent session ID was accepted for saving")
+ }
+ if _, err := LoadAgentTodos(".."); err == nil {
+ t.Fatal("invalid Agent session ID was accepted for loading")
+ }
+}