-
Notifications
You must be signed in to change notification settings - Fork 965
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
xiaoyijun
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
mcp-auth:feat-support-oidc-discovery-in-client-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−46
Open
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.`); | ||
|
@@ -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()); | ||
} | ||
|
||
return OAuthMetadataSchema.parse(await response.json()); | ||
// If all URLs returned 404, discovery is not supported by the server. | ||
return undefined; | ||
} | ||
|
||
/** | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be an |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 hasuserinfo_endpoint
,subject_types_supported
,id_token_signing_alg_values_supported
for example.