Skip to content

Fix ResourceLinkBlock deserialization by adding missing "name" case #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 23, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 23, 2025

Summary

Fixed a critical deserialization bug in ResourceLinkBlock where the "name" property was not being read from JSON, causing a JsonException to be thrown during deserialization.

Problem

When attempting to deserialize a ResourceLinkBlock from JSON, the following exception was thrown:

JsonException: Name must be provided for 'resource_link' type.

This occurred because the ContentBlock.Converter.Read method was missing a switch case for the "name" property, even though:

  • The name variable was declared (line 75)
  • The name property was being written correctly during serialization (line 245)
  • The name variable was expected to be non-null when creating the ResourceLinkBlock (line 178)

Root Cause

In src/ModelContextProtocol.Core/Protocol/ContentBlock.cs, the switch statement in the Read method (lines 96-140) was missing a case for "name", causing the name property to remain null during deserialization.

Solution

Added the missing switch case to properly handle the "name" property during JSON deserialization:

case "name":
    name = reader.GetString();
    break;

Testing

  • Manual testing confirmed that ResourceLinkBlock now serializes and deserializes correctly
  • All required properties (uri, name) are preserved during round-trip serialization
  • Optional properties (description, mimeType, size) are also handled correctly
  • Added comprehensive test cases in ContentBlockTests.cs to prevent regression
  • Verified that existing ContentBlock types (TextContentBlock, ImageContentBlock, etc.) continue to work correctly

Example

Before this fix:

var resourceLink = new ResourceLinkBlock { Uri = "https://example.com", Name = "Test" };
var json = JsonSerializer.Serialize(resourceLink, McpJsonUtilities.DefaultOptions);
var deserialized = JsonSerializer.Deserialize<ContentBlock>(json, McpJsonUtilities.DefaultOptions);
// ❌ Throws JsonException: Name must be provided for 'resource_link' type.

After this fix:

var resourceLink = new ResourceLinkBlock { Uri = "https://example.com", Name = "Test" };
var json = JsonSerializer.Serialize(resourceLink, McpJsonUtilities.DefaultOptions);
var deserialized = JsonSerializer.Deserialize<ContentBlock>(json, McpJsonUtilities.DefaultOptions);
// ✅ Works correctly, preserves all properties

Fixes #644.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: curl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits July 23, 2025 12:47
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Cannot deserialize ResourceLinkBlock Fix ResourceLinkBlock deserialization by adding missing "name" case Jul 23, 2025
@Copilot Copilot AI requested a review from eiriktsarpalis July 23, 2025 12:55
Copilot finished work on behalf of eiriktsarpalis July 23, 2025 12:55
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
@Copilot Copilot AI requested a review from eiriktsarpalis July 23, 2025 15:13
Copilot finished work on behalf of eiriktsarpalis July 23, 2025 15:13
@stephentoub stephentoub marked this pull request as ready for review July 23, 2025 16:03
Copilot AI and others added 2 commits July 23, 2025 16:09
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@Copilot Copilot AI requested a review from stephentoub July 23, 2025 16:10
Copilot finished work on behalf of stephentoub July 23, 2025 16:10
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot finished work on behalf of stephentoub July 23, 2025 16:18
@eiriktsarpalis eiriktsarpalis merged commit 650df63 into main Jul 23, 2025
9 checks passed
@eiriktsarpalis eiriktsarpalis deleted the copilot/fix-644 branch July 23, 2025 16:52
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.

Cannot deserialize ResourceLinkBlock
3 participants