@@ -181,6 +181,7 @@ func TestMCPServer_Tools(t *testing.T) {
181181 err := server .RegisterSession (& fakeSession {
182182 sessionID : "test" ,
183183 notificationChannel : notificationChannel ,
184+ initialized : true ,
184185 })
185186 require .NoError (t , err )
186187 server .SetTools (ServerTool {
@@ -211,6 +212,16 @@ func TestMCPServer_Tools(t *testing.T) {
211212 err := server .RegisterSession (& fakeSession {
212213 sessionID : fmt .Sprintf ("test%d" , i ),
213214 notificationChannel : notificationChannel ,
215+ initialized : true ,
216+ })
217+ require .NoError (t , err )
218+ }
219+ // also let's register inactive sessions
220+ for i := range 5 {
221+ err := server .RegisterSession (& fakeSession {
222+ sessionID : fmt .Sprintf ("test%d" , i + 5 ),
223+ notificationChannel : notificationChannel ,
224+ initialized : false ,
214225 })
215226 require .NoError (t , err )
216227 }
@@ -243,6 +254,7 @@ func TestMCPServer_Tools(t *testing.T) {
243254 err := server .RegisterSession (& fakeSession {
244255 sessionID : "test" ,
245256 notificationChannel : notificationChannel ,
257+ initialized : true ,
246258 })
247259 require .NoError (t , err )
248260 server .AddTool (mcp .NewTool ("test-tool-1" ),
@@ -270,6 +282,7 @@ func TestMCPServer_Tools(t *testing.T) {
270282 err := server .RegisterSession (& fakeSession {
271283 sessionID : "test" ,
272284 notificationChannel : notificationChannel ,
285+ initialized : true ,
273286 })
274287 require .NoError (t , err )
275288 server .SetTools (
@@ -489,12 +502,28 @@ func TestMCPServer_SendNotificationToClient(t *testing.T) {
489502 require .Error (t , srv .SendNotificationToClient (ctx , "method" , nil ))
490503 },
491504 },
505+ {
506+ name : "uninit session" ,
507+ contextPrepare : func (ctx context.Context , srv * MCPServer ) context.Context {
508+ return srv .WithContext (ctx , fakeSession {
509+ sessionID : "test" ,
510+ notificationChannel : make (chan mcp.JSONRPCNotification , 10 ),
511+ initialized : false ,
512+ })
513+ },
514+ validate : func (t * testing.T , ctx context.Context , srv * MCPServer ) {
515+ require .Error (t , srv .SendNotificationToClient (ctx , "method" , nil ))
516+ _ , ok := ClientSessionFromContext (ctx ).(fakeSession )
517+ require .True (t , ok , "session not found or of incorrect type" )
518+ },
519+ },
492520 {
493521 name : "active session" ,
494522 contextPrepare : func (ctx context.Context , srv * MCPServer ) context.Context {
495523 return srv .WithContext (ctx , fakeSession {
496524 sessionID : "test" ,
497525 notificationChannel : make (chan mcp.JSONRPCNotification , 10 ),
526+ initialized : true ,
498527 })
499528 },
500529 validate : func (t * testing.T , ctx context.Context , srv * MCPServer ) {
@@ -519,6 +548,7 @@ func TestMCPServer_SendNotificationToClient(t *testing.T) {
519548 return srv .WithContext (ctx , fakeSession {
520549 sessionID : "test" ,
521550 notificationChannel : make (chan mcp.JSONRPCNotification , 1 ),
551+ initialized : true ,
522552 })
523553 },
524554 validate : func (t * testing.T , ctx context.Context , srv * MCPServer ) {
@@ -1136,6 +1166,7 @@ func createTestServer() *MCPServer {
11361166type fakeSession struct {
11371167 sessionID string
11381168 notificationChannel chan mcp.JSONRPCNotification
1169+ initialized bool
11391170}
11401171
11411172func (f fakeSession ) SessionID () string {
@@ -1146,6 +1177,13 @@ func (f fakeSession) NotificationChannel() chan<- mcp.JSONRPCNotification {
11461177 return f .notificationChannel
11471178}
11481179
1180+ func (f fakeSession ) Initialize () {
1181+ }
1182+
1183+ func (f fakeSession ) Initialized () bool {
1184+ return f .initialized
1185+ }
1186+
11491187var _ ClientSession = fakeSession {}
11501188
11511189func TestMCPServer_WithHooks (t * testing.T ) {
0 commit comments