|
| 1 | +// |
| 2 | +// Tencent is pleased to support the open source community by making trpc-agent-go available. |
| 3 | +// |
| 4 | +// Copyright (C) 2025 Tencent. All rights reserved. |
| 5 | +// |
| 6 | +// trpc-agent-go is licensed under the Apache License Version 2.0. |
| 7 | +// |
| 8 | +// |
| 9 | + |
| 10 | +package e2e |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + "trpc.group/trpc-go/trpc-agent-go/tool" |
| 20 | + "trpc.group/trpc-go/trpc-agent-go/tool/mcp" |
| 21 | +) |
| 22 | + |
| 23 | +// TestFilterPriority_Integration tests the filter logic using a real STDIO MCP server. |
| 24 | +func TestFilterPriority_Integration(t *testing.T) { |
| 25 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 26 | + defer cancel() |
| 27 | + |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + setupConfig func() mcp.ConnectionConfig |
| 31 | + options []mcp.ToolSetOption |
| 32 | + expectedTools []string |
| 33 | + description string |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "include filter", |
| 37 | + setupConfig: func() mcp.ConnectionConfig { |
| 38 | + return mcp.ConnectionConfig{ |
| 39 | + Transport: "stdio", |
| 40 | + Command: "go", |
| 41 | + Args: []string{"run", "./test_server/main.go"}, |
| 42 | + Timeout: 10 * time.Second, |
| 43 | + } |
| 44 | + }, |
| 45 | + options: []mcp.ToolSetOption{ |
| 46 | + mcp.WithToolFilterFunc(tool.NewIncludeToolNamesFilter("tool1")), |
| 47 | + }, |
| 48 | + expectedTools: []string{"tool1"}, |
| 49 | + description: "Include filter should only return tool1", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "exclude filter", |
| 53 | + setupConfig: func() mcp.ConnectionConfig { |
| 54 | + return mcp.ConnectionConfig{ |
| 55 | + Transport: "stdio", |
| 56 | + Command: "go", |
| 57 | + Args: []string{"run", "./test_server/main.go"}, |
| 58 | + Timeout: 10 * time.Second, |
| 59 | + } |
| 60 | + }, |
| 61 | + options: []mcp.ToolSetOption{ |
| 62 | + mcp.WithToolFilterFunc(tool.NewExcludeToolNamesFilter("tool2")), |
| 63 | + }, |
| 64 | + expectedTools: []string{"tool1", "tool3"}, |
| 65 | + description: "Exclude filter should work correctly", |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tt := range tests { |
| 70 | + t.Run(tt.name, func(t *testing.T) { |
| 71 | + // Create toolset with test configuration |
| 72 | + config := tt.setupConfig() |
| 73 | + ts := mcp.NewMCPToolSet(config, tt.options...) |
| 74 | + defer ts.Close() |
| 75 | + |
| 76 | + // Call Tools() - this executes the filter priority logic |
| 77 | + tools := ts.Tools(ctx) |
| 78 | + |
| 79 | + // Verify filtered results |
| 80 | + require.Len(t, tools, len(tt.expectedTools), tt.description) |
| 81 | + |
| 82 | + actualNames := make([]string, len(tools)) |
| 83 | + for i, tool := range tools { |
| 84 | + actualNames[i] = tool.Declaration().Name |
| 85 | + } |
| 86 | + |
| 87 | + assert.ElementsMatch(t, tt.expectedTools, actualNames, tt.description) |
| 88 | + |
| 89 | + t.Logf("✓ %s - got tools: %v", tt.description, actualNames) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments