@@ -2,7 +2,7 @@ import type { JSONRPCMessage, JSONRPCRequest } from '@modelcontextprotocol/core'
22import { OAuthError , OAuthErrorCode , SdkErrorCode , SdkHttpError } from '@modelcontextprotocol/core' ;
33import type { Mock , Mocked } from 'vitest' ;
44
5- import type { OAuthClientProvider } from '../../src/client/auth.js' ;
5+ import type { AuthProvider , OAuthClientProvider } from '../../src/client/auth.js' ;
66import { UnauthorizedError } from '../../src/client/auth.js' ;
77import type { ReconnectionScheduler , StartSSEOptions , StreamableHTTPReconnectionOptions } from '../../src/client/streamableHttp.js' ;
88import { StreamableHTTPClientTransport } from '../../src/client/streamableHttp.js' ;
@@ -573,6 +573,36 @@ describe('StreamableHTTPClientTransport', () => {
573573 expect ( globalThis . fetch ) . toHaveBeenCalledTimes ( 2 ) ;
574574 } ) ;
575575
576+ it ( 'should let auth provider headers override custom Authorization headers' , async ( ) => {
577+ const authProvider : AuthProvider = {
578+ token : async ( ) => 'fresh-token'
579+ } ;
580+
581+ transport = new StreamableHTTPClientTransport ( new URL ( 'http://localhost:1234/mcp' ) , {
582+ authProvider,
583+ requestInit : {
584+ headers : {
585+ Authorization : 'Bearer stale-token' ,
586+ 'X-Custom-Header' : 'CustomValue'
587+ }
588+ }
589+ } ) ;
590+
591+ let actualReqInit : RequestInit = { } ;
592+
593+ ( globalThis . fetch as Mock ) . mockImplementation ( async ( _url , reqInit ) => {
594+ actualReqInit = reqInit ;
595+ return new Response ( null , { status : 200 , headers : { 'content-type' : 'text/event-stream' } } ) ;
596+ } ) ;
597+
598+ await transport . start ( ) ;
599+
600+ await transport [ '_startOrAuthSse' ] ( { } ) ;
601+
602+ expect ( ( actualReqInit . headers as Headers ) . get ( 'authorization' ) ) . toBe ( 'Bearer fresh-token' ) ;
603+ expect ( ( actualReqInit . headers as Headers ) . get ( 'x-custom-header' ) ) . toBe ( 'CustomValue' ) ;
604+ } ) ;
605+
576606 it ( 'should always send specified custom headers (Headers class)' , async ( ) => {
577607 const requestInit = {
578608 headers : new Headers ( {
0 commit comments