menu_lister.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package mcpTool
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/flipped-aurora/gin-vue-admin/server/global"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  8. "github.com/mark3labs/mcp-go/mcp"
  9. "go.uber.org/zap"
  10. )
  11. // 注册工具
  12. func init() {
  13. // 注册工具将在enter.go中统一处理
  14. RegisterTool(&MenuLister{})
  15. }
  16. // MenuListResponse 菜单列表响应结构
  17. type MenuListResponse struct {
  18. Success bool `json:"success"`
  19. Message string `json:"message"`
  20. Menus []system.SysBaseMenu `json:"menus"`
  21. TotalCount int `json:"totalCount"`
  22. Description string `json:"description"`
  23. }
  24. // MenuLister 菜单列表工具
  25. type MenuLister struct{}
  26. // New 创建菜单列表工具
  27. func (m *MenuLister) New() mcp.Tool {
  28. return mcp.NewTool("list_all_menus",
  29. mcp.WithDescription(`获取系统中所有菜单信息,包括菜单树结构、路由信息、组件路径等,用于前端编写vue-router时正确跳转
  30. **功能说明:**
  31. - 返回完整的菜单树形结构
  32. - 包含路由配置信息(path、name、component)
  33. - 包含菜单元数据(title、icon、keepAlive等)
  34. - 包含菜单参数和按钮配置
  35. - 支持父子菜单关系展示
  36. **使用场景:**
  37. - 前端路由配置:获取所有菜单信息用于配置vue-router
  38. - 菜单权限管理:了解系统中所有可用的菜单项
  39. - 导航组件开发:构建动态导航菜单
  40. - 系统架构分析:了解系统的菜单结构和页面组织`),
  41. )
  42. }
  43. // Handle 处理菜单列表请求
  44. func (m *MenuLister) Handle(_ context.Context, _ mcp.CallToolRequest) (*mcp.CallToolResult, error) {
  45. // 获取所有基础菜单
  46. allMenus, err := m.getAllMenus()
  47. if err != nil {
  48. global.GVA_LOG.Error("获取菜单列表失败", zap.Error(err))
  49. return &mcp.CallToolResult{
  50. Content: []mcp.Content{
  51. mcp.TextContent{
  52. Type: "text",
  53. Text: fmt.Sprintf("获取菜单列表失败: %v", err),
  54. },
  55. },
  56. IsError: true,
  57. }, nil
  58. }
  59. // 构建返回结果
  60. response := MenuListResponse{
  61. Success: true,
  62. Message: "获取菜单列表成功",
  63. Menus: allMenus,
  64. TotalCount: len(allMenus),
  65. Description: "系统中所有菜单信息的标准列表,包含路由配置和组件信息",
  66. }
  67. // 序列化响应
  68. responseJSON, err := json.MarshalIndent(response, "", " ")
  69. if err != nil {
  70. global.GVA_LOG.Error("序列化菜单响应失败", zap.Error(err))
  71. return &mcp.CallToolResult{
  72. Content: []mcp.Content{
  73. mcp.TextContent{
  74. Type: "text",
  75. Text: fmt.Sprintf("序列化响应失败: %v", err),
  76. },
  77. },
  78. IsError: true,
  79. }, nil
  80. }
  81. return &mcp.CallToolResult{
  82. Content: []mcp.Content{
  83. mcp.TextContent{
  84. Type: "text",
  85. Text: string(responseJSON),
  86. },
  87. },
  88. }, nil
  89. }
  90. // getAllMenus 获取所有基础菜单
  91. func (m *MenuLister) getAllMenus() ([]system.SysBaseMenu, error) {
  92. var menus []system.SysBaseMenu
  93. err := global.GVA_DB.Order("sort").Preload("Parameters").Preload("MenuBtn").Find(&menus).Error
  94. if err != nil {
  95. return nil, err
  96. }
  97. return menus, nil
  98. }