Skip to content

feat(mcp): the consent page where a person connects an app - #2272

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-oauth-consent
Nov 10, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-oauth-consent

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

PR 4 of the MCP over OAuth plan (#2141). Based on main: #2260, #2261 and #2262 are merged, and upstream/main is merged into this branch, so the diff is this PR alone. It also merges cleanly into current main (f947d77).

It adds the authorization endpoint and the consent page: screens 1 to 6 of the approved mockup. It is still behind MCP_OAUTH_ENABLED: while the flag is off, the endpoint and the page answer 404, and the sign-in page is unchanged.

GET /api/oauth/authorize (web/src/app/api/oauth/authorize/route.ts, web/src/lib/mcp-oauth/authorize.ts)

  • Checked first, against the client: client_id, then redirect_uri, which must be one the client registered (exactly, or loopback on any port). If the client has only one, it may be left out.
  • If either fails, the request ends on Ansvisor's own page (/oauth/consent?error=unknown_client|invalid_redirect_uri) and is never redirected to the address it names (RFC 6749 §4.1.2.1).
  • After that, errors go back to the app with state and iss (RFC 9207):
    • unsupported_response_type (only code);
    • invalid_request (PKCE with code_challenge_method=S256 is required, plain is refused, and state may be at most 1,000 characters);
    • invalid_target (a resource other than ours; none means ours).
  • Scopes: no mcp:* scope asked for means both. Unknown ones such as offline_access are ignored.
  • A good request is kept 10 minutes in oauth_authorization_requests. The person goes on to /oauth/consent?request=<id>: the id is all the address holds. Expired requests are swept.
  • Limit: 60 requests per address in ten minutes, then the rate_limited page.

The consent page (web/src/app/[locale]/(marketing)/oauth/consent/page.tsx, web/src/lib/mcp-oauth/consent.ts, web/src/components/oauth/consent-card.tsx)

  • Signed out: the person goes to /sign-in?next=… and comes back. Google sign-in comes back too, through the existing next cookie. Screen 1: the sign-in page shows "Sign in to connect Claude to Ansvisor. Next, you'll choose what it can do." It says "an app" for one Ansvisor hasn't checked.
  • The request belongs to the first signed-in person who opens it. That is claimed atomically. Another account gets "This request was opened with another Ansvisor account", so a passed-on link or a forced-open page can't be approved by someone else.
  • Screen 2, a recognised app:
    • It is named by where its code goes: claude.ai → Claude, chatgpt.com or platform.openai.com → ChatGPT. The name it registered is never used for this: anyone can register "Claude", but only Claude receives codes at claude.ai.
    • It shows a "Recognised app" pill and "You'll go back to claude.ai".
    • Then a table: the signed-in e-mail and role (with Switch account, which signs out and comes back), the workspace and plan, and the brands (All brands with the count, or the brands shared with a Selected-brands person).
    • Then "Read your Ansvisor data" (always) and "Make changes you ask for" (ticked by default; unticked gives a read-only connection), the fine print, and Cancel / Allow.
  • Screen 3, an app Ansvisor hasn't checked:
    • Its own name in quotes, a "Not checked by Ansvisor" pill, and "Allow it only if you started this connection".
    • When the app runs on this computer (loopback): "… (this computer)" and the "any program there could use this address" warning, as the MCP spec asks for loopback-only redirects.
    • No logo is ever fetched or shown.
    • On a paid plan below Growth, the changes box says "On Starter, apps can read. Changes need Growth: Ansvisor refuses them and says why."
  • Screens 4 and 5, the plan blocks (owner, 2026-11-09):
    • The free trial shows "Connecting apps needs a paid plan", with the API-key note.
    • An ended plan shows "Your workspace's plan has ended".
    • An admin gets Choose a plan (Settings › Billing). A member gets "Ask an admin of {workspace} to choose a plan in Settings › Billing".
    • Back to {app} tells the app access_denied.
    • Self-hosted never shows these. An Enterprise trial counts as paid, as in plan-facts.
    • Someone without a workspace yet gets "Finish setting up your workspace first".
  • Screen 6, a request that can't be used: unknown client, unregistered redirect URI, expired, another account's, malformed, or too many requests. It is explained, with its code and Open Ansvisor, and sends nowhere.

Allow and Cancel (web/src/lib/mcp-oauth/consent-actions.ts, server actions)

  • Next.js checks a server action's Origin against the host, and the page can't be framed, so another site can't approve for someone.
  • Each action reads again that the request is still pending and is this person's. Allow also reads the plan again: if it no longer allows the connection, it shows the page again with no code.
  • Allow:
    • writes a one-time code (ans_oac_, 5 minutes, stored as sha256) for the request's client, redirect URI, PKCE challenge and resource, with the scopes left ticked (reading always), for the organization the person is in now;
    • uses the request up and logs consent_granted;
    • sends the app to redirect_uri?code=…&state=…&iss=….
    • 20 decisions per person in ten minutes.
  • Cancel and "Back to …": access_denied with state and iss. The request is used up, and the log records consent_denied, blocked_trial or blocked_ended.
  • These actions change no workspace data, so they don't take the read-only check (assertWritable); the plan rule here is the sign-in one. They live in lib/mcp-oauth, not lib/actions, so subscription-guard-coverage doesn't list them.

Other changes

  • web/next.config.ts: /oauth/* (and /en/oauth/*) get X-Frame-Options: DENY, frame-ancestors 'none', Referrer-Policy: no-referrer (the request id never leaks to the app) and Cache-Control: no-store.
  • AuthShell gains an optional aside (here, "Signed in as …" in the header) and pitch={false}, which centres the card. cta is now optional. The sign-in and sign-up pages are unchanged.
  • web/src/lib/mcp-oauth/known-apps.ts: the known apps' list, also used by Connected apps (PR 6, built on feat(mcp): accept OAuth access tokens at /api/mcp, behind MCP_OAUTH_ENABLED (00510) #2261). The file is identical in both branches, so they merge cleanly.
  • web/messages/en.json: the oauthConsent namespace, and auth.oauthSignInLine / oauthSignInLineUnchecked.
  • No migration (00510 has the tables) and no dependency change.

Tests

  • authorize/route.test.ts (14 tests): a good request, the client's only redirect URI, loopback on any port, the cases that end on Ansvisor's page without redirecting, the cases sent back with state and iss, scopes, the limit, and 404 while off.
  • consent.test.ts (18 tests): the view for a known and an unchecked app; Selected brands; trial, ended, Enterprise trial, self-hosted and no workspace; sign-in, bad, unknown and expired requests; the first-person claim; the sign-in line's app. Allow → token endpoint → checkAccessToken, end to end. Read-only when unticked; no code on a plan that stopped; another person, signed out, spent request and flag off; Cancel with state/iss; the blocked log; Switch account.
  • consent-card.test.tsx (12 tests): every card and error, as rendered.

No Help or docs change: nothing is switched on. Help and docs come with PR 7.

Related issue

Part of #2141

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd web && npx vitest run src/app/api/oauth src/lib/mcp-oauth src/components/oauth.
  2. Run a dev server with MCP_OAUTH_ENABLED=true, NEXT_PUBLIC_APP_URL=http://localhost:3000, and 00510 applied to the dev database.
  3. Register a client:
    curl -s -X POST localhost:3000/api/oauth/register -H 'content-type: application/json' \
      -d '{"client_name":"Test","redirect_uris":["http://127.0.0.1:9999/cb"]}'
  4. Open ``'http://localhost:3000/api/oauth/authorize?response_type=code&client_id=&lt;id&gt;&redirect_uri=http://127.0.0.1:9999/cb&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256&state=x':''
    • you sign in, then see screen 3 (not checked, this computer);
    • Allow goes to http://127.0.0.1:9999/cb?code=ans_oac_…&state=x&iss=…;
    • redeem it at /api/oauth/token with code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk.
  5. Try redirect_uri=https://evil.example/cb: you land on "This connection request can't be used", and nothing goes to evil.example.
  6. In a trial workspace, the page shows "Connecting apps needs a paid plan" instead of Allow.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change) — see help/README.md: nothing is switched on; Help comes with PR 7

Checks run:

  • yarn build passes (with CI's placeholder env).
  • Tests, rerun after merging upstream/main: src/components/oauth, src/components/auth, auth-next, src/lib/mcp-oauth, src/app/api/oauth, src/app/api/mcp, src/lib/mcp, both mcp-auth tests, embed-hints, subscription-guard-coverage and brand-access-functions: 73 files, 651 tests passed.

Generated by Claude Code

PR 4 of MCP over OAuth (ansvisor#2141), stacked on feature/mcp-oauth-endpoints
(ansvisor#2262). Still behind MCP_OAUTH_ENABLED: the authorize endpoint and the
page answer 404 while it is off.

- /api/oauth/authorize checks the request: an unknown client or a
  redirect URI it never registered ends on Ansvisor's own page and is
  never redirected to; after that, errors go back to the app with state
  and iss (response_type code, PKCE S256, our resource). A good request
  is kept 10 minutes and the person goes on to /oauth/consent with its
  id alone. 60 requests an address in ten minutes.
- /oauth/consent (the approved mockup's screens 1 to 6): sign in first,
  with a line naming the app on the sign-in page; then the app (a known
  one named by where its code goes, claude.ai or chatgpt.com; any other
  by its own name, marked not checked, with a warning when it runs on
  this computer), the person, workspace, plan and brands, and Read plus
  "Make changes you ask for". The free trial and an ended plan show why
  instead of Allow (an admin gets Choose a plan); no workspace yet says
  so; a request that can't be used is explained and goes nowhere.
- Allow and Cancel are server actions: the request must still be pending
  and the person's who first opened it, and Allow reads the plan again.
  Allow writes a one-time code (5 minutes) for the client, redirect URI,
  PKCE challenge and resource, with the scopes left ticked, and sends
  the app code, state and iss; Cancel sends access_denied. Both use the
  request up and are logged. Switch account signs out and comes back.
- The page is never framed (X-Frame-Options, frame-ancestors), cached or
  given as a referrer. AuthShell takes an aside and can drop its pitch.
- The known apps' list moves to its own module, shared with Settings.

Part of ansvisor#2141.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

@gkhngyk
gkhngyk merged commit 0eedb16 into ansvisor:main Nov 10, 2026
13 checks passed
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.

3 participants