Implement RFC9728 - Support WWW-Authenticate header by MCP client #1071
+230
−92
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.
Resolves #1054
This PR implements proper WWW-Authenticate header parsing for protected resource metadata URL discovery as required by RFC9728 and the MCP specification. This change ensures full compliance with MCP authentication requirements and includes several related improvements to the authentication flow.
Changes Made
Primary Changes
Indirect Improvements
Motivation and Context
As per MCP specification:
This implementation was missing from the current MCP client, creating a gap in specification compliance that has real-world implications. The absence of proper WWW-Authenticate header handling prevents hosting multiple MCP servers that share the same domain but function as separate OAuth resources.
How Has This Been Tested?
The changes were verified by the comprehensive unit-test coverage for different scenarios, as well as simulating the Authorization scenario by executing the client locally.
Breaking Changes
Authorization Flow Change: The client now follows proper OAuth flow by sending an initial request to the resource and only performing authorization upon receiving a 401 status code. This aligns with OAuth specifications and security best practices.
Impact: Clients that relied on the previous behavior (where authorization was attempted regardless of initial response) may need to be updated. However, this change improves security and compliance with OAuth standards.
Migration: Most properly configured MCP servers should work without changes. Servers that expect immediate authorization without sending a 401 response should be updated to follow OAuth specifications.
Types of changes
Checklist
Additional context