mirror of
https://github.com/alibaba/higress.git
synced 2026-06-01 00:27:26 +08:00
88 lines
2.9 KiB
Go
88 lines
2.9 KiB
Go
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package tools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"amap-tools/config"
|
|
|
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
|
)
|
|
|
|
var _ server.Tool = ReGeocodeRequest{}
|
|
|
|
type ReGeocodeRequest struct {
|
|
Location string `json:"location" jsonschema_description:"经纬度"`
|
|
}
|
|
|
|
func (t ReGeocodeRequest) Description() string {
|
|
return "将一个高德经纬度坐标转换为行政区划地址信息"
|
|
}
|
|
|
|
func (t ReGeocodeRequest) InputSchema() map[string]any {
|
|
return server.ToInputSchema(&ReGeocodeRequest{})
|
|
}
|
|
|
|
func (t ReGeocodeRequest) Create(params []byte) server.Tool {
|
|
request := &ReGeocodeRequest{}
|
|
json.Unmarshal(params, &request)
|
|
return request
|
|
}
|
|
|
|
func (t ReGeocodeRequest) Call(ctx server.HttpContext, s server.Server) error {
|
|
serverConfig := &config.AmapServerConfig{}
|
|
s.GetConfig(serverConfig)
|
|
if serverConfig.ApiKey == "" {
|
|
return errors.New("amap API-KEY is not configured")
|
|
}
|
|
|
|
url := fmt.Sprintf("http://restapi.amap.com/v3/geocode/regeo?location=%s&key=%s&source=ts_mcp", url.QueryEscape(t.Location), serverConfig.ApiKey)
|
|
return ctx.RouteCall(http.MethodGet, url,
|
|
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
|
|
if statusCode != http.StatusOK {
|
|
utils.OnMCPToolCallError(ctx, fmt.Errorf("regeocode call failed, status: %d", statusCode))
|
|
return
|
|
}
|
|
var response struct {
|
|
Status string `json:"status"`
|
|
Info string `json:"info"`
|
|
Regeocode struct {
|
|
AddressComponent struct {
|
|
Province string `json:"province"`
|
|
City string `json:"city"`
|
|
District string `json:"district"`
|
|
} `json:"addressComponent"`
|
|
} `json:"regeocode"`
|
|
}
|
|
err := json.Unmarshal(responseBody, &response)
|
|
if err != nil {
|
|
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse regeocode response: %v", err))
|
|
return
|
|
}
|
|
if response.Status != "1" {
|
|
utils.OnMCPToolCallError(ctx, fmt.Errorf("regeocode failed: %s", response.Info))
|
|
return
|
|
}
|
|
result := fmt.Sprintf(`{"province": "%s", "city": "%s", "district": "%s"}`, response.Regeocode.AddressComponent.Province, response.Regeocode.AddressComponent.City, response.Regeocode.AddressComponent.District)
|
|
utils.SendMCPToolTextResult(ctx, result)
|
|
})
|
|
}
|