Skip to content

feat: support oidc discovery in client sdk #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/client/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe("OAuth Authorization", () => {
code_challenge_methods_supported: ["S256"],
};

it("returns metadata when discovery succeeds", async () => {
it("returns metadata when oauth-authorization-server discovery succeeds", async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
Expand All @@ -207,6 +207,28 @@ describe("OAuth Authorization", () => {
});
});

it("returns metadata when oidc discovery succeeds", async () => {
mockFetch.mockImplementation((url) => {
if (url.toString().includes('openid-configuration')) {
return Promise.resolve({
ok: true,
status: 200,
json: async () => validMetadata,
});
}
return Promise.resolve({
ok: false,
status: 404,
});
});

const metadata = await discoverOAuthMetadata("https://auth.example.com");
expect(metadata).toEqual(validMetadata);
expect(mockFetch).toHaveBeenCalledTimes(2);
expect(mockFetch.mock.calls[0][0].toString()).toBe("https://auth.example.com/.well-known/oauth-authorization-server");
expect(mockFetch.mock.calls[1][0].toString()).toBe("https://auth.example.com/.well-known/openid-configuration");
});

it("returns metadata when first fetch fails but second without MCP header succeeds", async () => {
// Set up a counter to control behavior
let callCount = 0;
Expand Down Expand Up @@ -266,13 +288,14 @@ describe("OAuth Authorization", () => {
});

it("returns undefined when discovery endpoint returns 404", async () => {
mockFetch.mockResolvedValueOnce({
mockFetch.mockResolvedValue({
ok: false,
status: 404,
});

const metadata = await discoverOAuthMetadata("https://auth.example.com");
expect(metadata).toBeUndefined();
expect(mockFetch).toHaveBeenCalledTimes(2);
});

it("throws on non-404 errors", async () => {
Expand Down
104 changes: 63 additions & 41 deletions src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,8 @@ export async function discoverOAuthProtectedResourceMetadata(
} else {
url = new URL("/.well-known/oauth-protected-resource", serverUrl);
}

let response: Response;
try {
response = await fetch(url, {
headers: {
"MCP-Protocol-Version": opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION
}
});
} catch (error) {
// CORS errors come back as TypeError
if (error instanceof TypeError) {
response = await fetch(url);
} else {
throw error;
}
}

const response = await fetchWithCorsFallback(url, opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION);

if (response.status === 404) {
throw new Error(`Resource server does not implement OAuth 2.0 Protected Resource Metadata.`);
Expand All @@ -260,43 +246,59 @@ export async function discoverOAuthProtectedResourceMetadata(
}

/**
* Looks up RFC 8414 OAuth 2.0 Authorization Server Metadata.
* Looks up authorization server metadata from an MCP-compliant server.
*
* If the server returns a 404 for the well-known endpoint, this function will
* Per the MCP specification, clients **MUST** support both OAuth 2.0
* Authorization Server Metadata ([RFC8414](https://datatracker.ietf.org/doc/html/rfc8414))
* and [OpenID Connect Discovery 1.0](https://openid.net/specs/openid-connect-discovery-1_0-final.html).
* This function implements this requirement by checking the well-known
* discovery endpoints for both standards.
*
* The function can parse responses from both types of endpoints because OIDC
* discovery metadata is a superset of the metadata defined in RFC 8414.
*
* If the server returns a 404 for all known endpoints, this function will
* return `undefined`. Any other errors will be thrown as exceptions.
*/
export async function discoverOAuthMetadata(
authorizationServerUrl: string | URL,
opts?: { protocolVersion?: string },
): Promise<OAuthMetadata | undefined> {
const url = new URL("/.well-known/oauth-authorization-server", authorizationServerUrl);
let response: Response;
try {
response = await fetch(url, {
headers: {
"MCP-Protocol-Version": opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION
}
});
} catch (error) {
// CORS errors come back as TypeError
if (error instanceof TypeError) {
response = await fetch(url);
} else {
throw error;

/**
* To support both OIDC and plain OAuth2 servers, this checks for their
* respective discovery endpoints.
*/
const potentialAuthServerMetadataUrls = [
new URL("/.well-known/oauth-authorization-server", authorizationServerUrl),
new URL("/.well-known/openid-configuration", authorizationServerUrl),
];

for (const url of potentialAuthServerMetadataUrls) {
const response = await fetchWithCorsFallback(url, opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION);

if (response.status === 404) {
// Try the next URL
continue;
}
}

if (response.status === 404) {
return undefined;
}
if (!response.ok) {
throw new Error(
`HTTP ${response.status} trying to load well-known OAuth metadata from ${url.toString()}`,
);
}

if (!response.ok) {
throw new Error(
`HTTP ${response.status} trying to load well-known OAuth metadata`,
);
/**
* The `OAuthMetadataSchema` is compatible with both OIDC and OAuth2
* discovery responses. Because OIDC's metadata is a superset, `zod` will
* correctly parse the fields defined in our schema and simply ignore any
* additional OIDC-specific fields.
*/
return OAuthMetadataSchema.parse(await response.json());
Comment on lines +291 to +297
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OICD is not a superset. They have a common base, but they do have both fields that they dont share, AS Metadata has introspection_endpoint, revocation_endpoint, introspection_endpoint_auth_methods_supported and OICD has userinfo_endpoint, subject_types_supported, id_token_signing_alg_values_supported for example.

}

return OAuthMetadataSchema.parse(await response.json());
// If all URLs returned 404, discovery is not supported by the server.
return undefined;
}

/**
Expand Down Expand Up @@ -530,3 +532,23 @@ export async function registerClient(

return OAuthClientInformationFullSchema.parse(await response.json());
}

/**
* A fetch wrapper that attempts to set the MCP-Protocol-Version header, but
* falls back to a header-less request if a cors error occurs.
*/
const fetchWithCorsFallback = async (url: URL, protocolVersion: string) => {
try {
return await fetch(url, {
headers: {
"MCP-Protocol-Version": protocolVersion
}
})
} catch (error) {
if (error instanceof TypeError) {
// CORS errors come back as TypeError, try again without protocol version header
return await fetch(url);
}
throw error;
}
}
Comment on lines +540 to +554
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an async function fetchWithCorsFallback() and not const fetchWithCorsFallback =, to stay in line with everything else here and ensure that we define a proper return type.

11 changes: 8 additions & 3 deletions src/client/sse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ describe("SSEClientTransport", () => {
});

describe("auth handling", () => {
const authServerMetadataUrls = [
"/.well-known/oauth-authorization-server",
"/.well-known/openid-configuration",
];

let mockAuthProvider: jest.Mocked<OAuthClientProvider>;

beforeEach(() => {
Expand Down Expand Up @@ -551,7 +556,7 @@ describe("SSEClientTransport", () => {
authServer.close();

authServer = createServer((req, res) => {
if (req.url === "/.well-known/oauth-authorization-server") {
if (req.url && authServerMetadataUrls.includes(req.url)) {
res.writeHead(404).end();
return;
}
Expand Down Expand Up @@ -673,7 +678,7 @@ describe("SSEClientTransport", () => {
authServer.close();

authServer = createServer((req, res) => {
if (req.url === "/.well-known/oauth-authorization-server") {
if (req.url && authServerMetadataUrls.includes(req.url)) {
res.writeHead(404).end();
return;
}
Expand Down Expand Up @@ -818,7 +823,7 @@ describe("SSEClientTransport", () => {
authServer.close();

authServer = createServer((req, res) => {
if (req.url === "/.well-known/oauth-authorization-server") {
if (req.url && authServerMetadataUrls.includes(req.url)) {
res.writeHead(404).end();
return;
}
Expand Down