Repository navigation
Conversation
There was a problem hiding this comment.
The fix works and the code looks sound, with nothing blocking.
How I checked: I ran the issue's reproduction against the real Fabric IQ endpoint and Microsoft's live metadata, from source on both the base and this PR.
- On the base,
mcp auth fabriciqfails with the{tenantid}issuer mismatch. - With this PR, it reaches the Entra
organizationsauthorize URL. test/mcp-oauth.test.tspasses. The two new sign-in and refresh tests fail on the base code, as expected.
Code: the SDK's own login path reuses the discovery result your check has already accepted, so it doesn't run the strict check again. The saved-discovery and rediscovery refresh paths both use the new check. The IssuerChanged guard compares the stored template issuer with the discovered template issuer, so it stays consistent. The extra case only applies on the same origin, so it doesn't meaningfully relax trust.
Public API: unchanged.
Related: #50036
|
I reproduced this on Three regression tests fail on Patch (apply from a clean dev checkout)git apply - <<'PATCH'
diff --git a/packages/core/src/mcp/oauth.ts b/packages/core/src/mcp/oauth.ts
index f290701508..542b3db225 100644
--- a/packages/core/src/mcp/oauth.ts
+++ b/packages/core/src/mcp/oauth.ts
@@ -124,19 +124,36 @@ const configuredDiscovery = async (input: {
}
}
+// Entra's multi-tenant authorities (common, organizations, consumers) name the literal `{tenantid}` template as the
+// issuer, which the SDK's check rejects. Forgive the tenant segment and nothing else; URL percent-encodes the braces.
+const tenantTemplate = (expected: URL, received: URL) => {
+ const want = expected.pathname.split("/")
+ const got = received.pathname.split("/")
+ return (
+ expected.origin === received.origin &&
+ want.length === got.length &&
+ Boolean(want[1]) &&
+ got[1] === "%7Btenantid%7D" &&
+ want.every((segment, i) => i === 1 || segment === got[i])
+ )
+}
+
// RFC 8414 §3.3 compares the issuer with the URL its metadata was discovered at. The SDK forgives only the trailing slash
-// URL parsing adds to that URL, not one the authorization server adds to its issuer (JumpCloud), so that is forgiven here.
-const slashOnly = (error: unknown): error is IssuerMismatchError =>
+// URL parsing adds to that URL, not one the authorization server adds to its issuer (JumpCloud), nor Entra's template.
+const forgivableIssuer = (error: unknown): error is IssuerMismatchError =>
error instanceof IssuerMismatchError &&
error.kind === "metadata" &&
error.received !== undefined &&
error.expected !== undefined &&
- error.expected.replace(/\/$/, "") === error.received.replace(/\/$/, "")
+ (error.expected.replace(/\/$/, "") === error.received.replace(/\/$/, "") ||
+ (URL.canParse(error.expected) &&
+ URL.canParse(error.received) &&
+ tenantTemplate(new URL(error.expected), new URL(error.received))))
// Without validation any issuer passes, so the retry holds it to the one the failed check reported.
const discover = (serverUrl: URL, options: { readonly resourceMetadataUrl?: URL; readonly fetchFn: FetchLike }) =>
discoverOAuthServerInfo(serverUrl, options).catch(async (error: unknown) => {
- if (!slashOnly(error)) throw error
+ if (!forgivableIssuer(error)) throw error
const info = await discoverOAuthServerInfo(serverUrl, { ...options, skipIssuerMetadataValidation: true })
if (info.authorizationServerMetadata?.issuer !== error.received) throw error
return info
@@ -144,7 +161,7 @@ const discover = (serverUrl: URL, options: { readonly resourceMetadataUrl?: URL;
const discoverMetadata = (authorizationServer: string, fetchFn: FetchLike) =>
discoverAuthorizationServerMetadata(authorizationServer, { fetchFn }).catch(async (error: unknown) => {
- if (!slashOnly(error)) throw error
+ if (!forgivableIssuer(error)) throw error
const metadata = await discoverAuthorizationServerMetadata(authorizationServer, {
fetchFn,
skipIssuerValidation: true,
diff --git a/packages/core/test/mcp/oauth.test.ts b/packages/core/test/mcp/oauth.test.ts
index 7bf0ee3d91..8791955c26 100644
--- a/packages/core/test/mcp/oauth.test.ts
+++ b/packages/core/test/mcp/oauth.test.ts
@@ -369,6 +369,19 @@ const signIns: ReadonlyArray<{
expect(String(yield* Effect.flip(signIn(server.url, { client_id: "client" })))).toContain("Issuer mismatch")
}),
},
+ {
+ // Entra's multi-tenant authorities name the literal `{tenantid}` issuer template instead of the authority URL.
+ name: "accepts Entra's {tenantid} issuer template on a multi-tenant authority, and nothing else (#64854)",
+ server: { prm: "header", resource: "/mcp", as: "/organizations/v2.0", issuer: "/{tenantid}/v2.0" },
+ run: (server) =>
+ Effect.gen(function* () {
+ expect((yield* signIn(server.url, { client_id: "client" })).sent.pathname).toBe("/organizations/v2.0/authorize")
+ for (const issuer of ["/{tenant}/v2.0", "/{tenantid}/v1.0", "/{tenantid}"]) {
+ server.state.issuer = issuer
+ expect(String(yield* Effect.flip(signIn(server.url, { client_id: "client" })))).toContain("Issuer mismatch")
+ }
+ }),
+ },
{
name: "asks Google for offline access so it issues a refresh token (#59820)",
server: { metadata: { authorization_endpoint: "https://accounts.google.com/o/oauth2/v2/auth" } },
@@ -674,6 +687,35 @@ const refreshes: ReadonlyArray<{
})
},
},
+ {
+ // Entra's multi-tenant authorities name the literal `{tenantid}` issuer template (#64854).
+ name: "rediscovers through Entra's {tenantid} issuer template after a 401 (#64854)",
+ server: { prm: "header", resource: "/", as: "/organizations/v2.0", issuer: "/{tenantid}/v2.0" },
+ saved: () => ({ expires: Date.now() - 1_000 }),
+ run: async (server, saved) => {
+ await unauthorized(await saved.provider(), server.url, `Bearer resource_metadata="${server.origin}/prm"`)
+ expect((await saved.stored())?.access).toBe("a2")
+ expect((await saved.stored())?.metadata?.discovery).toEqual({
+ authorizationServer: `${server.origin}/organizations/v2.0`,
+ resource: `${server.origin}/`,
+ })
+ },
+ },
+ {
+ name: "refreshes through a saved discovery whose issuer is Entra's {tenantid} template (#64854)",
+ server: { as: "/organizations/v2.0", issuer: "/{tenantid}/v2.0" },
+ saved: (server) => ({
+ expires: Date.now() + 10_000,
+ metadata: {
+ discovery: { authorizationServer: `${server.origin}/organizations/v2.0`, resource: server.url },
+ },
+ }),
+ run: async (server, saved) => {
+ expect(await (await saved.provider()).token()).toBe("a2")
+ expect(server.state.tokens.map((body) => body.get("grant_type"))).toEqual(["refresh_token"])
+ expect(server.state.paths).not.toContain("/mcp")
+ },
+ },
{
name: "refreshes through configured authorization server metadata",
config: (server) =>
PATCH |
Issue for this PR
Closes #64854
Type of change
What does this PR do?
MCP servers behind Microsoft Entra's multi-tenant authorities (
common,organizations,consumers) couldn't sign in. Entra's metadata forhttps://login.microsoftonline.com/organizations/v2.0names the issuer as the literal templatehttps://login.microsoftonline.com/{tenantid}/v2.0, so the MCP SDK's RFC 8414 §3.3 check rejects it. Strictly that's Entra breaking the spec, but every Entra-backed MCP server (Power BI, Fabric) hits it.The SDK only offers
skipIssuerValidation, which turns the check off completely (upstream: modelcontextprotocol/typescript-sdk#3207). SoMcpOAuthnow skips the SDK's check and runs its own: the same rule as the SDK (exact match or one trailing slash), plus one extra case. The issuer can have the first path segment replaced by{tenantid}, on the same origin, with the rest of the path identical. Anything else still throws the SDK'sIssuerMismatchError. This applies to login discovery and to both refresh paths (saved discovery, and rediscovery after a 401).The callback
isscheck is unaffected: Entra doesn't advertise or sendiss. Tokens get stamped with the template issuer consistently, so the existing "issuer changed" guard on refresh still compares like with like.Fabric IQ (
fabriciq.svc.cloud.microsoft) gets past this now, but will still fail at Microsoft withAADSTS9010010. Its resource metadata names its full MCP URL as the resource, which Entra rejects (microsoft/skills-for-fabric#129). That's on the server side and out of scope here.No flag: bug fix.
How did you verify your code works?
packages/core/test/mcp-oauth.test.ts: sign in through an authority serving the{tenantid}issuer, refresh through it (saved discovery and after a 401), and still reject other origins, other tenant segments and other path differences. Without the fix, the sign-in and refresh tests fail.McpOAuth.authorizeagainst the livehttps://api.fabric.microsoft.com/v1/mcp/powerbiandhttps://fabriciq.svc.cloud.microsoft/v1/mcp/fabriciq: both now return alogin.microsoftonline.com/organizations/oauth2/v2.0/authorizeURL instead of the issuer mismatch. I didn't finish a full browser sign-in, since that needs an Entra app registration.bun run checkpasses.Screenshots / recordings
N/A
Checklist
Added by the opencode bot (AI-generated).
Related: #50036