An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Handle null and reject non-string MCP attribute values https://github.com/siyuan-note/siyuan/issues/19495
Danielbba919c4510f
diff --git a/kernel/mcp/tools/attr.go b/kernel/mcp/tools/attr.goindex f4ff37cf64..38ff2eeac4 100644--- a/kernel/mcp/tools/attr.go+++ b/kernel/mcp/tools/attr.go@@ -34,7 +34,7 @@ var AttrTool = &Tool{"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).\nCommon attributes:\n- icon: emoji hex codepoint like \"1f4ca\", emoji character like \"📊\", custom image path like \"1/b3log.png\", network image URL like \"https://example.com/icon.png\", or dynamic icon URL like \"api/icon/getDynamicIcon?type=8&color=%23d23f31&content=SiYuan&id=xxx\"\n- title-img: CSS format like 'background-image:url(\"assets/example.jpg\")', NOT a bare asset path\n- tags: comma-separated tag names"},+ "attrs": {Type: "object", Description: "Attribute key-value pairs (for set). Values must be strings or null; null and empty strings delete attributes.\nCommon attributes:\n- icon: emoji hex codepoint like \"1f4ca\", emoji character like \"📊\", custom image path like \"1/b3log.png\", network image URL like \"https://example.com/icon.png\", or dynamic icon URL like \"api/icon/getDynamicIcon?type=8&color=%23d23f31&content=SiYuan&id=xxx\"\n- title-img: CSS format like 'background-image:url(\"assets/example.jpg\")', NOT a bare asset path\n- tags: comma-separated tag names"},},Required: []string{"action"},},@@ -91,9 +91,9 @@ func attrSet(args map[string]any) (CallToolResult, error) {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "attrs (key-value object) is required"}}, IsError: true}, nil}- nameValues := make(map[string]string, len(attrsArg))- for k, v := range attrsArg {- nameValues[k] = fmt.Sprintf("%v", v)+ nameValues, err := parseAttrValues(attrsArg)+ if err != nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: err.Error()}}, IsError: true}, nil}if err := model.SetBlockAttrs(id, nameValues); err != nil {@@ -104,6 +104,21 @@ func attrSet(args map[string]any) (CallToolResult, error) {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "attributes set for: " + id}}}, nil}+func parseAttrValues(attrs map[string]any) (map[string]string, error) {+ nameValues := make(map[string]string, len(attrs))+ for name, value := range attrs {+ switch typed := value.(type) {+ case nil:+ nameValues[name] = ""+ case string:+ nameValues[name] = typed+ default:+ return nil, fmt.Errorf("attr %q must be a string or null (got %T)", name, value)+ }+ }+ return nameValues, nil+}+func attrBatchGet(args map[string]any) (CallToolResult, error) {idsStr, _ := args["ids"].(string)if idsStr == "" {diff --git a/kernel/mcp/tools/attr_test.go b/kernel/mcp/tools/attr_test.gonew file mode 100644index 0000000000..9a6b386a28--- /dev/null+++ b/kernel/mcp/tools/attr_test.go@@ -0,0 +1,64 @@+// 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.++package tools++import (+ "encoding/json"+ "reflect"+ "strings"+ "testing"+)++func TestParseAttrValues(t *testing.T) {+ var attrs map[string]any+ if err := json.Unmarshal([]byte(`{"custom-null":null,"custom-empty":"","custom-text":"baseline","custom-literal":"<nil>"}`), &attrs); err != nil {+ t.Fatal(err)+ }+ got, err := parseAttrValues(attrs)+ want := map[string]string{"custom-null": "", "custom-empty": "", "custom-text": "baseline", "custom-literal": "<nil>"}+ if err != nil || !reflect.DeepEqual(got, want) {+ t.Fatalf("parseAttrValues() = %v, %v; want %v", got, err, want)+ }+}++func TestAttrSetRejectsNonStringValues(t *testing.T) {+ for _, value := range []string{`5`, `true`, `{"a":1,"b":"x"}`, `["a","b"]`} {+ t.Run(value, func(t *testing.T) {+ var args map[string]any+ if err := json.Unmarshal([]byte(`{"action":"set","id":"20260914120000-abcdefg","attrs":{"custom-valid":"baseline","custom-invalid":`+value+`}}`), &args); err != nil {+ t.Fatal(err)+ }+ attrs, err := parseAttrValues(args["attrs"].(map[string]any))+ if err == nil || attrs != nil {+ t.Fatalf("invalid batch must return no attributes: %v, %v", attrs, err)+ }+ result, err := attrHandler(args)+ if err != nil || !result.IsError || len(result.Content) != 1 || !strings.Contains(result.Content[0].Text, `attr "custom-invalid" must be a string or null`) {+ t.Fatalf("unexpected result: %+v, %v", result, err)+ }+ })+ }+}++func TestAttrHandlerRequiredArguments(t *testing.T) {+ for _, args := range []map[string]any{+ {"action": "get"},+ {"action": "batch-get"},+ {"action": "set"},+ {"action": "set", "id": "20260914120000-abcdefg"},+ {"action": "set", "id": "20260914120000-abcdefg", "attrs": map[string]any{}},+ {"action": "set", "id": "20260914120000-abcdefg", "attrs": "invalid"},+ {"action": "unknown"},+ } {+ result, err := attrHandler(args)+ if err != nil || !result.IsError {+ t.Fatalf("expected argument error for %v: %+v, %v", args, result, err)+ }+ }+}