@@ -894,6 +894,101 @@ func TestStreamableHTTP_HeaderPassthrough(t *testing.T) {
894894 }
895895}
896896
897+ func TestStreamableHTTP_PongResponseHandling (t * testing.T ) {
898+ // Ping/Pong does not require session ID
899+ // https://modelcontextprotocol.io/specification/2025-03-26/basic/utilities/ping
900+ mcpServer := NewMCPServer ("test-mcp-server" , "1.0" )
901+ server := NewTestStreamableHTTPServer (mcpServer )
902+ defer server .Close ()
903+
904+ t .Run ("Pong response with empty result should not be treated as sampling response" , func (t * testing.T ) {
905+ // According to MCP spec, pong responses have empty result: {"jsonrpc": "2.0", "id": "123", "result": {}}
906+ pongResponse := map [string ]any {
907+ "jsonrpc" : "2.0" ,
908+ "id" : 123 ,
909+ "result" : map [string ]any {},
910+ }
911+
912+ resp , err := postJSON (server .URL , pongResponse )
913+ if err != nil {
914+ t .Fatalf ("Failed to send pong response: %v" , err )
915+ }
916+ defer func () { _ = resp .Body .Close () }()
917+
918+ bodyBytes , err := io .ReadAll (resp .Body )
919+ if err != nil {
920+ t .Fatalf ("Failed to read response body: %v" , err )
921+ }
922+ bodyStr := string (bodyBytes )
923+
924+ if strings .Contains (bodyStr , "Missing session ID for sampling response" ) {
925+ t .Errorf ("Pong response was incorrectly detected as sampling response. Response: %s" , bodyStr )
926+ }
927+ if strings .Contains (bodyStr , "Failed to handle sampling response" ) {
928+ t .Errorf ("Pong response was incorrectly detected as sampling response. Response: %s" , bodyStr )
929+ }
930+
931+ if resp .StatusCode != http .StatusOK {
932+ t .Errorf ("Expected status 200 for pong response, got %d. Body: %s" , resp .StatusCode , bodyStr )
933+ }
934+ })
935+
936+ t .Run ("Pong response with null result should not be treated as sampling response" , func (t * testing.T ) {
937+ pongResponse := map [string ]any {
938+ "jsonrpc" : "2.0" ,
939+ "id" : 124 ,
940+ }
941+
942+ resp , err := postJSON (server .URL , pongResponse )
943+ if err != nil {
944+ t .Fatalf ("Failed to send pong response: %v" , err )
945+ }
946+ defer func () { _ = resp .Body .Close () }()
947+
948+ bodyBytes , err := io .ReadAll (resp .Body )
949+ if err != nil {
950+ t .Fatalf ("Failed to read response body: %v" , err )
951+ }
952+ bodyStr := string (bodyBytes )
953+
954+ if strings .Contains (bodyStr , "Missing session ID for sampling response" ) {
955+ t .Errorf ("Pong response with omitted result was incorrectly detected as sampling response. Response: %s" , bodyStr )
956+ }
957+
958+ if resp .StatusCode != http .StatusOK {
959+ t .Errorf ("Expected status 200 for pong response, got %d. Body: %s" , resp .StatusCode , bodyStr )
960+ }
961+ })
962+
963+ t .Run ("Response with empty error should not be treated as sampling response" , func (t * testing.T ) {
964+ response := map [string ]any {
965+ "jsonrpc" : "2.0" ,
966+ "id" : 125 ,
967+ "error" : map [string ]any {},
968+ }
969+
970+ resp , err := postJSON (server .URL , response )
971+ if err != nil {
972+ t .Fatalf ("Failed to send response: %v" , err )
973+ }
974+ defer func () { _ = resp .Body .Close () }()
975+
976+ bodyBytes , err := io .ReadAll (resp .Body )
977+ if err != nil {
978+ t .Fatalf ("Failed to read response body: %v" , err )
979+ }
980+ bodyStr := string (bodyBytes )
981+
982+ if strings .Contains (bodyStr , "Missing session ID for sampling response" ) {
983+ t .Errorf ("Response with empty error was incorrectly detected as sampling response. Response: %s" , bodyStr )
984+ }
985+
986+ if resp .StatusCode != http .StatusOK {
987+ t .Errorf ("Expected status 200 for response with empty error, got %d. Body: %s" , resp .StatusCode , bodyStr )
988+ }
989+ })
990+ }
991+
897992func TestStreamableHTTPServer_TLS (t * testing.T ) {
898993 t .Run ("TLS options are set correctly" , func (t * testing.T ) {
899994 mcpServer := NewMCPServer ("test-mcp-server" , "1.0.0" )
0 commit comments