Skip to content

[AGE-2723] Fall back to MCP auth flow when supplied credentials get a 401 (RemoteMCP onUnauthorized) - #1314

Closed
rutvij-menavlikar wants to merge 2 commits into
rutvij/age-2723_1from
rutvij/age-2723_2
Closed

rutvij-menavlikar wants to merge 2 commits into
rutvij/age-2723_1from
rutvij/age-2723_2

Conversation

@rutvij-menavlikar

@rutvij-menavlikar rutvij-menavlikar commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Second of two stacked PRs for AGE-2723. Stacked on #1313.

#1313 lets a caller's Authorization in x-tfg-mcp-headers replace an MCP server's consent flow. This PR covers the case where the upstream rejects that credential with a 401: TrueForge then falls back to the server's regular authorization flow instead of failing the tool call. The gateway doesn't do this itself; it forwards upstream 401s verbatim.

Changes

  • core: ResolveHeadersResult (headers variant) gains an optional onUnauthorized resolver. RemoteMCP consults it once when the upstream returns a 401, either at connect or mid-session. It first resets only the connection that carried the rejected credentials, then reconnects with the fallback headers or returns authRequired, which surfaces the consent URL. isAuthError is exported and recognizes McpConnectionError with status 401. Resolvers without onUnauthorized are unchanged.
  • TrueFoundry mode: the fallback is the existing authorize path, which returns a consent URL or the gateway-held token.
  • Open-source mode: the fallback is the registered auth, which is DCR (stored token or consent URL) or the configured headers.

How was this tested?

  • Core: two small tests. A 401 at connect returns the fallback's authRequired, and a 401 mid-session reconnects with the fallback headers.
  • TrueForge: the TrueFoundry test asserts onUnauthorized returns the consent URL from authorize.
  • Core and TrueForge typecheck, the core MCP tests, pnpm test:trueforge (934 passing), eslint, and prettier.

Checklist

  • I have read the contributing guidelines
  • pnpm typecheck / tests / eslint / pnpm format:check pass locally for the touched packages
  • Tests added/updated where it makes sense
  • No hand-edits to generated code
  • Docs / .env.example updated if configuration or behavior changed (n/a)

Note

Medium Risk
Changes authentication retry behavior for MCP tooling; mistakes could mis-route identity or skip consent, but behavior is scoped to 401 responses and one-shot fallback.

Overview
Remote MCP header resolvers can now attach an optional onUnauthorized callback. RemoteMCP invokes it once when connect or mid-session work gets a 401, resets the rejected connection, then retries with fallback headers or returns authRequired. isAuthError is exported and treats McpConnectionError with status 401 as auth failures.

TrueForge / OSS MCP stores wire this for caller Authorization in x-tfg-mcp-headers / x-tfy-mcp-headers: credentials are tried first, but a 401 triggers the normal consent/DCR or gateway authorize path instead of failing the tool call.

Reviewed by Cursor Bugbot for commit 820ffb3. Bugbot is set up for automated code reviews on this repo. Configure here.

@changeset-bot

changeset-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 820ffb3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@truefoundry/trueforge-core Patch
@truefoundry/trueforge Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@veria-ai

veria-ai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

PR overview

The PR adds an MCP authentication fallback in RemoteMCP when supplied credentials receive a 401 response, reconnecting with headers obtained through the fallback flow.

One open issue remains: after fallback authentication, subsequent operations reuse the cached connection without rechecking the OAuth resolver. For a DCR server, deleting a user's stored MCP authorization mid-turn does not stop subsequent tool calls using the cached bearer token, leaving a limited revocation-enforcement gap. No issues have been addressed yet.

Open issues (1)

Fixed/addressed: 0 · PR risk: 5/10

Reviews

@rutvij-menavlikar rutvij-menavlikar changed the title [AGE-2723] x-tfg-mcp-headers: Authorization skips MCP consent, forwarded (OSS) / mapped to x-tfy-mcp-headers (TFY) [AGE-2723] Fall back to MCP auth flow when supplied credentials get a 401 (RemoteMCP onUnauthorized) Nov 10, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4d5d106. Configure here.

…SS) / mapped to x-tfy-mcp-headers (TFY) (AGE-2723)

Co-authored-by: Cursor <cursoragent@cursor.com>
if ('authRequired' in fallback) {
return { authRequired: fallback.authRequired };
}
return this.connectAndRun(fallback.headers, operation, true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Medium: OAuth revocation is skipped after fallback

After reconnecting with fallback.headers, subsequent operations still resolve the original caller headers and reuse the fallback connection without invoking the registered OAuth resolver again. For a DCR server, deleting the user's stored MCP authorization mid-turn therefore still permits tool calls with the cached bearer token; retain and recheck the fallback resolver on subsequent operations so local revocation returns authRequired before dispatch.

…(AGE-2723)

Co-authored-by: Cursor <cursoragent@cursor.com>
@rutvij-menavlikar

Copy link
Copy Markdown
Contributor Author

Folded into #1313.

@rutvij-menavlikar
rutvij-menavlikar deleted the rutvij/age-2723_2 branch November 10, 2026 11:18
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