Skip to content

Commit 827d6e6

Browse files
author
piexlMax(奇淼
committed
feat(mcp): 添加菜单列表工具用于获取系统所有菜单信息
1 parent d4a571f commit 827d6e6

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

server/mcp/menu_lister.go

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

0 commit comments

Comments
 (0)