fix: implement MCP Streamable HTTP protocol for Cursor integration
- Replace basic JSON-RPC implementation with Streamable HTTP protocol - Add support for GET (SSE) and POST (JSON-RPC) requests - Update protocol version from 2024-11-05 to 2025-03-26 - Fix list_feeds tool definition to match actual API (remove unused page/pageSize params) - Add ping method support for MCP Inspector - Update Cursor configuration to use url field instead of curl command - Add comprehensive MCP integration documentation Fixes #32 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
165
mcp_handlers.go
Normal file
165
mcp_handlers.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// MCP 工具处理函数
|
||||
|
||||
// handleCheckLoginStatus 处理检查登录状态
|
||||
func (s *AppServer) handleCheckLoginStatus(ctx context.Context) *MCPToolResult {
|
||||
logrus.Info("MCP: 检查登录状态")
|
||||
|
||||
status, err := s.xiaohongshuService.CheckLoginStatus(ctx)
|
||||
if err != nil {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: "检查登录状态失败: " + err.Error(),
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
resultText := fmt.Sprintf("登录状态检查成功: %+v", status)
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: resultText,
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// handlePublishContent 处理发布内容
|
||||
func (s *AppServer) handlePublishContent(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
||||
logrus.Info("MCP: 发布内容")
|
||||
|
||||
// 解析参数
|
||||
title, _ := args["title"].(string)
|
||||
content, _ := args["content"].(string)
|
||||
imagePathsInterface, _ := args["images"].([]interface{})
|
||||
|
||||
var imagePaths []string
|
||||
for _, path := range imagePathsInterface {
|
||||
if pathStr, ok := path.(string); ok {
|
||||
imagePaths = append(imagePaths, pathStr)
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Infof("MCP: 发布内容 - 标题: %s, 图片数量: %d", title, len(imagePaths))
|
||||
|
||||
// 构建发布请求
|
||||
req := &PublishRequest{
|
||||
Title: title,
|
||||
Content: content,
|
||||
Images: imagePaths,
|
||||
}
|
||||
|
||||
// 执行发布
|
||||
result, err := s.xiaohongshuService.PublishContent(ctx, req)
|
||||
if err != nil {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: "发布失败: " + err.Error(),
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
resultText := fmt.Sprintf("内容发布成功: %+v", result)
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: resultText,
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// handleListFeeds 处理获取Feeds列表
|
||||
func (s *AppServer) handleListFeeds(ctx context.Context) *MCPToolResult {
|
||||
logrus.Info("MCP: 获取Feeds列表")
|
||||
|
||||
result, err := s.xiaohongshuService.ListFeeds(ctx)
|
||||
if err != nil {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: "获取Feeds列表失败: " + err.Error(),
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化输出,转换为JSON字符串
|
||||
jsonData, err := json.MarshalIndent(result, "", " ")
|
||||
if err != nil {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: fmt.Sprintf("获取Feeds列表成功,但序列化失败: %v", err),
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: string(jsonData),
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// handleSearchFeeds 处理搜索Feeds
|
||||
func (s *AppServer) handleSearchFeeds(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
||||
logrus.Info("MCP: 搜索Feeds")
|
||||
|
||||
// 解析参数
|
||||
keyword, ok := args["keyword"].(string)
|
||||
if !ok || keyword == "" {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: "搜索Feeds失败: 缺少关键词参数",
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Infof("MCP: 搜索Feeds - 关键词: %s", keyword)
|
||||
|
||||
result, err := s.xiaohongshuService.SearchFeeds(ctx, keyword)
|
||||
if err != nil {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: "搜索Feeds失败: " + err.Error(),
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化输出,转换为JSON字符串
|
||||
jsonData, err := json.MarshalIndent(result, "", " ")
|
||||
if err != nil {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: fmt.Sprintf("搜索Feeds成功,但序列化失败: %v", err),
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: string(jsonData),
|
||||
}},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user