Skip to content

Commit c47d154

Browse files
committed
Use none as the token-endpoint auth method for CIMD public clients
A client identified by a Client ID Metadata Document is a public client: its identifier is a URL and it has no client secret, so it must authenticate at the token endpoint with 'none' and prove possession via PKCE. Previously ApplyClientIdMetadataDocument left the auth method unset, so GetAccessTokenAsync fell through to the first method the authorization server advertised. Servers like Auth0 advertise client_secret_basic first, which made the token exchange send a Basic header built from the CIMD URL and an empty secret and omit client_id from the body, so the exchange failed with 401 access_denied. ApplyClientIdMetadataDocument now sets the auth method to 'none'. The test OAuth server can advertise an arbitrary ordered auth-method list, and a new regression test proves a CIMD client authenticates when the server advertises client_secret_basic before none. Fixes #1612
1 parent 609499b commit c47d154

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ private void ApplyClientIdMetadataDocument(Uri metadataUri)
494494

495495
_clientId = metadataUri.AbsoluteUri;
496496

497+
// A CIMD client is a public client (it has no client secret, and its identifier is a URL),
498+
// so it authenticates at the token endpoint with "none" and proves possession via PKCE.
499+
// Without this, GetAccessTokenAsync falls through to the first method the authorization
500+
// server advertises (e.g. client_secret_basic on Auth0), which fails with 401 access_denied.
501+
// See https://github.com/modelcontextprotocol/csharp-sdk/issues/1612.
502+
_tokenEndpointAuthMethod = "none";
503+
497504
// See: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-client-id-metadata-document-00#section-3
498505
static bool IsValidClientMetadataDocumentUri(Uri uri)
499506
=> uri.IsAbsoluteUri

‎tests/ModelContextProtocol.AspNetCore.Tests/OAuth/AuthTests.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,34 @@ public async Task CanAuthenticate_WithClientMetadataDocument()
360360
transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken);
361361
}
362362

363+
[Fact]
364+
public async Task CanAuthenticate_WithClientMetadataDocument_WhenServerAdvertisesClientSecretBasicFirst()
365+
{
366+
// Auth0 advertises client_secret_basic ahead of none. A CIMD client is a public client and
367+
// must use "none" (PKCE) regardless of the advertised order, otherwise the token exchange
368+
// sends an empty-secret Basic header and is rejected with 401 access_denied (#1612).
369+
TestOAuthServer.TokenEndpointAuthMethodsSupported = ["client_secret_basic", "client_secret_post", "private_key_jwt", "none"];
370+
await using var app = await StartMcpServerAsync();
371+
372+
await using var transport = new HttpClientTransport(new()
373+
{
374+
Endpoint = new(McpServerUrl),
375+
OAuth = new ClientOAuthOptions()
376+
{
377+
RedirectUri = new Uri("http://localhost:1179/callback"),
378+
AuthorizationCallbackHandler = HandleAuthorizationUrlAsync,
379+
ClientMetadataDocumentUri = new Uri(ClientMetadataDocumentUrl),
380+
DynamicClientRegistration = new()
381+
{
382+
ApplicationType = "web",
383+
},
384+
},
385+
}, HttpClient, LoggerFactory);
386+
387+
await using var client = await McpClient.CreateAsync(
388+
transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken);
389+
}
390+
363391
[Fact]
364392
public async Task CannotAuthenticate_WhenMetadataOmitsPkceMethods()
365393
{

‎tests/ModelContextProtocol.TestOAuthServer/Program.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public Program(ILoggerProvider? loggerProvider = null, IConnectionListenerFactor
8080
/// </remarks>
8181
public bool ClientIdMetadataDocumentSupported { get; set; } = true;
8282

83+
/// <summary>
84+
/// Gets or sets the <c>token_endpoint_auth_methods_supported</c> values the authorization server
85+
/// advertises in its discovery document. Tests set this to a list that does not lead with
86+
/// <c>none</c> (e.g. <c>["client_secret_basic", "none"]</c>, mirroring Auth0) to verify that a
87+
/// CIMD public client still authenticates with <c>none</c> rather than the first advertised method.
88+
/// </summary>
89+
/// <remarks>
90+
/// The default value is <c>["client_secret_post"]</c>.
91+
/// </remarks>
92+
public List<string> TokenEndpointAuthMethodsSupported { get; set; } = ["client_secret_post"];
93+
8394
/// <summary>
8495
/// Gets or sets a value indicating whether the authorization server expects a resource parameter.
8596
/// When <c>true</c>, the resource parameter must be present and match a valid resource.
@@ -276,7 +287,7 @@ IResult HandleMetadataRequest(HttpContext context, string? issuerPath = null)
276287
ScopesSupported = IncludeOfflineAccessInMetadata
277288
? ["openid", "profile", "email", "mcp:tools", "offline_access"]
278289
: ["openid", "profile", "email", "mcp:tools"],
279-
TokenEndpointAuthMethodsSupported = ["client_secret_post"],
290+
TokenEndpointAuthMethodsSupported = TokenEndpointAuthMethodsSupported,
280291
ClaimsSupported = ["sub", "iss", "name", "email", "aud"],
281292
CodeChallengeMethodsSupported = MetadataPathsWithoutPkceSupport.Contains(context.Request.Path)
282293
? null

0 commit comments

Comments
 (0)