validAudiences in the OAuth server config is set to the raw betterAuthUrl string (http://localhost:3000/api/auth) instead of its origin (http://localhost:3000), so when the CLI's token exchange presents resource=http://localhost:3000, the set-membership check fails and the server returns 400.
5 Whys Chain
Why
Finding
1
Token endpoint throws "requested resource invalid" — checkResource() in @better-auth/oauth-provider rejects the audience
BETTER_AUTH_URL=http://localhost:3000/api/auth is passed raw into validAudiences with no path stripping
4
validAudiences expects a resource origin per RFC 8707, not an auth-path URL — semantically different values were mixed
5
ROOT CAUSE: No URL.origin extraction before registering the audience; works only when BETTER_AUTH_URL lacks the /api/auth suffix
Proposed Fix
File: app/src/server/auth/config.ts line 130
typescript
// Before
validAudiences: [config.betterAuthUrl],
// After
validAudiences: [new URL(config.betterAuthUrl).origin],
One expression. No-op when BETTER_AUTH_URL already has no path. Risk: LOW.
Side effect: JWTs issued after the fix will carry aud: "http://localhost:3000" instead of aud: "http://localhost:3000/api/auth". Active tokens expire within 1 hour naturally.
Questions for you
Does this root cause match your understanding?
Do you approve the proposed one-line fix?
Should I also add BETTER_AUTH_URL=http://localhost:3000 with a clarifying comment to .env.example to prevent recurrence?
User
yes
Assistant
Now let me apply the fix and write a regression test. First, let me look at existing unit tests for the OAuth/auth area:
Assistant
Now I'll write the regression test:
Assistant
All 3 pass. Let me also update .env.example as agreed:
Assistant
Done. Here's a summary of the changes:
Changes
app/src/server/auth/config.ts (bug fix)
typescript
// Before
validAudiences: [config.betterAuthUrl],
// After
validAudiences: [new URL(config.betterAuthUrl).origin],
tests/unit/auth/oauth-valid-audiences.test.ts (regression test — 3 cases, all passing)
Path-suffix URL (http://localhost:3000/api/auth) → would have failed before fix
No-path URL (http://localhost:3000) → was always working, still passes
Production HTTPS URL → covers deployment scenario
.env.example — added BETTER_AUTH_URL=http://localhost:3000 with a comment clarifying the "no /api/auth suffix" requirement.
User
are u sure that removing /api/auth from BETTER_AUTH_URL doesnt break anything else?
Assistant
Good question. Let me check all the places betterAuthUrl is used before making that claim.
Assistant
Found two concerns. Let me show you the key spots:
Assistant
Not entirely, no. I found two spots to carefully consider:
// Primary: validate against betterAuthUrl as-is
// Secondary: if betterAuthUrl has no /api/auth, try betterAuthUrl + /api/auth
const authAudience = betterAuthUrl.endsWith("/api/auth")
? betterAuthUrl // already tried this as primary, skip
: `${betterAuthUrl}/api/auth`;
if (authAudience === betterAuthUrl) return undefined; // skip redundant secondary
The catch: if BETTER_AUTH_URL=http://localhost:3000/api/auth, the secondary validation is skipped (because they'd be identical). The code only tries http://localhost:3000/api/auth. Our fix now issues JWTs with aud: http://localhost:3000 (origin). So if someone keeps the /api/auth suffix in their env, proxy token auth breaks — the JWT audience no longer matches the single audience the proxy tries.
If BETTER_AUTH_URL=http://localhost:3000, the proxy tries origin first (matches new JWTs), then /api/auth as fallback (matches pre-fix tokens during the 1h window). That's correct.
With origin-only, authorization_servers: ["http://localhost:3000"] — MCP clients look for metadata at http://localhost:3000/.well-known/oauth-authorization-server. The server already proxies that path to Better Auth, so this works fine.
With /api/auth suffix, clients would look at http://localhost:3000/api/auth/.well-known/oauth-authorization-server (RFC 8414 path-issuer convention) — a path the server does not proxy. So origin-only is actually more correct here.
3. betterAuth({ baseURL }) — the tests confirm origin-only is right
The acceptance test kit always passes http://localhost:{port} (no path). Better Auth's own docs show BETTER_AUTH_URL as the app origin. With basePath: "/api/auth" also set in createAuth, giving it baseURL=http://localhost:3000/api/auth would be redundant/wrong.
Verdict: the .env.example recommendation is correct and necessary — BETTER_AUTH_URL should be origin-only. If you leave it as http://localhost:3000/api/auth, the proxy token route will silently reject the new JWTs the fix starts issuing. The comment I added is accurate.
User
backwards compatibility doesnt matter
Assistant
Then everything is good. Summary of what was changed: