Skip to content

Conversation

@BabyChrist666
Copy link

Summary

Fixes #1919

This PR fixes the OAuth trailing slash issue where Pydantic's AnyHttpUrl automatically adds a trailing slash to URLs without path components, breaking OAuth discovery for clients like Google ADK and IBM MCP Context Forge that require exact URL matching per RFC 8414 Section 3.3.

Changes

  • Added @field_serializer("issuer") to OAuthMetadata to strip trailing slashes during JSON serialization
  • Added @field_serializer("resource") to ProtectedResourceMetadata for the same reason
  • Added @field_serializer("authorization_servers") to strip trailing slashes from all auth server URLs
  • Added comprehensive tests for the trailing slash behavior

Technical Details

  • RFC 8414 examples show issuer URLs without trailing slashes
  • The fix only affects JSON serialization output, not internal type handling
  • Paths are preserved (e.g., https://example.com/auth stays as-is)
  • Only lone trailing slashes are stripped (e.g., https://example.com/https://example.com)

Test plan

  • Added tests in tests/shared/test_auth.py for OAuthMetadata.issuer serialization
  • Added tests for ProtectedResourceMetadata.resource and authorization_servers
  • Updated snapshot test in tests/server/auth/test_protected_resource.py
  • All 52 tests pass

🤖 Generated with Claude Code

…tion

Fixes modelcontextprotocol#1919

RFC 8414 examples show issuer URLs without trailing slashes (e.g.,
`https://example.com` rather than `https://example.com/`). Some OAuth
clients (Google ADK, IBM MCP Context Forge) require exact match between
the discovery URL and the returned issuer per RFC 8414 Section 3.3.

Pydantic's AnyHttpUrl automatically adds a trailing slash when serializing
URLs without a path component. This breaks clients that perform exact URL
matching during the OAuth discovery flow.

This fix adds field_serializer decorators to:
- OAuthMetadata.issuer
- ProtectedResourceMetadata.resource
- ProtectedResourceMetadata.authorization_servers

These serializers strip the trailing slash only during JSON serialization,
preserving the internal AnyHttpUrl type while ensuring spec-compliant output.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@maxisbey maxisbey added auth Issues and PRs related to Authentication / OAuth bug Something isn't working needs confirmation Needs confirmation that the PR is actually required or needed. labels Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Issues and PRs related to Authentication / OAuth bug Something isn't working needs confirmation Needs confirmation that the PR is actually required or needed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trailing slash in OAuthMetadata's issuer causes issues with clients

2 participants