diff --git a/docs/concepts/tools/tools.md b/docs/concepts/tools/tools.md index 3a68d619b..73c6dd4d8 100644 --- a/docs/concepts/tools/tools.md +++ b/docs/concepts/tools/tools.md @@ -151,6 +151,24 @@ directly, so a tool returning `72` produces `structuredContent: 72`, not `structuredContent: { "result": 72 }`. Clients should read the value according to the advertised output schema rather than assuming a `result` wrapper. +By default, tools serialize return values with , +which writes `null` property values rather than omitting them. This keeps structured content consistent +with generated output schemas that mark nullable properties as required, such as positional record +parameters and `required` members. To omit `null` values, apply +`[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]` to individual properties or supply + with +`DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull`. When doing so, make sure the output +schema doesn't require the omitted properties. + +MCP protocol types such as still omit `null` optional +properties, because they're annotated individually rather than relying on the serializer options. + +```csharp +public record Order( + string Id, + [property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] string? Note = null); +``` + #### Content annotations Any content block can include to provide hints about the intended audience and priority: diff --git a/src/ModelContextProtocol.Core/Authentication/AuthorizationServerMetadata.cs b/src/ModelContextProtocol.Core/Authentication/AuthorizationServerMetadata.cs index d8d684a3b..0250eccfd 100644 --- a/src/ModelContextProtocol.Core/Authentication/AuthorizationServerMetadata.cs +++ b/src/ModelContextProtocol.Core/Authentication/AuthorizationServerMetadata.cs @@ -23,48 +23,56 @@ internal sealed class AuthorizationServerMetadata /// The registration endpoint URI. /// [JsonPropertyName("registration_endpoint")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public Uri? RegistrationEndpoint { get; set; } /// /// The revocation endpoint URI. /// [JsonPropertyName("revocation_endpoint")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public Uri? RevocationEndpoint { get; set; } /// /// The response types supported by the authorization server. /// [JsonPropertyName("response_types_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public List? ResponseTypesSupported { get; set; } /// /// The grant types supported by the authorization server. /// [JsonPropertyName("grant_types_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public List? GrantTypesSupported { get; set; } /// /// The token endpoint authentication methods supported by the authorization server. /// [JsonPropertyName("token_endpoint_auth_methods_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public List? TokenEndpointAuthMethodsSupported { get; set; } /// /// The code challenge methods supported by the authorization server. /// [JsonPropertyName("code_challenge_methods_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public List? CodeChallengeMethodsSupported { get; set; } /// /// The issuer URI of the authorization server. /// [JsonPropertyName("issuer")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public Uri? Issuer { get; set; } /// /// The scopes supported by the authorization server. /// [JsonPropertyName("scopes_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public List? ScopesSupported { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationRequest.cs b/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationRequest.cs index 6ad8bf2c1..85839a814 100644 --- a/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationRequest.cs +++ b/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationRequest.cs @@ -17,41 +17,48 @@ internal sealed class DynamicClientRegistrationRequest /// Gets or sets the token endpoint authentication method. /// [JsonPropertyName("token_endpoint_auth_method")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? TokenEndpointAuthMethod { get; init; } /// /// Gets or sets the grant types that the client will use. /// [JsonPropertyName("grant_types")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string[]? GrantTypes { get; init; } /// /// Gets or sets the response types that the client will use. /// [JsonPropertyName("response_types")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string[]? ResponseTypes { get; init; } /// /// Gets or sets the human-readable name of the client. /// [JsonPropertyName("client_name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ClientName { get; init; } /// /// Gets or sets the URL of the client's home page. /// [JsonPropertyName("client_uri")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ClientUri { get; init; } /// /// Gets or sets the scope values that the client will use. /// [JsonPropertyName("scope")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Scope { get; init; } /// /// Gets or sets the OIDC application type ("native" or "web") for the client. /// [JsonPropertyName("application_type")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ApplicationType { get; init; } } \ No newline at end of file diff --git a/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationResponse.cs b/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationResponse.cs index ecc2ddc0e..1ef52351a 100644 --- a/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationResponse.cs +++ b/src/ModelContextProtocol.Core/Authentication/DynamicClientRegistrationResponse.cs @@ -17,41 +17,48 @@ public sealed class DynamicClientRegistrationResponse /// Gets or initializes the client secret. /// [JsonPropertyName("client_secret")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ClientSecret { get; init; } /// /// Gets or initializes the redirect URIs for the client. /// [JsonPropertyName("redirect_uris")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? RedirectUris { get; init; } /// /// Gets or initializes the token endpoint authentication method. /// [JsonPropertyName("token_endpoint_auth_method")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? TokenEndpointAuthMethod { get; init; } /// /// Gets or initializes the grant types that the client will use. /// [JsonPropertyName("grant_types")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? GrantTypes { get; init; } /// /// Gets or initializes the response types that the client will use. /// [JsonPropertyName("response_types")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? ResponseTypes { get; init; } /// /// Gets or initializes the timestamp at which the client ID was issued. /// [JsonPropertyName("client_id_issued_at")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public long? ClientIdIssuedAt { get; init; } /// /// Gets or initializes the client secret expiration time. /// [JsonPropertyName("client_secret_expires_at")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public long? ClientSecretExpiresAt { get; init; } } diff --git a/src/ModelContextProtocol.Core/Authentication/JagTokenExchangeResponse.cs b/src/ModelContextProtocol.Core/Authentication/JagTokenExchangeResponse.cs index 35a08f646..6d0883638 100644 --- a/src/ModelContextProtocol.Core/Authentication/JagTokenExchangeResponse.cs +++ b/src/ModelContextProtocol.Core/Authentication/JagTokenExchangeResponse.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace ModelContextProtocol.Authentication; /// @@ -30,11 +32,13 @@ internal sealed class JagTokenExchangeResponse /// Gets or sets the scope of the issued token, if different from the request. /// [System.Text.Json.Serialization.JsonPropertyName("scope")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Scope { get; set; } /// /// Gets or sets the lifetime in seconds of the issued token. /// [System.Text.Json.Serialization.JsonPropertyName("expires_in")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? ExpiresIn { get; set; } } diff --git a/src/ModelContextProtocol.Core/Authentication/JwtBearerAccessTokenResponse.cs b/src/ModelContextProtocol.Core/Authentication/JwtBearerAccessTokenResponse.cs index 9a0a4004e..c5d857ed5 100644 --- a/src/ModelContextProtocol.Core/Authentication/JwtBearerAccessTokenResponse.cs +++ b/src/ModelContextProtocol.Core/Authentication/JwtBearerAccessTokenResponse.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace ModelContextProtocol.Authentication; /// @@ -21,17 +23,20 @@ internal sealed class JwtBearerAccessTokenResponse /// Gets or sets the lifetime in seconds of the access token. /// [System.Text.Json.Serialization.JsonPropertyName("expires_in")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? ExpiresIn { get; set; } /// /// Gets or sets the refresh token. /// [System.Text.Json.Serialization.JsonPropertyName("refresh_token")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? RefreshToken { get; set; } /// /// Gets or sets the scope of the access token. /// [System.Text.Json.Serialization.JsonPropertyName("scope")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Scope { get; set; } } diff --git a/src/ModelContextProtocol.Core/Authentication/OAuthErrorResponse.cs b/src/ModelContextProtocol.Core/Authentication/OAuthErrorResponse.cs index a8822fa32..1638bf570 100644 --- a/src/ModelContextProtocol.Core/Authentication/OAuthErrorResponse.cs +++ b/src/ModelContextProtocol.Core/Authentication/OAuthErrorResponse.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace ModelContextProtocol.Authentication; /// @@ -10,17 +12,20 @@ internal sealed class OAuthErrorResponse /// Gets or sets the error code. /// [System.Text.Json.Serialization.JsonPropertyName("error")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Error { get; set; } /// /// Gets or sets the human-readable error description. /// [System.Text.Json.Serialization.JsonPropertyName("error_description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ErrorDescription { get; set; } /// /// Gets or sets the URI identifying a human-readable web page with error information. /// [System.Text.Json.Serialization.JsonPropertyName("error_uri")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ErrorUri { get; set; } } diff --git a/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs b/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs index bed9e26d6..9536adf3c 100644 --- a/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs +++ b/src/ModelContextProtocol.Core/Authentication/ProtectedResourceMetadata.cs @@ -25,6 +25,7 @@ public sealed class ProtectedResourceMetadata /// [JsonPropertyName("resource")] [StringSyntax(StringSyntaxAttribute.Uri)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Resource { get; set; } /// @@ -75,6 +76,7 @@ public sealed class ProtectedResourceMetadata /// [JsonPropertyName("jwks_uri")] [StringSyntax(StringSyntaxAttribute.Uri)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? JwksUri { get; set; } /// @@ -88,6 +90,7 @@ public sealed class ProtectedResourceMetadata /// OPTIONAL. No default algorithms are implied if this entry is omitted. The value "none" MUST NOT be used. /// [JsonPropertyName("resource_signing_alg_values_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? ResourceSigningAlgValuesSupported { get; set; } /// @@ -98,6 +101,7 @@ public sealed class ProtectedResourceMetadata /// The value of this field MAY be internationalized. /// [JsonPropertyName("resource_name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ResourceName { get; set; } /// @@ -112,6 +116,7 @@ public sealed class ProtectedResourceMetadata /// [JsonPropertyName("resource_documentation")] [StringSyntax(StringSyntaxAttribute.Uri)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ResourceDocumentation { get; set; } /// @@ -125,6 +130,7 @@ public sealed class ProtectedResourceMetadata /// [JsonPropertyName("resource_policy_uri")] [StringSyntax(StringSyntaxAttribute.Uri)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ResourcePolicyUri { get; set; } /// @@ -135,6 +141,7 @@ public sealed class ProtectedResourceMetadata /// [JsonPropertyName("resource_tos_uri")] [StringSyntax(StringSyntaxAttribute.Uri)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ResourceTosUri { get; set; } /// @@ -147,6 +154,7 @@ public sealed class ProtectedResourceMetadata /// OPTIONAL. /// [JsonPropertyName("tls_client_certificate_bound_access_tokens")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? TlsClientCertificateBoundAccessTokens { get; set; } /// @@ -160,6 +168,7 @@ public sealed class ProtectedResourceMetadata /// OPTIONAL. /// [JsonPropertyName("authorization_details_types_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? AuthorizationDetailsTypesSupported { get; set; } /// @@ -173,6 +182,7 @@ public sealed class ProtectedResourceMetadata /// OPTIONAL. /// [JsonPropertyName("dpop_signing_alg_values_supported")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? DpopSigningAlgValuesSupported { get; set; } /// @@ -185,6 +195,7 @@ public sealed class ProtectedResourceMetadata /// OPTIONAL. /// [JsonPropertyName("dpop_bound_access_tokens_required")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? DpopBoundAccessTokensRequired { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Authentication/TokenResponse.cs b/src/ModelContextProtocol.Core/Authentication/TokenResponse.cs index 721196734..be00dd477 100644 --- a/src/ModelContextProtocol.Core/Authentication/TokenResponse.cs +++ b/src/ModelContextProtocol.Core/Authentication/TokenResponse.cs @@ -17,12 +17,14 @@ internal sealed class TokenResponse /// Gets or sets the refresh token. /// [JsonPropertyName("refresh_token")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? RefreshToken { get; set; } /// /// Gets or sets the number of seconds until the access token expires. /// [JsonPropertyName("expires_in")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? ExpiresIn { get; set; } /// @@ -35,5 +37,6 @@ internal sealed class TokenResponse /// Gets or sets the scope of the access token. /// [JsonPropertyName("scope")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Scope { get; set; } } diff --git a/src/ModelContextProtocol.Core/McpJsonUtilities.cs b/src/ModelContextProtocol.Core/McpJsonUtilities.cs index 13eaf8a99..9df3e8e62 100644 --- a/src/ModelContextProtocol.Core/McpJsonUtilities.cs +++ b/src/ModelContextProtocol.Core/McpJsonUtilities.cs @@ -23,7 +23,6 @@ public static partial class McpJsonUtilities /// It additionally turns on the following settings: /// /// Enables defaults. - /// Enables as the default ignore condition for properties. /// Enables as the default number handling for number types. /// /// @@ -134,7 +133,6 @@ internal static bool IsValidToolOutputSchema(JsonElement element) => // case-sensitively: these contracts deserialize protocol messages, whose property names are defined // by the MCP and JSON-RPC specifications, and must agree with the JSON-RPC envelope converter. [JsonSourceGenerationOptions(JsonSerializerDefaults.Web, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, NumberHandling = JsonNumberHandling.AllowReadingFromString, PropertyNameCaseInsensitive = false)] diff --git a/src/ModelContextProtocol.Core/Protocol/Annotations.cs b/src/ModelContextProtocol.Core/Protocol/Annotations.cs index e2e8cfe83..8c7519fa1 100644 --- a/src/ModelContextProtocol.Core/Protocol/Annotations.cs +++ b/src/ModelContextProtocol.Core/Protocol/Annotations.cs @@ -15,6 +15,7 @@ public sealed class Annotations /// Gets or sets the intended audience for this content as an array of values. /// [JsonPropertyName("audience")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Audience { get; set; } /// @@ -25,6 +26,7 @@ public sealed class Annotations /// and 1 represents the highest priority. /// [JsonPropertyName("priority")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public float? Priority { get; set; } /// @@ -35,5 +37,6 @@ public sealed class Annotations /// Examples of when the resource was last modified include last activity in an open file or when the resource was attached. /// [JsonPropertyName("lastModified")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public DateTimeOffset? LastModified { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/CallToolRequestParams.cs b/src/ModelContextProtocol.Core/Protocol/CallToolRequestParams.cs index d311c6b4f..1c49514ec 100644 --- a/src/ModelContextProtocol.Core/Protocol/CallToolRequestParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/CallToolRequestParams.cs @@ -25,5 +25,6 @@ public sealed class CallToolRequestParams : RequestParams /// a parameter name and its corresponding argument value. /// [JsonPropertyName("arguments")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Arguments { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/CallToolResult.cs b/src/ModelContextProtocol.Core/Protocol/CallToolResult.cs index b2fdb3d05..61ceb0110 100644 --- a/src/ModelContextProtocol.Core/Protocol/CallToolResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/CallToolResult.cs @@ -41,6 +41,7 @@ public sealed class CallToolResult : Result /// Gets or sets an optional JSON object representing the structured result of the tool call. /// [JsonPropertyName("structuredContent")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonElement? StructuredContent { get; set; } /// @@ -63,5 +64,6 @@ public sealed class CallToolResult : Result /// /// [JsonPropertyName("isError")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? IsError { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/CancelledNotificationParams.cs b/src/ModelContextProtocol.Core/Protocol/CancelledNotificationParams.cs index bf09cc59b..9c195d725 100644 --- a/src/ModelContextProtocol.Core/Protocol/CancelledNotificationParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/CancelledNotificationParams.cs @@ -26,5 +26,6 @@ public sealed class CancelledNotificationParams : NotificationParams /// Gets or sets an optional string describing the reason for the cancellation request. /// [JsonPropertyName("reason")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Reason { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ClientCapabilities.cs b/src/ModelContextProtocol.Core/Protocol/ClientCapabilities.cs index 2828602fc..540139693 100644 --- a/src/ModelContextProtocol.Core/Protocol/ClientCapabilities.cs +++ b/src/ModelContextProtocol.Core/Protocol/ClientCapabilities.cs @@ -34,6 +34,7 @@ public sealed class ClientCapabilities /// /// [JsonPropertyName("experimental")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Experimental { get; set; } /// @@ -51,6 +52,7 @@ public sealed class ClientCapabilities /// [JsonPropertyName("roots")] [Obsolete(Obsoletions.DeprecatedRoots_Message, DiagnosticId = Obsoletions.Deprecated_DiagnosticId, UrlFormat = Obsoletions.Deprecated_Url)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public RootsCapability? Roots { get; set; } /// @@ -59,6 +61,7 @@ public sealed class ClientCapabilities /// [JsonPropertyName("sampling")] [Obsolete(Obsoletions.DeprecatedSampling_Message, DiagnosticId = Obsoletions.Deprecated_DiagnosticId, UrlFormat = Obsoletions.Deprecated_Url)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public SamplingCapability? Sampling { get; set; } /// @@ -66,6 +69,7 @@ public sealed class ClientCapabilities /// supports elicitation of additional information from the user on behalf of the server. /// [JsonPropertyName("elicitation")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ElicitationCapability? Elicitation { get; set; } /// @@ -83,5 +87,6 @@ public sealed class ClientCapabilities /// /// [JsonPropertyName("extensions")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Extensions { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/CompleteContext.cs b/src/ModelContextProtocol.Core/Protocol/CompleteContext.cs index 76ea765fd..3bf985291 100644 --- a/src/ModelContextProtocol.Core/Protocol/CompleteContext.cs +++ b/src/ModelContextProtocol.Core/Protocol/CompleteContext.cs @@ -15,5 +15,6 @@ public sealed class CompleteContext /// Gets or sets previously-resolved variables in a URI template or prompt. /// [JsonPropertyName("arguments")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Arguments { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/CompleteRequestParams.cs b/src/ModelContextProtocol.Core/Protocol/CompleteRequestParams.cs index 806cf7935..df78437a4 100644 --- a/src/ModelContextProtocol.Core/Protocol/CompleteRequestParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/CompleteRequestParams.cs @@ -36,5 +36,6 @@ public sealed class CompleteRequestParams : RequestParams /// Gets or sets additional, optional context for completions. /// [JsonPropertyName("context")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CompleteContext? Context { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/Completion.cs b/src/ModelContextProtocol.Core/Protocol/Completion.cs index 2cb7eaf04..823ff2a3b 100644 --- a/src/ModelContextProtocol.Core/Protocol/Completion.cs +++ b/src/ModelContextProtocol.Core/Protocol/Completion.cs @@ -28,6 +28,7 @@ public sealed class Completion /// This value can exceed the number of values actually sent in the response. /// [JsonPropertyName("total")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? Total { get; set; } /// @@ -35,5 +36,6 @@ public sealed class Completion /// those provided in the current response, even if the exact total is unknown. /// [JsonPropertyName("hasMore")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? HasMore { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ContentBlock.cs b/src/ModelContextProtocol.Core/Protocol/ContentBlock.cs index 7a19ec7a0..073ff24c1 100644 --- a/src/ModelContextProtocol.Core/Protocol/ContentBlock.cs +++ b/src/ModelContextProtocol.Core/Protocol/ContentBlock.cs @@ -64,6 +64,7 @@ private protected ContentBlock() /// and the priority level of the content. Clients can use this information to filter or prioritize content for different roles. /// [JsonPropertyName("annotations")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public Annotations? Annotations { get; set; } /// @@ -73,6 +74,7 @@ private protected ContentBlock() /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } /// @@ -740,6 +742,7 @@ public sealed class ResourceLinkBlock : ContentBlock /// If not provided, can be used for display. /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -759,6 +762,7 @@ public sealed class ResourceLinkBlock : ContentBlock /// /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -775,6 +779,7 @@ public sealed class ResourceLinkBlock : ContentBlock /// /// [JsonPropertyName("mimeType")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? MimeType { get; set; } /// @@ -784,6 +789,7 @@ public sealed class ResourceLinkBlock : ContentBlock /// This value can be used by applications to display file sizes and estimate context window usage. /// [JsonPropertyName("size")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public long? Size { get; set; } /// @@ -793,6 +799,7 @@ public sealed class ResourceLinkBlock : ContentBlock /// This can be used by clients to display the resource's icon in a user interface. /// [JsonPropertyName("icons")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Icons { get; set; } } @@ -864,6 +871,7 @@ public sealed class ToolResultContentBlock : ContentBlock /// If the tool defined an outputSchema, this object should conform to that schema. /// [JsonPropertyName("structuredContent")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonElement? StructuredContent { get; set; } /// @@ -876,6 +884,7 @@ public sealed class ToolResultContentBlock : ContentBlock /// If , the content typically describes the error that occurred. /// [JsonPropertyName("isError")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? IsError { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] diff --git a/src/ModelContextProtocol.Core/Protocol/CreateMessageRequestParams.cs b/src/ModelContextProtocol.Core/Protocol/CreateMessageRequestParams.cs index 7a4f32984..685df69a3 100644 --- a/src/ModelContextProtocol.Core/Protocol/CreateMessageRequestParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/CreateMessageRequestParams.cs @@ -31,6 +31,7 @@ public sealed class CreateMessageRequestParams : RequestParams /// /// [JsonPropertyName("includeContext")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ContextInclusion? IncludeContext { get; set; } /// @@ -71,6 +72,7 @@ public sealed class CreateMessageRequestParams : RequestParams /// /// [JsonPropertyName("metadata")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Metadata { get; set; } /// @@ -91,6 +93,7 @@ public sealed class CreateMessageRequestParams : RequestParams /// /// [JsonPropertyName("modelPreferences")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ModelPreferences? ModelPreferences { get; set; } /// @@ -112,6 +115,7 @@ public sealed class CreateMessageRequestParams : RequestParams /// /// [JsonPropertyName("stopSequences")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? StopSequences { get; set; } /// @@ -121,6 +125,7 @@ public sealed class CreateMessageRequestParams : RequestParams /// The client might modify or omit this prompt. /// [JsonPropertyName("systemPrompt")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? SystemPrompt { get; set; } /// @@ -136,6 +141,7 @@ public sealed class CreateMessageRequestParams : RequestParams /// /// [JsonPropertyName("temperature")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public float? Temperature { get; set; } /// @@ -146,6 +152,7 @@ public sealed class CreateMessageRequestParams : RequestParams /// They do not need to correspond to tools registered on the server via . /// [JsonPropertyName("tools")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Tools { get; set; } /// @@ -155,5 +162,6 @@ public sealed class CreateMessageRequestParams : RequestParams /// This controls whether and how the model uses the request-scoped during sampling. /// [JsonPropertyName("toolChoice")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ToolChoice? ToolChoice { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/CreateMessageResult.cs b/src/ModelContextProtocol.Core/Protocol/CreateMessageResult.cs index 7568b4552..240587345 100644 --- a/src/ModelContextProtocol.Core/Protocol/CreateMessageResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/CreateMessageResult.cs @@ -52,6 +52,7 @@ public sealed class CreateMessageResult : Result /// This field is an open string to allow for provider-specific stop reasons. /// [JsonPropertyName("stopReason")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? StopReason { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/DiscoverResult.cs b/src/ModelContextProtocol.Core/Protocol/DiscoverResult.cs index b178a3a98..c717824ed 100644 --- a/src/ModelContextProtocol.Core/Protocol/DiscoverResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/DiscoverResult.cs @@ -39,6 +39,7 @@ public sealed class DiscoverResult : Result, ICacheableResult /// for example by including it in a system prompt. /// [JsonPropertyName("instructions")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Instructions { get; set; } /// @@ -51,6 +52,7 @@ public sealed class DiscoverResult : Result, ICacheableResult /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// @@ -62,5 +64,6 @@ public sealed class DiscoverResult : Result, ICacheableResult /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ElicitRequestParams.cs b/src/ModelContextProtocol.Core/Protocol/ElicitRequestParams.cs index 9dc1ac903..2069c6fce 100644 --- a/src/ModelContextProtocol.Core/Protocol/ElicitRequestParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/ElicitRequestParams.cs @@ -49,6 +49,7 @@ public string Mode /// /// [JsonPropertyName("elicitationId")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ElicitationId { get; set; } /// @@ -65,6 +66,7 @@ public string Mode /// /// [JsonPropertyName("url")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Url { get; set; } /// @@ -90,6 +92,7 @@ public string Mode /// and (deprecated). /// [JsonPropertyName("requestedSchema")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public RequestSchema? RequestedSchema { get; set; } /// Represents a request schema used in a form mode elicitation request. @@ -115,6 +118,7 @@ public IDictionary Properties /// Gets or sets the required properties of the schema. [JsonPropertyName("required")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Required { get; set; } } @@ -155,10 +159,12 @@ protected private PrimitiveSchemaDefinition() /// Gets or sets a title for the schema. [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// Gets or sets a description for the schema. [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -711,6 +717,7 @@ public override string Type /// Gets or sets the minimum length for the string. [JsonPropertyName("minLength")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? MinLength { get => field; @@ -727,6 +734,7 @@ public int? MinLength /// Gets or sets the maximum length for the string. [JsonPropertyName("maxLength")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? MaxLength { get => field; @@ -743,6 +751,7 @@ public int? MaxLength /// Gets or sets a specific format for the string ("email", "uri", "date", or "date-time"). [JsonPropertyName("format")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Format { get => field; @@ -759,6 +768,7 @@ public string? Format /// Gets or sets the default value for the string. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Default { get; set; } } @@ -783,14 +793,17 @@ public override string Type /// Gets or sets the minimum allowed value. [JsonPropertyName("minimum")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public double? Minimum { get; set; } /// Gets or sets the maximum allowed value. [JsonPropertyName("maximum")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public double? Maximum { get; set; } /// Gets or sets the default value for the number. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public double? Default { get; set; } } @@ -813,6 +826,7 @@ public override string Type /// Gets or sets the default value for the Boolean. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? Default { get; set; } } @@ -850,6 +864,7 @@ public IList Enum /// Gets or sets the default value for the enum. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Default { get; set; } } @@ -901,6 +916,7 @@ public IList OneOf /// Gets or sets the default value for the enum. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Default { get; set; } } @@ -949,10 +965,12 @@ public override string Type /// Gets or sets the minimum number of items that can be selected. [JsonPropertyName("minItems")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? MinItems { get; set; } /// Gets or sets the maximum number of items that can be selected. [JsonPropertyName("maxItems")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? MaxItems { get; set; } /// Gets or sets the schema for items in the array. @@ -961,6 +979,7 @@ public override string Type /// Gets or sets the default values for the enum. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Default { get; set; } } @@ -985,10 +1004,12 @@ public override string Type /// Gets or sets the minimum number of items that can be selected. [JsonPropertyName("minItems")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? MinItems { get; set; } /// Gets or sets the maximum number of items that can be selected. [JsonPropertyName("maxItems")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public int? MaxItems { get; set; } /// Gets or sets the schema for items in the array. @@ -997,6 +1018,7 @@ public override string Type /// Gets or sets the default values for the enum. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Default { get; set; } } @@ -1038,10 +1060,12 @@ public IList Enum /// Gets or sets optional display names corresponding to the enum values. [JsonPropertyName("enumNames")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? EnumNames { get; set; } /// Gets or sets the default value for the enum. [JsonPropertyName("default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Default { get; set; } } } diff --git a/src/ModelContextProtocol.Core/Protocol/ElicitResult.cs b/src/ModelContextProtocol.Core/Protocol/ElicitResult.cs index cf0dcd946..e932342fa 100644 --- a/src/ModelContextProtocol.Core/Protocol/ElicitResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/ElicitResult.cs @@ -55,6 +55,7 @@ public sealed class ElicitResult : Result /// /// [JsonPropertyName("content")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Content { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/GetPromptRequestParams.cs b/src/ModelContextProtocol.Core/Protocol/GetPromptRequestParams.cs index 04ea63ea2..eae576062 100644 --- a/src/ModelContextProtocol.Core/Protocol/GetPromptRequestParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/GetPromptRequestParams.cs @@ -27,5 +27,6 @@ public sealed class GetPromptRequestParams : RequestParams /// choose to use these arguments in any way it deems appropriate to generate the prompt. /// [JsonPropertyName("arguments")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Arguments { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/GetPromptResult.cs b/src/ModelContextProtocol.Core/Protocol/GetPromptResult.cs index f78c5a326..adc709ce5 100644 --- a/src/ModelContextProtocol.Core/Protocol/GetPromptResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/GetPromptResult.cs @@ -32,6 +32,7 @@ public sealed class GetPromptResult : Result /// /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/Icon.cs b/src/ModelContextProtocol.Core/Protocol/Icon.cs index e187f191a..19fdf75d2 100644 --- a/src/ModelContextProtocol.Core/Protocol/Icon.cs +++ b/src/ModelContextProtocol.Core/Protocol/Icon.cs @@ -58,6 +58,7 @@ public sealed class Icon /// Common values include "image/png", "image/jpeg", "image/svg+xml", and "image/webp". /// [JsonPropertyName("mimeType")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? MimeType { get; set; } /// @@ -73,6 +74,7 @@ public sealed class Icon /// /// [JsonPropertyName("sizes")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Sizes { get; set; } /// @@ -84,6 +86,7 @@ public sealed class Icon /// If not provided, clients should assume the icon can be used with any theme. /// [JsonPropertyName("theme")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Theme { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] diff --git a/src/ModelContextProtocol.Core/Protocol/Implementation.cs b/src/ModelContextProtocol.Core/Protocol/Implementation.cs index 3998fe1f5..00f399082 100644 --- a/src/ModelContextProtocol.Core/Protocol/Implementation.cs +++ b/src/ModelContextProtocol.Core/Protocol/Implementation.cs @@ -25,6 +25,7 @@ public sealed class Implementation : IBaseMetadata /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -51,6 +52,7 @@ public sealed class Implementation : IBaseMetadata /// /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -60,6 +62,7 @@ public sealed class Implementation : IBaseMetadata /// This value can be used by clients to display the implementation's icon in a user interface. /// [JsonPropertyName("icons")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Icons { get; set; } /// @@ -75,5 +78,6 @@ public sealed class Implementation : IBaseMetadata /// /// [JsonPropertyName("websiteUrl")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? WebsiteUrl { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/InitializeResult.cs b/src/ModelContextProtocol.Core/Protocol/InitializeResult.cs index 4c0f014f0..18d1447d1 100644 --- a/src/ModelContextProtocol.Core/Protocol/InitializeResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/InitializeResult.cs @@ -76,5 +76,6 @@ public sealed class InitializeResult : Result /// /// [JsonPropertyName("instructions")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Instructions { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/InputRequiredResult.cs b/src/ModelContextProtocol.Core/Protocol/InputRequiredResult.cs index e82c3ec4d..c53003daa 100644 --- a/src/ModelContextProtocol.Core/Protocol/InputRequiredResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/InputRequiredResult.cs @@ -41,6 +41,7 @@ public InputRequiredResult() /// /// [JsonPropertyName("inputRequests")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? InputRequests { get; set; } /// @@ -59,5 +60,6 @@ public InputRequiredResult() /// /// [JsonPropertyName("requestState")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? RequestState { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/JsonRpcErrorDetail.cs b/src/ModelContextProtocol.Core/Protocol/JsonRpcErrorDetail.cs index 31f5dbe87..59e92e683 100644 --- a/src/ModelContextProtocol.Core/Protocol/JsonRpcErrorDetail.cs +++ b/src/ModelContextProtocol.Core/Protocol/JsonRpcErrorDetail.cs @@ -40,5 +40,6 @@ public sealed class JsonRpcErrorDetail /// the error condition. /// [JsonPropertyName("data")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public object? Data { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/JsonRpcMessageContext.cs b/src/ModelContextProtocol.Core/Protocol/JsonRpcMessageContext.cs index 676c89d1c..4a2a63405 100644 --- a/src/ModelContextProtocol.Core/Protocol/JsonRpcMessageContext.cs +++ b/src/ModelContextProtocol.Core/Protocol/JsonRpcMessageContext.cs @@ -1,7 +1,6 @@ using ModelContextProtocol.Server; using System.Diagnostics.CodeAnalysis; using System.Security.Claims; -using System.Text.Json.Serialization; namespace ModelContextProtocol.Protocol; @@ -84,7 +83,6 @@ public sealed class JsonRpcMessageContext /// extension methods to identify the named resource targeted by a request. /// [Experimental(Experimentals.Extensibility_DiagnosticId, UrlFormat = Experimentals.Extensibility_Url)] - [JsonIgnore] public string? RoutingName { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/JsonRpcNotification.cs b/src/ModelContextProtocol.Core/Protocol/JsonRpcNotification.cs index 4ff77007d..a3f0570a1 100644 --- a/src/ModelContextProtocol.Core/Protocol/JsonRpcNotification.cs +++ b/src/ModelContextProtocol.Core/Protocol/JsonRpcNotification.cs @@ -23,5 +23,6 @@ public sealed class JsonRpcNotification : JsonRpcMessage /// Gets or sets optional parameters for the notification. /// [JsonPropertyName("params")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonNode? Params { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/JsonRpcRequest.cs b/src/ModelContextProtocol.Core/Protocol/JsonRpcRequest.cs index fd982626c..cf9a0001f 100644 --- a/src/ModelContextProtocol.Core/Protocol/JsonRpcRequest.cs +++ b/src/ModelContextProtocol.Core/Protocol/JsonRpcRequest.cs @@ -26,6 +26,7 @@ public sealed class JsonRpcRequest : JsonRpcMessageWithId /// Gets or sets optional parameters for the method. /// [JsonPropertyName("params")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonNode? Params { get; set; } internal JsonRpcRequest WithId(RequestId id) diff --git a/src/ModelContextProtocol.Core/Protocol/JsonRpcResponse.cs b/src/ModelContextProtocol.Core/Protocol/JsonRpcResponse.cs index 9111244cc..316c2cbc3 100644 --- a/src/ModelContextProtocol.Core/Protocol/JsonRpcResponse.cs +++ b/src/ModelContextProtocol.Core/Protocol/JsonRpcResponse.cs @@ -25,5 +25,6 @@ public sealed class JsonRpcResponse : JsonRpcMessageWithId /// This property contains the result data returned by the server in response to the JSON-RPC method request. /// [JsonPropertyName("result")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public required JsonNode? Result { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ListPromptsResult.cs b/src/ModelContextProtocol.Core/Protocol/ListPromptsResult.cs index a7f26b521..0991d5efe 100644 --- a/src/ModelContextProtocol.Core/Protocol/ListPromptsResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/ListPromptsResult.cs @@ -29,10 +29,12 @@ public sealed class ListPromptsResult : PaginatedResult, ICacheableResult /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ListResourceTemplatesResult.cs b/src/ModelContextProtocol.Core/Protocol/ListResourceTemplatesResult.cs index 988d6f186..9f1f023b9 100644 --- a/src/ModelContextProtocol.Core/Protocol/ListResourceTemplatesResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/ListResourceTemplatesResult.cs @@ -36,10 +36,12 @@ public sealed class ListResourceTemplatesResult : PaginatedResult, ICacheableRes /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } \ No newline at end of file diff --git a/src/ModelContextProtocol.Core/Protocol/ListResourcesResult.cs b/src/ModelContextProtocol.Core/Protocol/ListResourcesResult.cs index 54c1df601..1438e0d32 100644 --- a/src/ModelContextProtocol.Core/Protocol/ListResourcesResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/ListResourcesResult.cs @@ -29,10 +29,12 @@ public sealed class ListResourcesResult : PaginatedResult, ICacheableResult /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ListToolsResult.cs b/src/ModelContextProtocol.Core/Protocol/ListToolsResult.cs index 55eed5ddb..f42c6e26e 100644 --- a/src/ModelContextProtocol.Core/Protocol/ListToolsResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/ListToolsResult.cs @@ -29,10 +29,12 @@ public sealed class ListToolsResult : PaginatedResult, ICacheableResult /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/LoggingMessageNotificationParams.cs b/src/ModelContextProtocol.Core/Protocol/LoggingMessageNotificationParams.cs index 0840c9c7c..8addfedb6 100644 --- a/src/ModelContextProtocol.Core/Protocol/LoggingMessageNotificationParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/LoggingMessageNotificationParams.cs @@ -43,6 +43,7 @@ public sealed class LoggingMessageNotificationParams : NotificationParams /// /// [JsonPropertyName("logger")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Logger { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/ModelHint.cs b/src/ModelContextProtocol.Core/Protocol/ModelHint.cs index 9be4c755f..a98f1acb0 100644 --- a/src/ModelContextProtocol.Core/Protocol/ModelHint.cs +++ b/src/ModelContextProtocol.Core/Protocol/ModelHint.cs @@ -28,5 +28,6 @@ public sealed class ModelHint /// selection based on these preferences and their available models. /// [JsonPropertyName("name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Name { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ModelPreferences.cs b/src/ModelContextProtocol.Core/Protocol/ModelPreferences.cs index f20b33956..027118d6c 100644 --- a/src/ModelContextProtocol.Core/Protocol/ModelPreferences.cs +++ b/src/ModelContextProtocol.Core/Protocol/ModelPreferences.cs @@ -34,12 +34,14 @@ public sealed class ModelPreferences /// A value of 0 means cost is not important, while a value of 1 means cost is the most important factor. /// [JsonPropertyName("costPriority")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public float? CostPriority { get; set; } /// /// Gets or sets optional hints to use for model selection. /// [JsonPropertyName("hints")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Hints { get; set; } /// @@ -49,6 +51,7 @@ public sealed class ModelPreferences /// A value of 0 means speed is not important, while a value of 1 means speed is the most important factor. /// [JsonPropertyName("speedPriority")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public float? SpeedPriority { get; set; } /// @@ -58,5 +61,6 @@ public sealed class ModelPreferences /// A value of 0 means intelligence is not important, while a value of 1 means intelligence is the most important factor. /// [JsonPropertyName("intelligencePriority")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public float? IntelligencePriority { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/NotificationParams.cs b/src/ModelContextProtocol.Core/Protocol/NotificationParams.cs index b13a746cd..5aa0137f2 100644 --- a/src/ModelContextProtocol.Core/Protocol/NotificationParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/NotificationParams.cs @@ -20,5 +20,6 @@ protected NotificationParams() /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } } \ No newline at end of file diff --git a/src/ModelContextProtocol.Core/Protocol/PaginatedRequest.cs b/src/ModelContextProtocol.Core/Protocol/PaginatedRequest.cs index 0713feb84..3338eaf14 100644 --- a/src/ModelContextProtocol.Core/Protocol/PaginatedRequest.cs +++ b/src/ModelContextProtocol.Core/Protocol/PaginatedRequest.cs @@ -30,5 +30,6 @@ protected PaginatedRequestParams() /// property of a previous request's response. /// [JsonPropertyName("cursor")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Cursor { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/PaginatedResult.cs b/src/ModelContextProtocol.Core/Protocol/PaginatedResult.cs index e496e0921..d0c1520eb 100644 --- a/src/ModelContextProtocol.Core/Protocol/PaginatedResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/PaginatedResult.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace ModelContextProtocol.Protocol; /// @@ -37,5 +39,6 @@ protected PaginatedResult() /// to fetch the next page. When there are no more results to return, the property /// will be . /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? NextCursor { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/Prompt.cs b/src/ModelContextProtocol.Core/Protocol/Prompt.cs index bc4624f1d..a5f0d2018 100644 --- a/src/ModelContextProtocol.Core/Protocol/Prompt.cs +++ b/src/ModelContextProtocol.Core/Protocol/Prompt.cs @@ -19,6 +19,7 @@ public sealed class Prompt : IBaseMetadata /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -35,6 +36,7 @@ public sealed class Prompt : IBaseMetadata /// /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -51,6 +53,7 @@ public sealed class Prompt : IBaseMetadata /// /// [JsonPropertyName("arguments")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Arguments { get; set; } /// @@ -60,6 +63,7 @@ public sealed class Prompt : IBaseMetadata /// This list can be used by clients to display the prompt's icon in a user interface. /// [JsonPropertyName("icons")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Icons { get; set; } /// @@ -69,6 +73,7 @@ public sealed class Prompt : IBaseMetadata /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] diff --git a/src/ModelContextProtocol.Core/Protocol/PromptArgument.cs b/src/ModelContextProtocol.Core/Protocol/PromptArgument.cs index a352ae017..7aa1494cf 100644 --- a/src/ModelContextProtocol.Core/Protocol/PromptArgument.cs +++ b/src/ModelContextProtocol.Core/Protocol/PromptArgument.cs @@ -23,6 +23,7 @@ public sealed class PromptArgument : IBaseMetadata /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -33,6 +34,7 @@ public sealed class PromptArgument : IBaseMetadata /// for this argument and how it will affect the generated prompt. /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -43,5 +45,6 @@ public sealed class PromptArgument : IBaseMetadata /// If a required argument is missing, the server should respond with an error. /// [JsonPropertyName("required")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? Required { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/PromptsCapability.cs b/src/ModelContextProtocol.Core/Protocol/PromptsCapability.cs index 4a830c48e..11eda0cf0 100644 --- a/src/ModelContextProtocol.Core/Protocol/PromptsCapability.cs +++ b/src/ModelContextProtocol.Core/Protocol/PromptsCapability.cs @@ -30,5 +30,6 @@ public sealed class PromptsCapability /// to available prompts. /// [JsonPropertyName("listChanged")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? ListChanged { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ReadResourceResult.cs b/src/ModelContextProtocol.Core/Protocol/ReadResourceResult.cs index 53e138806..ea4477e2e 100644 --- a/src/ModelContextProtocol.Core/Protocol/ReadResourceResult.cs +++ b/src/ModelContextProtocol.Core/Protocol/ReadResourceResult.cs @@ -24,10 +24,12 @@ public sealed class ReadResourceResult : Result, ICacheableResult /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/Reference.cs b/src/ModelContextProtocol.Core/Protocol/Reference.cs index b18e67641..5ae407afa 100644 --- a/src/ModelContextProtocol.Core/Protocol/Reference.cs +++ b/src/ModelContextProtocol.Core/Protocol/Reference.cs @@ -169,6 +169,7 @@ public sealed class PromptReference : Reference, IBaseMetadata /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -188,6 +189,7 @@ public sealed class ResourceTemplateReference : Reference /// [JsonPropertyName("uri")] [StringSyntax(StringSyntaxAttribute.Uri)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public required string? Uri { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/RequestParams.cs b/src/ModelContextProtocol.Core/Protocol/RequestParams.cs index 04c3d82ef..fd5bded07 100644 --- a/src/ModelContextProtocol.Core/Protocol/RequestParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/RequestParams.cs @@ -23,6 +23,7 @@ protected RequestParams() /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } /// @@ -36,6 +37,7 @@ protected RequestParams() /// /// [JsonPropertyName("inputResponses")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? InputResponses { get; set; } /// @@ -49,6 +51,7 @@ protected RequestParams() /// /// [JsonPropertyName("requestState")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? RequestState { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/RequestParamsMetadata.cs b/src/ModelContextProtocol.Core/Protocol/RequestParamsMetadata.cs index 19400d394..c586d47a2 100644 --- a/src/ModelContextProtocol.Core/Protocol/RequestParamsMetadata.cs +++ b/src/ModelContextProtocol.Core/Protocol/RequestParamsMetadata.cs @@ -18,5 +18,6 @@ public sealed class RequestParamsMetadata /// The receiver is not obligated to provide these notifications. /// [JsonPropertyName("progressToken")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ProgressToken? ProgressToken { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/Resource.cs b/src/ModelContextProtocol.Core/Protocol/Resource.cs index 42bf22c57..388ce9c5d 100644 --- a/src/ModelContextProtocol.Core/Protocol/Resource.cs +++ b/src/ModelContextProtocol.Core/Protocol/Resource.cs @@ -20,6 +20,7 @@ public sealed class Resource : IBaseMetadata /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -46,6 +47,7 @@ public sealed class Resource : IBaseMetadata /// /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -62,6 +64,7 @@ public sealed class Resource : IBaseMetadata /// /// [JsonPropertyName("mimeType")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? MimeType { get; set; } /// @@ -72,6 +75,7 @@ public sealed class Resource : IBaseMetadata /// and the priority level of the resource. Clients can use this information to filter or prioritize resources for different roles. /// [JsonPropertyName("annotations")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public Annotations? Annotations { get; set; } /// @@ -81,6 +85,7 @@ public sealed class Resource : IBaseMetadata /// This can be used by applications to display file sizes and estimate context window usage. /// [JsonPropertyName("size")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public long? Size { get; set; } /// @@ -90,6 +95,7 @@ public sealed class Resource : IBaseMetadata /// This can be used by clients to display the resource's icon in a user interface. /// [JsonPropertyName("icons")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Icons { get; set; } /// @@ -99,5 +105,6 @@ public sealed class Resource : IBaseMetadata /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ResourceContents.cs b/src/ModelContextProtocol.Core/Protocol/ResourceContents.cs index 283c47d6b..c513d0f01 100644 --- a/src/ModelContextProtocol.Core/Protocol/ResourceContents.cs +++ b/src/ModelContextProtocol.Core/Protocol/ResourceContents.cs @@ -47,6 +47,7 @@ private protected ResourceContents() /// Gets or sets the MIME type of the resource content. /// [JsonPropertyName("mimeType")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? MimeType { get; set; } /// @@ -56,6 +57,7 @@ private protected ResourceContents() /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } /// diff --git a/src/ModelContextProtocol.Core/Protocol/ResourceTemplate.cs b/src/ModelContextProtocol.Core/Protocol/ResourceTemplate.cs index 09edb09fc..1c04522dc 100644 --- a/src/ModelContextProtocol.Core/Protocol/ResourceTemplate.cs +++ b/src/ModelContextProtocol.Core/Protocol/ResourceTemplate.cs @@ -18,6 +18,7 @@ public sealed class ResourceTemplate : IBaseMetadata /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -41,6 +42,7 @@ public sealed class ResourceTemplate : IBaseMetadata /// /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -58,6 +60,7 @@ public sealed class ResourceTemplate : IBaseMetadata /// /// [JsonPropertyName("mimeType")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? MimeType { get; set; } /// @@ -69,6 +72,7 @@ public sealed class ResourceTemplate : IBaseMetadata /// or prioritize resource templates for different roles. /// [JsonPropertyName("annotations")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public Annotations? Annotations { get; set; } /// @@ -78,6 +82,7 @@ public sealed class ResourceTemplate : IBaseMetadata /// This value can be used by clients to display the resource template's icon in a user interface. /// [JsonPropertyName("icons")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Icons { get; set; } /// @@ -87,6 +92,7 @@ public sealed class ResourceTemplate : IBaseMetadata /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } /// Gets a value that indicates whether contains any template expressions. diff --git a/src/ModelContextProtocol.Core/Protocol/ResourcesCapability.cs b/src/ModelContextProtocol.Core/Protocol/ResourcesCapability.cs index d7b8f6e44..fe03bf3ba 100644 --- a/src/ModelContextProtocol.Core/Protocol/ResourcesCapability.cs +++ b/src/ModelContextProtocol.Core/Protocol/ResourcesCapability.cs @@ -16,6 +16,7 @@ public sealed class ResourcesCapability /// Gets or sets a value that indicates whether this server supports subscribing to resource updates. /// [JsonPropertyName("subscribe")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? Subscribe { get; set; } /// @@ -28,5 +29,6 @@ public sealed class ResourcesCapability /// refresh their resource cache. /// [JsonPropertyName("listChanged")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? ListChanged { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/Result.cs b/src/ModelContextProtocol.Core/Protocol/Result.cs index e64be9728..aa37cefe2 100644 --- a/src/ModelContextProtocol.Core/Protocol/Result.cs +++ b/src/ModelContextProtocol.Core/Protocol/Result.cs @@ -20,6 +20,7 @@ protected Result() /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } /// @@ -34,5 +35,6 @@ protected Result() /// /// Defaults to , which is equivalent to "complete". [JsonPropertyName("resultType")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ResultType { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/Root.cs b/src/ModelContextProtocol.Core/Protocol/Root.cs index debeefd57..9ea70c475 100644 --- a/src/ModelContextProtocol.Core/Protocol/Root.cs +++ b/src/ModelContextProtocol.Core/Protocol/Root.cs @@ -28,6 +28,7 @@ public sealed class Root /// Gets or sets a human-readable name for the root. /// [JsonPropertyName("name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Name { get; set; } /// @@ -37,5 +38,6 @@ public sealed class Root /// This is reserved by the protocol for future use. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/RootsCapability.cs b/src/ModelContextProtocol.Core/Protocol/RootsCapability.cs index eebcb741d..723be70f8 100644 --- a/src/ModelContextProtocol.Core/Protocol/RootsCapability.cs +++ b/src/ModelContextProtocol.Core/Protocol/RootsCapability.cs @@ -33,5 +33,6 @@ public sealed class RootsCapability /// This enables servers to stay synchronized with client-side changes to available roots. /// [JsonPropertyName("listChanged")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? ListChanged { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/SamplingCapability.cs b/src/ModelContextProtocol.Core/Protocol/SamplingCapability.cs index 7a4d015de..bb3ff5d25 100644 --- a/src/ModelContextProtocol.Core/Protocol/SamplingCapability.cs +++ b/src/ModelContextProtocol.Core/Protocol/SamplingCapability.cs @@ -27,11 +27,13 @@ public sealed class SamplingCapability /// If not declared, servers should only use includeContext: "none". /// [JsonPropertyName("context")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public SamplingContextCapability? Context { get; set; } /// /// Gets or sets whether the client supports tool use via tools and toolChoice parameters. /// [JsonPropertyName("tools")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public SamplingToolsCapability? Tools { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/SamplingMessage.cs b/src/ModelContextProtocol.Core/Protocol/SamplingMessage.cs index 1e2fa4c10..3659ff37c 100644 --- a/src/ModelContextProtocol.Core/Protocol/SamplingMessage.cs +++ b/src/ModelContextProtocol.Core/Protocol/SamplingMessage.cs @@ -51,6 +51,7 @@ public sealed class SamplingMessage /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] diff --git a/src/ModelContextProtocol.Core/Protocol/ServerCapabilities.cs b/src/ModelContextProtocol.Core/Protocol/ServerCapabilities.cs index 95cfa078d..b70022588 100644 --- a/src/ModelContextProtocol.Core/Protocol/ServerCapabilities.cs +++ b/src/ModelContextProtocol.Core/Protocol/ServerCapabilities.cs @@ -33,6 +33,7 @@ public sealed class ServerCapabilities /// /// [JsonPropertyName("experimental")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Experimental { get; set; } /// @@ -40,30 +41,35 @@ public sealed class ServerCapabilities /// [JsonPropertyName("logging")] [Obsolete(Obsoletions.DeprecatedLogging_Message, DiagnosticId = Obsoletions.Deprecated_DiagnosticId, UrlFormat = Obsoletions.Deprecated_Url)] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public LoggingCapability? Logging { get; set; } /// /// Gets or sets a server's prompts capability for serving predefined prompt templates that clients can discover and use. /// [JsonPropertyName("prompts")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public PromptsCapability? Prompts { get; set; } /// /// Gets or sets a server's resources capability for serving predefined resources that clients can discover and use. /// [JsonPropertyName("resources")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ResourcesCapability? Resources { get; set; } /// /// Gets or sets a server's tools capability for listing tools that a client is able to invoke. /// [JsonPropertyName("tools")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ToolsCapability? Tools { get; set; } /// /// Gets or sets a server's completions capability for supporting argument auto-completion suggestions. /// [JsonPropertyName("completions")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CompletionsCapability? Completions { get; set; } /// @@ -81,5 +87,6 @@ public sealed class ServerCapabilities /// /// [JsonPropertyName("extensions")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? Extensions { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/SubscriptionsListenRequestParams.cs b/src/ModelContextProtocol.Core/Protocol/SubscriptionsListenRequestParams.cs index ac6c38c5b..654ee2b36 100644 --- a/src/ModelContextProtocol.Core/Protocol/SubscriptionsListenRequestParams.cs +++ b/src/ModelContextProtocol.Core/Protocol/SubscriptionsListenRequestParams.cs @@ -42,6 +42,7 @@ public sealed class SubscriptionsListenNotifications /// notifications. /// [JsonPropertyName("toolsListChanged")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? ToolsListChanged { get; set; } /// @@ -49,6 +50,7 @@ public sealed class SubscriptionsListenNotifications /// notifications. /// [JsonPropertyName("promptsListChanged")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? PromptsListChanged { get; set; } /// @@ -56,6 +58,7 @@ public sealed class SubscriptionsListenNotifications /// notifications. /// [JsonPropertyName("resourcesListChanged")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? ResourcesListChanged { get; set; } /// @@ -67,5 +70,6 @@ public sealed class SubscriptionsListenNotifications /// RPCs from prior protocol revisions. /// [JsonPropertyName("resourceSubscriptions")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? ResourceSubscriptions { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/Tool.cs b/src/ModelContextProtocol.Core/Protocol/Tool.cs index 274d53be3..85635b29c 100644 --- a/src/ModelContextProtocol.Core/Protocol/Tool.cs +++ b/src/ModelContextProtocol.Core/Protocol/Tool.cs @@ -18,6 +18,7 @@ public sealed class Tool : IBaseMetadata /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -42,6 +43,7 @@ public sealed class Tool : IBaseMetadata /// /// [JsonPropertyName("description")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } /// @@ -102,6 +104,7 @@ public JsonElement InputSchema /// /// [JsonPropertyName("outputSchema")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonElement? OutputSchema { get => field; @@ -125,6 +128,7 @@ public JsonElement? OutputSchema /// Note that these are hints and should not be relied upon for security decisions. /// [JsonPropertyName("annotations")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ToolAnnotations? Annotations { get; set; } /// @@ -134,6 +138,7 @@ public JsonElement? OutputSchema /// This can be used by clients to display the tool's icon in a user interface. /// [JsonPropertyName("icons")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Icons { get; set; } /// @@ -143,6 +148,7 @@ public JsonElement? OutputSchema /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] diff --git a/src/ModelContextProtocol.Core/Protocol/ToolAnnotations.cs b/src/ModelContextProtocol.Core/Protocol/ToolAnnotations.cs index 6a3cc4f03..23eb6944b 100644 --- a/src/ModelContextProtocol.Core/Protocol/ToolAnnotations.cs +++ b/src/ModelContextProtocol.Core/Protocol/ToolAnnotations.cs @@ -27,6 +27,7 @@ public sealed class ToolAnnotations /// /// [JsonPropertyName("title")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Title { get; set; } /// @@ -41,6 +42,7 @@ public sealed class ToolAnnotations /// This property is most relevant when the tool modifies its environment (ReadOnly = false). /// [JsonPropertyName("destructiveHint")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? DestructiveHint { get; set; } /// @@ -56,6 +58,7 @@ public sealed class ToolAnnotations /// This property is most relevant when the tool modifies its environment (ReadOnly = false). /// [JsonPropertyName("idempotentHint")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? IdempotentHint { get; set; } /// @@ -67,6 +70,7 @@ public sealed class ToolAnnotations /// if unspecified, in which case clients should assume . /// [JsonPropertyName("openWorldHint")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? OpenWorldHint { get; set; } /// @@ -84,5 +88,6 @@ public sealed class ToolAnnotations /// /// [JsonPropertyName("readOnlyHint")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? ReadOnlyHint { get; set; } } diff --git a/src/ModelContextProtocol.Core/Protocol/ToolChoice.cs b/src/ModelContextProtocol.Core/Protocol/ToolChoice.cs index 903e978c6..2d772808a 100644 --- a/src/ModelContextProtocol.Core/Protocol/ToolChoice.cs +++ b/src/ModelContextProtocol.Core/Protocol/ToolChoice.cs @@ -21,6 +21,7 @@ public sealed class ToolChoice /// /// [JsonPropertyName("mode")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Mode { get; set; } /// The mode value "auto". diff --git a/src/ModelContextProtocol.Core/Protocol/ToolsCapability.cs b/src/ModelContextProtocol.Core/Protocol/ToolsCapability.cs index b06c2f123..e1b93fc08 100644 --- a/src/ModelContextProtocol.Core/Protocol/ToolsCapability.cs +++ b/src/ModelContextProtocol.Core/Protocol/ToolsCapability.cs @@ -21,5 +21,6 @@ public sealed class ToolsCapability /// changes to available tools. /// [JsonPropertyName("listChanged")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? ListChanged { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Apps/Server/McpAppsJsonContext.cs b/src/ModelContextProtocol.Extensions.Apps/Server/McpAppsJsonContext.cs index 8a03ab95d..b1e51d00f 100644 --- a/src/ModelContextProtocol.Extensions.Apps/Server/McpAppsJsonContext.cs +++ b/src/ModelContextProtocol.Extensions.Apps/Server/McpAppsJsonContext.cs @@ -7,7 +7,6 @@ namespace ModelContextProtocol.Extensions.Apps; /// Provides source-generated JSON serialization metadata for MCP Apps extension types. /// [JsonSourceGenerationOptions( - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] [JsonSerializable(typeof(McpUiToolMeta))] [JsonSerializable(typeof(McpUiClientCapabilities))] diff --git a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiClientCapabilities.cs b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiClientCapabilities.cs index a446d90cb..dfc92ca54 100644 --- a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiClientCapabilities.cs +++ b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiClientCapabilities.cs @@ -22,5 +22,6 @@ public sealed class McpUiClientCapabilities /// A client that supports MCP Apps must include "text/html;profile=mcp-app" in this list. /// [JsonPropertyName("mimeTypes")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? MimeTypes { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceCsp.cs b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceCsp.cs index da13efea3..f7db817b7 100644 --- a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceCsp.cs +++ b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceCsp.cs @@ -24,6 +24,7 @@ public sealed class McpUiResourceCsp /// connections (connect-src CSP directive). /// [JsonPropertyName("connectDomains")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? ConnectDomains { get; set; } /// @@ -31,6 +32,7 @@ public sealed class McpUiResourceCsp /// (script-src, style-src, img-src, font-src CSP directives). /// [JsonPropertyName("resourceDomains")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? ResourceDomains { get; set; } /// @@ -38,6 +40,7 @@ public sealed class McpUiResourceCsp /// (frame-src CSP directive). /// [JsonPropertyName("frameDomains")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? FrameDomains { get; set; } /// @@ -45,5 +48,6 @@ public sealed class McpUiResourceCsp /// (base-uri CSP directive). /// [JsonPropertyName("baseUris")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? BaseUris { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceMeta.cs b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceMeta.cs index fcb09d86a..064861686 100644 --- a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceMeta.cs +++ b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourceMeta.cs @@ -21,6 +21,7 @@ public sealed class McpUiResourceMeta /// Specifies the allowed origins for network requests, resource loads, and nested frames. /// [JsonPropertyName("csp")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public McpUiResourceCsp? Csp { get; set; } /// @@ -30,6 +31,7 @@ public sealed class McpUiResourceMeta /// Controls which browser sandbox features the UI resource is allowed to use. /// [JsonPropertyName("permissions")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public McpUiResourcePermissions? Permissions { get; set; } /// @@ -40,11 +42,13 @@ public sealed class McpUiResourceMeta /// enabling OAuth flows and CORS without wildcard exceptions. /// [JsonPropertyName("domain")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Domain { get; set; } /// /// Gets or sets a value indicating whether the host should render a visual border around the UI. /// [JsonPropertyName("prefersBorder")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public bool? PrefersBorder { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourcePermissions.cs b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourcePermissions.cs index 758fee03b..04e6b8cac 100644 --- a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourcePermissions.cs +++ b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiResourcePermissions.cs @@ -23,5 +23,6 @@ public sealed class McpUiResourcePermissions /// "clipboard-read", or "clipboard-write". /// [JsonPropertyName("allow")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Allow { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiToolMeta.cs b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiToolMeta.cs index 44fa369c1..6f5f74e82 100644 --- a/src/ModelContextProtocol.Extensions.Apps/Server/McpUiToolMeta.cs +++ b/src/ModelContextProtocol.Extensions.Apps/Server/McpUiToolMeta.cs @@ -24,6 +24,7 @@ public sealed class McpUiToolMeta /// with the server (e.g., "ui://weather/view.html"). /// [JsonPropertyName("resourceUri")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? ResourceUri { get; set; } /// @@ -37,5 +38,6 @@ public sealed class McpUiToolMeta /// /// [JsonPropertyName("visibility")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IList? Visibility { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Skills/McpSkillsJsonContext.cs b/src/ModelContextProtocol.Extensions.Skills/McpSkillsJsonContext.cs index 9d4467680..dc06fcc4f 100644 --- a/src/ModelContextProtocol.Extensions.Skills/McpSkillsJsonContext.cs +++ b/src/ModelContextProtocol.Extensions.Skills/McpSkillsJsonContext.cs @@ -7,7 +7,6 @@ namespace ModelContextProtocol.Extensions.Skills; /// Provides source-generated JSON serialization metadata for the MCP Skills extension types. /// [JsonSourceGenerationOptions( - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] [JsonSerializable(typeof(Skill))] [JsonSerializable(typeof(SkillResource))] diff --git a/src/ModelContextProtocol.Extensions.Skills/Protocol/GetSkillResult.cs b/src/ModelContextProtocol.Extensions.Skills/Protocol/GetSkillResult.cs index 7d89e3bc7..ed2d8ff85 100644 --- a/src/ModelContextProtocol.Extensions.Skills/Protocol/GetSkillResult.cs +++ b/src/ModelContextProtocol.Extensions.Skills/Protocol/GetSkillResult.cs @@ -29,10 +29,12 @@ public sealed class GetSkillResult : Result, ICacheableResult /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Skills/Protocol/ListSkillsResult.cs b/src/ModelContextProtocol.Extensions.Skills/Protocol/ListSkillsResult.cs index dafe4d32b..f44d99499 100644 --- a/src/ModelContextProtocol.Extensions.Skills/Protocol/ListSkillsResult.cs +++ b/src/ModelContextProtocol.Extensions.Skills/Protocol/ListSkillsResult.cs @@ -34,10 +34,12 @@ public sealed class ListSkillsResult : PaginatedResult, ICacheableResult /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// [JsonPropertyName("cacheScope")] [JsonConverter(typeof(CacheScopeConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public CacheScope? CacheScope { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Skills/Protocol/Skill.cs b/src/ModelContextProtocol.Extensions.Skills/Protocol/Skill.cs index 545f92e8a..fd69c63cd 100644 --- a/src/ModelContextProtocol.Extensions.Skills/Protocol/Skill.cs +++ b/src/ModelContextProtocol.Extensions.Skills/Protocol/Skill.cs @@ -60,6 +60,7 @@ public sealed class Skill /// Implementations must not make assumptions about its contents. /// [JsonPropertyName("_meta")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public JsonObject? Meta { get; set; } /// diff --git a/src/ModelContextProtocol.Extensions.Tasks/McpTasksJsonContext.cs b/src/ModelContextProtocol.Extensions.Tasks/McpTasksJsonContext.cs index 13c92ee14..c5fd195cc 100644 --- a/src/ModelContextProtocol.Extensions.Tasks/McpTasksJsonContext.cs +++ b/src/ModelContextProtocol.Extensions.Tasks/McpTasksJsonContext.cs @@ -7,7 +7,6 @@ namespace ModelContextProtocol.Extensions.Tasks; /// Provides source-generated JSON serialization metadata for MCP Tasks extension types. /// [JsonSourceGenerationOptions( - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] [JsonSerializable(typeof(CreateTaskResult))] [JsonSerializable(typeof(GetTaskRequestParams))] diff --git a/src/ModelContextProtocol.Extensions.Tasks/Protocol/CreateTaskResult.cs b/src/ModelContextProtocol.Extensions.Tasks/Protocol/CreateTaskResult.cs index 7cbb5d134..d428739f9 100644 --- a/src/ModelContextProtocol.Extensions.Tasks/Protocol/CreateTaskResult.cs +++ b/src/ModelContextProtocol.Extensions.Tasks/Protocol/CreateTaskResult.cs @@ -49,6 +49,7 @@ public CreateTaskResult() /// Gets or sets an optional message describing the current task state. /// [JsonPropertyName("statusMessage")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? StatusMessage { get; set; } /// @@ -68,11 +69,13 @@ public CreateTaskResult() /// [JsonPropertyName("ttlMs")] [JsonConverter(typeof(TimeSpanMillisecondsConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// /// Gets or sets the suggested polling interval in milliseconds. /// [JsonPropertyName("pollIntervalMs")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public long? PollIntervalMs { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Tasks/Protocol/GetTaskResult.cs b/src/ModelContextProtocol.Extensions.Tasks/Protocol/GetTaskResult.cs index 607e7f3b2..6eabaebc7 100644 --- a/src/ModelContextProtocol.Extensions.Tasks/Protocol/GetTaskResult.cs +++ b/src/ModelContextProtocol.Extensions.Tasks/Protocol/GetTaskResult.cs @@ -49,6 +49,7 @@ private protected GetTaskResult() /// Gets or sets an optional message describing the current task state. /// [JsonPropertyName("statusMessage")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? StatusMessage { get; set; } /// @@ -67,12 +68,14 @@ private protected GetTaskResult() /// Gets or sets the time-to-live duration from creation, or for unlimited. /// [JsonPropertyName("ttlMs")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// /// Gets or sets the suggested polling interval in milliseconds. /// [JsonPropertyName("pollIntervalMs")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public long? PollIntervalMs { get; set; } /// @@ -446,5 +449,6 @@ public sealed class InputRequiredTaskResult : GetTaskResult /// (e.g., a sampling, elicitation, or roots-list request). /// [JsonPropertyName("inputRequests")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? InputRequests { get; set; } } diff --git a/src/ModelContextProtocol.Extensions.Tasks/Protocol/TaskStatusNotificationParams.cs b/src/ModelContextProtocol.Extensions.Tasks/Protocol/TaskStatusNotificationParams.cs index bf51353ef..150c10b6f 100644 --- a/src/ModelContextProtocol.Extensions.Tasks/Protocol/TaskStatusNotificationParams.cs +++ b/src/ModelContextProtocol.Extensions.Tasks/Protocol/TaskStatusNotificationParams.cs @@ -55,6 +55,7 @@ private protected TaskStatusNotificationParams() /// Gets or sets an optional message describing the current task state. /// [JsonPropertyName("statusMessage")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? StatusMessage { get; set; } /// @@ -73,12 +74,14 @@ private protected TaskStatusNotificationParams() /// Gets or sets the time-to-live duration from creation, or for unlimited. /// [JsonPropertyName("ttlMs")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public TimeSpan? TimeToLive { get; set; } /// /// Gets or sets the suggested polling interval in milliseconds. /// [JsonPropertyName("pollIntervalMs")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public long? PollIntervalMs { get; set; } /// @@ -388,5 +391,6 @@ public sealed class InputRequiredTaskNotificationParams : TaskStatusNotification /// the typed format defined by the Multi Round-Trip Requests (MRTR) extension (SEP-2322). /// [JsonPropertyName("inputRequests")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public IDictionary? InputRequests { get; set; } } diff --git a/tests/ModelContextProtocol.Tests/ExperimentalInternalPropertyTests.cs b/tests/ModelContextProtocol.Tests/ExperimentalInternalPropertyTests.cs index 17e18dc2b..721622eba 100644 --- a/tests/ModelContextProtocol.Tests/ExperimentalInternalPropertyTests.cs +++ b/tests/ModelContextProtocol.Tests/ExperimentalInternalPropertyTests.cs @@ -48,7 +48,9 @@ public void ExperimentalProperties_MustBeHiddenFromSourceGenerator() private static IEnumerable<(Type Type, PropertyInfo Property)> GetExperimentalPropertiesOnStableTypes() { var protocolTypes = typeof(Tool).Assembly.GetTypes() - .Where(t => t.Namespace == "ModelContextProtocol.Protocol" && t.IsClass && !t.IsAbstract); + .Where(t => t.Namespace == "ModelContextProtocol.Protocol" && t.IsClass && !t.IsAbstract) + // JsonRpcMessageContext is never serialized. + .Where(t => t != typeof(JsonRpcMessageContext)); foreach (var type in protocolTypes) { diff --git a/tests/ModelContextProtocol.Tests/McpJsonUtilitiesTests.cs b/tests/ModelContextProtocol.Tests/McpJsonUtilitiesTests.cs index 6d87d9827..018f72691 100644 --- a/tests/ModelContextProtocol.Tests/McpJsonUtilitiesTests.cs +++ b/tests/ModelContextProtocol.Tests/McpJsonUtilitiesTests.cs @@ -1,4 +1,4 @@ -using System.Text.Json; +using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; using ModelContextProtocol.Protocol; @@ -36,6 +36,109 @@ private sealed class WeatherQuery public string? City { get; set; } } + [Theory] + [InlineData(false)] + [InlineData(true)] + public static void Options_WriteUserNullsButOmitProtocolNulls(bool useProtocolOptions) + { + var options = useProtocolOptions ? McpJsonUtilities.ProtocolOptions : McpJsonUtilities.DefaultOptions; + + Assert.Equal("""{"name":"t","inputSchema":{"type":"object"}}""", JsonSerializer.Serialize(new Tool { Name = "t" }, options)); + + if (JsonSerializer.IsReflectionEnabledByDefault) + { + Assert.Equal("""{"city":null}""", JsonSerializer.Serialize(new WeatherQuery(), options)); + Assert.Equal("""{"type":"string"}""", JsonSerializer.Serialize(new ElicitRequestParams.StringSchema(), options)); + Assert.Equal("""{"type":"number"}""", JsonSerializer.Serialize(new ElicitRequestParams.NumberSchema(), options)); + Assert.Equal("{}", JsonSerializer.Serialize(new RequestParamsMetadata(), options)); + } + } + + [Fact] + public static void SdkProtocolTypes_OmitNullOptionalProperties() + { + // Protocol types must not rely on the serializer options to omit null optional properties, + // because DefaultOptions and ProtocolOptions write nulls. Annotate nullable properties with + // [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] instead. + JsonSerializerContext[] contexts = + [ + GetContext("ModelContextProtocol.McpJsonUtilities+JsonContext, ModelContextProtocol.Core"), + GetContext("ModelContextProtocol.Extensions.Apps.McpAppsJsonContext, ModelContextProtocol.Extensions.Apps"), + GetContext("ModelContextProtocol.Extensions.Skills.McpSkillsJsonContext, ModelContextProtocol.Extensions.Skills"), + GetContext("ModelContextProtocol.Extensions.Tasks.McpTasksJsonContext, ModelContextProtocol.Extensions.Tasks"), + ]; + + JsonSerializerOptions options = new(JsonSerializerDefaults.Web) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine([.. contexts, new DefaultJsonTypeInfoResolver()]), + }; + + Queue pending = new(contexts.SelectMany(c => c.GetType().GetCustomAttributesData()) + .Where(a => a.AttributeType == typeof(JsonSerializableAttribute)) + .Select(a => (Type)a.ConstructorArguments[0].Value!) + .Concat(contexts.SelectMany(c => c.GetType().Assembly.GetExportedTypes()) + .Where(t => !t.ContainsGenericParameters && t.GetProperties().Any(p => p.IsDefined(typeof(JsonPropertyNameAttribute), inherit: true))))); + HashSet visited = []; + List failures = []; + + while (pending.Count > 0) + { + Type type = pending.Dequeue(); + if (!visited.Add(type)) + { + continue; + } + + if (type.IsArray) + { + pending.Enqueue(type.GetElementType()!); + continue; + } + + if (type.IsGenericType) + { + type.GetGenericArguments().ToList().ForEach(pending.Enqueue); + } + + if (type.Assembly.GetName().Name?.StartsWith("ModelContextProtocol", StringComparison.Ordinal) is not true || + type.IsEnum || type.IsInterface || type.ContainsGenericParameters) + { + continue; + } + + // Types with custom converters may still serialize derived types using default contracts. + foreach (Type derived in type.Assembly.GetTypes().Where(t => t.IsSubclassOf(type))) + { + pending.Enqueue(derived); + } + + JsonTypeInfo typeInfo = options.GetTypeInfo(type); + if (typeInfo.Kind is not JsonTypeInfoKind.Object) + { + continue; + } + + foreach (JsonPropertyInfo property in typeInfo.Properties.Where(p => p.Get is not null)) + { + pending.Enqueue(Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType); + + bool isNullable = property.PropertyType.IsValueType + ? Nullable.GetUnderlyingType(property.PropertyType) is not null + : property.IsGetNullable; + + if (isNullable && property.ShouldSerialize is null) + { + failures.Add($"{type.FullName}.{property.Name}"); + } + } + } + + Assert.True(failures.Count == 0, "Nullable protocol properties that would write null:" + Environment.NewLine + string.Join(Environment.NewLine, failures.Distinct())); + + static JsonSerializerContext GetContext(string assemblyQualifiedTypeName) => + (JsonSerializerContext)Type.GetType(assemblyQualifiedTypeName, throwOnError: true)!.GetProperty("Default")!.GetValue(null)!; + } + [Fact] public static void ProtocolOptions_IsSingletonAndMatchesPropertyNamesCaseSensitively() { diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index c77151884..d2064dc6f 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -516,6 +516,57 @@ public async Task StructuredOutput_Enabled_ReturnsExpectedSchema(T value) AssertMatchesJsonSchema(tool.ProtocolTool.OutputSchema.Value, result.StructuredContent); } + [Theory(Skip = "Requires reflection-based serialization", SkipType = typeof(JsonSerializer), SkipUnless = nameof(JsonSerializer.IsReflectionEnabledByDefault))] + [InlineData(true)] + [InlineData(false)] + public async Task StructuredOutput_DefaultOptions_IncludesRequiredNullProperties(bool useRecord) + { + McpServerTool tool = useRecord + ? McpServerTool.Create(() => new NullableNoteOrder("42", null), new() { Name = "tool", UseStructuredContent = true }) + : McpServerTool.Create(() => new NullableNoteParcel { Id = "P-7", Note = null }, new() { Name = "tool", UseStructuredContent = true }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(tool.ProtocolTool.OutputSchema); + Assert.Contains(tool.ProtocolTool.OutputSchema.Value.GetProperty("required").EnumerateArray(), e => e.GetString() == "note"); + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(tool.ProtocolTool.OutputSchema.Value, result.StructuredContent); + } + + [Theory(Skip = "Requires reflection-based serialization", SkipType = typeof(JsonSerializer), SkipUnless = nameof(JsonSerializer.IsReflectionEnabledByDefault))] + [InlineData(true)] + [InlineData(false)] + public async Task StructuredOutput_OmitsNullProperties_WhenConfigured(bool useAttribute) + { + McpServerTool tool = useAttribute + ? McpServerTool.Create(() => new OptionalNoteOrder("42"), new() { Name = "tool", UseStructuredContent = true }) + : McpServerTool.Create(() => new OptionalNoteShipment("S-3"), new() + { + Name = "tool", + UseStructuredContent = true, + SerializerOptions = new(McpJsonUtilities.DefaultOptions) { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }, + }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.False(result.StructuredContent.Value.TryGetProperty("note", out _)); + AssertMatchesJsonSchema(tool.ProtocolTool.OutputSchema!.Value, result.StructuredContent); + } + + public sealed record NullableNoteOrder(string Id, string? Note); + + public sealed class NullableNoteParcel + { + public required string Id { get; init; } + public required string? Note { get; init; } + } + + public sealed record OptionalNoteOrder(string Id, [property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] string? Note = null); + + public sealed record OptionalNoteShipment(string Id, string? Note = null); + [Fact] public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSchema() {