mirror of
https://github.com/alibaba/higress.git
synced 2026-02-27 22:20:57 +08:00
486 lines
12 KiB
Go
486 lines
12 KiB
Go
// 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 server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestToolsListForwarding tests the tools/list request forwarding
|
|
func TestToolsListForwarding(t *testing.T) {
|
|
// Create proxy server with tools
|
|
server := NewMcpProxyServer("tools-list-test")
|
|
|
|
// Set server fields directly
|
|
server.SetMcpServerURL("http://backend.example.com/mcp")
|
|
server.SetTimeout(5000)
|
|
|
|
// Add test tools
|
|
toolConfigs := []McpProxyToolConfig{
|
|
{
|
|
Name: "get_weather",
|
|
Description: "Get weather information",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "location",
|
|
Description: "City name",
|
|
Type: "string",
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Name: "get_news",
|
|
Description: "Get latest news",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "category",
|
|
Description: "News category",
|
|
Type: "string",
|
|
Required: false,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, toolConfig := range toolConfigs {
|
|
err := server.AddProxyTool(toolConfig)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// Skip HttpContext-dependent test for now - will be tested in integration
|
|
// Test that tools were added to server successfully
|
|
tools := server.GetMCPTools()
|
|
assert.Len(t, tools, 2)
|
|
assert.Contains(t, tools, "get_weather")
|
|
assert.Contains(t, tools, "get_news")
|
|
}
|
|
|
|
// TestToolsCallForwarding tests the tools/call request forwarding
|
|
func TestToolsCallForwarding(t *testing.T) {
|
|
server := NewMcpProxyServer("tools-call-test")
|
|
|
|
// Set server fields directly
|
|
server.SetMcpServerURL("http://backend.example.com/mcp")
|
|
server.SetTimeout(5000)
|
|
|
|
// Add test tool
|
|
toolConfig := McpProxyToolConfig{
|
|
Name: "test_tool",
|
|
Description: "Test tool for call forwarding",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "input",
|
|
Description: "Input parameter",
|
|
Type: "string",
|
|
Required: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
err := server.AddProxyTool(toolConfig)
|
|
require.NoError(t, err)
|
|
|
|
// Get the tool and create instance
|
|
tool, exists := server.GetMCPTools()["test_tool"]
|
|
require.True(t, exists)
|
|
|
|
params := map[string]interface{}{
|
|
"input": "test value",
|
|
}
|
|
paramsBytes, err := json.Marshal(params)
|
|
require.NoError(t, err)
|
|
|
|
toolInstance := tool.Create(paramsBytes)
|
|
require.NotNil(t, toolInstance)
|
|
|
|
// Skip HttpContext-dependent test for now - will be tested in integration
|
|
// Test tool instance creation was successful
|
|
assert.NotNil(t, toolInstance)
|
|
assert.Equal(t, "test_tool", toolInstance.(*McpProxyTool).name)
|
|
assert.Equal(t, "test value", toolInstance.(*McpProxyTool).arguments["input"])
|
|
}
|
|
|
|
// TestToolsCallWithParameters tests tool call with various parameter types
|
|
func TestToolsCallWithParameters(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
toolConfig McpProxyToolConfig
|
|
params map[string]interface{}
|
|
shouldErr bool
|
|
}{
|
|
{
|
|
name: "string parameter",
|
|
toolConfig: McpProxyToolConfig{
|
|
Name: "string_tool",
|
|
Description: "Tool with string parameter",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "text",
|
|
Description: "Text input",
|
|
Type: "string",
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
params: map[string]interface{}{
|
|
"text": "hello world",
|
|
},
|
|
shouldErr: false,
|
|
},
|
|
{
|
|
name: "number parameter",
|
|
toolConfig: McpProxyToolConfig{
|
|
Name: "number_tool",
|
|
Description: "Tool with number parameter",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "value",
|
|
Description: "Numeric value",
|
|
Type: "number",
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
params: map[string]interface{}{
|
|
"value": 42.5,
|
|
},
|
|
shouldErr: false,
|
|
},
|
|
{
|
|
name: "object parameter",
|
|
toolConfig: McpProxyToolConfig{
|
|
Name: "object_tool",
|
|
Description: "Tool with object parameter",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "data",
|
|
Description: "Object data",
|
|
Type: "object",
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
params: map[string]interface{}{
|
|
"data": map[string]interface{}{
|
|
"key1": "value1",
|
|
"key2": 123,
|
|
},
|
|
},
|
|
shouldErr: false,
|
|
},
|
|
{
|
|
name: "missing required parameter",
|
|
toolConfig: McpProxyToolConfig{
|
|
Name: "required_tool",
|
|
Description: "Tool with required parameter",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "required_param",
|
|
Description: "Required parameter",
|
|
Type: "string",
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
params: map[string]interface{}{},
|
|
shouldErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
server := NewMcpProxyServer("param-test")
|
|
|
|
// Set server fields directly
|
|
server.SetMcpServerURL("http://backend.example.com/mcp")
|
|
server.SetTimeout(5000)
|
|
|
|
err := server.AddProxyTool(tt.toolConfig)
|
|
require.NoError(t, err)
|
|
|
|
tool, exists := server.GetMCPTools()[tt.toolConfig.Name]
|
|
require.True(t, exists)
|
|
|
|
paramsBytes, err := json.Marshal(tt.params)
|
|
require.NoError(t, err)
|
|
|
|
toolInstance := tool.Create(paramsBytes)
|
|
require.NotNil(t, toolInstance)
|
|
|
|
// Skip HttpContext-dependent test for now - will be tested in integration
|
|
// Test tool instance creation
|
|
assert.NotNil(t, toolInstance)
|
|
if !tt.shouldErr {
|
|
assert.Equal(t, tt.toolConfig.Name, toolInstance.(*McpProxyTool).name)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestToolsCallWithCursor tests tools/list with pagination cursor
|
|
func TestToolsCallWithCursor(t *testing.T) {
|
|
server := NewMcpProxyServer("cursor-test")
|
|
|
|
// Set server fields directly
|
|
server.SetMcpServerURL("http://backend.example.com/mcp")
|
|
server.SetTimeout(5000)
|
|
|
|
// Skip HttpContext-dependent test for now - will be tested in integration
|
|
// Test cursor parameter handling logic (basic validation)
|
|
cursor := "page-2-cursor"
|
|
assert.NotNil(t, cursor)
|
|
assert.NotEmpty(t, cursor)
|
|
}
|
|
|
|
// TestBackendErrorHandling tests handling of backend MCP server errors
|
|
func TestBackendErrorHandling(t *testing.T) {
|
|
server := NewMcpProxyServer("error-test")
|
|
|
|
// Set server fields directly
|
|
server.SetMcpServerURL("http://failing-backend.example.com/mcp")
|
|
server.SetTimeout(5000)
|
|
|
|
toolConfig := McpProxyToolConfig{
|
|
Name: "failing_tool",
|
|
Description: "Tool that will fail on backend",
|
|
Args: []ToolArg{
|
|
{
|
|
Name: "input",
|
|
Description: "Input parameter",
|
|
Type: "string",
|
|
Required: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
err := server.AddProxyTool(toolConfig)
|
|
require.NoError(t, err)
|
|
|
|
tool, exists := server.GetMCPTools()["failing_tool"]
|
|
require.True(t, exists)
|
|
|
|
params := map[string]interface{}{
|
|
"input": "test value",
|
|
}
|
|
paramsBytes, err := json.Marshal(params)
|
|
require.NoError(t, err)
|
|
|
|
toolInstance := tool.Create(paramsBytes)
|
|
require.NotNil(t, toolInstance)
|
|
|
|
// Skip HttpContext-dependent test for now - will be tested in integration
|
|
// Test tool instance creation for error scenario
|
|
assert.NotNil(t, toolInstance)
|
|
assert.Equal(t, "failing_tool", toolInstance.(*McpProxyTool).name)
|
|
}
|
|
|
|
// TestParseSSEResponse tests the SSE response parsing functionality
|
|
func TestParseSSEResponse(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
sseData string
|
|
expectedData string
|
|
shouldErr bool
|
|
}{
|
|
{
|
|
name: "valid SSE with JSON data",
|
|
sseData: `event: message
|
|
data: {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"experimental":{},"prompts":{"listChanged":true},"resources":{"subscribe":false,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"Echo Server","version":"1.17.0"}}}
|
|
|
|
`,
|
|
expectedData: `{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"experimental":{},"prompts":{"listChanged":true},"resources":{"subscribe":false,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"Echo Server","version":"1.17.0"}}}`,
|
|
shouldErr: false,
|
|
},
|
|
{
|
|
name: "SSE with multiple lines",
|
|
sseData: `event: message
|
|
data: {"jsonrpc":"2.0","id":2,"result":{"success":true}}
|
|
|
|
event: close
|
|
data: {"jsonrpc":"2.0","method":"close"}
|
|
|
|
`,
|
|
expectedData: `{"jsonrpc":"2.0","id":2,"result":{"success":true}}`,
|
|
shouldErr: false,
|
|
},
|
|
{
|
|
name: "SSE with comments and empty lines",
|
|
sseData: `: This is a comment
|
|
event: message
|
|
|
|
data: {"jsonrpc":"2.0","id":3,"result":{"test":true}}
|
|
|
|
: Another comment
|
|
`,
|
|
expectedData: `{"jsonrpc":"2.0","id":3,"result":{"test":true}}`,
|
|
shouldErr: false,
|
|
},
|
|
{
|
|
name: "SSE with any data content",
|
|
sseData: `event: message
|
|
data: {invalid json}
|
|
|
|
`,
|
|
expectedData: `{invalid json}`,
|
|
shouldErr: false,
|
|
},
|
|
{
|
|
name: "SSE with no data field",
|
|
sseData: `event: message
|
|
id: 123
|
|
|
|
`,
|
|
shouldErr: true,
|
|
},
|
|
{
|
|
name: "empty SSE data",
|
|
sseData: ``,
|
|
shouldErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := parseSSEResponse([]byte(tt.sseData))
|
|
|
|
if tt.shouldErr {
|
|
assert.Error(t, err)
|
|
assert.Nil(t, result)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, result)
|
|
assert.Equal(t, tt.expectedData, string(result))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestIsBackendError tests detection of backend error responses
|
|
func TestIsBackendError(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
response string
|
|
expectError bool
|
|
expectErrType string
|
|
}{
|
|
{
|
|
name: "JSON-RPC 2.0 error with unknown tool",
|
|
response: `{
|
|
"jsonrpc": "2.0",
|
|
"id": 3,
|
|
"error": {
|
|
"code": -32602,
|
|
"message": "Unknown tool: invalid_tool_name"
|
|
}
|
|
}`,
|
|
expectError: true,
|
|
expectErrType: "jsonrpc_error",
|
|
},
|
|
{
|
|
name: "JSON-RPC 2.0 error with method not found",
|
|
response: `{
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"error": {
|
|
"code": -32601,
|
|
"message": "Method not found"
|
|
}
|
|
}`,
|
|
expectError: true,
|
|
expectErrType: "jsonrpc_error",
|
|
},
|
|
{
|
|
name: "result.isError format",
|
|
response: `{
|
|
"jsonrpc": "2.0",
|
|
"id": 3,
|
|
"result": {
|
|
"isError": true,
|
|
"content": [
|
|
{
|
|
"type": "text",
|
|
"text": "Tool execution failed: connection timeout"
|
|
}
|
|
]
|
|
}
|
|
}`,
|
|
expectError: true,
|
|
expectErrType: "result_isError",
|
|
},
|
|
{
|
|
name: "successful response with result",
|
|
response: `{
|
|
"jsonrpc": "2.0",
|
|
"id": 3,
|
|
"result": {
|
|
"content": [
|
|
{
|
|
"type": "text",
|
|
"text": "Success!"
|
|
}
|
|
]
|
|
}
|
|
}`,
|
|
expectError: false,
|
|
expectErrType: "",
|
|
},
|
|
{
|
|
name: "successful response with isError false",
|
|
response: `{
|
|
"jsonrpc": "2.0",
|
|
"id": 3,
|
|
"result": {
|
|
"isError": false,
|
|
"content": [
|
|
{
|
|
"type": "text",
|
|
"text": "Success!"
|
|
}
|
|
]
|
|
}
|
|
}`,
|
|
expectError: false,
|
|
expectErrType: "",
|
|
},
|
|
{
|
|
name: "invalid JSON",
|
|
response: `{invalid json}`,
|
|
expectError: false,
|
|
expectErrType: "",
|
|
},
|
|
{
|
|
name: "empty response",
|
|
response: `{}`,
|
|
expectError: false,
|
|
expectErrType: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
isError, errType := IsBackendError([]byte(tt.response))
|
|
assert.Equal(t, tt.expectError, isError, "isError mismatch")
|
|
assert.Equal(t, tt.expectErrType, errType, "error type mismatch")
|
|
})
|
|
}
|
|
}
|
|
|
|
// ForwardToolsList is now implemented in proxy_server.go
|