@@ -754,6 +754,182 @@ 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 < ( error : Error ) => void > > ;
761+
762+ beforeEach ( async ( ) => {
763+ onerrorSpy = vi . fn < ( error : Error ) => void > ( ) ;
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 ) . toHaveBeenCalled ( ) ;
810+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / I n v a l i d J S O N / ) ;
811+ } ) ;
812+
813+ it ( 'should call onerror for invalid JSON-RPC message' , async ( ) => {
814+ const sessionId = await initializeServer ( ) ;
815+ const request = new Request ( 'http://localhost/mcp' , {
816+ method : 'POST' ,
817+ headers : {
818+ Accept : 'application/json, text/event-stream' ,
819+ 'Content-Type' : 'application/json' ,
820+ 'mcp-session-id' : sessionId ,
821+ 'mcp-protocol-version' : '2025-11-25'
822+ } ,
823+ body : JSON . stringify ( { not : 'a valid jsonrpc message' } )
824+ } ) ;
825+ await transport . handleRequest ( request ) ;
826+
827+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
828+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / I n v a l i d J S O N - R P C m e s s a g e / ) ;
829+ } ) ;
830+
831+ it ( 'should call onerror for missing Accept header on POST' , async ( ) => {
832+ const request = createRequest ( 'POST' , TEST_MESSAGES . initialize , { accept : 'application/json' } ) ;
833+ await transport . handleRequest ( request ) ;
834+
835+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
836+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / N o t A c c e p t a b l e / ) ;
837+ } ) ;
838+
839+ it ( 'should call onerror for unsupported Content-Type' , async ( ) => {
840+ const request = new Request ( 'http://localhost/mcp' , {
841+ method : 'POST' ,
842+ headers : {
843+ Accept : 'application/json, text/event-stream' ,
844+ 'Content-Type' : 'text/plain'
845+ } ,
846+ body : JSON . stringify ( TEST_MESSAGES . initialize )
847+ } ) ;
848+ await transport . handleRequest ( request ) ;
849+
850+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
851+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / U n s u p p o r t e d M e d i a T y p e / ) ;
852+ } ) ;
853+
854+ it ( 'should call onerror when server is not initialized' , async ( ) => {
855+ const request = createRequest ( 'POST' , TEST_MESSAGES . toolsList ) ;
856+ await transport . handleRequest ( request ) ;
857+
858+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
859+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / S e r v e r n o t i n i t i a l i z e d / ) ;
860+ } ) ;
861+
862+ it ( 'should call onerror for invalid session ID' , async ( ) => {
863+ await initializeServer ( ) ;
864+ const request = createRequest ( 'POST' , TEST_MESSAGES . toolsList , { sessionId : 'invalid-session-id' } ) ;
865+ await transport . handleRequest ( request ) ;
866+
867+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
868+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / S e s s i o n n o t f o u n d / ) ;
869+ } ) ;
870+
871+ it ( 'should call onerror for re-initialization attempt' , async ( ) => {
872+ await initializeServer ( ) ;
873+ const request = createRequest ( 'POST' , TEST_MESSAGES . initialize ) ;
874+ await transport . handleRequest ( request ) ;
875+
876+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
877+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / S e r v e r a l r e a d y i n i t i a l i z e d / ) ;
878+ } ) ;
879+
880+ it ( 'should call onerror for missing Accept header on GET' , async ( ) => {
881+ const sessionId = await initializeServer ( ) ;
882+ const request = createRequest ( 'GET' , undefined , { sessionId, accept : 'application/json' } ) ;
883+ await transport . handleRequest ( request ) ;
884+
885+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
886+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / N o t A c c e p t a b l e / ) ;
887+ } ) ;
888+
889+ it ( 'should call onerror for concurrent SSE streams' , async ( ) => {
890+ const sessionId = await initializeServer ( ) ;
891+
892+ // First SSE stream
893+ const request1 = createRequest ( 'GET' , undefined , { sessionId } ) ;
894+ const response1 = await transport . handleRequest ( request1 ) ;
895+ expect ( response1 . status ) . toBe ( 200 ) ;
896+
897+ // Second SSE stream should trigger onerror
898+ const request2 = createRequest ( 'GET' , undefined , { sessionId } ) ;
899+ await transport . handleRequest ( request2 ) ;
900+
901+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
902+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / O n l y o n e S S E s t r e a m / ) ;
903+ } ) ;
904+
905+ it ( 'should call onerror for unsupported protocol version' , async ( ) => {
906+ const sessionId = await initializeServer ( ) ;
907+
908+ const request = new Request ( 'http://localhost/mcp' , {
909+ method : 'POST' ,
910+ headers : {
911+ 'Content-Type' : 'application/json' ,
912+ Accept : 'application/json, text/event-stream' ,
913+ 'mcp-session-id' : sessionId ,
914+ 'mcp-protocol-version' : 'unsupported-version'
915+ } ,
916+ body : JSON . stringify ( TEST_MESSAGES . toolsList )
917+ } ) ;
918+ await transport . handleRequest ( request ) ;
919+
920+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
921+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / 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 / ) ;
922+ } ) ;
923+
924+ it ( 'should call onerror for unsupported HTTP methods' , async ( ) => {
925+ const request = new Request ( 'http://localhost/mcp' , { method : 'PUT' } ) ;
926+ await transport . handleRequest ( request ) ;
927+
928+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
929+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / M e t h o d n o t a l l o w e d / ) ;
930+ } ) ;
931+ } ) ;
932+
757933 describe ( 'HTTPServerTransport - start() method' , ( ) => {
758934 it ( 'should throw error when started twice' , async ( ) => {
759935 const transport = new WebStandardStreamableHTTPServerTransport ( {
0 commit comments