Repository navigation
Fix serialization of annotated content blocks in tool results - #2039
Merged
Merged
Conversation
ContentBlock.Converter failed with InvalidOperationException when serializing content blocks with annotations or icons using options bound to the SDK source-generated context, because the context cannot be chained into its own options. Use the caller options directly in that case. Fixes #2012
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the C# SDK change restoring serialization of annotated tool results.
No open issues 🎉
What changed in this PR
Fixes #2012, restoring serialization of annotated tool results in the C# SDK after the regression introduced by #1934.
Changes:
- Reuses SDK-bound serializer options for nested content metadata, avoiding the resolver-chaining exception while preserving behavior for other caller options.
- Adds a server regression test covering text and resource-link blocks with annotations and icons.
| File | Description |
|---|---|
tests/ModelContextProtocol.Tests/Server/McpServerTests.cs |
Verifies annotated tool results serialize successfully, including icon metadata. |
src/ModelContextProtocol.Core/Protocol/ContentBlock.cs |
Resolves nested metadata directly when options use the SDK context. |
Review effort: Balanced
halter73
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2012
Since #1934, tool results containing content blocks with
Annotations(or resource links withIcons) fail to serialize on the server, and the client receives-32603 "An error occurred.".The server serializes
CallToolResultwithMcpJsonUtilities.JsonContext.Default, so the options passed toContentBlock.Converterare bound to that context.CreateNestedOptionscopies those options and addsMcpJsonUtilities.JsonContext.Defaultto the resolver chain, which System.Text.Json rejects because a resolver cannot be chained into options already bound to it:Fix
When the caller options are bound to the SDK context, they already resolve the nested
AnnotationsandIconcontracts, soGetNestedTypeInfonow uses them directly instead of creating nested options. Behavior for any other caller options is unchanged.Tests
Added
Call_Tool_Result_With_Annotated_Content_Serializes, which returns a text block and a resource link with annotations and icons from atools/callhandler. It fails before the fix with the exception above and passes after it.ModelContextProtocol.TestsandModelContextProtocol.AspNetCore.Testspass on all target frameworks.