Skip to content

Implement RFC9728 - Support WWW-Authenticate header by MCP client #1071

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

yurikunash
Copy link

@yurikunash yurikunash commented Jul 2, 2025

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

  • WWW-Authenticate Header Parsing: Implemented parsing of WWW-Authenticate headers for protected resource metadata URL discovery per RFC9728
  • MCP Specification Compliance: Now properly handles HTTP 401 Unauthorized responses as required by the MCP specification

Indirect Improvements

  • Optimized Authorization Flow: Stop making unnecessary Authorize requests when the initial request doesn't respond with 401 status code
  • Code Cleanup: Removed code duplication in authentication flow logic

Motivation and Context

As per MCP specification:

MCP clients MUST be able to parse WWW-Authenticate headers and respond appropriately to HTTP 401 Unauthorized responses from the MCP server.

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • [] Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@yurikunash yurikunash changed the title RFC9728_www-authentication RFC9728 WWW-Authenticate parsing for MCP client Jul 2, 2025
@ihrpr ihrpr added this to the auth milestone Jul 3, 2025
@yurikunash yurikunash changed the title RFC9728 WWW-Authenticate parsing for MCP client Implement RFC9728 - Parse WWW-Authenticate parsing by MCP client Jul 4, 2025
@yurikunash yurikunash changed the title Implement RFC9728 - Parse WWW-Authenticate parsing by MCP client Implement RFC9728 - Support WWW-Authenticate header by MCP client Jul 4, 2025
@dead8309
Copy link

dead8309 commented Jul 8, 2025

Hi, Any updates on it ? @ihrpr

we rely on correct parsing of Auth header

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WWW-Authenticate header is not respected by Client SDK
3 participants