{"record":{"id":"30e6c236df8ad655","repo":"siyuan-note/siyuan","slug":"expected-one-content-item-got-d","errorCode":null,"errorMessage":"expected one content item, got %d","messagePattern":"expected one content item, got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/server.go","lineNumber":348,"sourceCode":"\t\t\tIsError:           result.IsError,\n\t\t}, nil\n\t})\n\treturn true\n}\n\nfunc convertContentItem(item tools.ContentItem) (mcpsdk.Content, error) {\n\tdata, err := json.Marshal(item)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\twrapped := append([]byte(`{\"content\":[`), data...)\n\twrapped = append(wrapped, []byte(`]}`)...)\n\tvar result mcpsdk.CallToolResult\n\tif err = json.Unmarshal(wrapped, &result); err != nil {\n\t\treturn nil, err\n\t}\n\tif len(result.Content) != 1 {\n\t\treturn nil, fmt.Errorf(\"expected one content item, got %d\", len(result.Content))\n\t}\n\treturn result.Content[0], nil\n}\n\nfunc toolErrorResult(message string) *mcpsdk.CallToolResult {\n\treturn &mcpsdk.CallToolResult{\n\t\tContent: []mcpsdk.Content{&mcpsdk.TextContent{Text: message}},\n\t\tIsError: true,\n\t}\n}\n","sourceCodeStart":330,"sourceCodeEnd":359,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/server.go#L330-L359","documentation":"convertContentItem re-marshals a tool result, unmarshals it as mcpsdk.CallToolResult, and expects exactly one content item. If the result carried zero or multiple content entries the conversion is ambiguous, so it errors with the actual count.","triggerScenarios":"A tool handler returns a CallToolResult whose Content slice has length != 1 (empty result, or multi-part content) and the result passes through convertContentItem (used by skill/resource conversion paths and covered by TestSkillResourceContentSurvivesMCPConversion).","commonSituations":"A tool returning plain text without appending a content item; a handler returning multiple content blocks (text + image) where the converter expects a single payload; an SDK shape change altering how content is populated.","solutions":["Make the tool return exactly one content item (join multiple texts into one, or pick/extract the primary item before conversion)","If multiple items are legitimate, change the conversion path to iterate result.Content instead of asserting len == 1","Log result.Content length at the source to find which handler emits unexpected shapes"],"exampleFix":"// before\nreturn &mcpsdk.CallToolResult{Content: []Content{a, b}}, nil\n// after\nreturn &mcpsdk.CallToolResult{Content: []Content{merged}}, nil","handlingStrategy":"validation","validationCode":"var probe mcpsdk.CallToolResult\n_ = json.Unmarshal(raw, &probe)\nif len(probe.Content) != 1 {\n    return fmt.Errorf(\"tool result must contain exactly one content item, got %d\", len(probe.Content))\n}","typeGuard":null,"tryCatchPattern":"item, err := convertContentItem(raw)\nif err != nil && strings.Contains(err.Error(), \"expected one content item\") {\n    // normalize the tool result to a single content item and retry\n}","preventionTips":["Make tool handlers return exactly one content item","Merge multi-part outputs into a single text item before returning","Test converters against empty and multi-content results"],"tags":["mcp","content","conversion"],"backgroundTag":"unexpected-response-shape","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}