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.
108 lines
3.0 KiB
108 lines
3.0 KiB
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/core/cbase"
|
|
"yunyan/lego/sys/log"
|
|
"yunyan/sys/amap"
|
|
"yunyan/utils"
|
|
"fmt"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
)
|
|
|
|
// 地图关键词搜索地址
|
|
type tool_gaode_map_searchplace struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *tool_gaode_map_searchplace) 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_gaode_map_searchplace) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
if this.module.AddTool(ToolGroup_CHINA, this.Tool(), this.Handl) {
|
|
if err := amap.OnInit(this.module.service.GetSettings().Sys["amap"]); err != nil {
|
|
panic(fmt.Sprintf("init sys.amap err: %s", err.Error()))
|
|
} else {
|
|
log.Infof("init sys.amap success!")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
func (this *tool_gaode_map_searchplace) Tool() (tool mcp.Tool) {
|
|
return mcp.NewTool("gaode_map_search_place",
|
|
mcp.WithDescription("根据关键词搜索地图地址,主要用于导航寻找目的地使用,比如查询深圳北站,天安门,北京南站等"),
|
|
mcp.WithString("keywords",
|
|
mcp.Description("搜索关键词 例如:深圳北站,天安门,北京南站等"),
|
|
mcp.Required(),
|
|
),
|
|
mcp.WithString("city",
|
|
mcp.Description("城市名称 例如:北京市"),
|
|
mcp.Required(),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Handl 工具处理接口
|
|
// 参数:
|
|
// - ctx: 上下文
|
|
// - request: MCP 工具调用请求
|
|
//
|
|
// 返回值:
|
|
// - result: MCP 工具调用结果
|
|
// - err: 处理过程中的错误
|
|
//
|
|
// 异常:
|
|
// - 无(不主动 panic)
|
|
func (this *tool_gaode_map_searchplace) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
var (
|
|
city, keywords string
|
|
ampsresult *amap.SearchPlaceResponse
|
|
context string
|
|
)
|
|
this.module.Debug("tool_maps_search_place",
|
|
log.Field{Key: "request.Params.Arguments", Value: request.GetRawArguments()},
|
|
)
|
|
if city, err = request.RequireString("city"); err != nil {
|
|
err = fmt.Errorf("必要参数[city]缺失")
|
|
context = "获取城市名称失败"
|
|
return
|
|
}
|
|
if keywords, err = request.RequireString("keywords"); err != nil {
|
|
err = fmt.Errorf("必要参数[keywords]缺失")
|
|
context = "获取目标地址失败"
|
|
return
|
|
}
|
|
ampsresult, err = amap.SearchPlace(keywords, city)
|
|
if err != nil {
|
|
context = "搜索失败"
|
|
this.module.Debug("tool_gaode_map_search_place fail",
|
|
log.Field{Key: "city", Value: city},
|
|
log.Field{Key: "keywords", Value: keywords},
|
|
log.Field{Key: "err", Value: err},
|
|
)
|
|
return
|
|
} else {
|
|
context = utils.ToString(ampsresult)
|
|
this.module.Debug("tool_gaode_map_search_place success",
|
|
log.Field{Key: "city", Value: city},
|
|
log.Field{Key: "keywords", Value: keywords},
|
|
log.Field{Key: "ampsresult size", Value: len(ampsresult.Pois)},
|
|
)
|
|
}
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: context,
|
|
},
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|