From 8d089f59f84950147b081b9dbd5dc45a55fda24b Mon Sep 17 00:00:00 2001 From: chekayo <9827969+chekayo@user.noreply.gitee.com> Date: Fri, 10 Oct 2025 00:23:51 +0800 Subject: [PATCH] refactor: enhance reply comment functionality with improved error handling and response structure - Simplified error handling in handleReplyComment to check for both comment_id and user_id simultaneously. - Updated response message to include both Comment ID and User ID upon successful reply. - Modified ReplyCommentArgs struct to make comment_id and user_id optional. - Renamed MCP tool for replying to comments for clarity. --- mcp_handlers.go | 22 ++++++---------------- mcp_server.go | 15 +++++++++++---- service.go | 14 +++++++++++--- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/mcp_handlers.go b/mcp_handlers.go index 5bcd1fb..9e89a3e 100644 --- a/mcp_handlers.go +++ b/mcp_handlers.go @@ -552,23 +552,13 @@ func (s *AppServer) handleReplyComment(ctx context.Context, args map[string]inte } } - commentID, ok := args["comment_id"].(string) - if !ok || commentID == "" { + commentID, _ := args["comment_id"].(string) + userID, _ := args["user_id"].(string) + if commentID == "" && userID == "" { return &MCPToolResult{ Content: []MCPContent{{ Type: "text", - Text: "回复评论失败: 缺少comment_id参数", - }}, - IsError: true, - } - } - - userID, ok := args["user_id"].(string) - if !ok || userID == "" { - return &MCPToolResult{ - Content: []MCPContent{{ - Type: "text", - Text: "回复评论失败: 缺少user_id参数", + Text: "回复评论失败: 缺少comment_id或user_id参数", }}, IsError: true, } @@ -600,11 +590,11 @@ func (s *AppServer) handleReplyComment(ctx context.Context, args map[string]inte } // 返回成功结果 - resultText := fmt.Sprintf("回复评论成功 - Feed ID: %s", result.FeedID) + responseText := fmt.Sprintf("评论回复成功 - Feed ID: %s, Comment ID: %s, User ID: %s", result.FeedID, result.TargetCommentID, result.TargetUserID) return &MCPToolResult{ Content: []MCPContent{{ Type: "text", - Text: resultText, + Text: responseText, }}, } } diff --git a/mcp_server.go b/mcp_server.go index 81c9a2b..c190c65 100644 --- a/mcp_server.go +++ b/mcp_server.go @@ -54,8 +54,8 @@ type PostCommentArgs struct { type ReplyCommentArgs struct { FeedID string `json:"feed_id" jsonschema:"小红书笔记ID,从Feed列表获取"` XsecToken string `json:"xsec_token" jsonschema:"访问令牌,从Feed列表的xsecToken字段获取"` - CommentID string `json:"comment_id" jsonschema:"评论ID"` - UserID string `json:"user_id" jsonschema:"用户ID"` + CommentID string `json:"comment_id,omitempty" jsonschema:"目标评论ID,从评论列表获取"` + UserID string `json:"user_id,omitempty" jsonschema:"目标评论用户ID,从评论列表获取"` Content string `json:"content" jsonschema:"回复内容"` } @@ -216,10 +216,17 @@ func registerTools(server *mcp.Server, appServer *AppServer) { // 工具 9: 回复评论 mcp.AddTool(server, &mcp.Tool{ - Name: "reply_to_comment", - Description: "回复小红书笔记的评论", + Name: "reply_comment_in_feed", + Description: "回复小红书笔记下的指定评论", }, func(ctx context.Context, req *mcp.CallToolRequest, args ReplyCommentArgs) (*mcp.CallToolResult, any, error) { + if args.CommentID == "" && args.UserID == "" { + return &mcp.CallToolResult{ + IsError: true, + Content: []mcp.Content{&mcp.TextContent{Text: "缺少 comment_id 或 user_id"}}, + }, nil, nil + } + argsMap := map[string]interface{}{ "feed_id": args.FeedID, "xsec_token": args.XsecToken, diff --git a/service.go b/service.go index 152adad..23bf100 100644 --- a/service.go +++ b/service.go @@ -443,8 +443,8 @@ func (s *XiaohongshuService) UnfavoriteFeed(ctx context.Context, feedID, xsecTok return &ActionResult{FeedID: feedID, Success: true, Message: "取消收藏成功或未收藏"}, nil } -// ReplyCommentToFeed 回复笔记评论 -func (s *XiaohongshuService) ReplyCommentToFeed(ctx context.Context, feedID, xsecToken, commentID, userID, content string) (*ActionResult, error) { +// ReplyCommentToFeed 回复指定评论 +func (s *XiaohongshuService) ReplyCommentToFeed(ctx context.Context, feedID, xsecToken, commentID, userID, content string) (*ReplyCommentResponse, error) { b := newBrowser() defer b.Close() @@ -452,10 +452,18 @@ func (s *XiaohongshuService) ReplyCommentToFeed(ctx context.Context, feedID, xse defer page.Close() action := xiaohongshu.NewCommentFeedAction(page) + if err := action.ReplyToComment(ctx, feedID, xsecToken, commentID, userID, content); err != nil { return nil, err } - return &ActionResult{FeedID: feedID, Success: true, Message: "回复评论成功"}, nil + + return &ReplyCommentResponse{ + FeedID: feedID, + TargetCommentID: commentID, + TargetUserID: userID, + Success: true, + Message: "评论回复成功", + }, nil } func newBrowser() *headless_browser.Browser {