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.
128 lines
3.5 KiB
128 lines
3.5 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_regeocode struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
// 组件初始化接口
|
|
// 参数:
|
|
// - service: 服务实例
|
|
// - module: 模块实例
|
|
// - comp: 组件实例
|
|
// - opt: 组件配置
|
|
// 返回值:
|
|
// - err: 初始化过程中的错误
|
|
// 异常:
|
|
// - 当初始化失败时返回错误
|
|
func (this *tool_google_map_regeocode) 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
|
|
}
|
|
|
|
// 组件启动接口
|
|
// 返回值:
|
|
// - err: 启动过程中的错误
|
|
// 异常:
|
|
// - 当注册工具或系统初始化失败时返回错误
|
|
func (this *tool_google_map_regeocode) 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
|
|
}
|
|
|
|
// 工具定义
|
|
// 返回值:
|
|
// - tool: MCP工具定义
|
|
func (this *tool_google_map_regeocode) Tool() (tool mcp.Tool) {
|
|
return mcp.NewTool("google_map_regeocode",
|
|
mcp.WithDescription("逆地理编码,将经纬度信息转换为结构化地址"),
|
|
mcp.WithString("lat",
|
|
mcp.Description("纬度坐标 例如:39.90909"),
|
|
mcp.Required(),
|
|
),
|
|
mcp.WithString("lng",
|
|
mcp.Description("经度坐标 例如:116.434307"),
|
|
mcp.Required(),
|
|
),
|
|
)
|
|
}
|
|
|
|
// 工具处理接口
|
|
// 参数:
|
|
// - ctx: 上下文
|
|
// - request: 工具调用请求,需包含lat、lng参数
|
|
// 返回值:
|
|
// - result: 工具调用结果,文本内容为逆地理编码的结构化地址信息
|
|
// - err: 调用过程中的错误
|
|
// 异常:
|
|
// - 当参数缺失或服务调用失败时返回错误
|
|
func (this *tool_google_map_regeocode) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
var (
|
|
lat, lng string
|
|
context string
|
|
)
|
|
this.module.Debug("tool_google_map_regeocode",
|
|
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)
|
|
georesult, err := googlemap.ReverseGeocode(location)
|
|
if err != nil {
|
|
context = "逆地理解析失败"
|
|
this.module.Debug("tool_google_map_regeocode fail",
|
|
log.Field{Key: "lat", Value: lat},
|
|
log.Field{Key: "lng", Value: lng},
|
|
log.Field{Key: "location", Value: location},
|
|
log.Field{Key: "georesult", Value: georesult},
|
|
log.Field{Key: "err", Value: err},
|
|
)
|
|
return
|
|
} else {
|
|
context = utils.ToString(georesult)
|
|
this.module.Debug("tool_google_map_regeocode success",
|
|
log.Field{Key: "lat", Value: lat},
|
|
log.Field{Key: "lng", Value: lng},
|
|
log.Field{Key: "location", Value: location},
|
|
log.Field{Key: "georesult", Value: georesult},
|
|
)
|
|
}
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: context,
|
|
},
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|