@@ -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' ;
@@ -629,6 +629,34 @@ describe('StreamableHTTPClientTransport', () => {
629629 expect ( ( actualReqInit . headers as Headers ) . get ( 'x-custom-header' ) ) . toBe ( 'CustomValue' ) ;
630630 } ) ;
631631
632+ it ( 'uses auth provider Authorization over requestInit Authorization' , async ( ) => {
633+ const authProvider : AuthProvider = {
634+ token : vi . fn ( async ( ) => 'fresh-token' )
635+ } ;
636+
637+ transport = new StreamableHTTPClientTransport ( new URL ( 'http://localhost:1234/mcp' ) , {
638+ authProvider,
639+ requestInit : {
640+ headers : {
641+ Authorization : 'Bearer stale-token' ,
642+ 'X-Custom-Header' : 'CustomValue'
643+ }
644+ }
645+ } ) ;
646+
647+ let actualReqInit : RequestInit = { } ;
648+ ( globalThis . fetch as Mock ) . mockImplementation ( async ( _url , reqInit ) => {
649+ actualReqInit = reqInit ;
650+ return new Response ( null , { status : 202 } ) ;
651+ } ) ;
652+
653+ await transport . send ( { jsonrpc : '2.0' , method : 'test' , params : { } } as JSONRPCMessage ) ;
654+
655+ const headers = actualReqInit . headers as Headers ;
656+ expect ( headers . get ( 'authorization' ) ) . toBe ( 'Bearer fresh-token' ) ;
657+ expect ( headers . get ( 'x-custom-header' ) ) . toBe ( 'CustomValue' ) ;
658+ } ) ;
659+
632660 it ( 'should append custom Accept header to required types on POST requests' , async ( ) => {
633661 transport = new StreamableHTTPClientTransport ( new URL ( 'http://localhost:1234/mcp' ) , {
634662 requestInit : {
0 commit comments