Skip to content

Commit c8ccd03

Browse files
committed
clean up unused param
1 parent fdc147e commit c8ccd03

File tree

2 files changed

+31
-107
lines changed

2 files changed

+31
-107
lines changed

src/client/auth.test.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,22 @@ describe("OAuth Authorization", () => {
776776
"https://auth.example.com/.well-known/openid-configuration/tenant1",
777777
"https://auth.example.com/tenant1/.well-known/openid-configuration"
778778
]
779+
},
780+
{
781+
description: "with path - all fails, returns undefined",
782+
serverUrl: "https://auth.example.com/tenant1",
783+
responses: [
784+
{ success: false, status: 404 }, // OAuth path
785+
{ success: false, status: 404 }, // OAuth root
786+
{ success: false, status: 404 }, // OIDC path insertion
787+
{ success: false, status: 404 }, // OIDC path appending
788+
],
789+
expectedPaths: [
790+
"https://auth.example.com/.well-known/oauth-authorization-server/tenant1",
791+
"https://auth.example.com/.well-known/oauth-authorization-server",
792+
"https://auth.example.com/.well-known/openid-configuration/tenant1",
793+
"https://auth.example.com/tenant1/.well-known/openid-configuration"
794+
]
779795
}
780796
];
781797

@@ -798,7 +814,6 @@ describe("OAuth Authorization", () => {
798814
});
799815

800816
const metadata = await discoverAuthorizationServerMetadata(
801-
"https://mcp.example.com",
802817
serverUrl
803818
);
804819

@@ -843,45 +858,11 @@ describe("OAuth Authorization", () => {
843858

844859
await expect(
845860
discoverAuthorizationServerMetadata(
846-
"https://mcp.example.com",
847861
"https://auth.example.com"
848862
)
849863
).rejects.toThrow("does not support S256 code challenge method required by MCP specification");
850864
});
851865

852-
it("falls back to legacy MCP server when authorizationServerUrl is undefined", async () => {
853-
mockFetch.mockResolvedValueOnce({
854-
ok: true,
855-
status: 200,
856-
json: async () => validOAuthMetadata,
857-
});
858-
859-
const metadata = await discoverAuthorizationServerMetadata(
860-
"https://mcp.example.com",
861-
undefined
862-
);
863-
864-
expect(metadata).toEqual(validOAuthMetadata);
865-
const calls = mockFetch.mock.calls;
866-
expect(calls.length).toBe(1);
867-
const [url] = calls[0];
868-
expect(url.toString()).toBe("https://mcp.example.com/.well-known/oauth-authorization-server");
869-
});
870-
871-
it("returns undefined when legacy MCP server returns 404", async () => {
872-
mockFetch.mockResolvedValueOnce({
873-
ok: false,
874-
status: 404,
875-
});
876-
877-
const metadata = await discoverAuthorizationServerMetadata(
878-
"https://mcp.example.com",
879-
undefined
880-
);
881-
882-
expect(metadata).toBeUndefined();
883-
});
884-
885866
it("throws on non-404 errors in legacy mode", async () => {
886867
mockFetch.mockResolvedValueOnce({
887868
ok: false,
@@ -905,7 +886,6 @@ describe("OAuth Authorization", () => {
905886
});
906887

907888
const metadata = await discoverAuthorizationServerMetadata(
908-
"https://mcp.example.com",
909889
"https://auth.example.com"
910890
);
911891

@@ -928,7 +908,6 @@ describe("OAuth Authorization", () => {
928908
});
929909

930910
const metadata = await discoverAuthorizationServerMetadata(
931-
"https://mcp.example.com",
932911
"https://auth.example.com",
933912
{ fetchFn: customFetch }
934913
);
@@ -946,7 +925,6 @@ describe("OAuth Authorization", () => {
946925
});
947926

948927
const metadata = await discoverAuthorizationServerMetadata(
949-
"https://mcp.example.com",
950928
"https://auth.example.com",
951929
{ protocolVersion: "2025-01-01" }
952930
);

src/client/auth.ts

Lines changed: 15 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,6 @@ async function authInternal(
332332
// Ignore errors and fall back to /.well-known/oauth-authorization-server
333333
}
334334

335-
const resource: URL | undefined = await selectResourceURL(serverUrl, provider, resourceMetadata);
336-
337-
const metadata = await discoverAuthorizationServerMetadata(serverUrl, authorizationServerUrl, {
338-
fetchFn,
339-
});
340-
341335
/**
342336
* If we don't get a valid authorization server metadata from protected resource metadata,
343337
* fallback to the legacy MCP spec's implementation (version 2025-03-26): MCP server acts as the Authorization server.
@@ -346,6 +340,12 @@ async function authInternal(
346340
authorizationServerUrl = serverUrl;
347341
}
348342

343+
const resource: URL | undefined = await selectResourceURL(serverUrl, provider, resourceMetadata);
344+
345+
const metadata = await discoverAuthorizationServerMetadata(authorizationServerUrl, {
346+
fetchFn,
347+
});
348+
349349
// Handle client registration if needed
350350
let clientInformation = await Promise.resolve(provider.clientInformation());
351351
if (!clientInformation) {
@@ -664,24 +664,19 @@ export async function discoverOAuthMetadata(
664664
* and OpenID Connect Discovery 1.0 specifications.
665665
*
666666
* This function implements a fallback strategy for authorization server discovery:
667-
* 1. If `authorizationServerUrl` is provided, attempts RFC 8414 OAuth metadata discovery first
667+
* 1. Attempts RFC 8414 OAuth metadata discovery first
668668
* 2. If OAuth discovery fails, falls back to OpenID Connect Discovery
669-
* 3. If `authorizationServerUrl` is not provided, uses legacy MCP specification behavior
670669
*
671-
* @param serverUrl - The MCP Server URL, used for legacy specification support where the MCP server
672-
* acts as both the resource server and authorization server
673670
* @param authorizationServerUrl - The authorization server URL obtained from the MCP Server's
674-
* protected resource metadata. If this parameter is `undefined`,
675-
* it indicates that protected resource metadata was not successfully
676-
* retrieved, triggering legacy fallback behavior
671+
* protected resource metadata, or the MCP server's URL if the
672+
* metadata was not found.
677673
* @param options - Configuration options
678674
* @param options.fetchFn - Optional fetch function for making HTTP requests, defaults to global fetch
679675
* @param options.protocolVersion - MCP protocol version to use, defaults to LATEST_PROTOCOL_VERSION
680676
* @returns Promise resolving to authorization server metadata, or undefined if discovery fails
681677
*/
682678
export async function discoverAuthorizationServerMetadata(
683-
serverUrl: string | URL,
684-
authorizationServerUrl?: string | URL,
679+
authorizationServerUrl: string | URL,
685680
{
686681
fetchFn = fetch,
687682
protocolVersion = LATEST_PROTOCOL_VERSION,
@@ -690,18 +685,10 @@ export async function discoverAuthorizationServerMetadata(
690685
protocolVersion?: string;
691686
} = {}
692687
): Promise<AuthorizationServerMetadata | undefined> {
693-
if (!authorizationServerUrl) {
694-
// Legacy support: MCP servers act as the Auth server.
695-
return retrieveOAuthMetadataFromMcpServer(serverUrl, {
696-
fetchFn,
697-
protocolVersion,
698-
});
699-
}
700-
701688
const url = typeof authorizationServerUrl === 'string' ? new URL(authorizationServerUrl) : authorizationServerUrl;
702689
const hasPath = url.pathname !== '/';
703690

704-
const oauthMetadata = await retrieveOAuthMetadataFromAuthorizationServer(authorizationServerUrl, {
691+
const oauthMetadata = await fetchOAuthMetadata(authorizationServerUrl, {
705692
fetchFn,
706693
protocolVersion,
707694
});
@@ -712,7 +699,7 @@ export async function discoverAuthorizationServerMetadata(
712699

713700
if (hasPath) {
714701
const rootUrl = new URL(url.origin);
715-
const rootOauthMetadata = await retrieveOAuthMetadataFromAuthorizationServer(rootUrl, {
702+
const rootOauthMetadata = await fetchOAuthMetadata(rootUrl, {
716703
fetchFn,
717704
protocolVersion,
718705
});
@@ -730,50 +717,9 @@ export async function discoverAuthorizationServerMetadata(
730717
return oidcMetadata;
731718
}
732719

733-
/**
734-
* Legacy implementation where the MCP server acts as the Auth server.
735-
* According to MCP spec version 2025-03-26.
736-
*
737-
* @param serverUrl - The MCP Server URL
738-
* @param options - Configuration options
739-
* @param options.fetchFn - Optional fetch function for making HTTP requests, defaults to global fetch
740-
* @param options.protocolVersion - MCP protocol version to use (required)
741-
* @returns Promise resolving to OAuth metadata, or undefined if discovery fails
742-
*/
743-
async function retrieveOAuthMetadataFromMcpServer(
744-
serverUrl: string | URL,
745-
{
746-
fetchFn = fetch,
747-
protocolVersion,
748-
}: {
749-
fetchFn?: FetchLike;
750-
protocolVersion: string;
751-
}
752-
): Promise<OAuthMetadata | undefined> {
753-
const serverOrigin = typeof serverUrl === 'string' ? new URL(serverUrl).origin : serverUrl.origin;
754-
755-
const metadataEndpoint = new URL(buildWellKnownPath('oauth-authorization-server'), serverOrigin);
756-
757-
const response = await fetchWithCorsRetry(metadataEndpoint, getProtocolVersionHeader(protocolVersion), fetchFn);
758-
759-
if (!response) {
760-
throw new Error(`CORS error trying to load OAuth metadata from ${metadataEndpoint}`);
761-
}
762-
763-
if (!response.ok) {
764-
if (response.status === 404) {
765-
return undefined;
766-
}
767-
768-
throw new Error(`HTTP ${response.status} trying to load OAuth metadata from ${metadataEndpoint}`);
769-
}
770-
771-
return OAuthMetadataSchema.parse(await response.json());
772-
}
773-
774720
/**
775721
* Retrieves RFC 8414 OAuth 2.0 Authorization Server Metadata from the authorization server.
776-
*
722+
*
777723
* Per RFC 8414 Section 3.1, when the issuer identifier contains path components,
778724
* the well-known URI is constructed by inserting "/.well-known/oauth-authorization-server"
779725
* before the path component.
@@ -784,7 +730,7 @@ async function retrieveOAuthMetadataFromMcpServer(
784730
* @param options.protocolVersion - MCP protocol version to use (required)
785731
* @returns Promise resolving to OAuth metadata, or undefined if discovery fails
786732
*/
787-
async function retrieveOAuthMetadataFromAuthorizationServer(
733+
async function fetchOAuthMetadata(
788734
authorizationServerUrl: string | URL,
789735
{
790736
fetchFn = fetch,
@@ -821,7 +767,7 @@ async function retrieveOAuthMetadataFromAuthorizationServer(
821767

822768
/**
823769
* Retrieves OpenID Connect Discovery 1.0 metadata from the authorization server.
824-
*
770+
*
825771
* Per RFC 8414 Section 5 compatibility notes and OpenID Connect Discovery 1.0 Section 4.1,
826772
* when the issuer identifier contains path components, discovery endpoints are tried in order:
827773
* 1. RFC 8414 style: Insert /.well-known/openid-configuration before the path

0 commit comments

Comments
 (0)