Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions auth/authorization_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ func errorFromChallenges(cs []oauthex.Challenge) string {
// If no metadata was found or the fetched metadata fails security checks,
// it returns an error.
func (h *AuthorizationCodeHandler) getProtectedResourceMetadata(ctx context.Context, wwwChallenges []oauthex.Challenge, mcpServerURL string) (*oauthex.ProtectedResourceMetadata, error) {
// RFC 8707 section 2: a resource indicator SHOULD NOT include a query component.
if u, err := url.Parse(mcpServerURL); err == nil {
u.RawQuery, u.ForceQuery, u.Fragment = "", false, ""
mcpServerURL = u.String()
}
// Use MCP server URL as the resource URI per
// https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization#canonical-server-uri.
for _, url := range protectedResourceMetadataURLs(resourceMetadataURLFromChallenges(wwwChallenges), mcpServerURL) {
Expand Down
32 changes: 32 additions & 0 deletions auth/authorization_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,38 @@ func TestGetProtectedResourceMetadata_Success(t *testing.T) {
}
}

// TestGetProtectedResourceMetadata_QueryInServerURL verifies that a query in
// the MCP server URL does not prevent matching metadata whose resource has no
// query (RFC 8707 section 2).
func TestGetProtectedResourceMetadata_QueryInServerURL(t *testing.T) {
handler, err := NewAuthorizationCodeHandler(validConfig())
if err != nil {
t.Fatalf("NewAuthorizationCodeHandler() error = %v", err)
}
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
metadata := &oauthex.ProtectedResourceMetadata{
Resource: server.URL + "/mcp",
AuthorizationServers: []string{"https://oauth.example.com"},
}
mux.Handle("/.well-known/oauth-protected-resource/mcp", ProtectedResourceMetadataHandler(metadata))
challenges := []oauthex.Challenge{{
Scheme: "Bearer",
Params: map[string]string{
"resource_metadata": server.URL + "/.well-known/oauth-protected-resource/mcp",
},
}}

got, err := handler.getProtectedResourceMetadata(t.Context(), challenges, server.URL+"/mcp?features=flags")
if err != nil {
t.Fatalf("getProtectedResourceMetadata() error = %v", err)
}
if diff := cmp.Diff(metadata, got); diff != "" {
t.Errorf("getProtectedResourceMetadata() metadata mismatch (-want +got):\n%s", diff)
}
}

func TestGetProtectedResourceMetadata_Backcompat(t *testing.T) {
handler, err := NewAuthorizationCodeHandler(validConfig())
if err != nil {
Expand Down
Loading