fix: Pass RequestInit options to auth requests #1066
      
        
          +171
        
        
          −21
        
        
          
        
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Summary
Fixes an issue where custom headers (like
user-agent) and otherRequestInitoptions set when creating transports were not being passed through to authorization requests.Problem
When clients set custom headers via
requestInit.headerswhen instantiating transports (StreamableHTTPClientTransport,SSEClientTransport), these headers were correctly used for normal MCP operations but were lost during authorization requests such as:.well-known/metadata discoverySolution
Created a
createFetchWithInit()utility function insrc/shared/transport.tsthat wraps the fetch function to automatically include baseRequestInitoptions. The transports now use this wrapper before passing fetch to auth functions.Key Changes
Added
createFetchWithInit()utility (src/shared/transport.ts)RequestInitoptionsRequestInitoptions (credentials, mode, cache, etc.)Updated transports (
src/client/streamableHttp.ts,src/client/sse.ts)createFetchWithInit()to create wrapped fetch for auth requestsExtracted
normalizeHeaders()utility (src/shared/transport.ts)Benefits
RequestInitoptions preserved, not just headersTesting
Added comprehensive tests in
src/client/auth.test.ts:RequestInitare passed to auth discovery requestsRequestInitoptions are preservedAll 553 tests pass ✅
Test plan
Run the test suite:
npm testAll tests should pass (pre-existing failures in some test suites are due to missing dependencies, not related to these changes).
🤖 Generated with Claude Code