@@ -754,6 +754,193 @@ describe('Zod v4', () => {
754754 } ) ;
755755 } ) ;
756756
757+ describe ( 'HTTPServerTransport - onerror callback' , ( ) => {
758+ let transport : WebStandardStreamableHTTPServerTransport ;
759+ let mcpServer : McpServer ;
760+ let onerrorSpy : ReturnType < typeof vi . fn > ;
761+
762+ beforeEach ( async ( ) => {
763+ onerrorSpy = vi . fn ( ) ;
764+ mcpServer = new McpServer ( { name : 'test-server' , version : '1.0.0' } , { capabilities : { logging : { } } } ) ;
765+
766+ mcpServer . registerTool (
767+ 'greet' ,
768+ {
769+ description : 'A simple greeting tool' ,
770+ inputSchema : z . object ( { name : z . string ( ) . describe ( 'Name to greet' ) } )
771+ } ,
772+ async ( { name } ) : Promise < CallToolResult > => {
773+ return { content : [ { type : 'text' , text : `Hello, ${ name } !` } ] } ;
774+ }
775+ ) ;
776+
777+ transport = new WebStandardStreamableHTTPServerTransport ( {
778+ sessionIdGenerator : ( ) => randomUUID ( )
779+ } ) ;
780+ transport . onerror = onerrorSpy ;
781+
782+ await mcpServer . connect ( transport ) ;
783+ } ) ;
784+
785+ afterEach ( async ( ) => {
786+ await transport . close ( ) ;
787+ } ) ;
788+
789+ async function initializeServer ( ) : Promise < string > {
790+ onerrorSpy . mockClear ( ) ;
791+ const request = createRequest ( 'POST' , TEST_MESSAGES . initialize ) ;
792+ const response = await transport . handleRequest ( request ) ;
793+ expect ( response . status ) . toBe ( 200 ) ;
794+ return response . headers . get ( 'mcp-session-id' ) as string ;
795+ }
796+
797+ it ( 'should call onerror for invalid JSON in POST' , async ( ) => {
798+ await initializeServer ( ) ;
799+ const request = new Request ( 'http://localhost/mcp' , {
800+ method : 'POST' ,
801+ headers : {
802+ Accept : 'application/json, text/event-stream' ,
803+ 'Content-Type' : 'application/json'
804+ } ,
805+ body : 'not valid json'
806+ } ) ;
807+ await transport . handleRequest ( request ) ;
808+
809+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
810+ message : expect . stringMatching ( / I n v a l i d J S O N / )
811+ } ) ) ;
812+ } ) ;
813+
814+ it ( 'should call onerror for invalid JSON-RPC message' , async ( ) => {
815+ const sessionId = await initializeServer ( ) ;
816+ const request = new Request ( 'http://localhost/mcp' , {
817+ method : 'POST' ,
818+ headers : {
819+ Accept : 'application/json, text/event-stream' ,
820+ 'Content-Type' : 'application/json' ,
821+ 'mcp-session-id' : sessionId ,
822+ 'mcp-protocol-version' : '2025-11-25'
823+ } ,
824+ body : JSON . stringify ( { not : 'a valid jsonrpc message' } )
825+ } ) ;
826+ await transport . handleRequest ( request ) ;
827+
828+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
829+ message : expect . stringMatching ( / I n v a l i d J S O N - R P C m e s s a g e / )
830+ } ) ) ;
831+ } ) ;
832+
833+ it ( 'should call onerror for missing Accept header on POST' , async ( ) => {
834+ const request = createRequest ( 'POST' , TEST_MESSAGES . initialize , { accept : 'application/json' } ) ;
835+ await transport . handleRequest ( request ) ;
836+
837+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
838+ message : expect . stringMatching ( / N o t A c c e p t a b l e / )
839+ } ) ) ;
840+ } ) ;
841+
842+ it ( 'should call onerror for unsupported Content-Type' , async ( ) => {
843+ const request = new Request ( 'http://localhost/mcp' , {
844+ method : 'POST' ,
845+ headers : {
846+ Accept : 'application/json, text/event-stream' ,
847+ 'Content-Type' : 'text/plain'
848+ } ,
849+ body : JSON . stringify ( TEST_MESSAGES . initialize )
850+ } ) ;
851+ await transport . handleRequest ( request ) ;
852+
853+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
854+ message : expect . stringMatching ( / U n s u p p o r t e d M e d i a T y p e / )
855+ } ) ) ;
856+ } ) ;
857+
858+ it ( 'should call onerror when server is not initialized' , async ( ) => {
859+ const request = createRequest ( 'POST' , TEST_MESSAGES . toolsList ) ;
860+ await transport . handleRequest ( request ) ;
861+
862+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
863+ message : expect . stringMatching ( / S e r v e r n o t i n i t i a l i z e d / )
864+ } ) ) ;
865+ } ) ;
866+
867+ it ( 'should call onerror for invalid session ID' , async ( ) => {
868+ await initializeServer ( ) ;
869+ const request = createRequest ( 'POST' , TEST_MESSAGES . toolsList , { sessionId : 'invalid-session-id' } ) ;
870+ await transport . handleRequest ( request ) ;
871+
872+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
873+ message : expect . stringMatching ( / S e s s i o n n o t f o u n d / )
874+ } ) ) ;
875+ } ) ;
876+
877+ it ( 'should call onerror for re-initialization attempt' , async ( ) => {
878+ const sessionId = await initializeServer ( ) ;
879+ const request = createRequest ( 'POST' , TEST_MESSAGES . initialize ) ;
880+ await transport . handleRequest ( request ) ;
881+
882+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
883+ message : expect . stringMatching ( / S e r v e r a l r e a d y i n i t i a l i z e d / )
884+ } ) ) ;
885+ } ) ;
886+
887+ it ( 'should call onerror for missing Accept header on GET' , async ( ) => {
888+ const sessionId = await initializeServer ( ) ;
889+ const request = createRequest ( 'GET' , undefined , { sessionId, accept : 'application/json' } ) ;
890+ await transport . handleRequest ( request ) ;
891+
892+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
893+ message : expect . stringMatching ( / N o t A c c e p t a b l e / )
894+ } ) ) ;
895+ } ) ;
896+
897+ it ( 'should call onerror for concurrent SSE streams' , async ( ) => {
898+ const sessionId = await initializeServer ( ) ;
899+
900+ // First SSE stream
901+ const request1 = createRequest ( 'GET' , undefined , { sessionId } ) ;
902+ const response1 = await transport . handleRequest ( request1 ) ;
903+ expect ( response1 . status ) . toBe ( 200 ) ;
904+
905+ // Second SSE stream should trigger onerror
906+ const request2 = createRequest ( 'GET' , undefined , { sessionId } ) ;
907+ await transport . handleRequest ( request2 ) ;
908+
909+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
910+ message : expect . stringMatching ( / O n l y o n e S S E s t r e a m / )
911+ } ) ) ;
912+ } ) ;
913+
914+ it ( 'should call onerror for unsupported protocol version' , async ( ) => {
915+ const sessionId = await initializeServer ( ) ;
916+
917+ const request = new Request ( 'http://localhost/mcp' , {
918+ method : 'POST' ,
919+ headers : {
920+ 'Content-Type' : 'application/json' ,
921+ Accept : 'application/json, text/event-stream' ,
922+ 'mcp-session-id' : sessionId ,
923+ 'mcp-protocol-version' : 'unsupported-version'
924+ } ,
925+ body : JSON . stringify ( TEST_MESSAGES . toolsList )
926+ } ) ;
927+ await transport . handleRequest ( request ) ;
928+
929+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
930+ message : expect . stringMatching ( / U n s u p p o r t e d p r o t o c o l v e r s i o n / )
931+ } ) ) ;
932+ } ) ;
933+
934+ it ( 'should call onerror for unsupported HTTP methods' , async ( ) => {
935+ const request = new Request ( 'http://localhost/mcp' , { method : 'PUT' } ) ;
936+ await transport . handleRequest ( request ) ;
937+
938+ expect ( onerrorSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( {
939+ message : expect . stringMatching ( / M e t h o d n o t a l l o w e d / )
940+ } ) ) ;
941+ } ) ;
942+ } ) ;
943+
757944 describe ( 'HTTPServerTransport - start() method' , ( ) => {
758945 it ( 'should throw error when started twice' , async ( ) => {
759946 const transport = new WebStandardStreamableHTTPServerTransport ( {
0 commit comments