Skip to content

Commit de24644

Browse files
committed
test(client): pin pathless PRM resource end to end, document selectResourceURL normalization (modelcontextprotocol#1968)
- auth.test.ts: drive auth() with PRM `resource: https://example.com` and assert the authorization redirect and the authorization-code token exchange both carry it without a trailing slash (fails on main with 'https://example.com/') - auth.ts: JSDoc on selectResourceURL explaining that it returns a parsed URL and that auth() sends the metadata string verbatim; comment at the decision site in auth() - changeset: describe the trailing-slash normalization, the Entra rejection, the widened `resource` inputs, and that selectResourceURL's signature is unchanged
1 parent dbf755f commit de24644

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎.changeset/plenty-plums-sip.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@modelcontextprotocol/client': patch
33
---
44

5-
Preserve exact OAuth resource indicators from protected resource metadata.
5+
Preserve the exact OAuth resource indicator from protected resource metadata when building authorization and token requests. Previously a pathless `resource` such as `https://example.com` was normalized to `https://example.com/` via `URL.href`, which breaks authorization servers that require the `resource` parameter to match the published value exactly (Microsoft Entra ID rejects it with `AADSTS9010010`). The exported OAuth helpers (`startAuthorization`, `exchangeAuthorization`, `refreshAuthorization`, `fetchToken`, `executeTokenRequest`) now also accept a `string` for `resource`; `selectResourceURL` still returns a `URL`, and a provider's `validateResourceURL` result is used unchanged. Fixes #1968.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,10 @@ async function authInternal(
12431243
await provider.saveDiscoveryState?.(freshDiscoveryState);
12441244
}
12451245

1246+
// Send the metadata's resource indicator verbatim: `selectResourceURL` returns a parsed
1247+
// `URL`, and `URL.href` appends "/" to a pathless indicator such as `https://example.com`,
1248+
// which exact-match authorization servers reject (#1968). A URL returned by the
1249+
// provider's own `validateResourceURL` is used as returned.
12461250
const selectedResource = await selectResourceURL(serverUrl, provider, resourceMetadata);
12471251
const resource: string | URL | undefined =
12481252
selectedResource && resourceMetadata && !provider.validateResourceURL ? resourceMetadata.resource : selectedResource;
@@ -1462,6 +1466,17 @@ export function isHttpsUrl(value?: string): boolean {
14621466
}
14631467
}
14641468

1469+
/**
1470+
* Selects the RFC 8707 resource indicator for an MCP server: the provider's
1471+
* {@linkcode OAuthClientProvider.validateResourceURL | validateResourceURL} result when
1472+
* implemented, otherwise the protected resource metadata's `resource` (checked against the
1473+
* server URL with `checkResourceAllowed`), or `undefined` when there is no metadata.
1474+
*
1475+
* The result is a parsed `URL`, so a pathless indicator such as `https://example.com` has
1476+
* the `href` `https://example.com/`. {@linkcode auth} therefore sends the metadata string
1477+
* verbatim instead of this URL's `href` (#1968); callers that emit the `resource`
1478+
* parameter themselves should do the same.
1479+
*/
14651480
export async function selectResourceURL(
14661481
serverUrl: string | URL,
14671482
provider: OAuthClientProvider,

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,75 @@ describe('OAuth Authorization', () => {
33673367
expect(authUrl.searchParams.get('resource')).toBe('https://api.example.com/');
33683368
});
33693369

3370+
it('sends a pathless PRM resource verbatim on the authorization and token requests (#1968)', async () => {
3371+
// RFC 9728 publishes the resource identifier and RFC 8707 requires it to be
3372+
// sent unchanged. `new URL('https://example.com').href` is 'https://example.com/',
3373+
// and authorization servers that match the indicator exactly (Microsoft Entra
3374+
// ID: AADSTS9010010) reject the extra slash.
3375+
mockFetch.mockImplementation(url => {
3376+
const urlString = url.toString();
3377+
3378+
if (urlString.includes('/.well-known/oauth-protected-resource')) {
3379+
return Promise.resolve({
3380+
ok: true,
3381+
status: 200,
3382+
json: async () => ({
3383+
resource: 'https://example.com',
3384+
authorization_servers: ['https://auth.example.com'],
3385+
scopes_supported: ['https://example.com/mcp:tools']
3386+
})
3387+
});
3388+
} else if (urlString.includes('/.well-known/oauth-authorization-server')) {
3389+
return Promise.resolve({
3390+
ok: true,
3391+
status: 200,
3392+
json: async () => ({
3393+
issuer: 'https://auth.example.com',
3394+
authorization_endpoint: 'https://auth.example.com/authorize',
3395+
token_endpoint: 'https://auth.example.com/token',
3396+
response_types_supported: ['code'],
3397+
code_challenge_methods_supported: ['S256']
3398+
})
3399+
});
3400+
} else if (urlString.includes('/token')) {
3401+
return Promise.resolve({
3402+
ok: true,
3403+
status: 200,
3404+
json: async () => ({ access_token: 'access123', token_type: 'bearer', expires_in: 3600 })
3405+
});
3406+
}
3407+
3408+
return Promise.resolve({ ok: false, status: 404 });
3409+
});
3410+
3411+
(mockProvider.clientInformation as Mock).mockResolvedValue({
3412+
client_id: 'test-client',
3413+
client_secret: 'test-secret'
3414+
});
3415+
(mockProvider.tokens as Mock).mockResolvedValue(undefined);
3416+
(mockProvider.saveCodeVerifier as Mock).mockResolvedValue(undefined);
3417+
(mockProvider.redirectToAuthorization as Mock).mockResolvedValue(undefined);
3418+
(mockProvider.codeVerifier as Mock).mockResolvedValue('verifier123');
3419+
(mockProvider.saveTokens as Mock).mockResolvedValue(undefined);
3420+
3421+
// Authorization request: the redirect carries the metadata value byte for byte.
3422+
const redirectResult = await auth(mockProvider, { serverUrl: 'https://example.com/mcp' });
3423+
expect(redirectResult).toBe('REDIRECT');
3424+
const authUrl: URL = (mockProvider.redirectToAuthorization as Mock).mock.calls[0]![0];
3425+
expect(authUrl.searchParams.get('resource')).toBe('https://example.com');
3426+
3427+
// Token request: the authorization-code exchange sends the same value.
3428+
const exchangeResult = await auth(mockProvider, {
3429+
serverUrl: 'https://example.com/mcp',
3430+
authorizationCode: 'code123'
3431+
});
3432+
expect(exchangeResult).toBe('AUTHORIZED');
3433+
const tokenCall = mockFetch.mock.calls.find(call => call[0].toString().includes('/token'));
3434+
expect(tokenCall).toBeDefined();
3435+
const body = tokenCall![1].body as URLSearchParams;
3436+
expect(body.get('resource')).toBe('https://example.com');
3437+
});
3438+
33703439
it('excludes resource parameter when Protected Resource Metadata is not present', async () => {
33713440
// Mock metadata discovery where protected resource metadata is not available (404)
33723441
// but authorization server metadata is available

0 commit comments

Comments
 (0)