Skip to content

Omit null mimeType from ResourceContents JSON serialization#1325

Merged
stephentoub merged 3 commits intomainfrom
copilot/fix-mimetype-null-handling
Feb 20, 2026
Merged

Omit null mimeType from ResourceContents JSON serialization#1325
stephentoub merged 3 commits intomainfrom
copilot/fix-mimetype-null-handling

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

ResourceContents.Converter.Write unconditionally wrote "mimeType": null when MimeType was unset, violating the MCP schema where the property is optional.

Changes

  • ResourceContents.cs: Guard the mimeType write with a null check, consistent with how _meta is already handled in the same method
  • ResourceContentsTests.cs: Add tests asserting mimeType is absent from serialized output when null, for both TextResourceContents and BlobResourceContents
// Before
writer.WriteString("mimeType", value.MimeType); // always written, even as null

// After
if (value.MimeType is not null)
{
    writer.WriteString("mimeType", value.MimeType);
}
Original prompt

In src/ModelContextProtocol.Core/Protocol/ResourceContents.cs, the custom JsonConverter for ResourceContents unconditionally writes the mimeType property in the Write method (line 161):

writer.WriteString("mimeType", value.MimeType);

However, MimeType is declared as string? (nullable/optional) on line 50:

public string? MimeType { get; set; }

This means that when MimeType is null, the serializer will still write "mimeType": null into the JSON output. Since the property is optional per the MCP schema, it should be omitted entirely when null, consistent with how Meta is handled (lines 173-177).

Fix: Wrap the mimeType write in a null check, similar to how _meta is handled:

if (value.MimeType is not null)
{
    writer.WriteString("mimeType", value.MimeType);
}

This change should be made in the Write method of the ResourceContents.Converter class at line 161 of src/ModelContextProtocol.Core/Protocol/ResourceContents.cs.

This pull request was created from Copilot chat.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub stephentoub marked this pull request as ready for review February 20, 2026 02:22
Copilot AI changed the title [WIP] Fix handling of null mimeType in JsonConverter Omit null mimeType from ResourceContents JSON serialization Feb 20, 2026
Copilot AI requested a review from stephentoub February 20, 2026 02:25
@stephentoub stephentoub enabled auto-merge (squash) February 20, 2026 03:44
@stephentoub stephentoub merged commit 3727dc4 into main Feb 20, 2026
9 checks passed
@stephentoub stephentoub deleted the copilot/fix-mimetype-null-handling branch February 20, 2026 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments