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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/concepts/tools/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <xref:ModelContextProtocol.McpJsonUtilities.DefaultOptions>,
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
<xref:ModelContextProtocol.Server.McpServerToolCreateOptions.SerializerOptions> with
`DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull`. When doing so, make sure the output
schema doesn't require the omitted properties.

MCP protocol types such as <xref:ModelContextProtocol.Protocol.Tool> 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 <xref:ModelContextProtocol.Protocol.Annotations> to provide hints about the intended audience and priority:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,56 @@ internal sealed class AuthorizationServerMetadata
/// The registration endpoint URI.
/// </summary>
[JsonPropertyName("registration_endpoint")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Uri? RegistrationEndpoint { get; set; }

/// <summary>
/// The revocation endpoint URI.
/// </summary>
[JsonPropertyName("revocation_endpoint")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Uri? RevocationEndpoint { get; set; }

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

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

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

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

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

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

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,48 @@ internal sealed class DynamicClientRegistrationRequest
/// Gets or sets the token endpoint authentication method.
/// </summary>
[JsonPropertyName("token_endpoint_auth_method")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? TokenEndpointAuthMethod { get; init; }

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

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

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

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

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

/// <summary>
/// Gets or sets the OIDC application type ("native" or "web") for the client.
/// </summary>
[JsonPropertyName("application_type")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ApplicationType { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,48 @@ public sealed class DynamicClientRegistrationResponse
/// Gets or initializes the client secret.
/// </summary>
[JsonPropertyName("client_secret")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ClientSecret { get; init; }

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

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

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

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

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

/// <summary>
/// Gets or initializes the client secret expiration time.
/// </summary>
[JsonPropertyName("client_secret_expires_at")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public long? ClientSecretExpiresAt { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace ModelContextProtocol.Authentication;

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

/// <summary>
/// Gets or sets the lifetime in seconds of the issued token.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("expires_in")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? ExpiresIn { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace ModelContextProtocol.Authentication;

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

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

/// <summary>
/// Gets or sets the scope of the access token.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("scope")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Scope { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace ModelContextProtocol.Authentication;

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

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

/// <summary>
/// Gets or sets the URI identifying a human-readable web page with error information.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("error_uri")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ErrorUri { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed class ProtectedResourceMetadata
/// </remarks>
[JsonPropertyName("resource")]
[StringSyntax(StringSyntaxAttribute.Uri)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Resource { get; set; }

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

/// <summary>
Expand All @@ -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.
/// </remarks>
[JsonPropertyName("resource_signing_alg_values_supported")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IList<string>? ResourceSigningAlgValuesSupported { get; set; }

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

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

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

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

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

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

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

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

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/ModelContextProtocol.Core/Authentication/TokenResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ internal sealed class TokenResponse
/// Gets or sets the refresh token.
/// </summary>
[JsonPropertyName("refresh_token")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? RefreshToken { get; set; }

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

/// <summary>
Expand All @@ -35,5 +37,6 @@ internal sealed class TokenResponse
/// Gets or sets the scope of the access token.
/// </summary>
[JsonPropertyName("scope")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Scope { get; set; }
}
2 changes: 0 additions & 2 deletions src/ModelContextProtocol.Core/McpJsonUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static partial class McpJsonUtilities
/// It additionally turns on the following settings:
/// <list type="number">
/// <item>Enables <see cref="JsonSerializerDefaults.Web"/> defaults.</item>
/// <item>Enables <see cref="JsonIgnoreCondition.WhenWritingNull"/> as the default ignore condition for properties.</item>
/// <item>Enables <see cref="JsonNumberHandling.AllowReadingFromString"/> as the default number handling for number types.</item>
/// </list>
/// </para>
Expand Down Expand Up @@ -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)]

Expand Down
Loading
Loading