From 10f62f69e969a0951e0e38bc43bd2a4f6bae532c Mon Sep 17 00:00:00 2001 From: shlee1223 Date: Fri, 6 Nov 2026 22:08:11 +0900 Subject: [PATCH] Omit empty authorization_servers and scopes_supported from protected resource metadata --- .../ProtectedResourceMetadata.cs | 22 ++++++- ...ectedResourceMetadataSerializationTests.cs | 59 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 tests/ModelContextProtocol.Tests/Authentication/ProtectedResourceMetadataSerializationTests.cs diff --git a/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs b/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs index bed9e26d6..146515f2d 100644 --- a/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs +++ b/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs @@ -37,9 +37,18 @@ public sealed class ProtectedResourceMetadata /// /// OPTIONAL. /// - [JsonPropertyName("authorization_servers")] + [JsonIgnore] public IList AuthorizationServers { get; set; } = []; + // RFC 9728 §3.2: parameters with zero values MUST be omitted from the response. + [JsonInclude] + [JsonPropertyName("authorization_servers")] + internal IList? AuthorizationServersCore + { + get => AuthorizationServers.Count > 0 ? AuthorizationServers : null; + set => AuthorizationServers = value ?? []; + } + /// /// Gets or sets the supported bearer token methods. /// @@ -63,9 +72,18 @@ public sealed class ProtectedResourceMetadata /// /// RECOMMENDED. /// - [JsonPropertyName("scopes_supported")] + [JsonIgnore] public IList ScopesSupported { get; set; } = []; + // RFC 9728 §3.2: parameters with zero values MUST be omitted from the response. + [JsonInclude] + [JsonPropertyName("scopes_supported")] + internal IList? ScopesSupportedCore + { + get => ScopesSupported.Count > 0 ? ScopesSupported : null; + set => ScopesSupported = value ?? []; + } + /// /// Gets or sets the URL of the protected resource's JSON Web Key (JWK) Set document. /// diff --git a/tests/ModelContextProtocol.Tests/Authentication/ProtectedResourceMetadataSerializationTests.cs b/tests/ModelContextProtocol.Tests/Authentication/ProtectedResourceMetadataSerializationTests.cs new file mode 100644 index 000000000..a104accd3 --- /dev/null +++ b/tests/ModelContextProtocol.Tests/Authentication/ProtectedResourceMetadataSerializationTests.cs @@ -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( + 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() + { + var metadata = JsonSerializer.Deserialize( + """{"resource":"https://mcp.example.com"}""", McpJsonUtilities.DefaultOptions); + + Assert.NotNull(metadata); + Assert.Empty(metadata.AuthorizationServers); + Assert.Empty(metadata.ScopesSupported); + } +}