|
| 1 | +package mcpgrafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/mark3labs/mcp-go/mcp" |
| 10 | + "github.com/mark3labs/mcp-go/server" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// mockSessionWithTools implements both server.ClientSession and server.SessionWithTools |
| 16 | +// for testing the horizontal scaling fix where AddSessionTools needs to find |
| 17 | +// the session in MCPServer.sessions. |
| 18 | +type mockSessionWithTools struct { |
| 19 | + id string |
| 20 | + notifChannel chan mcp.JSONRPCNotification |
| 21 | + isInitialized bool |
| 22 | + tools map[string]server.ServerTool |
| 23 | + mu sync.RWMutex |
| 24 | +} |
| 25 | + |
| 26 | +func newMockSessionWithTools(id string) *mockSessionWithTools { |
| 27 | + return &mockSessionWithTools{ |
| 28 | + id: id, |
| 29 | + notifChannel: make(chan mcp.JSONRPCNotification, 10), |
| 30 | + tools: make(map[string]server.ServerTool), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (m *mockSessionWithTools) SessionID() string { |
| 35 | + return m.id |
| 36 | +} |
| 37 | + |
| 38 | +func (m *mockSessionWithTools) NotificationChannel() chan<- mcp.JSONRPCNotification { |
| 39 | + return m.notifChannel |
| 40 | +} |
| 41 | + |
| 42 | +func (m *mockSessionWithTools) Initialize() { |
| 43 | + m.isInitialized = true |
| 44 | +} |
| 45 | + |
| 46 | +func (m *mockSessionWithTools) Initialized() bool { |
| 47 | + return m.isInitialized |
| 48 | +} |
| 49 | + |
| 50 | +func (m *mockSessionWithTools) GetSessionTools() map[string]server.ServerTool { |
| 51 | + m.mu.RLock() |
| 52 | + defer m.mu.RUnlock() |
| 53 | + cp := make(map[string]server.ServerTool, len(m.tools)) |
| 54 | + for k, v := range m.tools { |
| 55 | + cp[k] = v |
| 56 | + } |
| 57 | + return cp |
| 58 | +} |
| 59 | + |
| 60 | +func (m *mockSessionWithTools) SetSessionTools(tools map[string]server.ServerTool) { |
| 61 | + m.mu.Lock() |
| 62 | + defer m.mu.Unlock() |
| 63 | + m.tools = tools |
| 64 | +} |
| 65 | + |
| 66 | +// TestRegisterSessionFixesAddSessionTools verifies the core fix for issue #749: |
| 67 | +// when an ephemeral session is registered in MCPServer.sessions via RegisterSession, |
| 68 | +// AddSessionTools succeeds instead of returning ErrSessionNotFound. |
| 69 | +func TestRegisterSessionFixesAddSessionTools(t *testing.T) { |
| 70 | + t.Run("AddSessionTools fails without RegisterSession", func(t *testing.T) { |
| 71 | + s := server.NewMCPServer("test", "1.0.0") |
| 72 | + session := newMockSessionWithTools("unregistered-session") |
| 73 | + |
| 74 | + err := s.AddSessionTools(session.SessionID(), server.ServerTool{ |
| 75 | + Tool: mcp.NewTool("test-tool"), |
| 76 | + Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { return nil, nil }, |
| 77 | + }) |
| 78 | + assert.Error(t, err, "AddSessionTools should fail for unregistered session") |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("AddSessionTools succeeds after RegisterSession", func(t *testing.T) { |
| 82 | + s := server.NewMCPServer("test", "1.0.0") |
| 83 | + session := newMockSessionWithTools("cross-pod-session") |
| 84 | + |
| 85 | + // Simulate what the fix does: register the ephemeral session |
| 86 | + err := s.RegisterSession(context.Background(), session) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + // Now AddSessionTools should succeed |
| 90 | + err = s.AddSessionTools(session.SessionID(), server.ServerTool{ |
| 91 | + Tool: mcp.NewTool("tempo_traceql-search"), |
| 92 | + Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { return nil, nil }, |
| 93 | + }) |
| 94 | + assert.NoError(t, err, "AddSessionTools should succeed after RegisterSession") |
| 95 | + |
| 96 | + // Verify the tool was actually registered |
| 97 | + tools := session.GetSessionTools() |
| 98 | + assert.Contains(t, tools, "tempo_traceql-search") |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("RegisterSession is idempotent for already-registered sessions", func(t *testing.T) { |
| 102 | + s := server.NewMCPServer("test", "1.0.0") |
| 103 | + session := newMockSessionWithTools("existing-session") |
| 104 | + |
| 105 | + // First registration |
| 106 | + err := s.RegisterSession(context.Background(), session) |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + // Second registration should return ErrSessionExists but not panic |
| 110 | + err = s.RegisterSession(context.Background(), session) |
| 111 | + assert.Error(t, err, "Second RegisterSession should return error") |
| 112 | + |
| 113 | + // AddSessionTools should still work |
| 114 | + err = s.AddSessionTools(session.SessionID(), server.ServerTool{ |
| 115 | + Tool: mcp.NewTool("test-tool"), |
| 116 | + Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { return nil, nil }, |
| 117 | + }) |
| 118 | + assert.NoError(t, err) |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +// TestReaperUnregistersFromMCPServer verifies that the session reaper also |
| 123 | +// cleans up sessions from MCPServer.sessions when SetMCPServer has been called. |
| 124 | +func TestReaperUnregistersFromMCPServer(t *testing.T) { |
| 125 | + t.Run("reaper cleans up MCPServer sessions", func(t *testing.T) { |
| 126 | + s := server.NewMCPServer("test", "1.0.0") |
| 127 | + |
| 128 | + sm := NewSessionManager( |
| 129 | + WithSessionTTL(50 * time.Millisecond), |
| 130 | + ) |
| 131 | + sm.SetMCPServer(s) |
| 132 | + defer sm.Close() |
| 133 | + |
| 134 | + session := newMockSessionWithTools("reap-me") |
| 135 | + |
| 136 | + // Register in both the application SessionManager and MCPServer |
| 137 | + sm.CreateSession(context.Background(), session) |
| 138 | + err := s.RegisterSession(context.Background(), session) |
| 139 | + require.NoError(t, err) |
| 140 | + |
| 141 | + // Add a tool to prove the session is registered |
| 142 | + err = s.AddSessionTools(session.SessionID(), server.ServerTool{ |
| 143 | + Tool: mcp.NewTool("test-tool"), |
| 144 | + Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { return nil, nil }, |
| 145 | + }) |
| 146 | + require.NoError(t, err) |
| 147 | + |
| 148 | + // Wait for the session to become stale and be reaped |
| 149 | + time.Sleep(200 * time.Millisecond) |
| 150 | + |
| 151 | + // Session should be removed from both managers |
| 152 | + _, exists := sm.GetSession("reap-me") |
| 153 | + assert.False(t, exists, "Session should be removed from SessionManager") |
| 154 | + |
| 155 | + // Verify the session is gone from MCPServer too by trying to add tools |
| 156 | + err = s.AddSessionTools("reap-me", server.ServerTool{ |
| 157 | + Tool: mcp.NewTool("another-tool"), |
| 158 | + Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { return nil, nil }, |
| 159 | + }) |
| 160 | + assert.Error(t, err, "Session should be unregistered from MCPServer after reaping") |
| 161 | + }) |
| 162 | + |
| 163 | + t.Run("reaper works without MCPServer reference", func(t *testing.T) { |
| 164 | + sm := NewSessionManager( |
| 165 | + WithSessionTTL(50 * time.Millisecond), |
| 166 | + ) |
| 167 | + // Deliberately NOT calling sm.SetMCPServer() |
| 168 | + defer sm.Close() |
| 169 | + |
| 170 | + session := &mockClientSession{id: "no-mcp-server"} |
| 171 | + sm.CreateSession(context.Background(), session) |
| 172 | + |
| 173 | + // Wait for reaping |
| 174 | + time.Sleep(200 * time.Millisecond) |
| 175 | + |
| 176 | + _, exists := sm.GetSession("no-mcp-server") |
| 177 | + assert.False(t, exists, "Session should still be reaped without MCPServer reference") |
| 178 | + }) |
| 179 | +} |
| 180 | + |
| 181 | +// TestSetMCPServer verifies the SetMCPServer method. |
| 182 | +func TestSetMCPServer(t *testing.T) { |
| 183 | + t.Run("SetMCPServer sets the reference", func(t *testing.T) { |
| 184 | + sm := NewSessionManager() |
| 185 | + defer sm.Close() |
| 186 | + |
| 187 | + s := server.NewMCPServer("test", "1.0.0") |
| 188 | + sm.SetMCPServer(s) |
| 189 | + |
| 190 | + sm.mutex.RLock() |
| 191 | + assert.Equal(t, s, sm.mcpServer) |
| 192 | + sm.mutex.RUnlock() |
| 193 | + }) |
| 194 | + |
| 195 | + t.Run("SetMCPServer can be called with nil", func(t *testing.T) { |
| 196 | + sm := NewSessionManager() |
| 197 | + defer sm.Close() |
| 198 | + |
| 199 | + s := server.NewMCPServer("test", "1.0.0") |
| 200 | + sm.SetMCPServer(s) |
| 201 | + sm.SetMCPServer(nil) |
| 202 | + |
| 203 | + sm.mutex.RLock() |
| 204 | + assert.Nil(t, sm.mcpServer) |
| 205 | + sm.mutex.RUnlock() |
| 206 | + }) |
| 207 | +} |
0 commit comments