Append offline_access to authorization scope when AS advertises it (SEP-2207) (#1479)

This commit is contained in:
Copilot 2026-05-19 14:47:10 -07:00 • committed by GitHub
commit 74788af04d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -492,6 +492,7 @@ internal sealed partial class ClientOAuthProvider : McpHttpClient
}
var scope = GetScopeParameter(protectedResourceMetadata);
scope = AugmentScopeWithOfflineAccess(scope, authServerMetadata);
if (!string.IsNullOrEmpty(scope))
{
queryParamsDictionary["scope"] = scope!;
@ -726,6 +727,37 @@ internal sealed partial class ClientOAuthProvider : McpHttpClient
return _configuredScopes;
}
/// <summary>
/// Augments the scope parameter with <c>offline_access</c> if the authorization server advertises it in
/// <c>scopes_supported</c> and it is not already present. This signals to OIDC-flavored authorization servers
/// that the client desires a refresh token, per SEP-2207.
/// </summary>
private static string? AugmentScopeWithOfflineAccess(string? scope, AuthorizationServerMetadata authServerMetadata)
{
const string OfflineAccess = "offline_access";
if (authServerMetadata.ScopesSupported?.Contains(OfflineAccess) is not true)
{
return scope;
}
if (scope is null)
{
return OfflineAccess;
}
// Check if offline_access is already in the scope string (space-separated tokens).
foreach (var token in scope.Split(' '))
{
if (token == OfflineAccess)
{
return scope;
}
}
return scope + " " + OfflineAccess;
}
/// <summary>
/// Verifies that the resource URI in the metadata exactly matches the original request URL as required by the RFC.
/// Per RFC: The resource value must be identical to the URL that the client used to make the request to the resource server.