Repository navigation
Description
Background
ToolHive's embedded OAuth/OIDC authorization server implements RFC 7591 Dynamic Client Registration (DCR). When a client registers, ToolHive validates each requested scope is in scopes_supported and registers the client with exactly that set (pkg/authserver/server/registration/dcr.go:264-305). The registered scope set is then used by fosite to validate later /oauth/authorize requests.
This is RFC-correct, but brittle against buggy DCR clients. Notably, anthropics/claude-code#4540 (open since ~Aug 2025): Claude Code's MCP OAuth client narrows the DCR scope field to openid, then later requests offline_access at /authorize. ToolHive correctly responds:
invalid_scope: The OAuth 2.0 Client is not allowed to request scope 'offline_access'
The asymmetry between well-behaved DCR clients and clients that narrow the scope field can be illustrated as:
| Client behavior | DCR-registered scope | /authorize requests |
Result |
|---|---|---|---|
| DCR sends full scope set | openid profile email offline_access |
openid profile email offline_access |
OK |
DCR narrows to openid only |
openid |
openid offline_access |
FAIL |
DCR narrows, but client uses pre-registered client_id |
openid profile email offline_access |
openid offline_access |
OK |
The current workaround — pre-register a public OAuth client at POST /oauth/register and ask every affected user to add the resulting client_id to their MCP client config — is per-user friction that doesn't scale across a multi-user deployment, and it hands out a shared OAuth client identity (audit attribution still works because the user-facing identity comes from the upstream provider's session, but the shape is leaky).
Proposal
Add an operator-configurable field requiredClientScopes on EmbeddedAuthServerConfig that declares a baseline scope set unioned into every DCR registration:
authServerConfig:
issuer: https://auth.example.com
requiredClientScopes:
- openid
- profile
- email
- offline_access
upstreamProviders: [...]Semantics:
- DCR request with empty
scope→ register withrequiredClientScopes(or hardcodedDefaultScopesif unset, preserving current behavior). - DCR request with non-empty
scope→ register withunion(requested, requiredClientScopes), deduped, validated as ⊆scopesSupported. requiredClientScopesunset → behavior unchanged.
RFC compliance: RFC 7591 §3.1.1 explicitly permits the AS to override the registered scope. The DCR handler already returns the registered scope in the response (pkg/authserver/server/handlers/dcr.go:115), so clients receive honest feedback about what they got.
Implementation outline
Six layers of plumbing; behavioral change isolated to one call site.
| Layer | File | Change |
|---|---|---|
| CRD | cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types.go (struct EmbeddedAuthServerConfig, ~L176) |
Add RequiredClientScopes []string with +optional, +listType=atomic, +kubebuilder:validation:MaxItems=N |
| Operator → RunConfig | cmd/thv-operator/pkg/controllerutil/authserver.go (BuildAuthServerRunConfig) |
Map alongside existing ScopesSupported |
| RunConfig | pkg/authserver/config.go (RunConfig, alongside ScopesSupported at L72) |
Add RequiredClientScopes []string |
| Runner | pkg/authserver/runner/embeddedauthserver.go (resolvedCfg block, L105–120) |
Passthrough |
| Server provider | pkg/authserver/server/provider.go (AuthorizationServerConfig and AuthorizationServerParams, L56–60) |
Add to both — they both already carry ScopesSupported |
| DCR handler (only behavioral change) | pkg/authserver/server/handlers/dcr.go:60-70 |
After ValidateScopes returns, union with h.config.RequiredClientScopes, dedupe, then call registration.New(...) |
ClientConfig.Scopes / DefaultScopes (pkg/authserver/server/registration/client.go:86, :113) need no changes — registration.New is constructed at exactly one production call site (the DCR handler), so doing the union upstream of that call is sufficient and consistent.
Validation
- Startup:
RunConfig.Validate()rejectsrequiredClientScopesnot ⊆scopesSupported. Fail loudly at startup per.claude/rules/go-style.mdconstructor-validation rule. - Runtime: existing per-scope check inside
ValidateScopes(dcr.go:278) re-runs the union result againstallowedScopesas a backstop.
Logging
WARN at DCR registration when the effective (union) scope set differs from what the client requested, including both sets and the diff. Lets operators see the field is firing in practice and gives them the evidence to report bugs upstream against the offending DCR client.
Documentation
This change has user-facing impact and must include doc updates as part of the same PR (or an immediate follow-up):
- User guide — add a section to
guides-k8s/auth-k8s.md(rendered at https://docs.stacklok.com/toolhive/guides-k8s/auth-k8s) explaining whatrequiredClientScopesdoes, when to set it, and the canonical example for working around DCR clients that narrow the scope field. Mirror the framing of the existing "upstream-specific authorization parameters" section so operators recognize the shape. - CRD reference docs — regenerate via
task crdref-genfromcmd/thv-operator/. The field's Go doc comment is what ends up in the rendered reference, so write it as user-facing prose, not as a code comment. - Release notes / changelog — flag as a new operator-facing config field; non-breaking.
Acceptance criteria
- CRD field added with admission validation (
+optional,+listType=atomic,+kubebuilder:validation:MaxItems=N). - Plumbed through operator → RunConfig → runner → server provider.
-
RunConfig.Validate()rejectsrequiredClientScopesnot ⊆scopesSupported. - DCR handler unions baseline into both empty-scope and non-empty-scope registration paths.
- Unit tests for the new union helper covering: empty input + baseline, non-empty subset + baseline, non-empty disjoint + baseline, nil baseline preserves current behavior, baseline scope not in
scopesSupportedfails at startup. - Integration test: a client that DCR-registers with
["openid"]can subsequently authorize withoffline_accesswhenrequiredClientScopesincludes it. - WARN log fires when union expands the registered set.
- User-facing docs section added to
guides-k8s/auth-k8s.md. - CRD reference docs regenerated (
task crdref-gen).
Out of scope
- A "DCR disabled" / lockdown mode (no such mode exists today; orthogonal feature).
- Per-upstream scope translation or filtering on the upstream-bound auth URL (separate, larger design).
- Auto-intersection with upstream
scopes_supporteddiscovery — appealing on paper but mostly useless against major cloud IdPs (Google, Microsoft) which under-advertise their supported scopes.
Prior art
- Claude Code DCR scope-narrowing bugs: anthropics/claude-code#4540 (open since Aug 2025), #7744, #41610.
- hyprmcp/mcp-gateway — server-side default scope injection on DCR.
- PostHog/posthog#45465 — "default to MCP scopes when client doesn't request any."
- Several MCP gateway projects ship custom DCR proxies for the same purpose.
Compatibility
- Backwards-compatible: when
requiredClientScopesis unset, behavior is unchanged. - Existing pre-registered clients (the
--client-idworkaround) are unaffected — they bypass DCR entirely and already have explicit scope sets, so the union step never runs against them. Deployments can keep that workaround in place during rollout and migrate at their own pace.
Activity
Add BaselineClientScopes to embedded auth CRD
Plumb BaselineClientScopes into auth server RunConfig
Validate baseline scopes are subset of supported
Resolve baseline scopes into runtime Config
Forward baseline scopes to AuthorizationServerConfig
Add UnionScopes helper for DCR baseline injection
Apply baseline scope union to DCR registration
23 remaining items
Regenerate swagger docs for BaselineClientScopes
Document baseline scope privilege risk; log at startup
Demote baseline-scope expansion log to Debug
Clone caller-supplied scope and audience slices
Test direct-caller baseline subset gate
Assert invalid_scope in the 400 branch of negative test
Reject empty and malformed baseline scope entries at admission
Drop call-site list from ValidateScopeSubset doc
Clarify scope-response comment in DCR handler
Add baseline subset check to Config.Validate
Honor scopesSupported defaults in baseline subset check
Regenerate swagger docs after baseline-validation fix
Allow operators to inject baseline scopes into DCR registrations (#5233)
jhrozek commentedon May 13, 2026
this was closed in #5233