Repository navigation
[AGE-2723] x-tfg-mcp-headers: pass MCP credentials and skip consent - #1313
rutvij-menavlikar wants to merge 24 commits into
Conversation
🦋 Changeset detectedLatest commit: 3206498 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
5fe41df to
719b56b
Compare
9c5e000 to
4be02c1
Compare
f38f92b to
98a4163
Compare
…h 401 fallback (AGE-2723) Co-authored-by: Cursor <cursoragent@cursor.com>
98a4163 to
6d85393
Compare
# Conflicts: # packages/trueforge/src/apis/turns.ts
…authorize on TrueFoundry turns (AGE-2723)
…source mode (AGE-2723)
…723) Co-authored-by: Cursor <cursoragent@cursor.com>
…ndry details out of general tests (AGE-2723) Co-authored-by: Rutvij Menavlikar <rutvij-menavlikar@users.noreply.github.com>
…-2723) Co-authored-by: Rutvij Menavlikar <rutvij-menavlikar@users.noreply.github.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 408c61d. Configure here.
…ry/ (AGE-2723)" This reverts commit 408c61d. Co-authored-by: Rutvij Menavlikar <rutvij-menavlikar@users.noreply.github.com>
… TrueFoundry details out of general tests (AGE-2723)" This reverts commit 5f8755e. Co-authored-by: Rutvij Menavlikar <rutvij-menavlikar@users.noreply.github.com>
…undry tests out (AGE-2723) Co-authored-by: Rutvij Menavlikar <rutvij-menavlikar@users.noreply.github.com>
| if (input.perServerHeaders !== undefined) { | ||
| return new McpServerWithAuthStore<TTransaction>({ | ||
| store: options.persistenceStore, | ||
| tokenStore: options.tokenStore, | ||
| clientName: configuration.MCP_DCR_OAUTH_CLIENT_NAME, | ||
| perServerHeaders: input.perServerHeaders, | ||
| }); | ||
| } |
There was a problem hiding this comment.
why don't we pass perServerHeaders: input.perServerHeaders, to line 118
const withAuthMcpPersistenceStore = new McpServerWithAuthStore<TTransaction>({
itself?
There was a problem hiding this comment.
withAuthMcpPersistenceStore is built once in buildResolveMcpServerStore and shared by every request, but x-tfg-mcp-headers is per request. Passing it there would leak one caller's headers into other requests. So a request carrying the header gets its own cheap instance (the constructor does no I/O), and requests without it reuse the shared one.
There was a problem hiding this comment.
Simpler version: that store is created once and shared by all requests, but x-tfg-mcp-headers belongs to one request. Passing it there would send one caller's headers to other callers. So we create a new (cheap) store only when a request has the header.
| if (error instanceof McpConnectionError) { | ||
| return error.statusCode === 401; | ||
| } |
There was a problem hiding this comment.
since McpAuthorizationRequiredError extends McpConnectionError will this create problems?
There was a problem hiding this comment.
It's intentional. As a 401 McpConnectionError it stops the connect probe without trying other transports, keeps its subclass through toConnectError, is counted as an auth outcome in metrics, and looks like an ordinary 401 to code that doesn't know the subclass. The one place that must tell it apart is the onUnauthorized fallback in RemoteMCP.executeWithAuthFallback. That code checks authRequiredFrom(error) before falling back, so a 401 carrying consent URLs becomes authRequired instead of retrying with fallback credentials.
There was a problem hiding this comment.
Simpler version: no, it's on purpose. It is still a 401, so everything that handles 401s keeps working (we stop trying other transports, metrics count it as auth, callers see a 401). Only RemoteMCP checks for the subclass, to turn it into mcp.auth_required with the consent URL.
| } | ||
|
|
||
| const AuthorizationRequiredBodySchema = z.object({ | ||
| authorization_urls: z.record(z.string(), z.url({ protocol: /^https?$/ })), |
There was a problem hiding this comment.
why do we want to put regex on protocol?
There was a problem hiding this comment.
z.url() accepts any scheme, including javascript:, data: and file:. These authorization_urls come from the upstream and are shown to the user as the consent link (auth_url). Restricting to http(s) stops a bad or compromised upstream from putting a script or local-file URL into that link. A body with any other scheme is treated as a bare 401.
There was a problem hiding this comment.
Simpler version: this URL comes from the server and is shown to the user as a link. Without the check, a bad server could send a javascript: or file: link. We only accept http and https.
…and move caller-header merging out of db/ - Open-source x-tfg-mcp-headers is a flat map per server; nested virtual MCP entries parse only in TrueFoundry mode (truefoundry/virtualMcpHeaders.ts). - McpServerWithAuthStore supplies its registered and configured headers; runtime/perServerMcpHeaders.ts withCallerMcpHeaders merges caller headers over them. - Rename override helpers: overrideHeaders -> mergeCallerHeaders, resolveWithOverride -> resolveWithCallerHeaders. Co-authored-by: Rutvij Menavlikar <rutvij-menavlikar@users.noreply.github.com>
…ent URLs or fails (AGE-2723) Co-authored-by: Rutvij Menavlikar <rutvij-menavlikar@users.noreply.github.com>

Summary
Implements AGE-2723: x-tfg-mcp-headers support to pass tokens and skip auth on MCP servers.
A server whose entry has a top-level
Authorization(any casing) skips its consent check (TrueFoundryauthorizeor open-source DCR) and uses the caller's credential instead. If the server rejects that credential with a 401, TrueForge falls back to the server's regular auth flow. This mirrors the TrueFoundry gateway'sx-tfy-mcp-headershandling, where anauthorizationheader skips token resolution.Changes
Authorization, DCR consent is skipped; without it, the entry is layered on top of the server's normal auth. Standalone (sqlite) mode now uses the same resolver.Authorization,/authorizeis skipped and the entry is sent as a flatx-tfy-mcp-headerson that server's gateway proxy connection (the gateway rejects a nested map there). The gateway Bearer stays the caller's. Entries withoutAuthorizationkeep today's behavior.ResolveHeadersResultgains an optionalonUnauthorized. On a 401 at connect or mid-session,RemoteMCPclears the rejected connection/session and retries once with the fallback headers, or returnsauthRequiredso the turn pauses with the consent URL. Fallback isauthorizein TrueFoundry mode and DCR or configured headers in open-source mode; the caller's non-Authorizationheaders are kept.perServerMcpHeadersmoved fromtruefoundry/tomcp/since both modes use it.x-tfg-mcp) servers and virtual MCPs are unchanged.How was this tested?
authorize, open-source forwarding and skipped DCR) and two coreRemoteMCPtests (401 at connect, 401 mid-session).tests/core/mcp, trueforgetests/unit/mcpandtests/unit/truefoundry, eslint and prettier on changed files.Checklist
pnpm typecheck/ tests / eslint /pnpm format:checkpass locally for the touched packages.env.exampleupdated if configuration or behavior changed (no config changes)Note
High Risk
Changes authentication and identity forwarding for every MCP connection in both OSS and TrueFoundry modes; incorrect header merge or consent bypass could expose wrong credentials or skip required consent.
Reviewed by Cursor Bugbot for commit 3206498. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary
Implements AGE-2723: x-tfg-mcp-headers support to pass tokens and skip auth on MCP servers.
A server whose entry has a top-level
Authorization(any casing) skips its consent check and uses the caller's credential instead. This mirrors the TrueFoundry gateway'sx-tfy-mcp-headershandling, where anauthorizationheader skips token resolution. On a 401, a body with consent URLs starts the consent flow; any other 401 fails as an MCP error, whether or not a stored token exists.Changes
Authorization, DCR consent is skipped; without it, the entry is layered on top of the server's normal auth. Nested entries are rejected with 400. Standalone (sqlite) mode now uses the same resolver./authorize; connections use the gateway Bearer plus turn metadata. An entry withAuthorization, or a virtual MCP's nested entry, is sent asx-tfy-mcp-headerson that server's gateway proxy connection. Other entries keep today's merge.authorizestays for the explicit Connect flow.authorization_urlsreturnsauthRequired(turn pauses withmcp.auth_required) and drops the rejected connection; a bare 401 throwsMcpConnectionError.x-tfg-mcp-headersnow rejectsmcp-session-id/mcp-protocol-version, which TrueForge manages.perServerMcpHeadersmoved fromtruefoundry/toruntime/since both modes use it; the TrueFoundry-only nested schema lives intruefoundry/virtualMcpHeaders.ts.x-tfg-mcp) servers are unchanged.How was this tested?
x-tfy-mcp-headersmapping, nested pass-through, noauthorizecall) and coreRemoteMCPtests (401 with consent URLs, reconnect after consent, bare 401).tests/core, trueforgetests/unit, eslint and prettier on changed files.Checklist
pnpm typecheck/ tests / eslint /pnpm format:checkpass locally for the touched packages.env.exampleupdated if configuration or behavior changed (no config changes)