@@ -184,6 +184,7 @@ export class StreamableHTTPServerTransport implements Transport {
184184 private _allowedOrigins ?: string [ ] ;
185185 private _enableDnsRebindingProtection : boolean ;
186186 private _retryInterval ?: number ;
187+ private _protocolVersion ?: string ;
187188
188189 sessionId ?: string ;
189190 onclose ?: ( ) => void ;
@@ -213,6 +214,21 @@ export class StreamableHTTPServerTransport implements Transport {
213214 this . _started = true ;
214215 }
215216
217+ /**
218+ * Sets the protocol version after negotiation during initialization.
219+ * This is called by the Server class after the initialize handshake completes.
220+ */
221+ setProtocolVersion ( version : string ) : void {
222+ this . _protocolVersion = version ;
223+ }
224+
225+ /**
226+ * Gets the negotiated protocol version, if set.
227+ */
228+ get protocolVersion ( ) : string | undefined {
229+ return this . _protocolVersion ;
230+ }
231+
216232 /**
217233 * Validates request headers for DNS rebinding protection.
218234 * @returns Error message if validation fails, undefined if validation passes.
@@ -794,19 +810,32 @@ export class StreamableHTTPServerTransport implements Transport {
794810 return true ;
795811 }
796812
813+ /**
814+ * Validates the MCP-Protocol-Version header on incoming requests.
815+ *
816+ * For initialization: Version negotiation handles unknown versions gracefully
817+ * (server responds with its supported version).
818+ *
819+ * For subsequent requests with MCP-Protocol-Version header:
820+ * - Accept if in supported list
821+ * - 400 if unsupported
822+ *
823+ * For HTTP requests without the MCP-Protocol-Version header:
824+ * - Accept and default to the version negotiated at initialization
825+ */
797826 private validateProtocolVersion ( req : IncomingMessage , res : ServerResponse ) : boolean {
798- let protocolVersion = req . headers [ 'mcp-protocol-version' ] ?? DEFAULT_NEGOTIATED_PROTOCOL_VERSION ;
827+ let protocolVersion = req . headers [ 'mcp-protocol-version' ] ;
799828 if ( Array . isArray ( protocolVersion ) ) {
800829 protocolVersion = protocolVersion [ protocolVersion . length - 1 ] ;
801830 }
802831
803- if ( ! SUPPORTED_PROTOCOL_VERSIONS . includes ( protocolVersion ) ) {
832+ if ( protocolVersion !== undefined && ! SUPPORTED_PROTOCOL_VERSIONS . includes ( protocolVersion ) ) {
804833 res . writeHead ( 400 ) . end (
805834 JSON . stringify ( {
806835 jsonrpc : '2.0' ,
807836 error : {
808837 code : - 32000 ,
809- message : `Bad Request: Unsupported protocol version (supported versions: ${ SUPPORTED_PROTOCOL_VERSIONS . join ( ', ' ) } )`
838+ message : `Bad Request: Unsupported protocol version: ${ protocolVersion } (supported versions: ${ SUPPORTED_PROTOCOL_VERSIONS . join ( ', ' ) } )`
810839 } ,
811840 id : null
812841 } )
0 commit comments