refactor: 迁移到官方 MCP SDK (#167)

- 添加官方 SDK 依赖 github.com/modelcontextprotocol/go-sdk v0.7.0
- 新增 mcp_server.go 使用官方 SDK 注册 8 个 MCP 工具
- 删除自实现的 streamable_http.go(约 400 行)
- 更新 routes.go 使用 mcp.NewStreamableHTTPHandler
- 优化服务器优雅关闭逻辑(5秒超时 + 警告日志)
- 清理 types.go 中的 JSON-RPC 相关类型

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

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
zy
2025-09-27 01:44:02 +08:00
committed by GitHub
parent 529ee71144
commit 53ea832773
8 changed files with 270 additions and 459 deletions

View File

@@ -9,21 +9,28 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/sirupsen/logrus"
)
// AppServer 应用服务器结构体,封装所有服务和处理器
type AppServer struct {
xiaohongshuService *XiaohongshuService
mcpServer *mcp.Server
router *gin.Engine
httpServer *http.Server
}
// NewAppServer 创建新的应用服务器实例
func NewAppServer(xiaohongshuService *XiaohongshuService) *AppServer {
return &AppServer{
appServer := &AppServer{
xiaohongshuService: xiaohongshuService,
}
// 初始化 MCP Server需要在创建 appServer 之后,因为工具注册需要访问 appServer
appServer.mcpServer = InitMCPServer(appServer)
return appServer
}
// Start 启动服务器
@@ -51,16 +58,14 @@ func (s *AppServer) Start(port string) error {
logrus.Infof("正在关闭服务器...")
// 优雅关闭
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// 关闭 HTTP 服务器
if err := s.httpServer.Shutdown(ctx); err != nil {
logrus.Errorf("服务器关闭失败: %v", err)
return err
logrus.Warnf("等待连接关闭超时,强制退出: %v", err)
} else {
logrus.Infof("服务器已优雅关闭")
}
logrus.Infof("服务器已关闭")
return nil
}