Skip to content

Commit 9cdf76a

Browse files
author
piexlMax(奇淼
committed
docs(mcp): 更新API创建工具的说明和错误处理日志
1 parent e310007 commit 9cdf76a

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

server/mcp/api_creator.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ApiCreator struct{}
4141
// New 创建API创建工具
4242
func (a *ApiCreator) New() mcp.Tool {
4343
return mcp.NewTool("create_api",
44-
mcp.WithDescription("创建后端API记录,用于在生成后端接口时自动创建对应的API权限记录,只要API层发生过变化,都需要调用此mcp。"),
44+
mcp.WithDescription("创建后端API记录,用于在生成后端接口时自动创建对应的API权限记录,只要创建了API层,router下的文件产生了路径变化等,都需要调用此mcp。"),
4545
mcp.WithString("path",
4646
mcp.Required(),
4747
mcp.Description("API路径,如:/user/create"),
@@ -67,9 +67,9 @@ func (a *ApiCreator) New() mcp.Tool {
6767
// Handle 处理API创建请求
6868
func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
6969
args := request.GetArguments()
70-
70+
7171
var apis []ApiCreateRequest
72-
72+
7373
// 检查是否是批量创建
7474
if apisStr, ok := args["apis"].(string); ok && apisStr != "" {
7575
if err := json.Unmarshal([]byte(apisStr), &apis); err != nil {
@@ -81,54 +81,54 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
8181
if !ok || path == "" {
8282
return nil, errors.New("path 参数是必需的")
8383
}
84-
84+
8585
description, ok := args["description"].(string)
8686
if !ok || description == "" {
8787
return nil, errors.New("description 参数是必需的")
8888
}
89-
89+
9090
apiGroup, ok := args["apiGroup"].(string)
9191
if !ok || apiGroup == "" {
9292
return nil, errors.New("apiGroup 参数是必需的")
9393
}
94-
94+
9595
method := "POST"
9696
if val, ok := args["method"].(string); ok && val != "" {
9797
method = val
9898
}
99-
99+
100100
apis = append(apis, ApiCreateRequest{
101101
Path: path,
102102
Description: description,
103103
ApiGroup: apiGroup,
104104
Method: method,
105105
})
106106
}
107-
107+
108108
if len(apis) == 0 {
109109
return nil, errors.New("没有要创建的API")
110110
}
111-
111+
112112
// 创建API记录
113113
apiService := service.ServiceGroupApp.SystemServiceGroup.ApiService
114114
var responses []ApiCreateResponse
115115
successCount := 0
116-
116+
117117
for _, apiReq := range apis {
118118
api := system.SysApi{
119119
Path: apiReq.Path,
120120
Description: apiReq.Description,
121121
ApiGroup: apiReq.ApiGroup,
122122
Method: apiReq.Method,
123123
}
124-
124+
125125
err := apiService.CreateApi(api)
126126
if err != nil {
127-
global.GVA_LOG.Warn("创建API失败",
127+
global.GVA_LOG.Warn("创建API失败",
128128
zap.String("path", apiReq.Path),
129129
zap.String("method", apiReq.Method),
130130
zap.Error(err))
131-
131+
132132
responses = append(responses, ApiCreateResponse{
133133
Success: false,
134134
Message: fmt.Sprintf("创建API失败: %v", err),
@@ -142,7 +142,7 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
142142
if err != nil {
143143
global.GVA_LOG.Warn("获取创建的API ID失败", zap.Error(err))
144144
}
145-
145+
146146
responses = append(responses, ApiCreateResponse{
147147
Success: true,
148148
Message: fmt.Sprintf("成功创建API %s %s", apiReq.Method, apiReq.Path),
@@ -153,15 +153,15 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
153153
successCount++
154154
}
155155
}
156-
156+
157157
// 构建总体响应
158158
var resultMessage string
159159
if len(apis) == 1 {
160160
resultMessage = responses[0].Message
161161
} else {
162162
resultMessage = fmt.Sprintf("批量创建API完成,成功 %d 个,失败 %d 个", successCount, len(apis)-successCount)
163163
}
164-
164+
165165
result := map[string]interface{}{
166166
"success": successCount > 0,
167167
"message": resultMessage,
@@ -170,12 +170,12 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
170170
"failedCount": len(apis) - successCount,
171171
"details": responses,
172172
}
173-
173+
174174
resultJSON, err := json.MarshalIndent(result, "", " ")
175175
if err != nil {
176176
return nil, fmt.Errorf("序列化结果失败: %v", err)
177177
}
178-
178+
179179
// 添加权限分配提醒
180180
permissionReminder := "\n\n⚠️ 重要提醒:\n" +
181181
"API创建完成后,请前往【系统管理】->【角色管理】中为相关角色分配新创建的API权限," +
@@ -194,4 +194,4 @@ func (a *ApiCreator) Handle(ctx context.Context, request mcp.CallToolRequest) (*
194194
},
195195
},
196196
}, nil
197-
}
197+
}

0 commit comments

Comments
 (0)