Files
xiaohongshu-mcp/server.go
zy 7cd35ebb71 feat: implement xiaohongshu automation with MCP server
Complete implementation of xiaohongshu (Little Red Book) automation system:

### Core Features:
- **QR Code Login**: Automated login with cookie persistence
- **Content Publishing**: Post text, images with AI-powered descriptions
- **Browser Management**: Headless Chrome automation via go-rod
- **Cookie Persistence**: Session management for login state
- **MCP Server**: Model Context Protocol integration for Claude

### Technical Components:
- go-rod browser automation with stealth mode
- MCP server for Claude Code integration
- Cookie-based session management
- Robust error handling and logging
- Cross-platform compatibility

### API Endpoints:
- Login status checking and QR code authentication
- Content publishing with image upload support
- Navigation and page interaction utilities

This provides a complete foundation for xiaohongshu automation
with proper session management and MCP integration.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:09:00 +08:00

87 lines
1.7 KiB
Go

package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// setupRouter 设置路由
func setupRouter() *gin.Engine {
// 设置 Gin 模式
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
// 添加中间件
router.Use(errorHandlingMiddleware())
router.Use(corsMiddleware())
// 健康检查
router.GET("/health", healthHandler)
// MCP 端点 - 使用 SSE 协议
mcpHandler := createMCPHandler()
router.Any("/mcp", gin.WrapH(mcpHandler))
router.Any("/mcp/*path", gin.WrapH(mcpHandler))
// API 路由组
api := router.Group("/api/v1")
{
api.GET("/login/status", checkLoginStatusHandler)
api.POST("/publish", publishHandler)
}
return router
}
// startServer 启动服务器
func startServer() error {
router := setupRouter()
port := ":18060"
server := &http.Server{
Addr: port,
Handler: router,
}
// 启动服务器的 goroutine
go func() {
logrus.Infof("启动 HTTP 服务器: %s", port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logrus.Errorf("服务器启动失败: %v", err)
os.Exit(1)
}
}()
// 等待中断信号
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logrus.Infof("正在关闭服务器...")
// 优雅关闭
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// 关闭 HTTP 服务器
if err := server.Shutdown(ctx); err != nil {
logrus.Errorf("服务器关闭失败: %v", err)
return err
}
logrus.Infof("服务器已关闭")
return nil
}