From 0e94e1a58abe3cc9d2e2166d997848b0f3e844ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BE=84=E6=BD=AD?= Date: Thu, 10 Apr 2025 17:08:01 +0800 Subject: [PATCH] mcp: support amap auto ip detection (#2041) --- plugins/wasm-go/mcp-servers/all-in-one/go.mod | 2 +- plugins/wasm-go/mcp-servers/all-in-one/go.sum | 4 ++ .../amap-tools/tools/maps_ip_location.go | 38 +++++++++++++++++-- .../amap-tools/tools/maps_weather.go | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/plugins/wasm-go/mcp-servers/all-in-one/go.mod b/plugins/wasm-go/mcp-servers/all-in-one/go.mod index b539a4dc4..703174627 100644 --- a/plugins/wasm-go/mcp-servers/all-in-one/go.mod +++ b/plugins/wasm-go/mcp-servers/all-in-one/go.mod @@ -8,7 +8,7 @@ replace amap-tools => ../amap-tools require ( amap-tools v0.0.0-00010101000000-000000000000 - github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250407124215-3431eeb8d374 + github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250409124819-db06b7ed4817 quark-search v0.0.0-00010101000000-000000000000 ) diff --git a/plugins/wasm-go/mcp-servers/all-in-one/go.sum b/plugins/wasm-go/mcp-servers/all-in-one/go.sum index 8c9939269..e4d63cbcc 100644 --- a/plugins/wasm-go/mcp-servers/all-in-one/go.sum +++ b/plugins/wasm-go/mcp-servers/all-in-one/go.sum @@ -8,6 +8,10 @@ github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0= github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250407124215-3431eeb8d374 h1:Ht+XEuYcuytDa6YkgTXR/94h+/XAafX0GhGXcnr9siw= github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250407124215-3431eeb8d374/go.mod h1:nAmuA22tHQhn8to3y980Ut7FFv/Ayjj/B7n/F8Wf5JY= +github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250409120823-55969c71d96b h1:5mJ7sEVzbB10EhDUSAIYQjE3kMyKYVd+puz2Bs3MZMo= +github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250409120823-55969c71d96b/go.mod h1:nAmuA22tHQhn8to3y980Ut7FFv/Ayjj/B7n/F8Wf5JY= +github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250409124819-db06b7ed4817 h1:+FcMFo9GZdy/AVtISgyo7Z/r+AqRrvxS18cScN9m0Vo= +github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250409124819-db06b7ed4817/go.mod h1:nAmuA22tHQhn8to3y980Ut7FFv/Ayjj/B7n/F8Wf5JY= github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= diff --git a/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_ip_location.go b/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_ip_location.go index 2c0e2d7ea..fba242e19 100644 --- a/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_ip_location.go +++ b/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_ip_location.go @@ -20,21 +20,23 @@ import ( "fmt" "net/http" "net/url" + "strings" "amap-tools/config" "github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server" "github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils" + "github.com/higress-group/proxy-wasm-go-sdk/proxywasm" ) var _ server.Tool = IPLocationRequest{} type IPLocationRequest struct { - IP string `json:"ip" jsonschema_description:"IP地址"` + IP string `json:"ip" jsonschema_description:"IP地址,获取不到则填写unknow,服务端将根据socket地址来获取IP"` } func (t IPLocationRequest) Description() string { - return "IP 定位根据用户输入的 IP 地址,定位 IP 的所在位置" + return "通过IP定位所在的国家和城市等位置信息" } func (t IPLocationRequest) InputSchema() map[string]any { @@ -53,7 +55,19 @@ func (t IPLocationRequest) Call(ctx server.HttpContext, s server.Server) error { if serverConfig.ApiKey == "" { return errors.New("amap API-KEY is not configured") } - + if t.IP == "" || strings.Contains(t.IP, "unknow") { + var bs []byte + var ipStr string + fromHeader := false + bs, _ = proxywasm.GetProperty([]string{"source", "address"}) + if len(bs) > 0 { + ipStr = string(bs) + } else { + ipStr, _ = proxywasm.GetHttpRequestHeader("x-forwarded-for") + fromHeader = true + } + t.IP = parseIP(ipStr, fromHeader) + } url := fmt.Sprintf("https://restapi.amap.com/v3/ip?ip=%s&key=%s&source=ts_mcp", url.QueryEscape(t.IP), serverConfig.ApiKey) return ctx.RouteCall(http.MethodGet, url, [][2]string{{"Accept", "application/json"}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) { @@ -64,3 +78,21 @@ func (t IPLocationRequest) Call(ctx server.HttpContext, s server.Server) error { utils.SendMCPToolTextResult(ctx, string(responseBody)) }) } + +// parseIP 解析IP +func parseIP(source string, fromHeader bool) string { + + if fromHeader { + source = strings.Split(source, ",")[0] + } + source = strings.Trim(source, " ") + if strings.Contains(source, ".") { + // parse ipv4 + return strings.Split(source, ":")[0] + } + //parse ipv6 + if strings.Contains(source, "]") { + return strings.Split(source, "]")[0][1:] + } + return source +} diff --git a/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_weather.go b/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_weather.go index ec0c47e94..a35b4bd36 100644 --- a/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_weather.go +++ b/plugins/wasm-go/mcp-servers/amap-tools/tools/maps_weather.go @@ -34,7 +34,7 @@ type WeatherRequest struct { } func (t WeatherRequest) Description() string { - return "根据城市名称或者标准adcode查询指定城市的天气" + return "根据城市名称或者标准adcode查询指定城市的天气,城市名称可以通过maps_ip_location工具获取" } func (t WeatherRequest) InputSchema() map[string]any {