Skip to content

Fall through to well-known discovery when resource metadata auth fails#4371

Merged
reyortiz3 merged 1 commit intomainfrom
fix/resource-metadata-discovery-fallthrough
Mar 26, 2026
Merged

Fall through to well-known discovery when resource metadata auth fails#4371
reyortiz3 merged 1 commit intomainfrom
fix/resource-metadata-discovery-fallthrough

Conversation

@reyortiz3
Copy link
Copy Markdown
Contributor

@reyortiz3 reyortiz3 commented Mar 25, 2026

Summary

  • Servers like Sourcegraph and Deepsearch include resource_metadata in their WWW-Authenticate header but list path-based URLs in their authorization_servers field (e.g. https://sourcegraph.com/.api/mcp/sourcegraph). Per RFC 8414, ToolHive extracts the path as a tenant and builds a discovery URL like /.well-known/oauth-authorization-server/.api/mcp/sourcegraph — an endpoint those servers don't serve. Validation fails and auth breaks.
  • Before 6cad0b59, DeriveIssuerFromURL always stripped the path and returned the root domain issuer, which worked. That commit introduced the RFC 9728 resource metadata flow with a hard return at Priority 3, so when validation fails Priorities 4 and 5 (including DeriveIssuerFromURL) are never reached.
  • Fix: capture the error from tryDiscoverFromResourceMetadata and only return on success. Failures log at DEBUG and fall through to Priority 4 (well-known probing) and Priority 5 (URL-derived issuer), restoring the previous working behaviour.

Type of change

  • Bug fix

Test plan

  • Unit tests (task test)

Does this introduce a user-facing change?

Yes — MCP servers that use path-based authorization_servers URLs in their resource metadata (e.g. Sourcegraph, Deepsearch) will successfully authenticate again instead of failing with a malformed discovery URL error.

Special notes for reviewers

The regression was introduced in 6cad0b59 and first shipped in v0.3.0. Two test cases were updated: malformed resource metadata URL and handles malicious resource metadata response. Both previously expected an error that can no longer be returned — the function now falls through to Priority 5 and returns a URL-derived issuer. The DoS protection property of the second test is unchanged (response body draining is still capped by MaxResponseBodyDrain; the test just now asserts the derived issuer rather than an error).

Generated with Claude Code

When a server returns resource_metadata in its WWW-Authenticate header,
Priority 3 (RFC 9728) would hard-return on any failure, preventing
Priorities 4 and 5 from running. For servers like Sourcegraph and
Deepsearch whose authorization_servers list contains path-based URLs
(e.g. /.api/mcp/deepsearch), RFC 8414 tenant extraction generates
/.well-known/oauth-authorization-server/.api/mcp/deepsearch — an
endpoint those servers don't serve. Validation fails, Priority 3
errors, and auth is broken despite Priorities 4/5 being able to
derive the correct root-domain issuer.

Fix by capturing the error from tryDiscoverFromResourceMetadata and
only returning on success; failures now log at DEBUG and fall through
to well-known probing (Priority 4) and URL-derived issuer (Priority 5),
restoring the behaviour that existed before 6cad0b5.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added the size/XS Extra small PR: < 100 lines changed label Mar 25, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.55%. Comparing base (a4dc39f) to head (5e9e133).
⚠️ Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4371      +/-   ##
==========================================
+ Coverage   69.52%   69.55%   +0.02%     
==========================================
  Files         480      480              
  Lines       48978    48981       +3     
==========================================
+ Hits        34051    34067      +16     
+ Misses      12302    12289      -13     
  Partials     2625     2625              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@lujunsan lujunsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@reyortiz3 reyortiz3 merged commit bf94a06 into main Mar 26, 2026
43 of 44 checks passed
@reyortiz3 reyortiz3 deleted the fix/resource-metadata-discovery-fallthrough branch March 26, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Extra small PR: < 100 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants