mirror of
https://github.com/alibaba/higress.git
synced 2026-05-28 22:57:31 +08:00
refactor mcp sdk (#1977)
This commit is contained in:
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user