auth: OAuth client rejects protected resource metadata when the MCP server URL has a query string
Describe the bug
The OAuth client fails to authorize against an MCP server whose URL carries a query string, when the server's Protected Resource Metadata names the resource without that query.
AuthorizationCodeHandler passes req.URL.String(), query included, as the resource URL. oauthex.GetProtectedResourceMetadata then compares it to the metadata's resource with !=, so every candidate is refused and the handler falls back to treating the server root as the authorization server.
RFC 8707 section 2 says a resource indicator "SHOULD NOT include a query component", so a server that takes configuration in its URL (for example https://mcp.example.com/mcp?features=flags) publishes "resource": "https://mcp.example.com/mcp", and its WWW-Authenticate challenge points at metadata for that resource.
This is the same strict comparison as #886 (trailing slash), for the query component.
To Reproduce
func TestQueryInServerURL(t *testing.T) {
var base string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"resource": base + "/mcp",
"authorization_servers": []string{"https://as.example"},
})
}))
defer srv.Close()
base = srv.URL
meta := base + "/.well-known/oauth-protected-resource/mcp"
if _, err := oauthex.GetProtectedResourceMetadata(context.Background(), meta, base+"/mcp", srv.Client()); err != nil {
t.Fatal(err) // passes
}
if _, err := oauthex.GetProtectedResourceMetadata(context.Background(), meta, base+"/mcp?features=flags", srv.Client()); err != nil {
t.Fatal(err) // got metadata resource ".../mcp", want ".../mcp?features=flags"
}
}
Expected behavior
The server URL's query (and fragment) is dropped before the resource is compared and before it is sent as the resource parameter.
The other official SDKs accept this case:
- TypeScript:
checkResourceAllowedcompares origin and path only. - Python:
check_resource_alloweddoes the same, and uses the metadata's resource. - Rust:
is_resource_identifier_validnotes "Query parameters may carry transport hints rather than resource identity."
Additional context
- go-sdk v1.8.0;
mainhas the same code (oauthex/resource_meta.go,auth/authorization_code.go). protectedResourceMetadataURLscopies the URL with its query too, so the well-known candidates it builds also carry the query.
I'd like to work on this issue.
I would close as working as intended, following the same rationale as #886.
Per RFC 9728 §3.3, the resource value in Protected Resource Metadata MUST be identical (RFC 3986 §6.2.1 simple string comparison) to the resource/request URL, and RFC 9728 §3.1 preserves query components when constructing path-based .well-known URLs. While RFC 8707 §2 says a resource URI SHOULD NOT include a query component, that same sentence explicitly notes that query components are valid and necessary when used to scope requests to an application or tenant, so unconditionally stripping or ignoring query parameters in GetProtectedResourceMetadata would violate RFC 9728 §3.3 and weaken resource/audience isolation for query-scoped endpoints.