Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/fix-oauth-resource-trailing-slash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@modelcontextprotocol/sdk': patch
---

Preserve the OAuth protected-resource URI without adding a trailing slash.
Previously `selectResourceURL` returned `new URL(metadata.resource).href` which
appended `/` to bare-domain URIs (e.g. `https://example.com` became
`https://example.com/`), breaking OAuth interop with Microsoft Entra ID.
Resolves #1968.
26 changes: 15 additions & 11 deletions src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ async function authInternal(
});
}

const resource: URL | undefined = await selectResourceURL(serverUrl, provider, resourceMetadata);
const resource: URL | string | undefined = await selectResourceURL(serverUrl, provider, resourceMetadata);

// Apply scope selection strategy (SEP-835):
// 1. WWW-Authenticate scope (passed via `scope` param)
Expand Down Expand Up @@ -633,7 +633,7 @@ export async function selectResourceURL(
serverUrl: string | URL,
provider: OAuthClientProvider,
resourceMetadata?: OAuthProtectedResourceMetadata
): Promise<URL | undefined> {
): Promise<URL | string | undefined> {
const defaultResource = resourceUrlFromServerUrl(serverUrl);

// If provider has custom validation, delegate to it
Expand All @@ -650,8 +650,12 @@ export async function selectResourceURL(
if (!checkResourceAllowed({ requestedResource: defaultResource, configuredResource: resourceMetadata.resource })) {
throw new Error(`Protected resource ${resourceMetadata.resource} does not match expected ${defaultResource} (or origin)`);
}
// Prefer the resource from metadata since it's what the server is telling us to request
return new URL(resourceMetadata.resource);
// Prefer the resource from metadata since it's what the server is telling us to request.
// Return the original string verbatim so we don't re-serialize through `URL.href`, which
// appends a trailing slash to bare-origin URIs (e.g. `https://example.com` becomes
// `https://example.com/`) and breaks providers that require an exact match against the
// configured resource indicator (RFC 8707), such as Microsoft Entra ID.
return resourceMetadata.resource;
}

/**
Expand Down Expand Up @@ -1126,7 +1130,7 @@ export async function startAuthorization(
redirectUrl: string | URL;
scope?: string;
state?: string;
resource?: URL;
resource?: URL | string;
}
): Promise<{ authorizationUrl: URL; codeVerifier: string }> {
let authorizationUrl: URL;
Expand Down Expand Up @@ -1174,7 +1178,7 @@ export async function startAuthorization(
}

if (resource) {
authorizationUrl.searchParams.set('resource', resource.href);
authorizationUrl.searchParams.set('resource', String(resource));
}

return { authorizationUrl, codeVerifier };
Expand Down Expand Up @@ -1222,7 +1226,7 @@ async function executeTokenRequest(
tokenRequestParams: URLSearchParams;
clientInformation?: OAuthClientInformationMixed;
addClientAuthentication?: OAuthClientProvider['addClientAuthentication'];
resource?: URL;
resource?: URL | string;
fetchFn?: FetchLike;
}
): Promise<OAuthTokens> {
Expand All @@ -1234,7 +1238,7 @@ async function executeTokenRequest(
});

if (resource) {
tokenRequestParams.set('resource', resource.href);
tokenRequestParams.set('resource', String(resource));
}

if (addClientAuthentication) {
Expand Down Expand Up @@ -1287,7 +1291,7 @@ export async function exchangeAuthorization(
authorizationCode: string;
codeVerifier: string;
redirectUri: string | URL;
resource?: URL;
resource?: URL | string;
addClientAuthentication?: OAuthClientProvider['addClientAuthentication'];
fetchFn?: FetchLike;
}
Expand Down Expand Up @@ -1329,7 +1333,7 @@ export async function refreshAuthorization(
metadata?: AuthorizationServerMetadata;
clientInformation: OAuthClientInformationMixed;
refreshToken: string;
resource?: URL;
resource?: URL | string;
addClientAuthentication?: OAuthClientProvider['addClientAuthentication'];
fetchFn?: FetchLike;
}
Expand Down Expand Up @@ -1388,7 +1392,7 @@ export async function fetchToken(
fetchFn
}: {
metadata?: AuthorizationServerMetadata;
resource?: URL;
resource?: URL | string;
/** Authorization code for the default authorization_code grant flow */
authorizationCode?: string;
fetchFn?: FetchLike;
Expand Down
59 changes: 57 additions & 2 deletions test/client/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1153,11 +1153,12 @@ describe('OAuth Authorization', () => {
);
expect(discoveryCalls).toHaveLength(0);

// Verify the token request includes the resource parameter from cached metadata
// Verify the token request includes the resource parameter from cached metadata,
// preserved verbatim (no trailing slash added — see #1968).
const tokenCall = mockFetch.mock.calls.find(call => call[0].toString().includes('/token'));
expect(tokenCall).toBeDefined();
const body = tokenCall![1].body as URLSearchParams;
expect(body.get('resource')).toBe('https://resource.example.com/');
expect(body.get('resource')).toBe('https://resource.example.com');
});

it('re-saves enriched state when partial cache is supplemented with fetched metadata', async () => {
Expand Down Expand Up @@ -2562,6 +2563,60 @@ describe('OAuth Authorization', () => {
expect(authUrl.searchParams.get('resource')).toBe('https://api.example.com/');
});

it('preserves bare-domain resource URI from PRM without adding a trailing slash', async () => {
// Regression test for #1968: `new URL("https://example.com").href` adds a trailing
// slash, which breaks providers (e.g. Microsoft Entra ID) that require an exact
// match between the OAuth `resource` parameter and the configured resource indicator.
mockFetch.mockImplementation(url => {
const urlString = url.toString();

if (urlString.includes('/.well-known/oauth-protected-resource')) {
return Promise.resolve({
ok: true,
status: 200,
json: async () => ({
// Bare-origin resource URI with no trailing slash
resource: 'https://api.example.com',
authorization_servers: ['https://auth.example.com']
})
});
} else if (urlString.includes('/.well-known/oauth-authorization-server')) {
return Promise.resolve({
ok: true,
status: 200,
json: async () => ({
issuer: 'https://auth.example.com',
authorization_endpoint: 'https://auth.example.com/authorize',
token_endpoint: 'https://auth.example.com/token',
response_types_supported: ['code'],
code_challenge_methods_supported: ['S256']
})
});
}

return Promise.resolve({ ok: false, status: 404 });
});

(mockProvider.clientInformation as Mock).mockResolvedValue({
client_id: 'test-client',
client_secret: 'test-secret'
});
(mockProvider.tokens as Mock).mockResolvedValue(undefined);
(mockProvider.saveCodeVerifier as Mock).mockResolvedValue(undefined);
(mockProvider.redirectToAuthorization as Mock).mockResolvedValue(undefined);

const result = await auth(mockProvider, {
serverUrl: 'https://api.example.com/mcp-server/endpoint'
});

expect(result).toBe('REDIRECT');

const redirectCall = (mockProvider.redirectToAuthorization as Mock).mock.calls[0];
const authUrl: URL = redirectCall[0];
// Resource indicator must round-trip exactly as published in PRM (no trailing slash)
expect(authUrl.searchParams.get('resource')).toBe('https://api.example.com');
});

it('excludes resource parameter when Protected Resource Metadata is not present', async () => {
// Mock metadata discovery where protected resource metadata is not available (404)
// but authorization server metadata is available
Expand Down
Loading