siyuan-note/siyuan · error

expected one content item, got

Error message

expected one content item, got %d

What it means

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.

Solutions

  1. Make the tool return exactly one content item (join multiple texts into one, or pick/extract the primary item before conversion)
  2. If multiple items are legitimate, change the conversion path to iterate result.Content instead of asserting len == 1
  3. Log result.Content length at the source to find which handler emits unexpected shapes

Example fix

// before
return &mcpsdk.CallToolResult{Content: []Content{a, b}}, nil
// after
return &mcpsdk.CallToolResult{Content: []Content{merged}}, nil
Defensive patterns

Strategy: validation

Validate before calling

var probe mcpsdk.CallToolResult
_ = json.Unmarshal(raw, &probe)
if len(probe.Content) != 1 {
    return fmt.Errorf("tool result must contain exactly one content item, got %d", len(probe.Content))
}

Try / catch

item, err := convertContentItem(raw)
if err != nil && strings.Contains(err.Error(), "expected one content item") {
    // normalize the tool result to a single content item and retry
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/30e6c236df8ad655. Report an issue: GitHub.

Appendix: source

Thrown at kernel/mcp/server.go:348

			IsError:           result.IsError,
		}, nil
	})
	return true
}

func convertContentItem(item tools.ContentItem) (mcpsdk.Content, error) {
	data, err := json.Marshal(item)
	if err != nil {
		return nil, err
	}
	wrapped := append([]byte(`{"content":[`), data...)
	wrapped = append(wrapped, []byte(`]}`)...)
	var result mcpsdk.CallToolResult
	if err = json.Unmarshal(wrapped, &result); err != nil {
		return nil, err
	}
	if len(result.Content) != 1 {
		return nil, fmt.Errorf("expected one content item, got %d", len(result.Content))
	}
	return result.Content[0], nil
}

func toolErrorResult(message string) *mcpsdk.CallToolResult {
	return &mcpsdk.CallToolResult{
		Content: []mcpsdk.Content{&mcpsdk.TextContent{Text: message}},
		IsError: true,
	}
}

View on GitHub (pinned to 9f775e8a12)