Skip to content

Commit 03e3786

Browse files
authored
Write null user-defined properties by default (#2067)
1 parent f1546dd commit 03e3786

86 files changed

Lines changed: 556 additions & 12 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/concepts/tools/tools.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ directly, so a tool returning `72` produces `structuredContent: 72`, not
151151
`structuredContent: { "result": 72 }`. Clients should read the value according to the advertised
152152
output schema rather than assuming a `result` wrapper.
153153

154+
By default, tools serialize return values with <xref:ModelContextProtocol.McpJsonUtilities.DefaultOptions>,
155+
which writes `null` property values rather than omitting them. This keeps structured content consistent
156+
with generated output schemas that mark nullable properties as required, such as positional record
157+
parameters and `required` members. To omit `null` values, apply
158+
`[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]` to individual properties or supply
159+
<xref:ModelContextProtocol.Server.McpServerToolCreateOptions.SerializerOptions> with
160+
`DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull`. When doing so, make sure the output
161+
schema doesn't require the omitted properties.
162+
163+
MCP protocol types such as <xref:ModelContextProtocol.Protocol.Tool> still omit `null` optional
164+
properties, because they're annotated individually rather than relying on the serializer options.
165+
166+
```csharp
167+
public record Order(
168+
string Id,
169+
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] string? Note = null);
170+
```
171+
154172
#### Content annotations
155173

156174
Any content block can include <xref:ModelContextProtocol.Protocol.Annotations> to provide hints about the intended audience and priority:

‎src/ModelContextProtocol.Core/Authentication/AuthorizationServerMetadata.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,56 @@ internal sealed class AuthorizationServerMetadata
2323
/// The registration endpoint URI.
2424
/// </summary>
2525
[JsonPropertyName("registration_endpoint")]
26+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2627
public Uri? RegistrationEndpoint { get; set; }
2728

2829
/// <summary>
2930
/// The revocation endpoint URI.
3031
/// </summary>
3132
[JsonPropertyName("revocation_endpoint")]
33+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3234
public Uri? RevocationEndpoint { get; set; }
3335

3436
/// <summary>
3537
/// The response types supported by the authorization server.
3638
/// </summary>
3739
[JsonPropertyName("response_types_supported")]
40+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3841
public List<string>? ResponseTypesSupported { get; set; }
3942

4043
/// <summary>
4144
/// The grant types supported by the authorization server.
4245
/// </summary>
4346
[JsonPropertyName("grant_types_supported")]
47+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
4448
public List<string>? GrantTypesSupported { get; set; }
4549

4650
/// <summary>
4751
/// The token endpoint authentication methods supported by the authorization server.
4852
/// </summary>
4953
[JsonPropertyName("token_endpoint_auth_methods_supported")]
54+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
5055
public List<string>? TokenEndpointAuthMethodsSupported { get; set; }
5156

5257
/// <summary>
5358
/// The code challenge methods supported by the authorization server.
5459
/// </summary>
5560
[JsonPropertyName("code_challenge_methods_supported")]
61+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
5662
public List<string>? CodeChallengeMethodsSupported { get; set; }
5763

5864
/// <summary>
5965
/// The issuer URI of the authorization server.
6066
/// </summary>
6167
[JsonPropertyName("issuer")]
68+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
6269
public Uri? Issuer { get; set; }
6370

6471
/// <summary>
6572
/// The scopes supported by the authorization server.
6673
/// </summary>
6774
[JsonPropertyName("scopes_supported")]
75+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
6876
public List<string>? ScopesSupported { get; set; }
6977

7078
/// <summary>

‎src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationRequest.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,48 @@ internal sealed class DynamicClientRegistrationRequest
1717
/// Gets or sets the token endpoint authentication method.
1818
/// </summary>
1919
[JsonPropertyName("token_endpoint_auth_method")]
20+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2021
public string? TokenEndpointAuthMethod { get; init; }
2122

2223
/// <summary>
2324
/// Gets or sets the grant types that the client will use.
2425
/// </summary>
2526
[JsonPropertyName("grant_types")]
27+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2628
public string[]? GrantTypes { get; init; }
2729

2830
/// <summary>
2931
/// Gets or sets the response types that the client will use.
3032
/// </summary>
3133
[JsonPropertyName("response_types")]
34+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3235
public string[]? ResponseTypes { get; init; }
3336

3437
/// <summary>
3538
/// Gets or sets the human-readable name of the client.
3639
/// </summary>
3740
[JsonPropertyName("client_name")]
41+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3842
public string? ClientName { get; init; }
3943

4044
/// <summary>
4145
/// Gets or sets the URL of the client's home page.
4246
/// </summary>
4347
[JsonPropertyName("client_uri")]
48+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
4449
public string? ClientUri { get; init; }
4550

4651
/// <summary>
4752
/// Gets or sets the scope values that the client will use.
4853
/// </summary>
4954
[JsonPropertyName("scope")]
55+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
5056
public string? Scope { get; init; }
5157

5258
/// <summary>
5359
/// Gets or sets the OIDC application type ("native" or "web") for the client.
5460
/// </summary>
5561
[JsonPropertyName("application_type")]
62+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
5663
public string? ApplicationType { get; init; }
5764
}

‎src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationResponse.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,48 @@ public sealed class DynamicClientRegistrationResponse
1717
/// Gets or initializes the client secret.
1818
/// </summary>
1919
[JsonPropertyName("client_secret")]
20+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2021
public string? ClientSecret { get; init; }
2122

2223
/// <summary>
2324
/// Gets or initializes the redirect URIs for the client.
2425
/// </summary>
2526
[JsonPropertyName("redirect_uris")]
27+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2628
public IList<string>? RedirectUris { get; init; }
2729

2830
/// <summary>
2931
/// Gets or initializes the token endpoint authentication method.
3032
/// </summary>
3133
[JsonPropertyName("token_endpoint_auth_method")]
34+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3235
public string? TokenEndpointAuthMethod { get; init; }
3336

3437
/// <summary>
3538
/// Gets or initializes the grant types that the client will use.
3639
/// </summary>
3740
[JsonPropertyName("grant_types")]
41+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3842
public IList<string>? GrantTypes { get; init; }
3943

4044
/// <summary>
4145
/// Gets or initializes the response types that the client will use.
4246
/// </summary>
4347
[JsonPropertyName("response_types")]
48+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
4449
public IList<string>? ResponseTypes { get; init; }
4550

4651
/// <summary>
4752
/// Gets or initializes the timestamp at which the client ID was issued.
4853
/// </summary>
4954
[JsonPropertyName("client_id_issued_at")]
55+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
5056
public long? ClientIdIssuedAt { get; init; }
5157

5258
/// <summary>
5359
/// Gets or initializes the client secret expiration time.
5460
/// </summary>
5561
[JsonPropertyName("client_secret_expires_at")]
62+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
5663
public long? ClientSecretExpiresAt { get; init; }
5764
}

‎src/ModelContextProtocol.Core/Authentication/JagTokenExchangeResponse.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace ModelContextProtocol.Authentication;
24

35
/// <summary>
@@ -30,11 +32,13 @@ internal sealed class JagTokenExchangeResponse
3032
/// Gets or sets the scope of the issued token, if different from the request.
3133
/// </summary>
3234
[System.Text.Json.Serialization.JsonPropertyName("scope")]
35+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3336
public string? Scope { get; set; }
3437

3538
/// <summary>
3639
/// Gets or sets the lifetime in seconds of the issued token.
3740
/// </summary>
3841
[System.Text.Json.Serialization.JsonPropertyName("expires_in")]
42+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3943
public int? ExpiresIn { get; set; }
4044
}

‎src/ModelContextProtocol.Core/Authentication/JwtBearerAccessTokenResponse.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace ModelContextProtocol.Authentication;
24

35
/// <summary>
@@ -21,17 +23,20 @@ internal sealed class JwtBearerAccessTokenResponse
2123
/// Gets or sets the lifetime in seconds of the access token.
2224
/// </summary>
2325
[System.Text.Json.Serialization.JsonPropertyName("expires_in")]
26+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2427
public int? ExpiresIn { get; set; }
2528

2629
/// <summary>
2730
/// Gets or sets the refresh token.
2831
/// </summary>
2932
[System.Text.Json.Serialization.JsonPropertyName("refresh_token")]
33+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3034
public string? RefreshToken { get; set; }
3135

3236
/// <summary>
3337
/// Gets or sets the scope of the access token.
3438
/// </summary>
3539
[System.Text.Json.Serialization.JsonPropertyName("scope")]
40+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3641
public string? Scope { get; set; }
3742
}

‎src/ModelContextProtocol.Core/Authentication/OAuthErrorResponse.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace ModelContextProtocol.Authentication;
24

35
/// <summary>
@@ -10,17 +12,20 @@ internal sealed class OAuthErrorResponse
1012
/// Gets or sets the error code.
1113
/// </summary>
1214
[System.Text.Json.Serialization.JsonPropertyName("error")]
15+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1316
public string? Error { get; set; }
1417

1518
/// <summary>
1619
/// Gets or sets the human-readable error description.
1720
/// </summary>
1821
[System.Text.Json.Serialization.JsonPropertyName("error_description")]
22+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1923
public string? ErrorDescription { get; set; }
2024

2125
/// <summary>
2226
/// Gets or sets the URI identifying a human-readable web page with error information.
2327
/// </summary>
2428
[System.Text.Json.Serialization.JsonPropertyName("error_uri")]
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2530
public string? ErrorUri { get; set; }
2631
}

‎src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public sealed class ProtectedResourceMetadata
2525
/// </remarks>
2626
[JsonPropertyName("resource")]
2727
[StringSyntax(StringSyntaxAttribute.Uri)]
28+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2829
public string? Resource { get; set; }
2930

3031
/// <summary>
@@ -75,6 +76,7 @@ public sealed class ProtectedResourceMetadata
7576
/// </remarks>
7677
[JsonPropertyName("jwks_uri")]
7778
[StringSyntax(StringSyntaxAttribute.Uri)]
79+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7880
public string? JwksUri { get; set; }
7981

8082
/// <summary>
@@ -88,6 +90,7 @@ public sealed class ProtectedResourceMetadata
8890
/// OPTIONAL. No default algorithms are implied if this entry is omitted. The value "none" MUST NOT be used.
8991
/// </remarks>
9092
[JsonPropertyName("resource_signing_alg_values_supported")]
93+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
9194
public IList<string>? ResourceSigningAlgValuesSupported { get; set; }
9295

9396
/// <summary>
@@ -98,6 +101,7 @@ public sealed class ProtectedResourceMetadata
98101
/// The value of this field MAY be internationalized.
99102
/// </remarks>
100103
[JsonPropertyName("resource_name")]
104+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
101105
public string? ResourceName { get; set; }
102106

103107
/// <summary>
@@ -112,6 +116,7 @@ public sealed class ProtectedResourceMetadata
112116
/// </remarks>
113117
[JsonPropertyName("resource_documentation")]
114118
[StringSyntax(StringSyntaxAttribute.Uri)]
119+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
115120
public string? ResourceDocumentation { get; set; }
116121

117122
/// <summary>
@@ -125,6 +130,7 @@ public sealed class ProtectedResourceMetadata
125130
/// </remarks>
126131
[JsonPropertyName("resource_policy_uri")]
127132
[StringSyntax(StringSyntaxAttribute.Uri)]
133+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
128134
public string? ResourcePolicyUri { get; set; }
129135

130136
/// <summary>
@@ -135,6 +141,7 @@ public sealed class ProtectedResourceMetadata
135141
/// </remarks>
136142
[JsonPropertyName("resource_tos_uri")]
137143
[StringSyntax(StringSyntaxAttribute.Uri)]
144+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
138145
public string? ResourceTosUri { get; set; }
139146

140147
/// <summary>
@@ -147,6 +154,7 @@ public sealed class ProtectedResourceMetadata
147154
/// OPTIONAL.
148155
/// </remarks>
149156
[JsonPropertyName("tls_client_certificate_bound_access_tokens")]
157+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
150158
public bool? TlsClientCertificateBoundAccessTokens { get; set; }
151159

152160
/// <summary>
@@ -160,6 +168,7 @@ public sealed class ProtectedResourceMetadata
160168
/// OPTIONAL.
161169
/// </remarks>
162170
[JsonPropertyName("authorization_details_types_supported")]
171+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
163172
public IList<string>? AuthorizationDetailsTypesSupported { get; set; }
164173

165174
/// <summary>
@@ -173,6 +182,7 @@ public sealed class ProtectedResourceMetadata
173182
/// OPTIONAL.
174183
/// </remarks>
175184
[JsonPropertyName("dpop_signing_alg_values_supported")]
185+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
176186
public IList<string>? DpopSigningAlgValuesSupported { get; set; }
177187

178188
/// <summary>
@@ -185,6 +195,7 @@ public sealed class ProtectedResourceMetadata
185195
/// OPTIONAL.
186196
/// </remarks>
187197
[JsonPropertyName("dpop_bound_access_tokens_required")]
198+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
188199
public bool? DpopBoundAccessTokensRequired { get; set; }
189200

190201
/// <summary>

‎src/ModelContextProtocol.Core/Authentication/TokenResponse.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ internal sealed class TokenResponse
1717
/// Gets or sets the refresh token.
1818
/// </summary>
1919
[JsonPropertyName("refresh_token")]
20+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2021
public string? RefreshToken { get; set; }
2122

2223
/// <summary>
2324
/// Gets or sets the number of seconds until the access token expires.
2425
/// </summary>
2526
[JsonPropertyName("expires_in")]
27+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2628
public int? ExpiresIn { get; set; }
2729

2830
/// <summary>
@@ -35,5 +37,6 @@ internal sealed class TokenResponse
3537
/// Gets or sets the scope of the access token.
3638
/// </summary>
3739
[JsonPropertyName("scope")]
40+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3841
public string? Scope { get; set; }
3942
}

‎src/ModelContextProtocol.Core/McpJsonUtilities.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public static partial class McpJsonUtilities
2323
/// It additionally turns on the following settings:
2424
/// <list type="number">
2525
/// <item>Enables <see cref="JsonSerializerDefaults.Web"/> defaults.</item>
26-
/// <item>Enables <see cref="JsonIgnoreCondition.WhenWritingNull"/> as the default ignore condition for properties.</item>
2726
/// <item>Enables <see cref="JsonNumberHandling.AllowReadingFromString"/> as the default number handling for number types.</item>
2827
/// </list>
2928
/// </para>
@@ -134,7 +133,6 @@ internal static bool IsValidToolOutputSchema(JsonElement element) =>
134133
// case-sensitively: these contracts deserialize protocol messages, whose property names are defined
135134
// by the MCP and JSON-RPC specifications, and must agree with the JSON-RPC envelope converter.
136135
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web,
137-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
138136
NumberHandling = JsonNumberHandling.AllowReadingFromString,
139137
PropertyNameCaseInsensitive = false)]
140138

0 commit comments

Comments
 (0)