@@ -754,6 +754,236 @@ 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+ // Verify spy starts clean (no calls from beforeEach setup)
856+ expect ( onerrorSpy ) . not . toHaveBeenCalled ( ) ;
857+
858+ const request = createRequest ( 'POST' , TEST_MESSAGES . toolsList ) ;
859+ await transport . handleRequest ( request ) ;
860+
861+ expect ( onerrorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
862+ 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 / ) ;
863+ } ) ;
864+
865+ it ( 'should call onerror for invalid session ID' , async ( ) => {
866+ await initializeServer ( ) ;
867+ const request = createRequest ( 'POST' , TEST_MESSAGES . toolsList , { sessionId : 'invalid-session-id' } ) ;
868+ await transport . handleRequest ( request ) ;
869+
870+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
871+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / S e s s i o n n o t f o u n d / ) ;
872+ } ) ;
873+
874+ it ( 'should call onerror for re-initialization attempt' , async ( ) => {
875+ await initializeServer ( ) ;
876+ const request = createRequest ( 'POST' , TEST_MESSAGES . initialize ) ;
877+ await transport . handleRequest ( request ) ;
878+
879+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
880+ 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 / ) ;
881+ } ) ;
882+
883+ it ( 'should call onerror for missing Accept header on GET' , async ( ) => {
884+ const sessionId = await initializeServer ( ) ;
885+ const request = createRequest ( 'GET' , undefined , { sessionId, accept : 'application/json' } ) ;
886+ await transport . handleRequest ( request ) ;
887+
888+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
889+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / N o t A c c e p t a b l e / ) ;
890+ } ) ;
891+
892+ it ( 'should call onerror for concurrent SSE streams' , async ( ) => {
893+ const sessionId = await initializeServer ( ) ;
894+
895+ // First SSE stream
896+ const request1 = createRequest ( 'GET' , undefined , { sessionId } ) ;
897+ const response1 = await transport . handleRequest ( request1 ) ;
898+ expect ( response1 . status ) . toBe ( 200 ) ;
899+
900+ // Second SSE stream should trigger onerror
901+ const request2 = createRequest ( 'GET' , undefined , { sessionId } ) ;
902+ await transport . handleRequest ( request2 ) ;
903+
904+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
905+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / O n l y o n e S S E s t r e a m / ) ;
906+ } ) ;
907+
908+ it ( 'should call onerror for unsupported protocol version' , async ( ) => {
909+ const sessionId = await initializeServer ( ) ;
910+
911+ const request = new Request ( 'http://localhost/mcp' , {
912+ method : 'POST' ,
913+ headers : {
914+ 'Content-Type' : 'application/json' ,
915+ Accept : 'application/json, text/event-stream' ,
916+ 'mcp-session-id' : sessionId ,
917+ 'mcp-protocol-version' : 'unsupported-version'
918+ } ,
919+ body : JSON . stringify ( TEST_MESSAGES . toolsList )
920+ } ) ;
921+ await transport . handleRequest ( request ) ;
922+
923+ expect ( onerrorSpy ) . toHaveBeenCalled ( ) ;
924+ 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 / ) ;
925+ } ) ;
926+
927+ it ( 'should call onerror for unsupported HTTP methods' , async ( ) => {
928+ // Verify spy starts clean (no calls from beforeEach setup)
929+ expect ( onerrorSpy ) . not . toHaveBeenCalled ( ) ;
930+
931+ const request = new Request ( 'http://localhost/mcp' , { method : 'PUT' } ) ;
932+ await transport . handleRequest ( request ) ;
933+
934+ expect ( onerrorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
935+ expect ( onerrorSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / M e t h o d n o t a l l o w e d / ) ;
936+ } ) ;
937+
938+ it ( 'should call onerror for invalid event ID in replay' , async ( ) => {
939+ // Transport with event store that returns undefined for unknown IDs
940+ const eventStore : EventStore = {
941+ async storeEvent ( ) : Promise < EventId > {
942+ return 'evt-1' ;
943+ } ,
944+ async getStreamIdForEventId ( ) : Promise < StreamId | undefined > {
945+ return undefined ; // Unknown event ID
946+ } ,
947+ async replayEventsAfter ( ) : Promise < StreamId > {
948+ return 'stream-1' ;
949+ }
950+ } ;
951+ const storeTransport = new WebStandardStreamableHTTPServerTransport ( {
952+ sessionIdGenerator : ( ) => randomUUID ( ) ,
953+ eventStore
954+ } ) ;
955+ const storeSpy = vi . fn < ( error : Error ) => void > ( ) ;
956+ storeTransport . onerror = storeSpy ;
957+
958+ const storeMcpServer = new McpServer ( { name : 'test' , version : '1.0.0' } ) ;
959+ await storeMcpServer . connect ( storeTransport ) ;
960+
961+ // Initialize first
962+ const initReq = createRequest ( 'POST' , TEST_MESSAGES . initialize ) ;
963+ const initResp = await storeTransport . handleRequest ( initReq ) ;
964+ const sid = initResp . headers . get ( 'mcp-session-id' ) as string ;
965+ storeSpy . mockClear ( ) ;
966+
967+ // GET with unknown Last-Event-ID
968+ const request = new Request ( 'http://localhost/mcp' , {
969+ method : 'GET' ,
970+ headers : {
971+ Accept : 'text/event-stream' ,
972+ 'mcp-session-id' : sid ,
973+ 'mcp-protocol-version' : '2025-11-25' ,
974+ 'Last-Event-ID' : 'unknown-event-id'
975+ }
976+ } ) ;
977+ const response = await storeTransport . handleRequest ( request ) ;
978+
979+ expect ( response . status ) . toBe ( 400 ) ;
980+ expect ( storeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
981+ expect ( storeSpy . mock . calls [ 0 ] ! [ 0 ] ! . message ) . toMatch ( / I n v a l i d e v e n t I D f o r m a t / ) ;
982+
983+ await storeTransport . close ( ) ;
984+ } ) ;
985+ } ) ;
986+
757987 describe ( 'HTTPServerTransport - start() method' , ( ) => {
758988 it ( 'should throw error when started twice' , async ( ) => {
759989 const transport = new WebStandardStreamableHTTPServerTransport ( {
0 commit comments