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
annotationsand resource links withiconsfailed to serialize or deserialize. They threwInvalidOperationException: 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
AnnotationsorIconsfrom 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.DefaultOptionsno longer omitsnullvalues. User-defined data serialized with those options now includesnullmembers. That data includes tool return values andstructuredContent, tool arguments sent by clients, and data serialized with copies ofDefaultOptions. Previously, a nullable member that the generatedoutputSchemalisted asrequiredwas dropped when it wasnull. A client that validated the result against that schema then rejected it.A tool that uses
UseStructuredContent = trueand returnsnew Order("42", null)forpublic record Order(string Id, string? Note):"outputSchema": { "type": "object", "properties": { "id": { "type": "string" }, "note": { "type": [ "string", "null" ] } }, "required": [ "id", "note" ] }
Before (2.4.0):
noteis omitted, so the result doesn't conform to the tool's ownoutputSchema."structuredContent": { "id": "42" }
After (2.4.1):
noteis written asnull, so the result conforms to the schema."structuredContent": { "id": "42", "note": null }
The same applies to
nullmembers of objects that clients pass as tool arguments. For afilterargument of typerecord 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
nullwere already sent asnulland are unchanged. MCP protocol types such asToolstill omit optionalnullproperties.Task results and status notifications are an exception: SEP-2663 requires
ttlMs, usingnullto 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
requiredentries, or supplied serializer options that keptnullvalues, you can remove those workarounds. - If a server or client treats an explicit
nulldifferently from an omitted member, update it to handle explicitnullvalues. - If an explicit output schema declares an optional, non-nullable property, either allow
nullin the schema or keep omitting that property. - To keep omitting
nullvalues, apply[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]to individual properties. Alternatively, provideMcpServerToolCreateOptions.SerializerOptions(or your ownJsonSerializerOptions) withDefaultIgnoreCondition = 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 finiteInMemoryMcpTaskStore.DefaultTimeToLiveso servers send a number.
- If you handled schema validation failures, stripped
-
Fix double base64 encoding of image/audio content in sampling messages #2034 by @atailakh
SamplingMessage.ContentandCreateMessageResult.Contentencoded the data for image and audio content blocks twice. They now encode it once, matching standaloneContentBlockserialization and the format the SDK reads. For example, this affected images sent throughMcpServer.SampleAsyncorAsSamplingChatClient(). 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 thedatamember 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
datatwice, 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 forTrather than the item's runtime type. If you use this converter with derived types thatTdoesn't describe, their extra members are no longer written. To include them, add[JsonDerivedType]metadata or a converter forT.
- If a sampling handler or client decoded
Repository Infrastructure Updates
- Use NuGet Trusted Publishing #2076 by @jeffhandley (co-authored by @Copilot)
Acknowledgements
- @atailakh made their first contribution in #2034
- @KenBrannigan submitted issue #690 (resolved by #2067)
- @AdCodicem submitted issue #1915 (resolved by #2067)
- @pbuffolino submitted issue #2012 (resolved by #2039)
- @atailakh submitted issue #2033 (resolved by #2034)
- @OliverHeffernan submitted issue #2072 (resolved by #2039)
- @tarekgh @halter73 @chrikrah @sivaraman-krishnan reviewed pull requests
Full Changelog: v2.4.0...v2.4.1
Note
These release notes were drafted with GitHub Copilot and reviewed before publishing.