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

@@ -12,30 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package server
package config
import (
"encoding/json"
"errors"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
type QuarkMCPServer struct {
type QuarkServerConfig struct {
ApiKey string `json:"apiKey"`
}
func (s QuarkMCPServer) ConfigHasError() error {
if s.ApiKey == "" {
return errors.New("missing api key")
}
return nil
}
func ParseFromConfig(configBytes []byte, server *QuarkMCPServer) error {
return json.Unmarshal(configBytes, server)
}
func ParseFromRequest(ctx wrapper.HttpContext, server *QuarkMCPServer) error {
return ctx.ParseMCPServerConfig(server)
}

View File

@@ -5,7 +5,7 @@ go 1.24
toolchain go1.24.1
require (
github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250324133957-dab499f6ade6
github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250329122411-e07be585ff55
github.com/tidwall/gjson v1.17.3
)

View File

@@ -1,5 +1,5 @@
github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250324133957-dab499f6ade6 h1:/iHNur+B0lHmcy97XYwHb6QrnHJichzKs37gnTyGP3k=
github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250324133957-dab499f6ade6/go.mod h1:csP9Mpkc+gVgbZsizCdcYSy0LJrQA+//RcnZBInyknc=
github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250329122411-e07be585ff55 h1:yGPhs3VhC4Mj4SmbaLcKSZ1sV8wRW/eGf/13P0c2+S8=
github.com/alibaba/higress/plugins/wasm-go v1.4.4-0.20250329122411-e07be585ff55/go.mod h1:csP9Mpkc+gVgbZsizCdcYSy0LJrQA+//RcnZBInyknc=
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=

View File

@@ -15,18 +15,16 @@
package main
import (
"quark-search/server"
"quark-search/tools"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/alibaba/higress/plugins/wasm-go/pkg/mcp/server"
)
func main() {}
func init() {
wrapper.SetCtx(
"quark-mcp-server",
wrapper.ParseRawConfig(server.ParseFromConfig),
wrapper.AddMCPTool("web_search", tools.WebSearch{}),
)
quarkSearchServer := &server.MCPServer{}
server.Load(server.AddMCPServer(
"quark-search",
quarkSearchServer.AddMCPTool("web_search", &tools.WebSearch{})))
}

View File

@@ -16,18 +16,21 @@ package tools
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"quark-search/server"
"quark-search/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 = WebSearch{}
type SearchResult struct {
Title string
Link string
@@ -70,12 +73,12 @@ Because Quark search performs poorly for English searches, please use Chinese fo
// which defines the JSON Schema for the tool's input parameters, including
// property types, descriptions, and required fields.
func (t WebSearch) InputSchema() map[string]any {
return wrapper.ToInputSchema(&WebSearch{})
return server.ToInputSchema(&WebSearch{})
}
// Create instantiates a new WebSearch tool instance based on the input parameters
// from an MCP tool call.
func (t WebSearch) Create(params []byte) wrapper.MCPTool[server.QuarkMCPServer] {
func (t WebSearch) Create(params []byte) server.Tool {
webSearch := &WebSearch{
ContentMode: "summary",
Number: 5,
@@ -88,20 +91,17 @@ func (t WebSearch) Create(params []byte) wrapper.MCPTool[server.QuarkMCPServer]
// when the tool is invoked through the MCP framework. It processes the configured parameters,
// makes the actual API request to the service, parses the response,
// and formats the results to be returned to the caller.
func (t WebSearch) Call(ctx wrapper.HttpContext, config server.QuarkMCPServer) error {
err := server.ParseFromRequest(ctx, &config)
if err != nil {
log.Errorf("parse config from request failed, err:%s", err)
}
err = config.ConfigHasError()
if err != nil {
return err
func (t WebSearch) Call(ctx server.HttpContext, s server.Server) error {
serverConfig := &config.QuarkServerConfig{}
s.GetConfig(serverConfig)
if serverConfig.ApiKey == "" {
return errors.New("Quark search API key not configured")
}
return ctx.RouteCall(http.MethodGet, fmt.Sprintf("https://cloud-iqs.aliyuncs.com/search/genericSearch?query=%s", url.QueryEscape(t.Query)),
[][2]string{{"Accept", "application/json"},
{"X-API-Key", config.ApiKey}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
{"X-API-Key", serverConfig.ApiKey}}, nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != http.StatusOK {
ctx.OnMCPToolCallError(fmt.Errorf("quark search call failed, status: %d", statusCode))
utils.OnMCPToolCallError(ctx, fmt.Errorf("quark search call failed, status: %d", statusCode))
return
}
jsonObj := gjson.ParseBytes(responseBody)
@@ -125,6 +125,6 @@ func (t WebSearch) Call(ctx wrapper.HttpContext, config server.QuarkMCPServer) e
results = append(results, result.Format())
}
}
ctx.SendMCPToolTextResult(fmt.Sprintf("# Search Results\n\n%s", strings.Join(results, "\n\n")))
utils.SendMCPToolTextResult(ctx, fmt.Sprintf("# Search Results\n\n%s", strings.Join(results, "\n\n")))
})
}