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_geocode struct { cbase.ModuleCompBase module *Mcp } // 组件初始化接口 func (this *tool_gaode_map_geocode) 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_geocode) 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_geocode) Tool() (tool mcp.Tool) { return mcp.NewTool("gaode_map_geocode", mcp.WithDescription("将详细的结构化地址转换为经纬度坐标,支持地标性的名胜景区和建筑名称解析为经纬度坐标"), mcp.WithString("address", mcp.Description("位置信息 例如:天安门"), mcp.Required(), ), mcp.WithString("city", mcp.Description("城市名称 例如:北京"), ), ) } // Handl 工具处理接口 // 参数: // - ctx: 上下文 // - request: MCP 工具调用请求 // // 返回值: // - result: MCP 工具调用结果 // - err: 处理过程中的错误 // // 异常: // - 无(不主动 panic) func (this *tool_gaode_map_geocode) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) { var ( city, address string georesult *amap.GeocodeResponse context string args map[string]any ) this.module.Debug("tool_gaode_map_geocode", log.Field{Key: "request.Params.Arguments", Value: request.GetRawArguments()}, ) args = request.GetArguments() city, _ = args["city"].(string) if address, err = request.RequireString("address"); err != nil { err = fmt.Errorf("必要参数丢失") context = "获取目标地址失败" return } georesult, err = amap.Geo(city, address) if err != nil { context = "地址解析失败" this.module.Debug("tool_gaode_map_geocode fail", log.Field{Key: "city", Value: city}, log.Field{Key: "address", Value: address}, log.Field{Key: "georesult", Value: georesult}, log.Field{Key: "err", Value: err}, ) return } else { context = utils.ToString(georesult) this.module.Debug("tool_gaode_map_geocode success", log.Field{Key: "city", Value: city}, log.Field{Key: "address", Value: address}, log.Field{Key: "georesult", Value: georesult}, ) } result = &mcp.CallToolResult{ Content: []mcp.Content{ mcp.TextContent{ Type: "text", Text: context, }, }, } return }