Repository navigation
[AGE-2723] x-tfg-mcp-headers: pass MCP credentials and skip consent #1313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6d85393
c09705e
b6007af
cf1d6c5
a15ecff
e57439b
014c4bc
0550d0e
8ca71f6
76eb522
d16ae4a
d5efb54
0027e08
11b1046
550abe6
59979c3
64fd20f
5f8755e
408c61d
3aa59d9
f7199c7
a7c46dd
39ed31a
3206498
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@truefoundry/trueforge-core': patch | ||
| '@truefoundry/trueforge': patch | ||
| --- | ||
|
|
||
| `x-tfg-mcp-headers` entries with `Authorization` skip that MCP server's consent flow (forwarded directly in open-source mode, sent as `x-tfy-mcp-headers` in TrueFoundry mode). Nested virtual MCP entries are accepted only in TrueFoundry mode, where they are sent as `x-tfy-mcp-headers`. TrueFoundry mode no longer calls MCP authorize during turns. A 401 with `authorization_urls` pauses the turn with `mcp.auth_required`; any other 401 fails as an MCP error. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/ | |
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import type { CallToolRequest, CallToolResult } from '@modelcontextprotocol/sdk/types.js'; | ||
| import { context, propagation } from '@opentelemetry/api'; | ||
| import { McpConnectionError } from '../errors'; | ||
| import { z } from 'zod'; | ||
| import { McpAuthorizationRequiredError, McpConnectionError } from '../errors'; | ||
| import { elapsedSeconds, getOperationMetrics, type McpConnectOutcome } from '../observability/operationMetrics'; | ||
| import { mcpSsrfFetch } from '../util/outboundFetch'; | ||
| import { PromiseTimeoutError, withTimeout } from '../util/promiseUtils'; | ||
|
|
@@ -35,11 +36,12 @@ const TRANSPORT_PROBE_ORDER: RemoteMcpTransportType[] = ['streamable-http', 'sse | |
|
|
||
| export const DEFAULT_MAX_MCP_RESPONSE_BYTES = 50 * 1024 * 1024; | ||
|
|
||
| /** GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ | ||
| /** A successful GET SSE is long-lived and uncapped; every other body aborts at `maxBytes`. */ | ||
| export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): FetchLike { | ||
| return async (url, init) => { | ||
| const response = await fetchFn(url, init); | ||
| const isGetSse = | ||
| response.ok && | ||
| (init?.method ?? 'GET').toUpperCase() === 'GET' && | ||
| (response.headers.get('content-type') ?? '').toLowerCase().includes('text/event-stream'); | ||
| if (isGetSse || !response.body) { | ||
|
|
@@ -69,6 +71,29 @@ export function withMaxResponseBytes(fetchFn: FetchLike, maxBytes: number): Fetc | |
| }; | ||
| } | ||
|
|
||
| const AuthorizationRequiredBodySchema = z.object({ | ||
| authorization_urls: z.record(z.string(), z.url({ protocol: /^https?$/ })), | ||
| }); | ||
|
|
||
| /** A 401 whose body lists `authorization_urls` throws `McpAuthorizationRequiredError`; other responses pass through. */ | ||
| export function withAuthorizationRequired(fetchFn: FetchLike): FetchLike { | ||
| return async (url, init) => { | ||
| const response = await fetchFn(url, init); | ||
| if (response.status !== 401) { | ||
| return response; | ||
| } | ||
| const body: unknown = await response | ||
| .clone() | ||
| .json() | ||
| .catch(() => undefined); | ||
| const parsed = AuthorizationRequiredBodySchema.safeParse(body); | ||
| if (!parsed.success || Object.keys(parsed.data.authorization_urls).length === 0) { | ||
| return response; | ||
| } | ||
| throw new McpAuthorizationRequiredError('upstream requires authorization', parsed.data.authorization_urls); | ||
| }; | ||
| } | ||
|
|
||
| class McpClientWithTimeout extends Client { | ||
| constructor(private readonly requestTimeoutMs: number) { | ||
| super(CLIENT_INFO, { capabilities: {} }); | ||
|
|
@@ -128,6 +153,9 @@ function isAuthError(error: unknown): boolean { | |
| if (!(error instanceof Error)) { | ||
| return false; | ||
| } | ||
| if (error instanceof McpConnectionError) { | ||
| return error.statusCode === 401; | ||
| } | ||
|
Comment on lines
+156
to
+158
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since McpAuthorizationRequiredError extends McpConnectionError will this create problems? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's intentional. As a 401 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
| if ('code' in error && error.code === 401) { | ||
| return true; | ||
| } | ||
|
|
@@ -212,7 +240,9 @@ export async function connectRemoteMcp(params: { | |
| const customFetch = params.fetch; | ||
| const transportFetch: RemoteMcpFetch = | ||
| customFetch === undefined ? mcpSsrfFetch : (input, init) => customFetch(input, { ...init, redirect: 'manual' }); | ||
| const fetchFn = withMaxResponseBytes(transportFetch, params.maxResponseBytes ?? DEFAULT_MAX_MCP_RESPONSE_BYTES); | ||
| const fetchFn = withAuthorizationRequired( | ||
| withMaxResponseBytes(transportFetch, params.maxResponseBytes ?? DEFAULT_MAX_MCP_RESPONSE_BYTES), | ||
| ); | ||
| const candidates = params.knownTransportType | ||
| ? [params.knownTransportType, ...TRANSPORT_PROBE_ORDER.filter(t => t !== params.knownTransportType)] | ||
| : TRANSPORT_PROBE_ORDER; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we want to put regex on protocol?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
z.url()accepts any scheme, includingjavascript:,data:andfile:. Theseauthorization_urlscome 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:orfile:link. We only accept http and https.