Append offline_access to authorization scope when AS advertises it (SEP-2207)#1479
Draft
Append offline_access to authorization scope when AS advertises it (SEP-2207)#1479
offline_access to authorization scope when AS advertises it (SEP-2207)#1479Conversation
Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/4d13c15e-a0f6-4d0f-9525-5f0b9e0c000b Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add offline_access to authorization request scopes for SEP-2207
Append Mar 30, 2026
offline_access to authorization scope when AS advertises it (SEP-2207)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
SEP-2207 specifies that MCP clients MAY add
offline_accessto the authorization request scope when the Authorization Server'sscopes_supportedmetadata includes it, signaling to OIDC-flavored servers that a refresh token is desired. The SDK was not doing this.Changes
ClientOAuthProvider: AddedAugmentScopeWithOfflineAccesshelper called fromBuildAuthorizationUrlafterGetScopeParameter. Appendsoffline_accessonly when the AS advertises it inscopes_supportedand it isn't already present in the scope string.TestOAuthServer: AddedIncludeOfflineAccessInMetadataproperty (defaultfalse) to opt the test AS into advertisingoffline_accessinscopes_supported.AuthTests: Three new integration tests covering: augmentation when AS advertisesoffline_access, no augmentation when it doesn't, and no duplication when it's already in the scope.Original prompt
Context
SEP-2207 (now merged) provides OIDC-flavored refresh token guidance for MCP OAuth clients. The C# SDK already satisfies all MUST/SHOULD requirements, but there is one MAY-level enhancement that the spec explicitly calls for reference implementations in official SDKs to adopt.
Problem
The SEP states that MCP Clients MAY add
offline_accessto the scope parameter in the authorization request when the Authorization Server metadata (scopes_supported) contains"offline_access". This signals to OIDC-flavored authorization servers that the client desires a refresh token.Currently,
ClientOAuthProvider.BuildAuthorizationUrl()and related methods pass through scopes from the protected resource metadata or configured scopes, but never augment them withoffline_accessby checkingAuthorizationServerMetadata.ScopesSupported.Desired Change
In
ClientOAuthProvider, after determining the scope string to use (viaGetScopeParameter), check ifauthServerMetadata.ScopesSupportedcontains"offline_access". If it does, andoffline_accessis not already present in the scope string, append it before sending the authorization request.Key locations to change:
ClientOAuthProvider.cs— Modify scope construction inBuildAuthorizationUrl(and optionallyExchangeCodeForTokenAsync) to augment the scope withoffline_accesswhen:authServerMetadata.ScopesSupportedcontains"offline_access"offline_accessTests — Add test coverage verifying that:
offline_accessis appended to the authorization URL scope when the AS metadata advertises it inscopes_supportedoffline_accessis NOT added when the AS metadata does not include itoffline_accessis NOT duplicated if it's already present in the configured scopesImportant notes:
AuthorizationServerMetadataclass already hasScopesSupported(scopes_supported) deserialized, so no model changes are needed.BuildAuthorizationUrlmethod already receivesauthServerMetadataas a parameter, so it has access to the needed information.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Is there any work needed in the C# SDK to adapt to this PR?
Assistant:
Let me examine the referenced PR and the current state of the C# SDK to understand what changes might be needed.
Assistant: Now let me look at the C# SDK's OAuth/authorization client code to see how it currently handles scopes, grant types, and refresh tokens:
Assistant: Now let me look at the key areas more closely — the
GetScopeParametermethod andBuildAuthorizationUrlto understand how scopes are currently assembled for the authorization request, and theAuthorizationServerMetadatato check forscopes_supported:Assistant: Based on my thorough analysis of the PR and the C# SDK codebase, here's my assessment:
SEP-2207 Impact on the C# SDK
The C# SDK already partially complies with the SEP-2207 guidance, but there is one concrete client-side improvement that could be made as a reference implementation, and everything else is already in good shape.
✅ What's already correct
Dynamic Client Registration already includes
refresh_tokeningrant_types— InClientOAuthProvider.csline 651, the DCR request already sendsGrantTypes = ["authorization_code", "refresh_token"], which satisfies the SHOULD requirement that clients includerefresh_tokenin theirgrant_typesclient metadata.Refresh token handling is already implemented — The SDK already stores, retrieves, and uses refresh tokens via
RefreshTokensAsync(),GetAccessTokenSilentAsync(), and the token cache infrastructure.No server-side
offline_accessviolation — The SDK's server-side code does not includeoffline_accessinWWW-Authenticatescope or Protected Resource Metadatascopes_supported, which aligns with the SHOULD NOT guidance.🔧 What could be added (optional, MAY-level)
The SEP says MCP Clients MAY add
offline_accessto the scope parameter when the Authorization Server metadata contains it inscopes_supported. The C# SDK currently does not do this — theBuildAuthorizationUrland [GetScopeParameter](https://github.com/modelcontextprotocol/csharp-sdk/blob/7662ce6b7092c1b026f7613ed3e79a658192fa83/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs...This pull request was created from Copilot chat.