-
Notifications
You must be signed in to change notification settings - Fork 269
[Fix] MCP OAuth registration fails for servers advertising unsupported grants #1532
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
Draft
zoomote
wants to merge
2
commits into
main
Choose a base branch
from
fix/oauth-grant-filter-2iwple0khlo0y
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import assert from "node:assert/strict" | ||
|
|
||
| import { | ||
| AUTHORIZATION_CODE_GRANT_TYPE, | ||
| buildMcpOAuthClientMetadata, | ||
| MCP_OAUTH_GRANT_TYPES, | ||
| REFRESH_TOKEN_GRANT_TYPE, | ||
| selectMcpOAuthGrantTypes, | ||
| } from "../src/services/mcp/oauthMetadata" | ||
|
|
||
| const advertisedGrantTypes = [ | ||
| AUTHORIZATION_CODE_GRANT_TYPE, | ||
| REFRESH_TOKEN_GRANT_TYPE, | ||
| "urn:ietf:params:oauth:grant-type:jwt-bearer", | ||
| "urn:example:grant-type:extension", | ||
| ] as const | ||
|
|
||
| let checkedCases = 0 | ||
|
|
||
| // Repository policy: Zoo Code implements only these two token-endpoint grants. | ||
| // Keeping this assertion literal prevents an allowlist expansion from silently | ||
| // broadening dynamic registration. | ||
| assert.deepEqual(MCP_OAUTH_GRANT_TYPES, ["authorization_code", "refresh_token"]) | ||
|
|
||
| for (let mask = 0; mask < 1 << advertisedGrantTypes.length; mask++) { | ||
| const advertised = advertisedGrantTypes.filter((_, index) => mask & (1 << index)) | ||
| const selected = selectMcpOAuthGrantTypes(advertised) | ||
| const expected = MCP_OAUTH_GRANT_TYPES.filter((grantType) => advertised.includes(grantType)) | ||
|
|
||
| // Normative MUST: RFC 7591 section 2 says grant_types describes grants the | ||
| // client can use, and each token-endpoint grant_type must match its registered | ||
| // value. https://www.rfc-editor.org/rfc/rfc7591.html#section-2 | ||
| // Repository policy: intersect server metadata with Zoo Code's implemented | ||
| // grants, canonicalize order, and never propagate unknown extension values. | ||
| assert.deepEqual(selected, expected, `unexpected grant selection for ${JSON.stringify(advertised)}`) | ||
| assert.equal(new Set(selected).size, selected.length, "registration grant types must be unique") | ||
|
|
||
| const buildMetadata = () => | ||
| buildMcpOAuthClientMetadata({ | ||
| clientName: "Zoo Code", | ||
| redirectUrl: "http://localhost:12345/callback", | ||
| grantTypes: selected, | ||
| tokenEndpointAuthMethod: "none", | ||
| }) | ||
|
|
||
| if (!selected.includes(AUTHORIZATION_CODE_GRANT_TYPE)) { | ||
| assert.throws(buildMetadata, /requires authorization_code support/) | ||
| checkedCases++ | ||
| continue | ||
| } | ||
|
|
||
| const metadata = buildMetadata() | ||
|
|
||
| assert.deepEqual(metadata.grant_types, selected) | ||
| // Normative SHOULD: RFC 7591 section 2.1 recommends consistent | ||
| // authorization_code/code metadata. | ||
| // https://www.rfc-editor.org/rfc/rfc7591.html#section-2.1 | ||
| assert.deepEqual(metadata.response_types, ["code"]) | ||
| // Normative MUST/SHOULD: MCP 2026-07-28 requires DCR clients to declare | ||
| // application_type; desktop clients using localhost should identify as native. | ||
| // https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration#application-type-and-redirect-uri-constraints | ||
| assert.equal(metadata.application_type, "native") | ||
| assert.match(metadata.redirect_uris[0], /^http:\/\/localhost:/) | ||
|
|
||
| checkedCases++ | ||
| } | ||
|
|
||
| // Repository policy: retain both implemented grants when RFC 8414's optional | ||
| // grant_types_supported metadata is omitted. | ||
| // https://www.rfc-editor.org/rfc/rfc8414.html#section-2 | ||
| // Normative SHOULD: MCP clients that use refresh tokens should include | ||
| // refresh_token in their grant_types client metadata. | ||
| // https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization#refresh-tokens | ||
| assert.deepEqual(selectMcpOAuthGrantTypes(), [...MCP_OAUTH_GRANT_TYPES]) | ||
| assert.deepEqual( | ||
| selectMcpOAuthGrantTypes([REFRESH_TOKEN_GRANT_TYPE, AUTHORIZATION_CODE_GRANT_TYPE, REFRESH_TOKEN_GRANT_TYPE]), | ||
| [...MCP_OAUTH_GRANT_TYPES], | ||
| ) | ||
|
|
||
| console.log(`MCP OAuth integration check passed (${checkedCases} advertised-grant combinations)`) |
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
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import type { OAuthClientMetadata } from "@modelcontextprotocol/sdk/shared/auth.js" | ||
|
|
||
| export const MCP_OAUTH_GRANT_TYPES = ["authorization_code", "refresh_token"] as const | ||
| export type McpOAuthGrantType = (typeof MCP_OAUTH_GRANT_TYPES)[number] | ||
|
|
||
| export const AUTHORIZATION_CODE_GRANT_TYPE = MCP_OAUTH_GRANT_TYPES[0] | ||
| export const REFRESH_TOKEN_GRANT_TYPE = MCP_OAUTH_GRANT_TYPES[1] | ||
|
|
||
| export interface McpOAuthClientMetadata extends OAuthClientMetadata { | ||
| application_type: "native" | ||
| } | ||
|
|
||
| export function selectMcpOAuthGrantTypes(supportedGrantTypes?: readonly string[]): McpOAuthGrantType[] { | ||
| const supported = new Set(supportedGrantTypes ?? MCP_OAUTH_GRANT_TYPES) | ||
| return MCP_OAUTH_GRANT_TYPES.filter((grantType) => supported.has(grantType)) | ||
| } | ||
|
|
||
| export function buildMcpOAuthClientMetadata(options: { | ||
| clientName: string | ||
| redirectUrl: string | ||
| grantTypes: readonly McpOAuthGrantType[] | ||
| tokenEndpointAuthMethod: string | ||
| }): McpOAuthClientMetadata { | ||
| if (!options.grantTypes.includes(AUTHORIZATION_CODE_GRANT_TYPE)) { | ||
| throw new Error("MCP OAuth registration requires authorization_code support") | ||
| } | ||
|
|
||
| return { | ||
| application_type: "native", | ||
| client_name: options.clientName, | ||
| redirect_uris: [options.redirectUrl], | ||
| grant_types: [...options.grantTypes], | ||
| response_types: ["code"], | ||
| token_endpoint_auth_method: options.tokenEndpointAuthMethod, | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Use the RFC 8414 default for omitted grant metadata.
Line 14 advertises
refresh_tokenwhengrant_types_supportedis absent. RFC 8414 defines the omitted-field default as["authorization_code", "implicit"], notrefresh_token. A conforming authorization server can reject this DCR request because it advertises an unsupported grant. Default to[AUTHORIZATION_CODE_GRANT_TYPE]and update the integration assertion atscripts/check-mcp-oauth-integration.tslines 74-78. (rfc-editor.org)Proposed fix
As per path instructions, verify external protocol behavior against official specifications.
🤖 Prompt for AI Agents
Source: Path instructions