refactor mcp sdk (#1977)

This commit is contained in:
澄潭
2025-03-29 20:28:10 +08:00
committed by GitHub
parent 9a07c50f44
commit 037c71a320
23 changed files with 652 additions and 576 deletions

View File

@@ -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))
})
}
}