Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ public sealed class ProtectedResourceMetadata
/// <remarks>
/// OPTIONAL.
/// </remarks>
[JsonPropertyName("authorization_servers")]
[JsonIgnore]

Copy link
Copy Markdown
Member

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 with TreatWarningsAsErrors. They also get metadata with no authorization_servers or scopes_supported in either direction, and a served PRM missing authorization_servers can't be used for discovery. That trade-off needs weighing for a stable type, against keeping the public properties as the serialized members.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this approach stays, two hardening points to consider:

  • The omission depends on the caller's options using WhenWritingNull. With JsonSerializerOptions.Web (reflection), this now writes "authorization_servers": null, which is neither [] nor omitted. Adding [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] to the *Core properties would make the omission independent of the ambient options.
  • AuthorizationServers.Count throws if someone assigned null! to the public property, which today is just omitted. AuthorizationServers is { Count: > 0 } servers ? servers : null avoids that. The same applies to ScopesSupportedCore.

{
get => AuthorizationServers.Count > 0 ? AuthorizationServers : null;
set => AuthorizationServers = value ?? [];
}

/// <summary>
/// Gets or sets the supported bearer token methods.
/// </summary>
Expand All @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

§3.2 applies to every parameter, so resource_signing_alg_values_supported, authorization_details_types_supported and dpop_signing_alg_values_supported would also be emitted as [] if set to an empty list. They default to null, so this is much less likely than the two fixed here. Whether to cover them in this PR or a follow-up is open. With the current pattern each would need its own shadow property, which is one reason a single modifier may be easier to maintain.

{
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>
Expand Down
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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One behavior change that seems worth pinning with a test: {"authorization_servers": null} used to deserialize to a null list, which would have thrown in ClientOAuthProvider at availableAuthorizationServers.Count. With the setter's ?? [] it now becomes empty, which is an improvement. A test with explicit nulls, plus one serializing with options that lack WhenWritingNull, would cover the paths the current tests don't reach.

{
var metadata = JsonSerializer.Deserialize<ProtectedResourceMetadata>(
"""{"resource":"https://mcp.example.com"}""", McpJsonUtilities.DefaultOptions);

Assert.NotNull(metadata);
Assert.Empty(metadata.AuthorizationServers);
Assert.Empty(metadata.ScopesSupported);
}
}