@@ -180,6 +180,7 @@ func TestMCPServer_Tools(t *testing.T) {
180180 err := server .RegisterSession (& fakeSession {
181181 sessionID : "test" ,
182182 notificationChannel : notificationChannel ,
183+ initialized : true ,
183184 })
184185 require .NoError (t , err )
185186 server .SetTools (ServerTool {
@@ -210,6 +211,16 @@ func TestMCPServer_Tools(t *testing.T) {
210211 err := server .RegisterSession (& fakeSession {
211212 sessionID : fmt .Sprintf ("test%d" , i ),
212213 notificationChannel : notificationChannel ,
214+ initialized : true ,
215+ })
216+ require .NoError (t , err )
217+ }
218+ // also let's register inactive sessions
219+ for i := range 5 {
220+ err := server .RegisterSession (& fakeSession {
221+ sessionID : fmt .Sprintf ("test%d" , i + 5 ),
222+ notificationChannel : notificationChannel ,
223+ initialized : false ,
213224 })
214225 require .NoError (t , err )
215226 }
@@ -242,6 +253,7 @@ func TestMCPServer_Tools(t *testing.T) {
242253 err := server .RegisterSession (& fakeSession {
243254 sessionID : "test" ,
244255 notificationChannel : notificationChannel ,
256+ initialized : true ,
245257 })
246258 require .NoError (t , err )
247259 server .AddTool (mcp .NewTool ("test-tool-1" ),
@@ -269,6 +281,7 @@ func TestMCPServer_Tools(t *testing.T) {
269281 err := server .RegisterSession (& fakeSession {
270282 sessionID : "test" ,
271283 notificationChannel : notificationChannel ,
284+ initialized : true ,
272285 })
273286 require .NoError (t , err )
274287 server .SetTools (
@@ -488,12 +501,28 @@ func TestMCPServer_SendNotificationToClient(t *testing.T) {
488501 require .Error (t , srv .SendNotificationToClient (ctx , "method" , nil ))
489502 },
490503 },
504+ {
505+ name : "uninit session" ,
506+ contextPrepare : func (ctx context.Context , srv * MCPServer ) context.Context {
507+ return srv .WithContext (ctx , fakeSession {
508+ sessionID : "test" ,
509+ notificationChannel : make (chan mcp.JSONRPCNotification , 10 ),
510+ initialized : false ,
511+ })
512+ },
513+ validate : func (t * testing.T , ctx context.Context , srv * MCPServer ) {
514+ require .Error (t , srv .SendNotificationToClient (ctx , "method" , nil ))
515+ _ , ok := ClientSessionFromContext (ctx ).(fakeSession )
516+ require .True (t , ok , "session not found or of incorrect type" )
517+ },
518+ },
491519 {
492520 name : "active session" ,
493521 contextPrepare : func (ctx context.Context , srv * MCPServer ) context.Context {
494522 return srv .WithContext (ctx , fakeSession {
495523 sessionID : "test" ,
496524 notificationChannel : make (chan mcp.JSONRPCNotification , 10 ),
525+ initialized : true ,
497526 })
498527 },
499528 validate : func (t * testing.T , ctx context.Context , srv * MCPServer ) {
@@ -518,6 +547,7 @@ func TestMCPServer_SendNotificationToClient(t *testing.T) {
518547 return srv .WithContext (ctx , fakeSession {
519548 sessionID : "test" ,
520549 notificationChannel : make (chan mcp.JSONRPCNotification , 1 ),
550+ initialized : true ,
521551 })
522552 },
523553 validate : func (t * testing.T , ctx context.Context , srv * MCPServer ) {
@@ -1051,6 +1081,7 @@ func createTestServer() *MCPServer {
10511081type fakeSession struct {
10521082 sessionID string
10531083 notificationChannel chan mcp.JSONRPCNotification
1084+ initialized bool
10541085}
10551086
10561087func (f fakeSession ) SessionID () string {
@@ -1061,4 +1092,11 @@ func (f fakeSession) NotificationChannel() chan<- mcp.JSONRPCNotification {
10611092 return f .notificationChannel
10621093}
10631094
1095+ func (f fakeSession ) Initialize () {
1096+ }
1097+
1098+ func (f fakeSession ) Initialized () bool {
1099+ return f .initialized
1100+ }
1101+
10641102var _ ClientSession = fakeSession {}
0 commit comments