mirror of
https://github.com/alibaba/higress.git
synced 2026-03-08 10:40:48 +08:00
69 lines
2.3 KiB
Go
69 lines
2.3 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/higress-group/wasm-go/pkg/mcp/server"
|
|
"github.com/higress-group/wasm-go/pkg/mcp/utils"
|
|
)
|
|
|
|
var _ server.Tool = GeoRequest{}
|
|
|
|
type GeoRequest struct {
|
|
Address string `json:"address" jsonschema_description:"待解析的结构化地址信息"`
|
|
City string `json:"city" jsonschema_description:"指定查询的城市"`
|
|
}
|
|
|
|
func (t GeoRequest) Description() string {
|
|
return "将详细的结构化地址转换为经纬度坐标。支持对地标性名胜景区、建筑物名称解析为经纬度坐标, 城市名称可以通过基于ip定位位置的mcp工具获取"
|
|
}
|
|
|
|
func (t GeoRequest) InputSchema() map[string]any {
|
|
return server.ToInputSchema(&GeoRequest{})
|
|
}
|
|
|
|
func (t GeoRequest) Create(params []byte) server.Tool {
|
|
request := &GeoRequest{}
|
|
json.Unmarshal(params, &request)
|
|
return request
|
|
}
|
|
|
|
func (t GeoRequest) 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")
|
|
}
|
|
|
|
apiKey := serverConfig.ApiKey
|
|
url := fmt.Sprintf("https://restapi.amap.com/v3/geocode/geo?key=%s&address=%s&city=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Address), url.QueryEscape(t.City))
|
|
return ctx.RouteCall(http.MethodGet, url,
|
|
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders [][2]string, responseBody []byte) {
|
|
if statusCode != http.StatusOK {
|
|
utils.OnMCPToolCallError(ctx, fmt.Errorf("geo call failed, status: %d", statusCode))
|
|
return
|
|
}
|
|
utils.SendMCPToolTextResult(ctx, string(responseBody))
|
|
})
|
|
}
|