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.
 
 
 
 
 
 

127 lines
3.6 KiB

package mcp
import (
"context"
"yunyan/lego/core"
"yunyan/lego/core/cbase"
"yunyan/lego/sys/log"
"yunyan/sys/googlemap"
"yunyan/utils"
"fmt"
"github.com/mark3labs/mcp-go/mcp"
)
// 天气查询
type tool_google_map_nearbysearch struct {
cbase.ModuleCompBase
module *Mcp
}
// 组件初始化接口
func (this *tool_google_map_nearbysearch) 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_google_map_nearbysearch) Start() (err error) {
err = this.ModuleCompBase.Start()
if this.module.AddTool(ToolGroup_OVERSEAS, this.Tool(), this.Handl) {
if err := googlemap.OnInit(this.module.service.GetSettings().Sys["googlemap"]); err != nil {
panic(fmt.Sprintf("init sys.googlemap err: %s", err.Error()))
} else {
log.Infof("init sys.googlemap success!")
}
}
return
}
func (this *tool_google_map_nearbysearch) Tool() (tool mcp.Tool) {
return mcp.NewTool("google_map_nearbysearch",
mcp.WithDescription("根据用户提供的地点和半径,搜索附近的地点"),
mcp.WithString("lat",
mcp.Description("纬度坐标 例如:39.90909"),
mcp.Required(),
),
mcp.WithString("lng",
mcp.Description("经度坐标 例如:116.434307"),
mcp.Required(),
),
mcp.WithString("radius",
mcp.Description("搜索半径,默认5000米,单位:米"),
mcp.DefaultString("5000"),
),
mcp.WithString("keyword",
mcp.Description("搜索关键词,比如:美食,酒店,银行"),
mcp.Required(),
),
)
}
// Handl 工具处理接口
// 参数:
// - ctx: 上下文
// - request: MCP 工具调用请求
//
// 返回值:
// - result: MCP 工具调用结果
// - err: 处理过程中的错误
//
// 异常:
// - 无(不主动 panic)
func (this *tool_google_map_nearbysearch) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
var (
lat, lng, location string
radius string
keyword string
context string
)
this.module.Debug("tool_google_map_nearbysearch",
log.Field{Key: "request.Params.Arguments", Value: request.GetRawArguments()},
)
if lat, err = request.RequireString("lat"); err != nil || lat == "" {
err = fmt.Errorf("必要参数:lat丢失")
context = "获取纬度坐标失败"
return
}
if lng, err = request.RequireString("lng"); err != nil || lng == "" {
err = fmt.Errorf("必要参数:lng丢失")
context = "获取经度坐标失败"
return
}
location = fmt.Sprintf("%s,%s", lat, lng)
radius = request.GetString("radius", "5000")
if keyword, err = request.RequireString("keyword"); err != nil || keyword == "" {
err = fmt.Errorf("必要参数:keyword丢失")
context = "获取搜索关键词失败"
return
}
georesult, err := googlemap.NearbySearch(location, radius, keyword)
if err != nil {
context = "地点搜索失败"
this.module.Debug("tool_google_map_nearbysearch fail",
log.Field{Key: "location", Value: location},
log.Field{Key: "radius", Value: radius},
log.Field{Key: "georesult", Value: georesult},
log.Field{Key: "err", Value: err},
)
return
} else {
context = utils.ToString(georesult)
this.module.Debug("tool_google_map_nearbysearch success",
log.Field{Key: "location", Value: location},
log.Field{Key: "radius", Value: radius},
log.Field{Key: "keyword", Value: keyword},
log.Field{Key: "georesult size", Value: len(georesult.Items)},
)
}
result = &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: context,
},
},
}
return
}