You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.9 KiB
97 lines
2.9 KiB
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/comm"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/core/cbase"
|
|
"yunyan/lego/sys/log"
|
|
"yunyan/lego/sys/mysql"
|
|
"yunyan/pb"
|
|
"yunyan/utils"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
)
|
|
|
|
// 查询用户任务工具
|
|
type tool_get_user_tasks struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
func (this *tool_get_user_tasks) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.ModuleCompBase.Init(service, module, comp, opt)
|
|
this.module = module.(*Mcp)
|
|
return
|
|
}
|
|
|
|
func (this *tool_get_user_tasks) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
this.module.AddTool(ToolGroup_GLOBAL, this.Tool(), this.Handl)
|
|
return
|
|
}
|
|
|
|
func (this *tool_get_user_tasks) Tool() mcp.Tool {
|
|
return mcp.NewTool("get_user_tasks",
|
|
mcp.WithDescription("查询用户的提醒任务列表,可以按状态筛选"),
|
|
// uid 不再是必填:身份由请求头里的 JWT 决定。保留这个参数只为兼容
|
|
// 百炼后台已配好的工具定义,传上来也只会被拿去和会话 uid 比对。
|
|
mcp.WithString("uid",
|
|
mcp.Description("用户ID(可省略,服务端以登录身份为准)"),
|
|
),
|
|
mcp.WithNumber("status",
|
|
mcp.Description("按状态筛选: -1=全部, 0=进行中, 2=已完成, 3=已取消"),
|
|
mcp.DefaultNumber(-1),
|
|
),
|
|
mcp.WithNumber("limit",
|
|
mcp.Description("返回数量限制 (默认20)"),
|
|
mcp.DefaultNumber(20),
|
|
),
|
|
)
|
|
}
|
|
|
|
func (this *tool_get_user_tasks) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
this.module.Debug("tool_get_user_tasks",
|
|
log.Field{Key: "request.Params.Arguments", Value: request.GetRawArguments()},
|
|
)
|
|
|
|
// ⚠️ 参数里的 uid 只用于比对,**绝不作为身份来源**。真正的身份从
|
|
// Authorization 里的 JWT 解出来(见 auth.go)。此前这里直接信任模型填的 uid,
|
|
// 而 MCP 是公网可达且无鉴权的独立服务,等于谁都能读改任意人的数据。
|
|
argUID := request.GetString("uid", "")
|
|
uid, err := this.module.ResolveUIDWithToken(ctx, argUID, request.GetString("auth_token", ""))
|
|
if err != nil {
|
|
return mcp.NewToolResultError(err.Error()), nil
|
|
}
|
|
status := request.GetInt("status", -1)
|
|
limit := request.GetInt("limit", 20)
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
|
|
var tasks []*pb.DBTask
|
|
tx := mysql.Table(comm.TableAllhelpTask).Where("uid = ?", uid)
|
|
if status >= 0 {
|
|
tx = tx.Where("status = ?", status)
|
|
}
|
|
if dbErr := tx.Order("create_time DESC").Limit(limit).Find(&tasks).Error; dbErr != nil {
|
|
this.module.Errorln("get_user_tasks error:", dbErr)
|
|
return mcp.NewToolResultError("查询任务失败"), nil
|
|
}
|
|
|
|
text := utils.ToString(map[string]interface{}{
|
|
"result": "success",
|
|
"total": len(tasks),
|
|
"tasks": tasks,
|
|
})
|
|
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: text,
|
|
},
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|