Skip to content

fix(mcp): advertise the scopes the tools require, so a client can ask for them - #121

Merged
imshashank merged 1 commit into
mainfrom
fix-mcp-scopes
Aug 7, 2026
Merged

imshashank merged 1 commit into
mainfrom
fix-mcp-scopes

Conversation

@imshashank

@imshashank imshashank commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

The MCP server has been refusing every tool call. This is why, and it is a regression from #115.

The two discovery documents disagreed

/.well-known/oauth-authorization-server advertises the full set:

"scopes_supported": ["openid","profile","email","offline_access","orbit.read","orbit.write"]

/.well-known/oauth-protected-resource advertises only four:

"scopes_supported": ["openid","profile","email","offline_access"]

The protected-resource document is the one an MCP client reads to learn what to ask for, and it was delegating wholesale to better-auth's oAuthProtectedResourceMetadata, which knows nothing about the Orbit scopes.

So the client requested a token without orbit.read, and #115's scope enforcement, which is correct and should stay, then refused all 74 tools. Checked against production: there is exactly one live access token and its scopes are openid profile email offline_access.

The nasty part is that re-authorising did not help. The client would read the same under-advertised document and mint another token missing the same scope, which is why this looked unfixable from the client end.

The fix

The protected-resource route now advertises MCP_SCOPES, the same constant the authorization server issues from, and preserves the CORS headers the metadata endpoints need. The /mcp variant re-exports it, so both stay in step.

A test pins the two documents together: adding a scope to the authorization server without adding it here fails the build. That is the invariant that was missing, and it is what let the two drift apart in the first place.

After this deploys

The existing token still lacks orbit.read, so it needs re-authorising once. I deliberately did not grandfather old tokens into the new scopes: that would undo the enforcement #115 added, and a single re-authorisation is the cheaper price.

Greptile Summary

The PR updates OAuth protected-resource metadata to advertise the same MCP scopes configured for token issuance and adds tests pinning that invariant.

  • Wraps the better-auth metadata response and replaces scopes_supported with MCP_SCOPES.
  • Preserves metadata CORS handling and shares the handler with the /mcp route variant.
  • Tests scope advertisement and preservation of unrelated metadata fields.

Confidence Score: 4/5

The PR appears safe to merge, with one non-blocking error-handling issue in the metadata wrapper.

Scope advertisement is correctly synchronized across both protected-resource routes, but a malformed upstream response would have its parsing failure hidden and its consumed body returned to the client.

Files Needing Attention: apps/web/src/app/.well-known/oauth-protected-resource/route.ts

Important Files Changed

Filename Overview
apps/web/src/app/.well-known/oauth-protected-resource/route.ts Correctly adds Orbit scopes, but the malformed-response fallback silently suppresses parsing errors and returns a consumed response.
apps/web/tests/app/well-known/protected-resource-scopes.test.ts Adds focused coverage for scope synchronization and preservation of unrelated metadata fields.

Fix All in Claude Code

Prompt To Fix All With AI
### Issue 1
apps/web/src/app/.well-known/oauth-protected-resource/route.ts:16-17
**Preserve metadata parse failures**

When the wrapped handler returns malformed or non-JSON content, `response.json()` consumes the body and suppresses the parsing error before the same response is returned, leaving the client without the original response body or useful diagnostics.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "fix(mcp): advertise the scopes the tools..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

Context used:

  • Context used - CLAUDE.md (source)

… for them

The two discovery documents disagreed. The authorization server offered
orbit.read and orbit.write, but the protected resource document, which is
the one an MCP client reads to learn what to request, listed only openid,
profile, email and offline_access, because it was delegating wholesale to
better-auth's default.

So a client asked for a token without orbit.read, and every tool call was
then refused by the scope enforcement added in #115. Production had one
live token and it carried exactly those four scopes. Re-authorising would
have minted another broken one, which is why this looked unfixable from
the client side.

The protected resource document now advertises MCP_SCOPES, the same list
the authorization server issues from, and a test pins the two together so
adding a scope to one without the other fails the build.
@vercel

vercel Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
orbit Ready Ready Preview Aug 7, 2026 5:45am

Request Review

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@imshashank, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 27 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e4297298-dc3c-416d-a614-fda5c9389f54

📥 Commits

Reviewing files that changed from the base of the PR and between 468980e and 4555ef6.

📒 Files selected for processing (2)
  • apps/web/src/app/.well-known/oauth-protected-resource/route.ts
  • apps/web/tests/app/well-known/protected-resource-scopes.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment on lines +16 to +17
const metadata: unknown = await response.json().catch(() => null);
if (metadata === null || typeof metadata !== 'object' || Array.isArray(metadata)) return response;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Preserve metadata parse failures

When the wrapped handler returns malformed or non-JSON content, response.json() consumes the body and suppresses the parsing error before the same response is returned, leaving the client without the original response body or useful diagnostics.

Context Used: CLAUDE.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/web/src/app/.well-known/oauth-protected-resource/route.ts
Line: 16-17

Comment:
**Preserve metadata parse failures**

When the wrapped handler returns malformed or non-JSON content, `response.json()` consumes the body and suppresses the parsing error before the same response is returned, leaving the client without the original response body or useful diagnostics.

**Context Used:** CLAUDE.md ([source](https://github.com/noveum/orbit/blob/main/CLAUDE.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code

@imshashank
imshashank merged commit 551b777 into main Aug 7, 2026
8 checks passed
@imshashank
imshashank deleted the fix-mcp-scopes branch August 7, 2026 18:52

This branch was successfully deployed

1 active deployment
Preview — 4555ef6a Deployed Aug 7, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant