Skip to content

Fix serialization of annotated content blocks in tool results - #2039

Merged
tarekgh merged 1 commit into
mainfrom
tarekgh/fix-annotated-content-serialization
Nov 24, 2026
Merged

tarekgh merged 1 commit into
mainfrom
tarekgh/fix-annotated-content-serialization

Conversation

@tarekgh

@tarekgh tarekgh commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #2012

Since #1934, tool results containing content blocks with Annotations (or resource links with Icons) fail to serialize on the server, and the client receives -32603 "An error occurred.".

The server serializes CallToolResult with McpJsonUtilities.JsonContext.Default, so the options passed to ContentBlock.Converter are bound to that context. CreateNestedOptions copies those options and adds McpJsonUtilities.JsonContext.Default to the resolver chain, which System.Text.Json rejects because a resolver cannot be chained into options already bound to it:

System.InvalidOperationException: Cannot insert the values of 'TypeInfoResolver' or 'TypeInfoResolverChain' to 'TypeInfoResolverChain'.

Fix

When the caller options are bound to the SDK context, they already resolve the nested Annotations and Icon contracts, so GetNestedTypeInfo now 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 a tools/call handler. It fails before the fix with the exception above and passes after it. ModelContextProtocol.Tests and ModelContextProtocol.AspNetCore.Tests pass on all target frameworks.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@tarekgh
tarekgh merged commit 90476da into main Nov 24, 2026
11 checks passed
@tarekgh
tarekgh deleted the tarekgh/fix-annotated-content-serialization branch November 24, 2026 16:13
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.

2.4.0: tools/call results with annotated content blocks fail to serialize (InvalidOperationException in ContentBlock.Converter.CreateNestedOptions)

3 participants