mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 04:37:31 +08:00
refactor mcp sdk (#1977)
This commit is contained in:
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
)
|
||||
|
||||
var _ server.Tool = AroundSearchRequest{}
|
||||
|
||||
type AroundSearchRequest struct {
|
||||
Location string `json:"location" jsonschema_description:"中心点经度纬度"`
|
||||
Radius string `json:"radius" jsonschema_description:"搜索半径"`
|
||||
@@ -23,42 +40,33 @@ func (t AroundSearchRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t AroundSearchRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&AroundSearchRequest{})
|
||||
return server.ToInputSchema(&AroundSearchRequest{})
|
||||
}
|
||||
|
||||
func (t AroundSearchRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t AroundSearchRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t AroundSearchRequest) 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 := 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))
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/place/around?key=%s&location=%s&radius=%s&keywords=%s&source=ts_mcp", serverConfig.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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("around search call failed, status: %d", statusCode))
|
||||
return
|
||||
}
|
||||
var response struct {
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Pois []struct {
|
||||
Pois []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
@@ -67,14 +75,14 @@ func (t AroundSearchRequest) Call(ctx wrapper.HttpContext, config server.AmapMCP
|
||||
}
|
||||
err := json.Unmarshal(responseBody, &response)
|
||||
if err != nil {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse around search response: %v", err))
|
||||
utils.OnMCPToolCallError(ctx, 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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("around search failed: %s", response.Info))
|
||||
return
|
||||
}
|
||||
result := fmt.Sprintf(`{"pois": %s}`, string(responseBody))
|
||||
ctx.SendMCPToolTextResult(result)
|
||||
result, _ := json.MarshalIndent(response.Pois, "", " ")
|
||||
utils.SendMCPToolTextResult(ctx, string(result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
)
|
||||
|
||||
var _ server.Tool = BicyclingRequest{}
|
||||
|
||||
type BicyclingRequest struct {
|
||||
Origin string `json:"origin" jsonschema_description:"出发点经纬度,坐标格式为:经度,纬度"`
|
||||
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
|
||||
@@ -22,66 +39,57 @@ func (t BicyclingRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t BicyclingRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&BicyclingRequest{})
|
||||
return server.ToInputSchema(&BicyclingRequest{})
|
||||
}
|
||||
|
||||
func (t BicyclingRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t BicyclingRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t BicyclingRequest) 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 := 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))
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v4/direction/bicycling?key=%s&origin=%s&destination=%s&source=ts_mcp", serverConfig.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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("bicycling call failed, status: %d", statusCode))
|
||||
return
|
||||
}
|
||||
var response struct {
|
||||
Errcode int `json:"errcode"`
|
||||
Data struct {
|
||||
Origin string `json:"origin"`
|
||||
Data struct {
|
||||
Origin string `json:"origin"`
|
||||
Destination string `json:"destination"`
|
||||
Paths []struct {
|
||||
Paths []struct {
|
||||
Distance string `json:"distance"`
|
||||
Duration string `json:"duration"`
|
||||
Steps []struct {
|
||||
Steps []struct {
|
||||
Instruction string `json:"instruction"`
|
||||
Road string `json:"road"`
|
||||
Distance string `json:"distance"`
|
||||
Road string `json:"road"`
|
||||
Distance string `json:"distance"`
|
||||
Orientation string `json:"orientation"`
|
||||
Duration string `json:"duration"`
|
||||
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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse bicycling response: %v", err))
|
||||
return
|
||||
}
|
||||
if response.Errcode != 0 {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("bicycling failed: %v", response))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
result, _ := json.MarshalIndent(response.Data.Paths, "", " ")
|
||||
utils.SendMCPToolTextResult(ctx, string(result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
)
|
||||
|
||||
var _ server.Tool = DrivingRequest{}
|
||||
|
||||
type DrivingRequest struct {
|
||||
Origin string `json:"origin" jsonschema_description:"出发点经度,纬度,坐标格式为:经度,纬度"`
|
||||
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
|
||||
@@ -22,68 +39,59 @@ func (t DrivingRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t DrivingRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&DrivingRequest{})
|
||||
}
|
||||
return server.ToInputSchema(&DrivingRequest{})
|
||||
|
||||
func (t DrivingRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
}
|
||||
func (t DrivingRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t DrivingRequest) 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 := 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))
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/direction/driving?key=%s&origin=%s&destination=%s&source=ts_mcp", serverConfig.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))
|
||||
utils.OnMCPToolCallError(ctx, 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"`
|
||||
Route struct {
|
||||
Origin string `json:"origin"`
|
||||
Destination string `json:"destination"`
|
||||
Paths []struct {
|
||||
Path string `json:"path"`
|
||||
Paths []struct {
|
||||
Path string `json:"path"`
|
||||
Distance string `json:"distance"`
|
||||
Duration string `json:"duration"`
|
||||
Steps []struct {
|
||||
Steps []struct {
|
||||
Instruction string `json:"instruction"`
|
||||
Road string `json:"road"`
|
||||
Distance string `json:"distance"`
|
||||
Road string `json:"road"`
|
||||
Distance string `json:"distance"`
|
||||
Orientation string `json:"orientation"`
|
||||
Duration string `json:"duration"`
|
||||
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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse driving response: %v", err))
|
||||
return
|
||||
}
|
||||
if response.Status != "1" {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("driving failed: %s", response.Info))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
result, _ := json.MarshalIndent(response.Route.Paths, "", " ")
|
||||
utils.SendMCPToolTextResult(ctx, string(result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,35 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var _ server.Tool = TransitIntegratedRequest{}
|
||||
|
||||
type TransitIntegratedRequest struct {
|
||||
Origin string `json:"origin" jsonschema_description:"出发点经纬度,坐标格式为:经度,纬度"`
|
||||
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
|
||||
@@ -24,65 +42,56 @@ func (t TransitIntegratedRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t TransitIntegratedRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&TransitIntegratedRequest{})
|
||||
return server.ToInputSchema(&TransitIntegratedRequest{})
|
||||
}
|
||||
|
||||
func (t TransitIntegratedRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t TransitIntegratedRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t TransitIntegratedRequest) 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 := 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))
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/direction/transit/integrated?key=%s&origin=%s&destination=%s&city=%s&cityd=%s&source=ts_mcp", serverConfig.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))
|
||||
utils.OnMCPToolCallError(ctx, 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"`
|
||||
Route struct {
|
||||
Origin string `json:"origin"`
|
||||
Destination string `json:"destination"`
|
||||
Distance string `json:"distance"`
|
||||
Transits []struct {
|
||||
Duration string `json:"duration"`
|
||||
Distance string `json:"distance"`
|
||||
Transits []struct {
|
||||
Duration string `json:"duration"`
|
||||
WalkingDistance string `json:"walking_distance"`
|
||||
Segments []struct {
|
||||
Segments []struct {
|
||||
Walking struct {
|
||||
Origin string `json:"origin"`
|
||||
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"`
|
||||
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"`
|
||||
Name string `json:"name"`
|
||||
DepartureStop struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"departure_stop"`
|
||||
@@ -112,14 +121,14 @@ func (t TransitIntegratedRequest) Call(ctx wrapper.HttpContext, config server.Am
|
||||
}
|
||||
err := json.Unmarshal(responseBody, &response)
|
||||
if err != nil {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse transit integrated response: %v", err))
|
||||
utils.OnMCPToolCallError(ctx, 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))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
result := fmt.Sprintf(`{"origin": "%s", "destination": "%s", "distance": "%s", "transits": %s}`, response.Route.Origin, response.Route.Destination, response.Route.Distance, gjson.GetBytes(responseBody, "route.transits").Raw)
|
||||
utils.SendMCPToolTextResult(ctx, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,35 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var _ server.Tool = WalkingRequest{}
|
||||
|
||||
type WalkingRequest struct {
|
||||
Origin string `json:"origin" jsonschema_description:"出发点经度,纬度,坐标格式为:经度,纬度"`
|
||||
Destination string `json:"destination" jsonschema_description:"目的地经纬度,坐标格式为:经度,纬度"`
|
||||
@@ -22,67 +40,58 @@ func (t WalkingRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t WalkingRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&WalkingRequest{})
|
||||
return server.ToInputSchema(&WalkingRequest{})
|
||||
}
|
||||
|
||||
func (t WalkingRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t WalkingRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t WalkingRequest) 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 := 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))
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/direction/walking?key=%s&origin=%s&destination=%s&source=ts_mcp", serverConfig.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))
|
||||
utils.OnMCPToolCallError(ctx, 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"`
|
||||
Route struct {
|
||||
Origin string `json:"origin"`
|
||||
Destination string `json:"destination"`
|
||||
Paths []struct {
|
||||
Paths []struct {
|
||||
Distance string `json:"distance"`
|
||||
Duration string `json:"duration"`
|
||||
Steps []struct {
|
||||
Steps []struct {
|
||||
Instruction string `json:"instruction"`
|
||||
Road string `json:"road"`
|
||||
Distance string `json:"distance"`
|
||||
Road string `json:"road"`
|
||||
Distance string `json:"distance"`
|
||||
Orientation string `json:"orientation"`
|
||||
Duration string `json:"duration"`
|
||||
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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse walking response: %v", err))
|
||||
return
|
||||
}
|
||||
if response.Status != "1" {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("walking failed: %s", response.Info))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
result := fmt.Sprintf(`{"origin": "%s", "destination": "%s", "paths": %s}`, response.Route.Origin, response.Route.Destination, gjson.GetBytes(responseBody, "route.paths").Raw)
|
||||
utils.SendMCPToolTextResult(ctx, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,35 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var _ server.Tool = DistanceRequest{}
|
||||
|
||||
type DistanceRequest struct {
|
||||
Origins string `json:"origins" jsonschema_description:"起点经度,纬度,可以传多个坐标,使用分号隔离,比如120,30;120,31,坐标格式为:经度,纬度"`
|
||||
Destination string `json:"destination" jsonschema_description:"终点经度,纬度,坐标格式为:经度,纬度"`
|
||||
@@ -23,41 +41,31 @@ func (t DistanceRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t DistanceRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&DistanceRequest{})
|
||||
return server.ToInputSchema(&DistanceRequest{})
|
||||
}
|
||||
|
||||
func (t DistanceRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t DistanceRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t DistanceRequest) 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 := 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))
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/distance?key=%s&origins=%s&destination=%s&type=%s&source=ts_mcp", serverConfig.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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("distance call failed, status: %d", statusCode))
|
||||
return
|
||||
}
|
||||
var response struct {
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Results []struct {
|
||||
OriginID string `json:"origin_id"`
|
||||
DestID string `json:"dest_id"`
|
||||
@@ -67,14 +75,14 @@ func (t DistanceRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServ
|
||||
}
|
||||
err := json.Unmarshal(responseBody, &response)
|
||||
if err != nil {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse distance response: %v", err))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse distance response: %v", err))
|
||||
return
|
||||
}
|
||||
if response.Status != "1" {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("distance failed: %s", response.Info))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("distance failed: %s", response.Info))
|
||||
return
|
||||
}
|
||||
result := fmt.Sprintf(`{"results": %s}`, string(responseBody))
|
||||
ctx.SendMCPToolTextResult(result)
|
||||
result := fmt.Sprintf(`{"results": %s}`, gjson.GetBytes(responseBody, "results").Raw)
|
||||
utils.SendMCPToolTextResult(ctx, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
)
|
||||
|
||||
var _ server.Tool = GeoRequest{}
|
||||
|
||||
type GeoRequest struct {
|
||||
Address string `json:"address" jsonschema_description:"待解析的结构化地址信息"`
|
||||
City string `json:"city" jsonschema_description:"指定查询的城市"`
|
||||
@@ -22,41 +39,33 @@ func (t GeoRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t GeoRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&GeoRequest{})
|
||||
return server.ToInputSchema(&GeoRequest{})
|
||||
}
|
||||
|
||||
func (t GeoRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t GeoRequest) Create(params []byte) server.Tool {
|
||||
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")
|
||||
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 http.Header, responseBody []byte) {
|
||||
if statusCode != http.StatusOK {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("geo call failed, status: %d", statusCode))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("geo call failed, status: %d", statusCode))
|
||||
return
|
||||
}
|
||||
var response struct {
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Geocodes []struct {
|
||||
Country string `json:"country"`
|
||||
Province string `json:"province"`
|
||||
@@ -72,11 +81,11 @@ func (t GeoRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) e
|
||||
}
|
||||
err := json.Unmarshal(responseBody, &response)
|
||||
if err != nil {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse geo response: %v", err))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse geo response: %v", err))
|
||||
return
|
||||
}
|
||||
if response.Status != "1" {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("geo failed: %s", response.Info))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("geo failed: %s", response.Info))
|
||||
return
|
||||
}
|
||||
var results []map[string]string
|
||||
@@ -96,6 +105,6 @@ func (t GeoRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPServer) e
|
||||
results = append(results, result)
|
||||
}
|
||||
result, _ := json.Marshal(results)
|
||||
ctx.SendMCPToolTextResult(string(result))
|
||||
utils.SendMCPToolTextResult(ctx, string(result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
)
|
||||
|
||||
var _ server.Tool = IPLocationRequest{}
|
||||
|
||||
type IPLocationRequest struct {
|
||||
IP string `json:"ip" jsonschema_description:"IP地址"`
|
||||
}
|
||||
@@ -21,56 +38,47 @@ func (t IPLocationRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t IPLocationRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&IPLocationRequest{})
|
||||
return server.ToInputSchema(&IPLocationRequest{})
|
||||
}
|
||||
|
||||
func (t IPLocationRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t IPLocationRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t IPLocationRequest) 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 := 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)
|
||||
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) {
|
||||
if statusCode != http.StatusOK {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("ip location call failed, status: %d", statusCode))
|
||||
utils.OnMCPToolCallError(ctx, 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"`
|
||||
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))
|
||||
utils.OnMCPToolCallError(ctx, 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))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
utils.SendMCPToolTextResult(ctx, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"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:"经纬度"`
|
||||
}
|
||||
@@ -21,41 +38,32 @@ func (t ReGeocodeRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t ReGeocodeRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&ReGeocodeRequest{})
|
||||
return server.ToInputSchema(&ReGeocodeRequest{})
|
||||
}
|
||||
|
||||
func (t ReGeocodeRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t ReGeocodeRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
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")
|
||||
}
|
||||
|
||||
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)
|
||||
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 {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("regeocode call failed, status: %d", statusCode))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("regeocode call failed, status: %d", statusCode))
|
||||
return
|
||||
}
|
||||
var response struct {
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Regeocode struct {
|
||||
AddressComponent struct {
|
||||
Province string `json:"province"`
|
||||
@@ -66,14 +74,14 @@ func (t ReGeocodeRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPSer
|
||||
}
|
||||
err := json.Unmarshal(responseBody, &response)
|
||||
if err != nil {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse regeocode response: %v", err))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse regeocode response: %v", err))
|
||||
return
|
||||
}
|
||||
if response.Status != "1" {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("regeocode failed: %s", response.Info))
|
||||
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)
|
||||
ctx.SendMCPToolTextResult(result)
|
||||
utils.SendMCPToolTextResult(ctx, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
)
|
||||
|
||||
var _ server.Tool = SearchDetailRequest{}
|
||||
|
||||
type SearchDetailRequest struct {
|
||||
ID string `json:"id" jsonschema_description:"关键词搜或者周边搜获取到的POI ID"`
|
||||
}
|
||||
@@ -21,64 +38,55 @@ func (t SearchDetailRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t SearchDetailRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&SearchDetailRequest{})
|
||||
return server.ToInputSchema(&SearchDetailRequest{})
|
||||
}
|
||||
|
||||
func (t SearchDetailRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t SearchDetailRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t SearchDetailRequest) 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 := 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)
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/place/detail?id=%s&key=%s&source=ts_mcp", url.QueryEscape(t.ID), 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 {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("search detail call failed, status: %d", statusCode))
|
||||
utils.OnMCPToolCallError(ctx, 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"`
|
||||
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))
|
||||
utils.OnMCPToolCallError(ctx, 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))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
result, _ := json.MarshalIndent(poi, "", " ")
|
||||
utils.SendMCPToolTextResult(ctx, string(result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,39 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var _ server.Tool = TextSearchRequest{}
|
||||
|
||||
type TextSearchRequest struct {
|
||||
Keywords string `json:"keywords" jsonschema_description:"搜索关键词"`
|
||||
City string `json:"city" jsonschema_description:"查询城市"`
|
||||
Citylimit string `json:"citylimit" jsonschema_description:"是否强制限制在设置的城市内搜索,默认值为false"`
|
||||
Keywords string `json:"keywords" jsonschema_description:"搜索关键词"`
|
||||
City string `json:"city" jsonschema_description:"查询城市"`
|
||||
Citylimit string `json:"citylimit" jsonschema_description:"是否强制限制在设置的城市内搜索,默认值为false"`
|
||||
}
|
||||
|
||||
func (t TextSearchRequest) Description() string {
|
||||
@@ -23,41 +41,32 @@ func (t TextSearchRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t TextSearchRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&TextSearchRequest{})
|
||||
return server.ToInputSchema(&TextSearchRequest{})
|
||||
}
|
||||
|
||||
func (t TextSearchRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t TextSearchRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t TextSearchRequest) 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 := 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))
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/place/text?key=%s&keywords=%s&city=%s&citylimit=%s&source=ts_mcp", serverConfig.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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("text search call failed, status: %d", statusCode))
|
||||
return
|
||||
}
|
||||
var response struct {
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Suggestion struct {
|
||||
Keywords []string `json:"keywords"`
|
||||
Cities []struct {
|
||||
@@ -73,18 +82,18 @@ func (t TextSearchRequest) Call(ctx wrapper.HttpContext, config server.AmapMCPSe
|
||||
}
|
||||
err := json.Unmarshal(responseBody, &response)
|
||||
if err != nil {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("failed to parse text search response: %v", err))
|
||||
utils.OnMCPToolCallError(ctx, 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))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
result := fmt.Sprintf(`{"suggestion": {"keywords": %s, "cities": %s}, "pois": %s}`, string(responseBody), string(responseBody), gjson.GetBytes(responseBody, "pois").Raw)
|
||||
utils.SendMCPToolTextResult(ctx, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
// 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/server"
|
||||
"amap-tools/config"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/utils"
|
||||
)
|
||||
|
||||
var _ server.Tool = WeatherRequest{}
|
||||
|
||||
type WeatherRequest struct {
|
||||
City string `json:"city" jsonschema_description:"城市名称或者adcode"`
|
||||
}
|
||||
@@ -21,69 +38,60 @@ func (t WeatherRequest) Description() string {
|
||||
}
|
||||
|
||||
func (t WeatherRequest) InputSchema() map[string]any {
|
||||
return wrapper.ToInputSchema(&WeatherRequest{})
|
||||
return server.ToInputSchema(&WeatherRequest{})
|
||||
}
|
||||
|
||||
func (t WeatherRequest) Create(params []byte) wrapper.MCPTool[server.AmapMCPServer] {
|
||||
func (t WeatherRequest) Create(params []byte) server.Tool {
|
||||
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
|
||||
func (t WeatherRequest) 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 := 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)
|
||||
url := fmt.Sprintf("http://restapi.amap.com/v3/weather/weatherInfo?city=%s&key=%s&source=ts_mcp&extensions=all", url.QueryEscape(t.City), 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 {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("weather call failed, status: %d", statusCode))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("weather call failed, status: %d", statusCode))
|
||||
return
|
||||
}
|
||||
var response struct {
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Forecasts []struct {
|
||||
City string `json:"city"`
|
||||
City string `json:"city"`
|
||||
Casts []struct {
|
||||
Date string `json:"date"`
|
||||
Week string `json:"week"`
|
||||
DayWeather string `json:"dayweather"`
|
||||
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"`
|
||||
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))
|
||||
utils.OnMCPToolCallError(ctx, fmt.Errorf("failed to parse weather response: %v", err))
|
||||
return
|
||||
}
|
||||
if response.Status != "1" {
|
||||
ctx.OnMCPToolCallError(fmt.Errorf("weather failed: %s", response.Info))
|
||||
utils.OnMCPToolCallError(ctx, 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)
|
||||
result, _ := json.MarshalIndent(forecasts, "", " ")
|
||||
utils.SendMCPToolTextResult(ctx, string(result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user