Skip to content

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 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
@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 Copilot AI requested a review from stephentoub July 23, 2025 16:10
Copilot finished work on behalf of stephentoub July 23, 2025 16:10
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
microsoft-github-policy-service bot pushed a commit to Azure/bicep that referenced this pull request Aug 25, 2025
…7897)

Updated
[ModelContextProtocol](https://github.com/modelcontextprotocol/csharp-sdk)
from 0.3.0-preview.2 to 0.3.0-preview.4.

<details>
<summary>Release notes</summary>

_Sourced from [ModelContextProtocol's
releases](https://github.com/modelcontextprotocol/csharp-sdk/releases)._

## 0.3.0-preview.4

## What's Changed
* Respect HandleResponse() and SkipHandler() calls in
OnResourceMetadataRequest by @​halter73 in
modelcontextprotocol/csharp-sdk#607
* UnreferenceDisposable made slimmer by @​Scooletz in
modelcontextprotocol/csharp-sdk#627
* IdleTracking uses lists instead of SortedSet by @​Scooletz in
modelcontextprotocol/csharp-sdk#629
* Fix ResourceLinkBlock deserialization by adding missing "name" case by
@​Copilot in modelcontextprotocol/csharp-sdk#645
* Add an in-memory transport sample by @​stephentoub in
modelcontextprotocol/csharp-sdk#664
* Remove 'Sse' from AspNetCoreSseServer sample name by @​halter73 in
modelcontextprotocol/csharp-sdk#665
* Fix NotSupportedException when returning IEnumerable<ContentBlock> by
@​Copilot in modelcontextprotocol/csharp-sdk#675
* Enhance HTTP and MCP session logging by @​theojiang25 in
modelcontextprotocol/csharp-sdk#608
* Remove special-casing of string enumerables in McpServerTool by
@​stephentoub in
modelcontextprotocol/csharp-sdk#699
* Prune idle sessions before starting new ones by @​halter73 in
modelcontextprotocol/csharp-sdk#701
* Add framework for conceptual docs by @​mikekistler in
modelcontextprotocol/csharp-sdk#708

## New Contributors
* @​Scooletz made their first contribution in
modelcontextprotocol/csharp-sdk#627
* @​Copilot made their first contribution in
modelcontextprotocol/csharp-sdk#645
* @​theojiang25 made their first contribution in
modelcontextprotocol/csharp-sdk#608

**Full Changelog**:
modelcontextprotocol/csharp-sdk@v0.3.0-preview.3...v0.3.0-preview.4

## 0.3.0-preview.3

## What's Changed
* Enable netfx testing. by @​eiriktsarpalis in
modelcontextprotocol/csharp-sdk#588
* fix: Prevent crash when Options.ResourceMetadata is null but handled
by event by @​DavidParks8 in
modelcontextprotocol/csharp-sdk#603
* Update to M.E.AI 9.7.0 by @​stephentoub in
modelcontextprotocol/csharp-sdk#602
* Ensure IsExternalInit is type forwarded on NET builds by @​stephentoub
in modelcontextprotocol/csharp-sdk#619
* Flow ExecutionContext with JsonRpcMessage by @​halter73 in
modelcontextprotocol/csharp-sdk#616
* Update MEAI version and add regression test for #​601. by
@​eiriktsarpalis in
modelcontextprotocol/csharp-sdk#628

## New Contributors
* @​DavidParks8 made their first contribution in
modelcontextprotocol/csharp-sdk#603

**Full Changelog**:
modelcontextprotocol/csharp-sdk@v0.3.0-preview.2...v0.3.0-preview.3

Commits viewable in [compare
view](modelcontextprotocol/csharp-sdk@v0.3.0-preview.2...v0.3.0-preview.4).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=ModelContextProtocol&package-manager=nuget&previous-version=0.3.0-preview.2&new-version=0.3.0-preview.4)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/17897)

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
ouvreboite pushed a commit to ouvreboite/csharp-sdk that referenced this pull request Aug 26, 2025
…odelcontextprotocol#645)

* Initial plan

* Initial analysis and plan for ResourceLinkBlock deserialization fix

Co-authored-by: eiriktsarpalis <[email protected]>

* Fix ResourceLinkBlock deserialization by adding missing "name" case

Co-authored-by: eiriktsarpalis <[email protected]>

* Remove unnecessary dotnet-install.sh file

Co-authored-by: eiriktsarpalis <[email protected]>

* Use const for JSON string variables and improve indentation

Co-authored-by: stephentoub <[email protected]>

* Remove temporary backup file

Co-authored-by: stephentoub <[email protected]>

* Change JSON variable names to use PascalCase (Json) as requested

Co-authored-by: stephentoub <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: eiriktsarpalis <[email protected]>
Co-authored-by: stephentoub <[email protected]>
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