Skip to content

Commit b415e60

Browse files
authored
fix(client/auth): allow opting out of prompt=consent for offline_access (modelcontextprotocol#2877)
1 parent 951fe56 commit b415e60

4 files changed

Lines changed: 93 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/client': patch
3+
---
4+
5+
Add `OAuthClientProvider.skipOfflineAccessConsentPrompt` (and the matching `startAuthorization()` option) so a client can omit the `prompt=consent` the SDK adds when the requested scope includes `offline_access`. Default behavior is unchanged. This unblocks authorization servers such as Microsoft Entra ID tenants that have user consent disabled and admin consent granted, where a forced consent prompt fails with `AADSTS90095` for non-admin users.

‎docs/clients/oauth.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ class MyOAuthProvider implements OAuthClientProvider {
9797

9898
The SDK calls the `save*` methods as the flow produces values and reads them back through `tokens()`, `clientInformation()`, `codeVerifier()`, and `discoveryState()`. On a later `connect()` it reads `tokens()` before anything else, so a provider backed by durable storage skips the browser round trip.
9999

100+
::: tip
101+
When the requested scope includes `offline_access`, the SDK adds `prompt=consent` to the authorization URL. Set `skipOfflineAccessConsentPrompt = true` on the provider to omit it when consent is already granted out of band, such as a Microsoft Entra ID tenant with admin consent and user consent disabled.
102+
:::
103+
100104
## Finish the flow from the callback
101105

102106
The authorization server redirects the end user to `redirectUrl` with `code` and `state` in the query. Compare `state`, hand the whole query to `finishAuth`, and reconnect.

‎packages/client/src/client/auth.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ export interface OAuthClientProvider {
258258
*/
259259
clientMetadataUrl?: string;
260260

261+
/**
262+
* When `true`, {@linkcode auth} does not add `prompt=consent` to the authorization
263+
* request when the requested scope includes `offline_access`.
264+
*
265+
* OIDC Core §11 allows omitting it when other conditions already permit offline
266+
* access, e.g. an administrator granted tenant-wide consent. Some authorization
267+
* servers (such as Microsoft Entra ID with user consent disabled) reject a forced
268+
* consent prompt from non-admin users even though consent is already in place.
269+
*
270+
* @default false
271+
*/
272+
skipOfflineAccessConsentPrompt?: boolean;
273+
261274
/**
262275
* Metadata about this OAuth client.
263276
*/
@@ -1455,7 +1468,8 @@ async function authInternal(
14551468
state,
14561469
redirectUrl: provider.redirectUrl,
14571470
scope: resolvedScope,
1458-
resource
1471+
resource,
1472+
skipOfflineAccessConsentPrompt: provider.skipOfflineAccessConsentPrompt
14591473
});
14601474

14611475
await provider.saveCodeVerifier(codeVerifier);
@@ -2098,14 +2112,20 @@ export async function startAuthorization(
20982112
redirectUrl,
20992113
scope,
21002114
state,
2101-
resource
2115+
resource,
2116+
skipOfflineAccessConsentPrompt
21022117
}: {
21032118
metadata?: AuthorizationServerMetadata;
21042119
clientInformation: OAuthClientInformationMixed;
21052120
redirectUrl: string | URL;
21062121
scope?: string;
21072122
state?: string;
21082123
resource?: string | URL;
2124+
/**
2125+
* When `true`, `prompt=consent` is not added for an `offline_access` scope.
2126+
* See {@linkcode OAuthClientProvider.skipOfflineAccessConsentPrompt}.
2127+
*/
2128+
skipOfflineAccessConsentPrompt?: boolean;
21092129
}
21102130
): Promise<{ authorizationUrl: URL; codeVerifier: string }> {
21112131
let authorizationUrl: URL;
@@ -2145,7 +2165,7 @@ export async function startAuthorization(
21452165
authorizationUrl.searchParams.set('scope', scope);
21462166
}
21472167

2148-
if (scope?.split(' ').includes('offline_access')) {
2168+
if (!skipOfflineAccessConsentPrompt && scope?.split(' ').includes('offline_access')) {
21492169
// if the request includes the OIDC-only "offline_access" scope,
21502170
// we need to set the prompt to "consent" to ensure the user is prompted to grant offline access
21512171
// https://openid.net/specs/openid-connect-core-1_0.html#OfflineAccess

‎packages/client/test/client/auth.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,18 @@ describe('OAuth Authorization', () => {
18461846
expect(authorizationUrl.searchParams.has('state')).toBe(false);
18471847
});
18481848

1849+
it("omits consent prompt parameter for 'offline_access' when skipOfflineAccessConsentPrompt is set", async () => {
1850+
const { authorizationUrl } = await startAuthorization('https://auth.example.com', {
1851+
clientInformation: validClientInfo,
1852+
redirectUrl: 'http://localhost:3000/callback',
1853+
scope: 'read write profile offline_access',
1854+
skipOfflineAccessConsentPrompt: true
1855+
});
1856+
1857+
expect(authorizationUrl.searchParams.get('scope')).toBe('read write profile offline_access');
1858+
expect(authorizationUrl.searchParams.has('prompt')).toBe(false);
1859+
});
1860+
18491861
// OpenID Connect requires that the user is prompted for consent if the scope includes 'offline_access'
18501862
it("includes consent prompt parameter if scope includes 'offline_access'", async () => {
18511863
const { authorizationUrl } = await startAuthorization('https://auth.example.com', {
@@ -5103,6 +5115,55 @@ describe('OAuth Authorization', () => {
51035115
expect(authorizationUrl.searchParams.get('scope')).toBe('mcp:read');
51045116
expect(authorizationUrl.searchParams.has('prompt')).toBe(false);
51055117
});
5118+
5119+
it('auth() omits prompt=consent for offline_access when provider.skipOfflineAccessConsentPrompt is true', async () => {
5120+
mockFetch.mockImplementation(url => {
5121+
const urlString = url.toString();
5122+
if (urlString.includes('/.well-known/oauth-protected-resource')) {
5123+
return Promise.resolve({ ok: false, status: 404 });
5124+
}
5125+
if (urlString.includes('/.well-known/oauth-authorization-server')) {
5126+
return Promise.resolve({
5127+
ok: true,
5128+
status: 200,
5129+
json: async () => ({
5130+
issuer: 'https://api.example.com',
5131+
authorization_endpoint: 'https://api.example.com/authorize',
5132+
token_endpoint: 'https://api.example.com/token',
5133+
response_types_supported: ['code'],
5134+
scopes_supported: ['mcp:read', 'offline_access']
5135+
})
5136+
});
5137+
}
5138+
return Promise.resolve({ ok: false, status: 404 });
5139+
});
5140+
const redirectToAuthorization = vi.fn();
5141+
const provider: OAuthClientProvider = {
5142+
get redirectUrl() {
5143+
return 'http://localhost:3000/callback';
5144+
},
5145+
get clientMetadata() {
5146+
return {
5147+
redirect_uris: ['http://localhost:3000/callback'],
5148+
scope: 'mcp:read',
5149+
grant_types: ['authorization_code', 'refresh_token']
5150+
};
5151+
},
5152+
skipOfflineAccessConsentPrompt: true,
5153+
clientInformation: () => ({ client_id: 'static' }),
5154+
tokens: () => undefined,
5155+
saveTokens: vi.fn(),
5156+
redirectToAuthorization,
5157+
saveCodeVerifier: vi.fn(),
5158+
codeVerifier: () => 'v'
5159+
};
5160+
5161+
const result = await auth(provider, { serverUrl: 'https://api.example.com/mcp' });
5162+
expect(result).toBe('REDIRECT');
5163+
const authorizationUrl = redirectToAuthorization.mock.calls[0]![0] as URL;
5164+
expect(authorizationUrl.searchParams.get('scope')).toBe('mcp:read offline_access');
5165+
expect(authorizationUrl.searchParams.has('prompt')).toBe(false);
5166+
});
51065167
});
51075168
});
51085169

0 commit comments

Comments
 (0)