github modelcontextprotocol/csharp-sdk v2.4.1

56 minutes ago

Release v2.4.1 fixes three serialization bugs in ModelContextProtocol.Core. Tool results and client responses containing annotated content blocks failed to serialize or deserialize. Schema-required null properties were omitted from structured content. Image and audio data in sampling messages was base64-encoded twice. Each fix corrects the JSON that the SDK writes or accepts. If your code compensated for the incorrect behavior, update it to match the corrected serialization. Each entry below includes before-and-after payloads.

What's Changed

  • Fix serialization of annotated content blocks in tool results #2039 by @tarekgh

    In 2.4.0, content blocks with annotations and resource links with icons failed to serialize or deserialize. They threw InvalidOperationException: Cannot insert the values of 'TypeInfoResolver' or 'TypeInfoResolverChain' to 'TypeInfoResolverChain'. Servers returned a generic error instead of the tool result, and clients failed to read results that contained annotations. Version 2.4.1 restores the 2.3.1 behavior.

    A tool that returns new TextContentBlock { Text = "x", Annotations = new Annotations { Audience = [Role.User] } }:

    Before (2.4.0): the client receives an error instead of the result.

    { "jsonrpc": "2.0", "id": 1, "error": { "code": -32603, "message": "An error occurred." } }

    After (2.4.1): the tool result is returned.

    { "jsonrpc": "2.0", "id": 1, "result": { "content": [ { "type": "text", "text": "x", "annotations": { "audience": [ "user" ] } } ] } }

    Clients also deserialize results like this one again. In 2.4.0, the same exception occurred when they read the result:

    { "content": [ { "type": "text", "text": "x", "annotations": { "audience": [ "user" ] } } ] }

    Adapting your code: If you removed Annotations or Icons from tool results, stayed on 2.3.1, or caught and retried around this exception, you can remove those workarounds.

  • Write null user-defined properties by default #2067 by @halter73 (co-authored by @Copilot)

    McpJsonUtilities.DefaultOptions no longer omits null values. User-defined data serialized with those options now includes null members. That data includes tool return values and structuredContent, tool arguments sent by clients, and data serialized with copies of DefaultOptions. Previously, a nullable member that the generated outputSchema listed as required was dropped when it was null. A client that validated the result against that schema then rejected it.

    A tool that uses UseStructuredContent = true and returns new Order("42", null) for public record Order(string Id, string? Note):

    "outputSchema": { "type": "object", "properties": { "id": { "type": "string" }, "note": { "type": [ "string", "null" ] } }, "required": [ "id", "note" ] }

    Before (2.4.0): note is omitted, so the result doesn't conform to the tool's own outputSchema.

    "structuredContent": { "id": "42" }

    After (2.4.1): note is written as null, so the result conforms to the schema.

    "structuredContent": { "id": "42", "note": null }

    The same applies to null members of objects that clients pass as tool arguments. For a filter argument of type record Filter(string City, string? Units):

    Before (2.4.0): the server received "arguments": { "filter": { "city": "Seattle" } }.

    After (2.4.1): the server receives "arguments": { "filter": { "city": "Seattle", "units": null } }.

    Top-level arguments whose value is null were already sent as null and are unchanged. MCP protocol types such as Tool still omit optional null properties.

    Task results and status notifications are an exception: SEP-2663 requires ttlMs, using null to mean an unlimited task lifetime. ModelContextProtocol.Extensions.Tasks now always includes that property. Representative task-result fields (other fields omitted):

    Before (2.4.0):

    { "taskId": "abc", "status": "working" }

    After (2.4.1):

    { "taskId": "abc", "status": "working", "ttlMs": null }

    Adapting your code:

    • If you handled schema validation failures, stripped required entries, or supplied serializer options that kept null values, you can remove those workarounds.
    • If a server or client treats an explicit null differently from an omitted member, update it to handle explicit null values.
    • If an explicit output schema declares an optional, non-nullable property, either allow null in the schema or keep omitting that property.
    • To keep omitting null values, apply [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] to individual properties. Alternatively, provide McpServerToolCreateOptions.SerializerOptions (or your own JsonSerializerOptions) with DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull. Make sure the schema doesn't require the omitted properties.
    • ModelContextProtocol.Extensions.Tasks clients from 2.4.0 fail to read "ttlMs": null. Upgrade clients before servers, or set a finite InMemoryMcpTaskStore.DefaultTimeToLive so servers send a number.
  • Fix double base64 encoding of image/audio content in sampling messages #2034 by @atailakh

    SamplingMessage.Content and CreateMessageResult.Content encoded the data for image and audio content blocks twice. They now encode it once, matching standalone ContentBlock serialization and the format the SDK reads. For example, this affected images sent through McpServer.SampleAsync or AsSamplingChatClient(). After one decoding step, recipients got base64 text instead of the media bytes, and model providers could reject the image.

    A sampling message that contains ImageContentBlock.FromBytes([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], "image/png"). Only the data member changes:

    Before (2.4.0): base64 of the base64 text. One decoding step produces the text iVBORw0KGgo=, not the PNG bytes.

    "data": "aVZCT1J3MEtHZ289"

    After (2.4.1): the PNG bytes encoded once.

    "data": "iVBORw0KGgo="

    Adapting your code:

    • If a sampling handler or client decoded data twice, or detected and unwrapped double-encoded media from C# SDK 2.4.0 senders, decode it once. Otherwise, it will corrupt correctly encoded data.
    • If a sender pre-decoded media to offset the extra encoding, pass the original bytes.
    • SingleItemOrListConverter<T> now serializes each item with the contract for T rather than the item's runtime type. If you use this converter with derived types that T doesn't describe, their extra members are no longer written. To include them, add [JsonDerivedType] metadata or a converter for T.

Repository Infrastructure Updates

Acknowledgements

Full Changelog: v2.4.0...v2.4.1

Note

These release notes were drafted with GitHub Copilot and reviewed before publishing.

Don't miss a new csharp-sdk release

NewReleases is sending notifications on new releases.