|
| 1 | +// Tencent is pleased to support the open source community by making trpc-mcp-go available. |
| 2 | +// |
| 3 | +// Copyright (C) 2025 Tencent. All rights reserved. |
| 4 | +// |
| 5 | +// trpc-mcp-go is licensed under the Apache License Version 2.0. |
| 6 | + |
| 7 | +package e2e |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + mcp "trpc.group/trpc-go/trpc-mcp-go" |
| 18 | +) |
| 19 | + |
| 20 | +// TestServerRootsProvider_Streamable tests server-to-client roots functionality over Streamable transport. |
| 21 | +func TestServerRootsProvider_Streamable(t *testing.T) { |
| 22 | + // Start a test server |
| 23 | + serverURL, cleanup := StartTestServer(t, WithTestTools()) |
| 24 | + defer cleanup() |
| 25 | + |
| 26 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 27 | + defer cancel() |
| 28 | + |
| 29 | + // Create client with RootsProvider capability |
| 30 | + client, err := mcp.NewClient( |
| 31 | + serverURL, |
| 32 | + mcp.Implementation{ |
| 33 | + Name: "roots-streamable-test-client", |
| 34 | + Version: "1.0.0", |
| 35 | + }, |
| 36 | + mcp.WithClientLogger(mcp.GetDefaultLogger()), |
| 37 | + ) |
| 38 | + require.NoError(t, err) |
| 39 | + defer client.Close() |
| 40 | + |
| 41 | + // Set up roots provider with test directories |
| 42 | + rootsProvider := mcp.NewDefaultRootsProvider() |
| 43 | + rootsProvider.AddRoot("/tmp", "Temporary Directory") |
| 44 | + rootsProvider.AddRoot("/home", "Home Directory") |
| 45 | + client.SetRootsProvider(rootsProvider) |
| 46 | + |
| 47 | + // Initialize with roots capability |
| 48 | + initResult, err := client.Initialize(ctx, &mcp.InitializeRequest{ |
| 49 | + Params: mcp.InitializeParams{ |
| 50 | + ProtocolVersion: mcp.ProtocolVersion_2025_03_26, |
| 51 | + ClientInfo: mcp.Implementation{ |
| 52 | + Name: "roots-streamable-test-client", |
| 53 | + Version: "1.0.0", |
| 54 | + }, |
| 55 | + Capabilities: mcp.ClientCapabilities{ |
| 56 | + Roots: &mcp.RootsCapability{ |
| 57 | + ListChanged: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }) |
| 62 | + require.NoError(t, err) |
| 63 | + assert.Equal(t, mcp.ProtocolVersion_2025_03_26, initResult.ProtocolVersion) |
| 64 | + |
| 65 | + // Test sending roots list changed notification |
| 66 | + err = client.SendRootsListChangedNotification(ctx) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + // Give the server a moment to process the notification |
| 70 | + time.Sleep(100 * time.Millisecond) |
| 71 | +} |
| 72 | + |
| 73 | +// TestServerRootsProvider_Notification tests roots notification handling. |
| 74 | +func TestServerRootsProvider_Notification(t *testing.T) { |
| 75 | + // Start a test server |
| 76 | + serverURL, cleanup := StartTestServer(t, WithTestTools()) |
| 77 | + defer cleanup() |
| 78 | + |
| 79 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 80 | + defer cancel() |
| 81 | + |
| 82 | + // Create client with RootsProvider capability |
| 83 | + client, err := mcp.NewClient( |
| 84 | + serverURL, |
| 85 | + mcp.Implementation{ |
| 86 | + Name: "roots-notification-test-client", |
| 87 | + Version: "1.0.0", |
| 88 | + }, |
| 89 | + mcp.WithClientLogger(mcp.GetDefaultLogger()), |
| 90 | + ) |
| 91 | + require.NoError(t, err) |
| 92 | + defer client.Close() |
| 93 | + |
| 94 | + // Set up roots provider with test directories |
| 95 | + rootsProvider := mcp.NewDefaultRootsProvider() |
| 96 | + rootsProvider.AddRoot("/opt", "Optional Directory") |
| 97 | + client.SetRootsProvider(rootsProvider) |
| 98 | + |
| 99 | + // Initialize with roots capability |
| 100 | + _, err = client.Initialize(ctx, &mcp.InitializeRequest{ |
| 101 | + Params: mcp.InitializeParams{ |
| 102 | + ProtocolVersion: mcp.ProtocolVersion_2025_03_26, |
| 103 | + ClientInfo: mcp.Implementation{ |
| 104 | + Name: "roots-notification-test-client", |
| 105 | + Version: "1.0.0", |
| 106 | + }, |
| 107 | + Capabilities: mcp.ClientCapabilities{ |
| 108 | + Roots: &mcp.RootsCapability{ |
| 109 | + ListChanged: true, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }) |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + // Register a tool that uses ListRoots |
| 117 | + server := mcp.GetServerFromContext(ctx) |
| 118 | + if server != nil { |
| 119 | + if s, ok := server.(*mcp.Server); ok { |
| 120 | + listRootsTool := mcp.NewTool("list-streamable-roots", |
| 121 | + mcp.WithDescription("List client's root directories via Streamable transport"), |
| 122 | + ) |
| 123 | + |
| 124 | + s.RegisterTool(listRootsTool, func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 125 | + // Call ListRoots to get client roots |
| 126 | + roots, err := s.ListRoots(ctx) |
| 127 | + if err != nil { |
| 128 | + return mcp.NewErrorResult(fmt.Sprintf("Failed to list roots: %v", err)), nil |
| 129 | + } |
| 130 | + |
| 131 | + // Format response |
| 132 | + message := fmt.Sprintf("Streamable client has %d root directories:\n", len(roots.Roots)) |
| 133 | + for i, root := range roots.Roots { |
| 134 | + message += fmt.Sprintf("%d. %s (%s)\n", i+1, root.Name, root.URI) |
| 135 | + } |
| 136 | + |
| 137 | + return mcp.NewTextResult(message), nil |
| 138 | + }) |
| 139 | + |
| 140 | + // Call the tool to test ListRoots functionality |
| 141 | + result, err := client.CallTool(ctx, &mcp.CallToolRequest{ |
| 142 | + Params: mcp.CallToolParams{ |
| 143 | + Name: "list-streamable-roots", |
| 144 | + }, |
| 145 | + }) |
| 146 | + |
| 147 | + if err == nil { |
| 148 | + // Verify the response contains our root directories |
| 149 | + textContent, ok := result.Content[0].(mcp.TextContent) |
| 150 | + require.True(t, ok) |
| 151 | + assert.Contains(t, textContent.Text, "Optional Directory") |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Send roots list changed notification |
| 157 | + err = client.SendRootsListChangedNotification(ctx) |
| 158 | + require.NoError(t, err) |
| 159 | + |
| 160 | + // Give the server a moment to process the notification |
| 161 | + time.Sleep(100 * time.Millisecond) |
| 162 | +} |
0 commit comments