@@ -122,6 +122,58 @@ test('should initialize with supported older protocol version', async () => {
122122 expect ( client . getInstructions ( ) ) . toBeUndefined ( ) ;
123123} ) ;
124124
125+ /***
126+ * Test: Reconnecting with the same Client restores protocol version on new transport
127+ */
128+ test ( 'should restore negotiated protocol version on transport when reconnecting with same client' , async ( ) => {
129+ const setProtocolVersion = vi . fn ( ) ;
130+ const initialTransport : Transport = {
131+ start : vi . fn ( ) . mockResolvedValue ( undefined ) ,
132+ close : vi . fn ( ) . mockResolvedValue ( undefined ) ,
133+ setProtocolVersion,
134+ send : vi . fn ( ) . mockImplementation ( message => {
135+ if ( message . method === 'initialize' ) {
136+ initialTransport . onmessage ?.( {
137+ jsonrpc : '2.0' ,
138+ id : message . id ,
139+ result : {
140+ protocolVersion : LATEST_PROTOCOL_VERSION ,
141+ capabilities : { } ,
142+ serverInfo : { name : 'test' , version : '1.0' }
143+ }
144+ } ) ;
145+ }
146+ return Promise . resolve ( ) ;
147+ } )
148+ } ;
149+
150+ const client = new Client ( { name : 'test client' , version : '1.0' } ) ;
151+ await client . connect ( initialTransport ) ;
152+
153+ // Initial handshake should have set the protocol version on the transport
154+ expect ( setProtocolVersion ) . toHaveBeenCalledWith ( LATEST_PROTOCOL_VERSION ) ;
155+ expect ( client . getNegotiatedProtocolVersion ( ) ) . toBe ( LATEST_PROTOCOL_VERSION ) ;
156+
157+ // Now simulate reconnection: new transport with a pre-existing sessionId.
158+ // connect() will early-return without re-initializing, but MUST restore the protocol version
159+ // so HTTP transports can keep sending the required mcp-protocol-version header.
160+ const reconnectSetProtocolVersion = vi . fn ( ) ;
161+ const reconnectTransport : Transport = {
162+ start : vi . fn ( ) . mockResolvedValue ( undefined ) ,
163+ close : vi . fn ( ) . mockResolvedValue ( undefined ) ,
164+ setProtocolVersion : reconnectSetProtocolVersion ,
165+ send : vi . fn ( ) . mockResolvedValue ( undefined ) ,
166+ sessionId : 'existing-session-id'
167+ } ;
168+
169+ await client . connect ( reconnectTransport ) ;
170+
171+ // No initialize request should have been sent (sessionId was set)
172+ expect ( reconnectTransport . send ) . not . toHaveBeenCalledWith ( expect . objectContaining ( { method : 'initialize' } ) , expect . anything ( ) ) ;
173+ // But the protocol version MUST have been restored onto the new transport
174+ expect ( reconnectSetProtocolVersion ) . toHaveBeenCalledWith ( LATEST_PROTOCOL_VERSION ) ;
175+ } ) ;
176+
125177/***
126178 * Test: Reject Unsupported Protocol Version
127179 */
0 commit comments