@@ -41,7 +41,7 @@ type ApiCreator struct{}
41
41
// New 创建API创建工具
42
42
func (a * ApiCreator ) New () mcp.Tool {
43
43
return mcp .NewTool ("create_api" ,
44
- mcp .WithDescription ("创建后端API记录,用于在生成后端接口时自动创建对应的API权限记录,只要API层发生过变化 ,都需要调用此mcp。" ),
44
+ mcp .WithDescription ("创建后端API记录,用于在生成后端接口时自动创建对应的API权限记录,只要创建了API层,router下的文件产生了路径变化等 ,都需要调用此mcp。" ),
45
45
mcp .WithString ("path" ,
46
46
mcp .Required (),
47
47
mcp .Description ("API路径,如:/user/create" ),
@@ -67,9 +67,9 @@ func (a *ApiCreator) New() mcp.Tool {
67
67
// Handle 处理API创建请求
68
68
func (a * ApiCreator ) Handle (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
69
69
args := request .GetArguments ()
70
-
70
+
71
71
var apis []ApiCreateRequest
72
-
72
+
73
73
// 检查是否是批量创建
74
74
if apisStr , ok := args ["apis" ].(string ); ok && apisStr != "" {
75
75
if err := json .Unmarshal ([]byte (apisStr ), & apis ); err != nil {
@@ -81,54 +81,54 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
81
81
if ! ok || path == "" {
82
82
return nil , errors .New ("path 参数是必需的" )
83
83
}
84
-
84
+
85
85
description , ok := args ["description" ].(string )
86
86
if ! ok || description == "" {
87
87
return nil , errors .New ("description 参数是必需的" )
88
88
}
89
-
89
+
90
90
apiGroup , ok := args ["apiGroup" ].(string )
91
91
if ! ok || apiGroup == "" {
92
92
return nil , errors .New ("apiGroup 参数是必需的" )
93
93
}
94
-
94
+
95
95
method := "POST"
96
96
if val , ok := args ["method" ].(string ); ok && val != "" {
97
97
method = val
98
98
}
99
-
99
+
100
100
apis = append (apis , ApiCreateRequest {
101
101
Path : path ,
102
102
Description : description ,
103
103
ApiGroup : apiGroup ,
104
104
Method : method ,
105
105
})
106
106
}
107
-
107
+
108
108
if len (apis ) == 0 {
109
109
return nil , errors .New ("没有要创建的API" )
110
110
}
111
-
111
+
112
112
// 创建API记录
113
113
apiService := service .ServiceGroupApp .SystemServiceGroup .ApiService
114
114
var responses []ApiCreateResponse
115
115
successCount := 0
116
-
116
+
117
117
for _ , apiReq := range apis {
118
118
api := system.SysApi {
119
119
Path : apiReq .Path ,
120
120
Description : apiReq .Description ,
121
121
ApiGroup : apiReq .ApiGroup ,
122
122
Method : apiReq .Method ,
123
123
}
124
-
124
+
125
125
err := apiService .CreateApi (api )
126
126
if err != nil {
127
- global .GVA_LOG .Warn ("创建API失败" ,
127
+ global .GVA_LOG .Warn ("创建API失败" ,
128
128
zap .String ("path" , apiReq .Path ),
129
129
zap .String ("method" , apiReq .Method ),
130
130
zap .Error (err ))
131
-
131
+
132
132
responses = append (responses , ApiCreateResponse {
133
133
Success : false ,
134
134
Message : fmt .Sprintf ("创建API失败: %v" , err ),
@@ -142,7 +142,7 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
142
142
if err != nil {
143
143
global .GVA_LOG .Warn ("获取创建的API ID失败" , zap .Error (err ))
144
144
}
145
-
145
+
146
146
responses = append (responses , ApiCreateResponse {
147
147
Success : true ,
148
148
Message : fmt .Sprintf ("成功创建API %s %s" , apiReq .Method , apiReq .Path ),
@@ -153,15 +153,15 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
153
153
successCount ++
154
154
}
155
155
}
156
-
156
+
157
157
// 构建总体响应
158
158
var resultMessage string
159
159
if len (apis ) == 1 {
160
160
resultMessage = responses [0 ].Message
161
161
} else {
162
162
resultMessage = fmt .Sprintf ("批量创建API完成,成功 %d 个,失败 %d 个" , successCount , len (apis )- successCount )
163
163
}
164
-
164
+
165
165
result := map [string ]interface {}{
166
166
"success" : successCount > 0 ,
167
167
"message" : resultMessage ,
@@ -170,12 +170,12 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
170
170
"failedCount" : len (apis ) - successCount ,
171
171
"details" : responses ,
172
172
}
173
-
173
+
174
174
resultJSON , err := json .MarshalIndent (result , "" , " " )
175
175
if err != nil {
176
176
return nil , fmt .Errorf ("序列化结果失败: %v" , err )
177
177
}
178
-
178
+
179
179
// 添加权限分配提醒
180
180
permissionReminder := "\n \n ⚠️ 重要提醒:\n " +
181
181
"API创建完成后,请前往【系统管理】->【角色管理】中为相关角色分配新创建的API权限," +
@@ -194,4 +194,4 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
194
194
},
195
195
},
196
196
}, nil
197
- }
197
+ }
0 commit comments