xpzouying/xiaohongshu-mcp · error
缺少 comment_id
Error message
缺少 comment_id
What it means
Like 方法的参数校验守卫:commentID trim 后为空字符串,调用方未提供要点赞的评论 ID,属必填参数缺失,非页面操作故障。
Source
Thrown at xiaohongshu/notification_like.go:29
"github.com/xpzouying/xiaohongshu-mcp/humanize"
)
// NotificationLikeResult 通知点赞的结果。
type NotificationLikeResult struct {
CommentID string `json:"comment_id"`
Nickname string `json:"nickname"`
Liked bool `json:"liked"`
// Skipped 表示调用前已是目标状态,本次未操作。
Skipped bool `json:"skipped"`
}
// likeSettleTimeout 是等待点赞状态生效的上限。
const likeSettleTimeout = 15 * time.Second
// Like 给一条评论点赞或取消点赞;已是目标状态时直接返回。
func (n *NotificationAction) Like(ctx context.Context, commentID string, unlike bool) (*NotificationLikeResult, error) {
if strings.TrimSpace(commentID) == "" {
return nil, fmt.Errorf("缺少 comment_id")
}
want := !unlike
page := n.page.Timeout(3 * time.Minute)
page.MustNavigate("https://www.xiaohongshu.com/notification").MustWaitLoad()
humanize.Delay(ctx, humanize.AfterNavigate)
target, index, err := n.locate(ctx, page, commentID)
if err != nil {
return nil, err
}
result := &NotificationLikeResult{
CommentID: commentID,
Nickname: target.from().Nickname,
Liked: want,
}View on GitHub (pinned to 332d196854)
Solutions
- 调用前用 strings.TrimSpace 校验 commentID 非空
- 从 List 结果中复制 commentID 时确认字段确实有值
- 在调用方对空值提前返回,不进入自动化流程
Example fix
// before
n.Like(ctx, commentID, false) // commentID 可能为 ""
// after
if strings.TrimSpace(commentID) == "" { return errors.New("comment_id required") }
n.Like(ctx, strings.TrimSpace(commentID), false) Defensive patterns
Strategy: validation
Validate before calling
func validCommentID(id string) bool { return strings.TrimSpace(id) != "" }
if !validCommentID(commentID) { return errors.New("comment_id required") } Type guard
func hasCommentID(raw map[string]string) (string, bool) {
id, ok := raw["comment_id"]
return id, ok && strings.TrimSpace(id) != ""
} Try / catch
if _, err := n.Like(ctx, commentID, false); err != nil && strings.Contains(err.Error(), "缺少 comment_id") {
return fmt.Errorf("caller bug: empty comment_id: %w", err)
} Prevention
- 封装统一参数校验函数,所有 action 调用前先过校验
- 从 JSON 反序列化后立即检查关键字段非空
- 对上游数据做白名单/必填字段检查
When it happens
Trigger: 调用 Like(ctx, "", unlike) 或传入只含空格的 commentID。
Common situations: 上游 list_notifications 结果未做判空就透传;JSON 反序列化时字段缺失得到空串;字符串裁剪后忘记重新赋值。
Understand the failure class
Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.
Related errors
AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05).
Data as JSON: /api/errors/63a0c6589479bc34.
Report an issue: GitHub.