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.
106 lines
2.7 KiB
106 lines
2.7 KiB
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/core/cbase"
|
|
"yunyan/lego/sys/log"
|
|
"yunyan/sys/websearch/tavilysearch"
|
|
"yunyan/utils"
|
|
"fmt"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
)
|
|
|
|
// 天气查询
|
|
type tool_tavily_search struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *tool_tavily_search) 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_tavily_search) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
if this.module.AddTool(ToolGroup_OVERSEAS, this.Tool(), this.Handl) {
|
|
if err := tavilysearch.OnInit(this.module.service.GetSettings().Sys["tavilysearch"]); err != nil {
|
|
panic(fmt.Sprintf("init sys.tavilysearch err: %s", err.Error()))
|
|
} else {
|
|
log.Infof("init sys.tavilysearch success!")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *tool_tavily_search) Tool() (tool mcp.Tool) {
|
|
return mcp.NewTool("tavily_web_search",
|
|
mcp.WithDescription("根据用户的搜索关键字,获取网络搜索结果"),
|
|
mcp.WithString("query",
|
|
mcp.Description("搜索内容 例如:今日热点新闻"),
|
|
mcp.Required(),
|
|
),
|
|
mcp.WithNumber("count",
|
|
mcp.Description("返回结果数量"),
|
|
mcp.DefaultNumber(5),
|
|
mcp.Min(1),
|
|
mcp.Max(10),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Handl 工具处理接口
|
|
// 参数:
|
|
// - ctx: 上下文
|
|
// - request: MCP 工具调用请求
|
|
//
|
|
// 返回值:
|
|
// - result: MCP 工具调用结果
|
|
// - err: 处理过程中的错误
|
|
//
|
|
// 异常:
|
|
// - 无(不主动 panic)
|
|
func (this *tool_tavily_search) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
var (
|
|
query string
|
|
count int
|
|
results *tavilysearch.TavilyResponse
|
|
context string
|
|
)
|
|
this.module.Debug("tool_tavily_search",
|
|
log.Field{Key: "request.Params.Arguments", Value: request.GetRawArguments()},
|
|
)
|
|
count = request.GetInt("count", 10)
|
|
if query, err = request.RequireString("query"); err != nil {
|
|
return
|
|
}
|
|
results, err = tavilysearch.Search(ctx, query, count, 0)
|
|
if err != nil {
|
|
context = "网络搜索失败"
|
|
this.module.Debug("tool_tavily_search fail",
|
|
log.Field{Key: "query", Value: query},
|
|
log.Field{Key: "count", Value: count},
|
|
log.Field{Key: "err", Value: err},
|
|
)
|
|
return
|
|
} else {
|
|
context = utils.ToString(results)
|
|
this.module.Debug("tool_tavily_search success",
|
|
log.Field{Key: "query", Value: query},
|
|
log.Field{Key: "count", Value: count},
|
|
log.Field{Key: "results size", Value: len(results.Results)},
|
|
)
|
|
}
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: context,
|
|
},
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|