@@ -168,7 +168,7 @@ func TestClientReplay(t *testing.T) {
168168 clientSession .CallTool (ctx , & CallToolParams {Name : "multiMessageTool" })
169169
170170 // 4. Read and verify messages until the server signals it's ready for the proxy kill.
171- receivedNotifications := readProgressNotifications (t , ctx , notifications , 2 )
171+ receivedNotifications := readNotifications (t , ctx , notifications , 2 )
172172
173173 wantReceived := []string {"msg1" , "msg2" }
174174 if diff := cmp .Diff (wantReceived , receivedNotifications ); diff != "" {
@@ -201,7 +201,7 @@ func TestClientReplay(t *testing.T) {
201201 // 7. Continue reading from the same connection object.
202202 // Its internal logic should successfully retry, reconnect to the new proxy,
203203 // and receive the replayed messages.
204- recoveredNotifications := readProgressNotifications (t , ctx , notifications , 2 )
204+ recoveredNotifications := readNotifications (t , ctx , notifications , 2 )
205205
206206 // 8. Verify the correct messages were received on the recovered connection.
207207 wantRecovered := []string {"msg3" , "msg4" }
@@ -211,8 +211,40 @@ func TestClientReplay(t *testing.T) {
211211 }
212212}
213213
214- // Helper to read a specific number of progress notifications.
215- func readProgressNotifications (t * testing.T , ctx context.Context , notifications chan string , count int ) []string {
214+ // TestServerInitiatedSSE verifies that the persistent SSE connection remains
215+ // open and can receive multiple, non-consecutive, server-initiated events.
216+ func TestServerInitiatedSSE (t * testing.T ) {
217+ notifications := make (chan string )
218+ server := NewServer (testImpl , & ServerOptions {})
219+
220+ httpServer := httptest .NewServer (NewStreamableHTTPHandler (func (* http.Request ) * Server { return server }, nil ))
221+ defer httpServer .Close ()
222+
223+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
224+ defer cancel ()
225+ client := NewClient (testImpl , & ClientOptions {ToolListChangedHandler : func (ctx context.Context , cc * ClientSession , params * ToolListChangedParams ) {
226+ notifications <- "toolListChanged"
227+ },
228+ })
229+ clientSession , err := client .Connect (ctx , NewStreamableClientTransport (httpServer .URL , nil ))
230+ if err != nil {
231+ t .Fatalf ("client.Connect() failed: %v" , err )
232+ }
233+ defer clientSession .Close ()
234+ time .Sleep (50 * time .Millisecond )
235+ server .AddTool (& Tool {Name : "testTool" , InputSchema : & jsonschema.Schema {}},
236+ func (ctx context.Context , ss * ServerSession , params * CallToolParamsFor [map [string ]any ]) (* CallToolResult , error ) {
237+ return & CallToolResult {}, nil
238+ })
239+ receivedNotifications := readNotifications (t , ctx , notifications , 1 )
240+ wantReceived := []string {"toolListChanged" }
241+ if diff := cmp .Diff (wantReceived , receivedNotifications ); diff != "" {
242+ t .Errorf ("Received notifications mismatch (-want +got):\n %s" , diff )
243+ }
244+ }
245+
246+ // Helper to read a specific number of notifications.
247+ func readNotifications (t * testing.T , ctx context.Context , notifications chan string , count int ) []string {
216248 t .Helper ()
217249 var collectedNotifications []string
218250 for {
0 commit comments