Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions mcp_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/sirupsen/logrus"
"github.com/xpzouying/xiaohongshu-mcp/cookies"
"github.com/xpzouying/xiaohongshu-mcp/xiaohongshu"
"strings"
"time"
)

// MCP 工具处理函数
Expand All @@ -35,7 +36,7 @@ func (s *AppServer) handleCheckLoginStatus(ctx context.Context) *MCPToolResult {
} else {
resultText = fmt.Sprintf("❌ 未登录\n\n请使用 get_login_qrcode 工具获取二维码进行登录。")
}

return &MCPToolResult{
Content: []MCPContent{{
Type: "text",
Expand Down Expand Up @@ -562,3 +563,69 @@ func (s *AppServer) handlePostComment(ctx context.Context, args map[string]inter
}},
}
}

// mcp: 获取我的个人信息
func (s *AppServer) handleGetMyProfile(ctx context.Context) *MCPToolResult {
myProfile, err := s.xiaohongshuService.GetMyProfile(ctx)
if err != nil {
return &MCPToolResult{
Content: []MCPContent{{
Type: "text",
Text: "获取我的个人信息失败: " + err.Error(),
}},
IsError: true,
}
}

jsonData, err := json.MarshalIndent(myProfile, "", " ")
if err != nil {
return &MCPToolResult{
Content: []MCPContent{{
Type: "text",
Text: fmt.Sprintf("获取我的个人信息,但序列化失败: %v", err),
}},
IsError: true,
}
}

return &MCPToolResult{
Content: []MCPContent{{
Type: "text",
Text: string(jsonData),
}},
}
}

// mcp:获取当前登录用户的第一页feed列表
func (s *AppServer) handleListMyFeeds(ctx context.Context) *MCPToolResult {
myProfile, err := s.xiaohongshuService.GetMyProfile(ctx)

if err != nil {
return &MCPToolResult{
Content: []MCPContent{{
Type: "text",
Text: "获取我的个人信息失败: " + err.Error(),
}},
IsError: true,
}
}

jsonData, err := json.MarshalIndent(myProfile.Feeds, "", " ")
if err != nil {
return &MCPToolResult{
Content: []MCPContent{{
Type: "text",
Text: fmt.Sprintf("获取我的个人信息,但序列化失败: %v", err),
}},
IsError: true,
}
}

return &MCPToolResult{
Content: []MCPContent{{
Type: "text",
Text: string(jsonData),
}},
}

}
26 changes: 25 additions & 1 deletion mcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,31 @@ func registerTools(server *mcp.Server, appServer *AppServer) {
}),
)

logrus.Infof("Registered %d MCP tools", 12)
// 工具 13: 获取当前登录账号的个人信息
mcp.AddTool(server,
&mcp.Tool{
Name: "get_my_profile",
Description: "获取当前登录用户的个人信息 (需要已登录)",
},
withPanicRecovery("get_my_profile", func(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
result := appServer.handleGetMyProfile(ctx)
return convertToMCPResult(result), nil, nil
}),
)

// 工具14: 获取当前登录账号的Feed列表
mcp.AddTool(server,
&mcp.Tool{
Name: "list_my_feeds",
Description: "获取当前登录用户的第一页笔记列表 (需要已登录)",
},
withPanicRecovery("list_my_feeds", func(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
result := appServer.handleListMyFeeds(ctx)
return convertToMCPResult(result), nil, nil
}),
)

logrus.Infof("Registered %d MCP tools", 13)
}

// convertToMCPResult 将自定义的 MCPToolResult 转换为官方 SDK 的格式
Expand Down