Repository navigation
Omit empty authorization_servers and scopes_supported from protected … #1999
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,9 +37,18 @@ public sealed class ProtectedResourceMetadata | |
| /// <remarks> | ||
| /// OPTIONAL. | ||
| /// </remarks> | ||
| [JsonPropertyName("authorization_servers")] | ||
| [JsonIgnore] | ||
| public IList<string> AuthorizationServers { get; set; } = []; | ||
|
|
||
| // RFC 9728 §3.2: parameters with zero values MUST be omitted from the response. | ||
| [JsonInclude] | ||
| [JsonPropertyName("authorization_servers")] | ||
| internal IList<string>? AuthorizationServersCore | ||
|
Member
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. If this approach stays, two hardening points to consider:
|
||
| { | ||
| get => AuthorizationServers.Count > 0 ? AuthorizationServers : null; | ||
| set => AuthorizationServers = value ?? []; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the supported bearer token methods. | ||
| /// </summary> | ||
|
|
@@ -63,9 +72,18 @@ public sealed class ProtectedResourceMetadata | |
| /// <remarks> | ||
| /// RECOMMENDED. | ||
| /// </remarks> | ||
| [JsonPropertyName("scopes_supported")] | ||
| [JsonIgnore] | ||
| public IList<string> ScopesSupported { get; set; } = []; | ||
|
|
||
| // RFC 9728 §3.2: parameters with zero values MUST be omitted from the response. | ||
| [JsonInclude] | ||
| [JsonPropertyName("scopes_supported")] | ||
| internal IList<string>? ScopesSupportedCore | ||
|
Member
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. §3.2 applies to every parameter, so |
||
| { | ||
| get => ScopesSupported.Count > 0 ? ScopesSupported : null; | ||
| set => ScopesSupported = value ?? []; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the URL of the protected resource's JSON Web Key (JWK) Set document. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.Text.Json; | ||
| using ModelContextProtocol.Authentication; | ||
|
|
||
| namespace ModelContextProtocol.Tests.Authentication; | ||
|
|
||
| public class ProtectedResourceMetadataSerializationTests | ||
| { | ||
| [Fact] | ||
| public void EmptyAuthorizationServersAndScopes_AreOmitted() | ||
| { | ||
| // RFC 9728 §3.2: parameters with zero values MUST be omitted from the response. | ||
| var metadata = new ProtectedResourceMetadata { Resource = "https://mcp.example.com" }; | ||
|
|
||
| using var json = JsonDocument.Parse(JsonSerializer.Serialize(metadata, McpJsonUtilities.DefaultOptions)); | ||
|
|
||
| Assert.False(json.RootElement.TryGetProperty("authorization_servers", out _)); | ||
| Assert.False(json.RootElement.TryGetProperty("scopes_supported", out _)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EmptyBearerMethods_AreKept() | ||
| { | ||
| // RFC 9728 §2: "The empty array [] can be used to indicate that no bearer methods are supported." | ||
| var metadata = new ProtectedResourceMetadata { Resource = "https://mcp.example.com", BearerMethodsSupported = [] }; | ||
|
|
||
| using var json = JsonDocument.Parse(JsonSerializer.Serialize(metadata, McpJsonUtilities.DefaultOptions)); | ||
|
|
||
| Assert.Equal(0, json.RootElement.GetProperty("bearer_methods_supported").GetArrayLength()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AuthorizationServersAndScopes_RoundTrip() | ||
| { | ||
| var metadata = new ProtectedResourceMetadata | ||
| { | ||
| Resource = "https://mcp.example.com", | ||
| AuthorizationServers = ["https://auth.example.com"], | ||
| ScopesSupported = ["files:read"], | ||
| }; | ||
|
|
||
| var roundTripped = JsonSerializer.Deserialize<ProtectedResourceMetadata>( | ||
| JsonSerializer.Serialize(metadata, McpJsonUtilities.DefaultOptions), McpJsonUtilities.DefaultOptions); | ||
|
|
||
| Assert.NotNull(roundTripped); | ||
| Assert.Equal(["https://auth.example.com"], roundTripped.AuthorizationServers); | ||
| Assert.Equal(["files:read"], roundTripped.ScopesSupported); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MissingAuthorizationServersAndScopes_DeserializeAsEmpty() | ||
|
Member
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. One behavior change that seems worth pinning with a test: |
||
| { | ||
| var metadata = JsonSerializer.Deserialize<ProtectedResourceMetadata>( | ||
| """{"resource":"https://mcp.example.com"}""", McpJsonUtilities.DefaultOptions); | ||
|
|
||
| Assert.NotNull(metadata); | ||
| Assert.Empty(metadata.AuthorizationServers); | ||
| Assert.Empty(metadata.ScopesSupported); | ||
| } | ||
| } | ||
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.
Since the public property is now
[JsonIgnore], the only serialized member is internal. Anyone who adds[JsonSerializable(typeof(ProtectedResourceMetadata))]to their own context gets SYSLIB1038, which breaks builds withTreatWarningsAsErrors. They also get metadata with noauthorization_serversorscopes_supportedin either direction, and a served PRM missingauthorization_serverscan't be used for discovery. That trade-off needs weighing for a stable type, against keeping the public properties as the serialized members.