-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: Pass RequestInit options to auth requests #1066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Fixes an issue where custom headers (like user-agent) and other RequestInit options set when creating transports were not being passed through to authorization requests (.well-known/ discovery, token exchange, DCR, etc.). Changes: - Created createFetchWithInit() utility in shared/transport.ts to wrap fetch with base RequestInit options - Transports now wrap their fetch function before passing to auth module - All RequestInit options (headers, credentials, mode, etc.) are now preserved - Auth-specific headers properly override base headers when needed - Extracted normalizeHeaders() to shared/transport.ts for reuse 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Added comprehensive tests to verify that: - Custom headers (like user-agent) from RequestInit are passed to auth requests - Auth-specific headers override base headers when needed - All RequestInit options (credentials, mode, cache, etc.) are preserved All 553 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Fix linting errors by removing unused destructured url variables.
80747de to
4005a0e
Compare
|
@pcarleton checking if we can review this. The underlying problem is that @dcramer is trying to ban clients that spam auth endpoint. because we dont pass user-agent to the http client for auth discovery, he ends up getting generic user-agents and cant ban a specific client. |
src/client/streamableHttp.ts
Outdated
| let result: AuthResult; | ||
| try { | ||
| // Wrap fetch to automatically include base RequestInit options | ||
| const fetchFn = createFetchWithInit(this._fetch, this._requestInit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason to do this each time vs. in the constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me ask claude why it thought this is a good idea :p
Move the createFetchWithInit() call from _authThenStart() to the constructor to avoid recreating the wrapper function on every auth attempt. This is more efficient and follows better practices. The wrapped fetch function is now stored in _fetchWithInit and reused across all auth-related calls in _authThenStart(), finishAuth(), and send(). Addresses code review feedback from @pcarleton 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
commit: |
pcarleton
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woops gotta do sse
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