diff --git a/plugins/wasm-go/mcp-filters/mcp-router/go.mod b/plugins/wasm-go/mcp-filters/mcp-router/go.mod index 450271df5..d553b98ae 100644 --- a/plugins/wasm-go/mcp-filters/mcp-router/go.mod +++ b/plugins/wasm-go/mcp-filters/mcp-router/go.mod @@ -4,7 +4,7 @@ go 1.24.1 require ( github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80 - github.com/higress-group/wasm-go v1.0.0 + github.com/higress-group/wasm-go v1.0.2-0.20250807064511-eb1cd98e1f57 github.com/tidwall/gjson v1.18.0 github.com/tidwall/sjson v1.2.5 ) diff --git a/plugins/wasm-go/mcp-filters/mcp-router/go.sum b/plugins/wasm-go/mcp-filters/mcp-router/go.sum index 0062a8784..610816df7 100644 --- a/plugins/wasm-go/mcp-filters/mcp-router/go.sum +++ b/plugins/wasm-go/mcp-filters/mcp-router/go.sum @@ -22,8 +22,8 @@ github.com/higress-group/gjson_template v0.0.0-20250413075336-4c4161ed428b h1:rR github.com/higress-group/gjson_template v0.0.0-20250413075336-4c4161ed428b/go.mod h1:rU3M+Tq5VrQOo0dxpKHGb03Ty0sdWIZfAH+YCOACx/Y= github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80 h1:xqmtTZI0JQ2O+Lg9/CE6c+Tw9KD6FnvWw8EpLVuuvfg= github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80/go.mod h1:tRI2LfMudSkKHhyv1uex3BWzcice2s/l8Ah8axporfA= -github.com/higress-group/wasm-go v1.0.0 h1:4Ik5n3FsJ5+r13KLQl2ky+8NuAE8dfWQwoKxXYD2KAw= -github.com/higress-group/wasm-go v1.0.0/go.mod h1:ODBV27sjmhIW8Cqv3R74EUcTnbdkE69bmXBQFuRkY1M= +github.com/higress-group/wasm-go v1.0.2-0.20250807064511-eb1cd98e1f57 h1:WhNdnKSDtAQrh4Yil8HAtbl7VW+WC85m7WS8kirnHAA= +github.com/higress-group/wasm-go v1.0.2-0.20250807064511-eb1cd98e1f57/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI= github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E= diff --git a/plugins/wasm-go/mcp-filters/mcp-router/main.go b/plugins/wasm-go/mcp-filters/mcp-router/main.go index 3cab08898..2672a6526 100644 --- a/plugins/wasm-go/mcp-filters/mcp-router/main.go +++ b/plugins/wasm-go/mcp-filters/mcp-router/main.go @@ -23,6 +23,7 @@ import ( "github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types" "github.com/higress-group/wasm-go/pkg/log" "github.com/higress-group/wasm-go/pkg/mcp" + "github.com/higress-group/wasm-go/pkg/mcp/consts" "github.com/higress-group/wasm-go/pkg/wrapper" "github.com/tidwall/gjson" "github.com/tidwall/sjson" @@ -33,7 +34,7 @@ func main() {} func init() { mcp.LoadMCPFilter( mcp.FilterName("mcp-router"), - mcp.SetConfigParser(ParseConfig), + mcp.SetConfigOverrideParser(ParseGlobalConfig, ParseOverrideConfig), mcp.SetToolCallRequestFilter(ProcessRequest), ) mcp.InitMCPFilter() @@ -46,13 +47,18 @@ type ServerConfig struct { Path string `json:"path"` } -// McpRouterConfig represents the configuration for the mcp-router filter -type McpRouterConfig struct { +// McpRouterGlobalConfig represents the global configuration for the mcp-router filter +type McpRouterGlobalConfig struct { Servers []ServerConfig `json:"servers"` } -func ParseConfig(configBytes []byte, filterConfig *any) error { - var config McpRouterConfig +type McpRouterConfig struct { + global *McpRouterGlobalConfig + enable bool +} + +func ParseGlobalConfig(configBytes []byte, globalConfig *any) error { + var config McpRouterGlobalConfig if err := json.Unmarshal(configBytes, &config); err != nil { return fmt.Errorf("failed to parse mcp-router config: %v", err) } @@ -62,7 +68,26 @@ func ParseConfig(configBytes []byte, filterConfig *any) error { log.Debugf("Server: %s -> %s%s", server.Name, server.Domain, server.Path) } - *filterConfig = config + *globalConfig = config + return nil +} + +func ParseOverrideConfig(configBytes []byte, globalConfig any, ruleConfig *any) error { + var config McpRouterConfig + if globalConfig == nil { + config.global = &McpRouterGlobalConfig{} + config.enable = false + *ruleConfig = config + log.Error("globalConfig not found, mcp router will not work") + return nil + } + parent, ok := globalConfig.(McpRouterGlobalConfig) + if !ok { + return fmt.Errorf("invalid globalConfig: %v", globalConfig) + } + config.global = &parent + config.enable = gjson.GetBytes(configBytes, "enable").Bool() + *ruleConfig = config return nil } @@ -72,9 +97,11 @@ func ProcessRequest(context wrapper.HttpContext, config any, toolName string, to log.Errorf("Invalid config type for mcp-router") return types.ActionContinue } - - // Extract server name from tool name (format: "serverName/toolName") - parts := strings.SplitN(toolName, "/", 2) + if !routerConfig.enable { + return types.ActionContinue + } + // Extract server name from tool name (format: "${serverName}HigressRouteTo${toolName}") + parts := strings.SplitN(toolName, consts.ToolSetNameSplitter, 2) if len(parts) != 2 { log.Debugf("Tool name '%s' does not contain server prefix, continuing without routing", toolName) return types.ActionContinue @@ -87,7 +114,7 @@ func ProcessRequest(context wrapper.HttpContext, config any, toolName string, to // Find the server configuration var targetServer *ServerConfig - for _, server := range routerConfig.Servers { + for _, server := range routerConfig.global.Servers { if server.Name == serverName { targetServer = &server break @@ -109,6 +136,8 @@ func ProcessRequest(context wrapper.HttpContext, config any, toolName string, to } } + proxywasm.ReplaceHttpRequestHeader("x-envoy-internal-route", "true") + // Modify the :path header if err := proxywasm.ReplaceHttpRequestHeader(":path", targetServer.Path); err != nil { log.Errorf("Failed to set :path header to '%s': %v", targetServer.Path, err) 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 a21b7a130..ac57fb43d 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/higress-group/wasm-go v1.0.2-0.20250716111441-3cf11ef966ee + github.com/higress-group/wasm-go v1.0.2-0.20250807070306-374f35270fed 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 09bd1c66a..e4aac1dbd 100644 --- a/plugins/wasm-go/mcp-servers/all-in-one/go.sum +++ b/plugins/wasm-go/mcp-servers/all-in-one/go.sum @@ -22,20 +22,10 @@ github.com/higress-group/gjson_template v0.0.0-20250413075336-4c4161ed428b h1:rR github.com/higress-group/gjson_template v0.0.0-20250413075336-4c4161ed428b/go.mod h1:rU3M+Tq5VrQOo0dxpKHGb03Ty0sdWIZfAH+YCOACx/Y= github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80 h1:xqmtTZI0JQ2O+Lg9/CE6c+Tw9KD6FnvWw8EpLVuuvfg= github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80/go.mod h1:tRI2LfMudSkKHhyv1uex3BWzcice2s/l8Ah8axporfA= -github.com/higress-group/wasm-go v1.0.1-0.20250626074812-b8840174d876 h1:dJEP8h+FN9Q5PUs5zttXl2bS6lGf/bOsX9GMBTHaL3I= -github.com/higress-group/wasm-go v1.0.1-0.20250626074812-b8840174d876/go.mod h1:ODBV27sjmhIW8Cqv3R74EUcTnbdkE69bmXBQFuRkY1M= -github.com/higress-group/wasm-go v1.0.2-0.20250715121020-5879b92dc5d7 h1:xDXsZvzqfKjqHFlIGslIm28ww55LbZzUvjH7T8W8FIM= -github.com/higress-group/wasm-go v1.0.2-0.20250715121020-5879b92dc5d7/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= -github.com/higress-group/wasm-go v1.0.2-0.20250715121418-f70be79831c7 h1:lg88PkUfdTiRvNP2xBwSYEUnfVEDqPbCFdzSi5Vhsus= -github.com/higress-group/wasm-go v1.0.2-0.20250715121418-f70be79831c7/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= -github.com/higress-group/wasm-go v1.0.2-0.20250715122353-fca24464ab2d h1:abvhxCCNJgQij0IvRvuLnudjAPXwfuv2+d9WZwkACNQ= -github.com/higress-group/wasm-go v1.0.2-0.20250715122353-fca24464ab2d/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= -github.com/higress-group/wasm-go v1.0.2-0.20250716075136-2fd71f44e538 h1:MPEGQ5lcxvgAQdhnbj9wK841t1V0N7iNcQT01VrDRu4= -github.com/higress-group/wasm-go v1.0.2-0.20250716075136-2fd71f44e538/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= -github.com/higress-group/wasm-go v1.0.2-0.20250716110618-3f09aca6b56c h1:36nJBMxlD2j9Qtwb/Ch/8moXaMQ+aWjmFdb3iDomNqs= -github.com/higress-group/wasm-go v1.0.2-0.20250716110618-3f09aca6b56c/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= -github.com/higress-group/wasm-go v1.0.2-0.20250716111441-3cf11ef966ee h1:Bdm+2fBOdBoD7EfVrS4jr0Ip9ePkQ39IZ4kI/m/rQ4o= -github.com/higress-group/wasm-go v1.0.2-0.20250716111441-3cf11ef966ee/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= +github.com/higress-group/wasm-go v1.0.2-0.20250807053623-866ca892b26a h1:vG5Wi3QDktTMPDTsdRdjrNqFYsn+cHJmX0gIbJjPBig= +github.com/higress-group/wasm-go v1.0.2-0.20250807053623-866ca892b26a/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= +github.com/higress-group/wasm-go v1.0.2-0.20250807070306-374f35270fed h1:FlQSHQYCCDnu2QkXSHWFKqkT4q9KDhMhiXYCEOciKOA= +github.com/higress-group/wasm-go v1.0.2-0.20250807070306-374f35270fed/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M= github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI= github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E=