refactor(mcp): 更新默认值描述并移除无用工具
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
package mcpTool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterTool(&CurrentTime{})
|
||||
}
|
||||
|
||||
type CurrentTime struct {
|
||||
}
|
||||
|
||||
// 获取当前系统时间
|
||||
func (t *CurrentTime) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
// 获取当前系统时间
|
||||
|
||||
timezone, ok := request.GetArguments()["timezone"].(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("参数错误:timezone 必须是字符串类型")
|
||||
}
|
||||
// 根据timezone参数加载对应时区
|
||||
loc, err := loadTimeZone(timezone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取当前时间并转换为指定时区
|
||||
currentTime := time.Now().In(loc).Format("2006-01-02 15:04:05")
|
||||
//返回
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{
|
||||
mcp.TextContent{
|
||||
Type: "text",
|
||||
Text: fmt.Sprintf("%s 时区的当前时间是:%s", timezone, currentTime),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *CurrentTime) New() mcp.Tool {
|
||||
return mcp.NewTool("currentTime",
|
||||
mcp.WithDescription("获取当前系统时间"),
|
||||
mcp.WithString("timezone",
|
||||
mcp.Required(),
|
||||
mcp.Description("时区"),
|
||||
mcp.Enum("UTC", "CST", "PST", "EST", "GMT", "CET", "JST", "MST", "IST", "AST", "HST"),
|
||||
))
|
||||
}
|
||||
|
||||
// 将简写时区转换为IANA标准时区
|
||||
func loadTimeZone(timezone string) (*time.Location, error) {
|
||||
// 时区映射表
|
||||
timezoneMap := map[string]string{
|
||||
"UTC": "UTC",
|
||||
"CST": "Asia/Shanghai", // 中国标准时间
|
||||
"PST": "America/Los_Angeles",
|
||||
"EST": "America/New_York",
|
||||
"GMT": "GMT",
|
||||
"CET": "Europe/Paris",
|
||||
"JST": "Asia/Tokyo",
|
||||
"MST": "America/Denver",
|
||||
"IST": "Asia/Kolkata",
|
||||
"AST": "Asia/Riyadh", // 阿拉伯标准时间
|
||||
"HST": "Pacific/Honolulu",
|
||||
}
|
||||
|
||||
// 获取标准时区名称
|
||||
tzName, exists := timezoneMap[timezone]
|
||||
if !exists {
|
||||
return nil, errors.New("不支持的时区: " + timezone)
|
||||
}
|
||||
|
||||
// 加载时区
|
||||
return time.LoadLocation(tzName)
|
||||
}
|
@@ -1,79 +0,0 @@
|
||||
package mcpTool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterTool(&GetNickname{})
|
||||
}
|
||||
|
||||
type GetNickname struct{}
|
||||
|
||||
// 根据用户username获取nickname
|
||||
func (t *GetNickname) New() mcp.Tool {
|
||||
return mcp.NewTool("getNickname",
|
||||
mcp.WithDescription("根据用户username获取nickname"),
|
||||
mcp.WithString("username",
|
||||
mcp.Required(),
|
||||
mcp.Description("用户的username"),
|
||||
))
|
||||
}
|
||||
|
||||
// Handle 处理获取昵称的请求
|
||||
func (t *GetNickname) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
// 1. 参数验证
|
||||
username, ok := request.GetArguments()["username"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("参数错误:username 必须是字符串类型")
|
||||
}
|
||||
|
||||
if username == "" {
|
||||
return nil, errors.New("参数错误:username 不能为空")
|
||||
}
|
||||
|
||||
// 2. 记录操作日志
|
||||
global.GVA_LOG.Info("getNickname 工具被调用")
|
||||
|
||||
// 3. 优化查询,只选择需要的字段
|
||||
var user struct {
|
||||
NickName string
|
||||
}
|
||||
|
||||
err := global.GVA_DB.Model(&system.SysUser{}).
|
||||
Select("nick_name").
|
||||
Where("username = ?", username).
|
||||
First(&user).Error
|
||||
|
||||
// 4. 优化错误处理
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{
|
||||
mcp.TextContent{
|
||||
Type: "text",
|
||||
Text: fmt.Sprintf("用户 %s 不存在", username),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
global.GVA_LOG.Error("数据库查询错误")
|
||||
return nil, errors.New("系统错误,请稍后再试")
|
||||
}
|
||||
|
||||
// 构造回复信息
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{
|
||||
mcp.TextContent{
|
||||
Type: "text",
|
||||
Text: fmt.Sprintf("用户 %s 的昵称是 %s", username, user.NickName),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
@@ -188,7 +188,7 @@ func (t *AutomationModuleAnalyzer) New() mcp.Tool {
|
||||
"desc": "详情显示(bool)",
|
||||
"excel": "导入导出(bool)",
|
||||
"require": "是否必填(bool)",
|
||||
"defaultValue": "默认值(string)",
|
||||
"defaultValue": "默认值(string),JSON类型(array,json,file,pictures)请保持为空他们不可以设置默认值",
|
||||
"errorText": "错误提示(string)",
|
||||
"clearable": "是否可清空(bool)",
|
||||
"sort": "是否排序(bool)",
|
||||
|
Reference in New Issue
Block a user