@@ -101,14 +101,20 @@ describe('MCP server transport request lifecycle instrumentation', () => {
101101 expect ( onRequestCompleted ) . not . toHaveBeenCalled ( ) ;
102102 } ) ;
103103
104- it ( 'completes duplicate responses only once' , async ( ) => {
104+ it ( 'ignores duplicate request IDs until the pending request completes' , async ( ) => {
105+ const onRequestStarted = vi . fn ( ) ;
105106 const onRequestCompleted = vi . fn ( ) ;
106- const { transport } = await createStartedInstrumentedTransport ( { onRequestCompleted } ) ;
107+ const { transport } = await createStartedInstrumentedTransport ( {
108+ onRequestStarted,
109+ onRequestCompleted,
110+ } ) ;
107111
108112 transport . onmessage ?.( { jsonrpc : '2.0' , id : '1' , method : 'initialize' } ) ;
113+ transport . onmessage ?.( { jsonrpc : '2.0' , id : '1' , method : 'tools/list' } ) ;
109114 await transport . send ( { jsonrpc : '2.0' , id : '1' , result : { } } ) ;
110115 await transport . send ( { jsonrpc : '2.0' , id : '1' , result : { } } ) ;
111116
117+ expect ( onRequestStarted ) . toHaveBeenCalledTimes ( 1 ) ;
112118 expect ( onRequestCompleted ) . toHaveBeenCalledTimes ( 1 ) ;
113119 } ) ;
114120
@@ -124,4 +130,26 @@ describe('MCP server transport request lifecycle instrumentation', () => {
124130
125131 expect ( onRequestCompleted ) . toHaveBeenCalledTimes ( 1 ) ;
126132 } ) ;
133+
134+ it ( 'marks completion when downstream message handling throws synchronously' , async ( ) => {
135+ const onRequestStarted = vi . fn ( ) ;
136+ const onRequestCompleted = vi . fn ( ) ;
137+ const transport = new TestTransport ( ) ;
138+ const downstreamOnMessage = vi . fn ( ( ) => {
139+ throw new Error ( 'handler failed' ) ;
140+ } ) ;
141+ instrumentMcpRequestLifecycle ( transport , { onRequestStarted, onRequestCompleted } ) ;
142+
143+ transport . onmessage = downstreamOnMessage ;
144+ await transport . start ( ) ;
145+
146+ expect ( ( ) => {
147+ transport . onmessage ?.( { jsonrpc : '2.0' , id : '1' , method : 'tools/list' } ) ;
148+ } ) . toThrow ( 'handler failed' ) ;
149+
150+ expect ( onRequestStarted ) . toHaveBeenCalledTimes ( 1 ) ;
151+ expect ( onRequestCompleted ) . toHaveBeenCalledTimes ( 1 ) ;
152+ await transport . send ( { jsonrpc : '2.0' , id : '1' , result : { } } ) ;
153+ expect ( onRequestCompleted ) . toHaveBeenCalledTimes ( 1 ) ;
154+ } ) ;
127155} ) ;
0 commit comments