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.
116 lines
3.1 KiB
116 lines
3.1 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_regeocode struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *tool_gaode_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
|
|
}
|
|
func (this *tool_gaode_map_regeocode) 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_regeocode) Tool() (tool mcp.Tool) {
|
|
return mcp.NewTool("gaode_map_regeocode",
|
|
mcp.WithDescription("逆地理编码,将经纬度信息转换为详细的结构化地址,获取目标点的详细地址信息"),
|
|
mcp.WithString("lng",
|
|
mcp.Description("经度坐标 例如:116.434307"),
|
|
mcp.Required(),
|
|
),
|
|
mcp.WithString("lat",
|
|
mcp.Description("经度坐标 例如:39.90909"),
|
|
mcp.Required(),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Handl 工具处理接口
|
|
// 参数:
|
|
// - ctx: 上下文
|
|
// - request: MCP 工具调用请求
|
|
//
|
|
// 返回值:
|
|
// - result: MCP 工具调用结果
|
|
// - err: 处理过程中的错误
|
|
//
|
|
// 异常:
|
|
// - 无(不主动 panic)
|
|
func (this *tool_gaode_map_regeocode) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
var (
|
|
lng, lat string
|
|
location *amap.LocationResponse
|
|
georesult *amap.ReverseGeocodeResponse
|
|
context string
|
|
ok bool
|
|
args map[string]any
|
|
)
|
|
this.module.Debug("tool_gaode_map_regeocode",
|
|
log.Field{Key: "request.Params.Arguments", Value: request.GetRawArguments()},
|
|
)
|
|
args = request.GetArguments()
|
|
if lng, ok = args["lng"].(string); !ok {
|
|
err = fmt.Errorf("必要参数丢失")
|
|
context = "获取目标地址失败"
|
|
return
|
|
}
|
|
if lat, ok = args["lat"].(string); !ok {
|
|
err = fmt.Errorf("必要参数丢失")
|
|
context = "获取目标地址失败"
|
|
return
|
|
}
|
|
if location, err = amap.Coordinate(lng, lat); err == nil {
|
|
georesult, err = amap.Geocode(location.Locations)
|
|
}
|
|
if err != nil {
|
|
context = "逆地理解析失败"
|
|
this.module.Debug("tool_gaode_map_regeocode fail",
|
|
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_gaode_map_regeocode success",
|
|
log.Field{Key: "lng", Value: lng},
|
|
log.Field{Key: "lat", Value: lat},
|
|
log.Field{Key: "location", Value: location},
|
|
log.Field{Key: "georesult", Value: georesult},
|
|
)
|
|
context = utils.ToString(georesult)
|
|
}
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: context,
|
|
},
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|