add: add mcp server amap tools (#1951)

This commit is contained in:
mirror
2025-03-25 21:20:36 +08:00
committed by GitHub
parent ee77ffb753
commit 386a208b14
16 changed files with 1194 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type AroundSearchRequest struct {
Location string `json:"location" jsonschema_description:"中心点经度纬度"`
Radius string `json:"radius" jsonschema_description:"搜索半径"`
Keywords string `json:"keywords" jsonschema_description:"搜索关键词"`
}
func (t AroundSearchRequest) Description() string {
return "周边搜根据用户传入关键词以及坐标location搜索出radius半径范围的POI"
}
func (t AroundSearchRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&AroundSearchRequest{})
}
func (t AroundSearchRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &AroundSearchRequest{}
json.Unmarshal(params, &request)
return request
}
func (t AroundSearchRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/place/around?key=%s&location=%s&radius=%s&keywords=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Location), url.QueryEscape(t.Radius), url.QueryEscape(t.Keywords))
return ctx.RouteCall(http.MethodGet, url,
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("around search call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Pois []struct {
ID string `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
Typecode string `json:"typecode"`
} `json:"pois"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse around search response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("around search failed: %s", response.Info))
return
}
result := fmt.Sprintf(`{"pois": %s}`, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,87 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type BicyclingRequest struct {
Origin string `json:"origin" jsonschema_description:"出发点经纬度,坐标格式为:经度,纬度"`
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
}
func (t BicyclingRequest) Description() string {
return "骑行路径规划用于规划骑行通勤方案,规划时会考虑天桥、单行线、封路等情况。最大支持 500km 的骑行路线规划"
}
func (t BicyclingRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&BicyclingRequest{})
}
func (t BicyclingRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &BicyclingRequest{}
json.Unmarshal(params, &request)
return request
}
func (t BicyclingRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v4/direction/bicycling?key=%s&origin=%s&destination=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Origin), url.QueryEscape(t.Destination))
return ctx.RouteCall(http.MethodGet, url,
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("bicycling call failed, status: %d", statusCode))
return
}
var response struct {
Errcode int `json:"errcode"`
Data struct {
Origin string `json:"origin"`
Destination string `json:"destination"`
Paths []struct {
Distance string `json:"distance"`
Duration string `json:"duration"`
Steps []struct {
Instruction string `json:"instruction"`
Road string `json:"road"`
Distance string `json:"distance"`
Orientation string `json:"orientation"`
Duration string `json:"duration"`
} `json:"steps"`
} `json:"paths"`
} `json:"data"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse bicycling response: %v", err))
return
}
if response.Errcode != 0 {
ctx.OnMCPToolCallError(fmt.Errorf("bicycling failed: %v", response))
return
}
result := fmt.Sprintf(`{"origin": "%s", "destination": "%s", "paths": %s}`, response.Data.Origin, response.Data.Destination, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,89 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type DrivingRequest struct {
Origin string `json:"origin" jsonschema_description:"出发点经度,纬度,坐标格式为:经度,纬度"`
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
}
func (t DrivingRequest) Description() string {
return "驾车路径规划 API 可以根据用户起终点经纬度坐标规划以小客车、轿车通勤出行的方案,并且返回通勤方案的数据"
}
func (t DrivingRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&DrivingRequest{})
}
func (t DrivingRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &DrivingRequest{}
json.Unmarshal(params, &request)
return request
}
func (t DrivingRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/direction/driving?key=%s&origin=%s&destination=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Origin), url.QueryEscape(t.Destination))
return ctx.RouteCall(http.MethodGet, url,
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("driving call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Route struct {
Origin string `json:"origin"`
Destination string `json:"destination"`
Paths []struct {
Path string `json:"path"`
Distance string `json:"distance"`
Duration string `json:"duration"`
Steps []struct {
Instruction string `json:"instruction"`
Road string `json:"road"`
Distance string `json:"distance"`
Orientation string `json:"orientation"`
Duration string `json:"duration"`
} `json:"steps"`
} `json:"paths"`
} `json:"route"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse driving response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("driving failed: %s", response.Info))
return
}
result := fmt.Sprintf(`{"origin": "%s", "destination": "%s", "paths": %s}`, response.Route.Origin, response.Route.Destination, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,125 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type TransitIntegratedRequest struct {
Origin string `json:"origin" jsonschema_description:"出发点经纬度,坐标格式为:经度,纬度"`
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
City string `json:"city" jsonschema_description:"公共交通规划起点城市"`
Cityd string `json:"cityd" jsonschema_description:"公共交通规划终点城市"`
}
func (t TransitIntegratedRequest) Description() string {
return "公交路径规划 API 可以根据用户起终点经纬度坐标规划综合各类公共(火车、公交、地铁)交通方式的通勤方案,并且返回通勤方案的数据,跨城场景下必须传起点城市与终点城市"
}
func (t TransitIntegratedRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&TransitIntegratedRequest{})
}
func (t TransitIntegratedRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &TransitIntegratedRequest{}
json.Unmarshal(params, &request)
return request
}
func (t TransitIntegratedRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/direction/transit/integrated?key=%s&origin=%s&destination=%s&city=%s&cityd=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Origin), url.QueryEscape(t.Destination), url.QueryEscape(t.City), url.QueryEscape(t.Cityd))
return ctx.RouteCall(http.MethodGet, url,
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("transit integrated call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Route struct {
Origin string `json:"origin"`
Destination string `json:"destination"`
Distance string `json:"distance"`
Transits []struct {
Duration string `json:"duration"`
WalkingDistance string `json:"walking_distance"`
Segments []struct {
Walking struct {
Origin string `json:"origin"`
Destination string `json:"destination"`
Distance string `json:"distance"`
Duration string `json:"duration"`
Steps []struct {
Instruction string `json:"instruction"`
Road string `json:"road"`
Distance string `json:"distance"`
Action string `json:"action"`
AssistantAction string `json:"assistant_action"`
} `json:"steps"`
} `json:"walking"`
Bus struct {
Buslines []struct {
Name string `json:"name"`
DepartureStop struct {
Name string `json:"name"`
} `json:"departure_stop"`
ArrivalStop struct {
Name string `json:"name"`
} `json:"arrival_stop"`
Distance string `json:"distance"`
Duration string `json:"duration"`
ViaStops []struct {
Name string `json:"name"`
} `json:"via_stops"`
} `json:"buslines"`
} `json:"bus"`
Entrance struct {
Name string `json:"name"`
} `json:"entrance"`
Exit struct {
Name string `json:"name"`
} `json:"exit"`
Railway struct {
Name string `json:"name"`
Trip string `json:"trip"`
} `json:"railway"`
} `json:"segments"`
} `json:"transits"`
} `json:"route"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse transit integrated response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("transit integrated failed: %s", response.Info))
return
}
result := fmt.Sprintf(`{"origin": "%s", "destination": "%s", "distance": "%s", "transits": %s}`, response.Route.Origin, response.Route.Destination, response.Route.Distance, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,88 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type WalkingRequest struct {
Origin string `json:"origin" jsonschema_description:"出发点经度,纬度,坐标格式为:经度,纬度"`
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
}
func (t WalkingRequest) Description() string {
return "步行路径规划 API 可以根据输入起点终点经纬度坐标规划100km 以内的步行通勤方案,并且返回通勤方案的数据"
}
func (t WalkingRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&WalkingRequest{})
}
func (t WalkingRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &WalkingRequest{}
json.Unmarshal(params, &request)
return request
}
func (t WalkingRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/direction/walking?key=%s&origin=%s&destination=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Origin), url.QueryEscape(t.Destination))
return ctx.RouteCall(http.MethodGet, url,
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("walking call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Route struct {
Origin string `json:"origin"`
Destination string `json:"destination"`
Paths []struct {
Distance string `json:"distance"`
Duration string `json:"duration"`
Steps []struct {
Instruction string `json:"instruction"`
Road string `json:"road"`
Distance string `json:"distance"`
Orientation string `json:"orientation"`
Duration string `json:"duration"`
} `json:"steps"`
} `json:"paths"`
} `json:"route"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse walking response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("walking failed: %s", response.Info))
return
}
result := fmt.Sprintf(`{"origin": "%s", "destination": "%s", "paths": %s}`, response.Route.Origin, response.Route.Destination, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,80 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type DistanceRequest struct {
Origins string `json:"origins" jsonschema_description:"起点经度纬度可以传多个坐标使用分号隔离比如120,30;120,31坐标格式为经度纬度"`
Destination string `json:"destination" jsonschema_description:"终点经度,纬度,坐标格式为:经度,纬度"`
Type string `json:"type" jsonschema_description:"距离测量类型,1代表驾车距离测量0代表直线距离测量3步行距离测量"`
}
func (t DistanceRequest) Description() string {
return "距离测量 API 可以测量两个经纬度坐标之间的距离,支持驾车、步行以及球面距离测量"
}
func (t DistanceRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&DistanceRequest{})
}
func (t DistanceRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &DistanceRequest{}
json.Unmarshal(params, &request)
return request
}
func (t DistanceRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/distance?key=%s&origins=%s&destination=%s&type=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Origins), url.QueryEscape(t.Destination), url.QueryEscape(t.Type))
return ctx.RouteCall(http.MethodGet, url,
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("distance call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Results []struct {
OriginID string `json:"origin_id"`
DestID string `json:"dest_id"`
Distance string `json:"distance"`
Duration string `json:"duration"`
} `json:"results"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse distance response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("distance failed: %s", response.Info))
return
}
result := fmt.Sprintf(`{"results": %s}`, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,101 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type GeoRequest struct {
Address string `json:"address" jsonschema_description:"待解析的结构化地址信息"`
City string `json:"city" jsonschema_description:"指定查询的城市"`
}
func (t GeoRequest) Description() string {
return "将详细的结构化地址转换为经纬度坐标。支持对地标性名胜景区、建筑物名称解析为经纬度坐标"
}
func (t GeoRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&GeoRequest{})
}
func (t GeoRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &GeoRequest{}
json.Unmarshal(params, &request)
return request
}
func (t GeoRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
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 http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("geo call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Geocodes []struct {
Country string `json:"country"`
Province string `json:"province"`
City string `json:"city"`
Citycode string `json:"citycode"`
District string `json:"district"`
Street string `json:"street"`
Number string `json:"number"`
Adcode string `json:"adcode"`
Location string `json:"location"`
Level string `json:"level"`
} `json:"geocodes"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse geo response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("geo failed: %s", response.Info))
return
}
var results []map[string]string
for _, geo := range response.Geocodes {
result := map[string]string{
"country": geo.Country,
"province": geo.Province,
"city": geo.City,
"citycode": geo.Citycode,
"district": geo.District,
"street": geo.Street,
"number": geo.Number,
"adcode": geo.Adcode,
"location": geo.Location,
"level": geo.Level,
}
results = append(results, result)
}
result, _ := json.Marshal(results)
ctx.SendMCPToolTextResult(string(result))
})
}

View File

@@ -0,0 +1,76 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type IPLocationRequest struct {
IP string `json:"ip" jsonschema_description:"IP地址"`
}
func (t IPLocationRequest) Description() string {
return "IP 定位根据用户输入的 IP 地址,定位 IP 的所在位置"
}
func (t IPLocationRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&IPLocationRequest{})
}
func (t IPLocationRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &IPLocationRequest{}
json.Unmarshal(params, &request)
return request
}
func (t IPLocationRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("https://restapi.amap.com/v3/ip?ip=%s&key=%s&source=ts_mcp", url.QueryEscape(t.IP), 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 {
ctx.OnMCPToolCallError(fmt.Errorf("ip location call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Province string `json:"province"`
City string `json:"city"`
Adcode string `json:"adcode"`
Rectangle string `json:"rectangle"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse ip location response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("ip location failed: %s", response.Info))
return
}
result := fmt.Sprintf(`{"province": "%s", "city": "%s", "adcode": "%s", "rectangle": "%s"}`, response.Province, response.City, response.Adcode, response.Rectangle)
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,79 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type ReGeocodeRequest struct {
Location string `json:"location" jsonschema_description:"经纬度"`
}
func (t ReGeocodeRequest) Description() string {
return "将一个高德经纬度坐标转换为行政区划地址信息"
}
func (t ReGeocodeRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&ReGeocodeRequest{})
}
func (t ReGeocodeRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &ReGeocodeRequest{}
json.Unmarshal(params, &request)
return request
}
func (t ReGeocodeRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/geocode/regeo?location=%s&key=%s&source=ts_mcp", url.QueryEscape(t.Location), 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 {
ctx.OnMCPToolCallError(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 {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse regeocode response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(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)
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,84 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type SearchDetailRequest struct {
ID string `json:"id" jsonschema_description:"关键词搜或者周边搜获取到的POI ID"`
}
func (t SearchDetailRequest) Description() string {
return "查询关键词搜或者周边搜获取到的POI ID的详细信息"
}
func (t SearchDetailRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&SearchDetailRequest{})
}
func (t SearchDetailRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &SearchDetailRequest{}
json.Unmarshal(params, &request)
return request
}
func (t SearchDetailRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/place/detail?id=%s&key=%s&source=ts_mcp", url.QueryEscape(t.ID), 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 {
ctx.OnMCPToolCallError(fmt.Errorf("search detail call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Pois []struct {
ID string `json:"id"`
Name string `json:"name"`
Location string `json:"location"`
Address string `json:"address"`
BusinessArea string `json:"business_area"`
Cityname string `json:"cityname"`
Type string `json:"type"`
Alias string `json:"alias"`
BizExt map[string]string `json:"biz_ext"`
} `json:"pois"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse search detail response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("search detail failed: %s", response.Info))
return
}
poi := response.Pois[0]
result := fmt.Sprintf(`{"id": "%s", "name": "%s", "location": "%s", "address": "%s", "business_area": "%s", "city": "%s", "type": "%s", "alias": "%s", "biz_ext": %s}`, poi.ID, poi.Name, poi.Location, poi.Address, poi.BusinessArea, poi.Cityname, poi.Type, poi.Alias, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,90 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type TextSearchRequest struct {
Keywords string `json:"keywords" jsonschema_description:"搜索关键词"`
City string `json:"city" jsonschema_description:"查询城市"`
Citylimit string `json:"citylimit" jsonschema_description:"是否强制限制在设置的城市内搜索默认值为false"`
}
func (t TextSearchRequest) Description() string {
return "关键词搜根据用户传入关键词搜索出相关的POI"
}
func (t TextSearchRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&TextSearchRequest{})
}
func (t TextSearchRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &TextSearchRequest{}
json.Unmarshal(params, &request)
return request
}
func (t TextSearchRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/place/text?key=%s&keywords=%s&city=%s&citylimit=%s&source=ts_mcp", apiKey, url.QueryEscape(t.Keywords), url.QueryEscape(t.City), url.QueryEscape(t.Citylimit))
return ctx.RouteCall(http.MethodGet, url,
[][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("text search call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Suggestion struct {
Keywords []string `json:"keywords"`
Cities []struct {
Name string `json:"name"`
} `json:"cities"`
} `json:"suggestion"`
Pois []struct {
ID string `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
Typecode string `json:"typecode"`
} `json:"pois"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse text search response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("text search failed: %s", response.Info))
return
}
var cities []string
for _, city := range response.Suggestion.Cities {
cities = append(cities, city.Name)
}
result := fmt.Sprintf(`{"suggestion": {"keywords": %s, "cities": %s}, "pois": %s}`, string(responseBody), string(responseBody), string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}

View File

@@ -0,0 +1,89 @@
package tools
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"amap-tools/server"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type WeatherRequest struct {
City string `json:"city" jsonschema_description:"城市名称或者adcode"`
}
func (t WeatherRequest) Description() string {
return "根据城市名称或者标准adcode查询指定城市的天气"
}
func (t WeatherRequest) InputSchema() map[string]any {
return wrapper.ToInputSchema(&WeatherRequest{})
}
func (t WeatherRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
request := &WeatherRequest{}
json.Unmarshal(params, &request)
return request
}
func (t WeatherRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
return err
}
err = config.ConfigHasError()
if err != nil {
return err
}
apiKey := config.ApiKey
if apiKey == "" {
return fmt.Errorf("amap API-KEY is not set")
}
url := fmt.Sprintf("http://restapi.amap.com/v3/weather/weatherInfo?city=%s&key=%s&source=ts_mcp&extensions=all", url.QueryEscape(t.City), 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 {
ctx.OnMCPToolCallError(fmt.Errorf("weather call failed, status: %d", statusCode))
return
}
var response struct {
Status string `json:"status"`
Info string `json:"info"`
Forecasts []struct {
City string `json:"city"`
Casts []struct {
Date string `json:"date"`
Week string `json:"week"`
DayWeather string `json:"dayweather"`
NightWeather string `json:"nightweather"`
DayTemp string `json:"daytemp"`
NightTemp string `json:"nighttemp"`
DayWind string `json:"daywind"`
NightWind string `json:"nightwind"`
DayPower string `json:"daypower"`
NightPower string `json:"nightpower"`
Humidity string `json:"humidity"`
} `json:"casts"`
} `json:"forecasts"`
}
err := json.Unmarshal(responseBody, &response)
if err != nil {
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse weather response: %v", err))
return
}
if response.Status != "1" {
ctx.OnMCPToolCallError(fmt.Errorf("weather failed: %s", response.Info))
return
}
forecasts := response.Forecasts[0]
result := fmt.Sprintf(`{"city": "%s", "forecasts": %s}`, forecasts.City, string(responseBody))
ctx.SendMCPToolTextResult(result)
})
}