-
Notifications
You must be signed in to change notification settings - Fork 585
Description
Describe the bug
When using Semantic Kernel sampling via MCP, the internal payload passed from Semantic Kernel to MCP contains a dictionary whose values may include anonymous types (objects generated by Semantic Kernel and not controllable by the application).
In ModelContextProtocol 0.5.0-preview.1, calling
ModelContextProtocol.AIContentExtensions.ToJsonObject(IReadOnlyDictionary<string, object?> properties)
throws a System.NotSupportedException when any dictionary value is an anonymous type.
This previously worked in 0.4.0-preview.3 (stdio transport only) but now fails in 0.5.0-preview.1, including stdio, indicating that stdio and web transports now share the same serialization pipeline.
To Reproduce
Use Semantic Kernel with MCP sampling (AddSamplingChatClient)
Trigger a prompt invocation where Semantic Kernel generates arguments/metadata internally
Ensure the dictionary passed into
ModelContextProtocol.AIContentExtensions.ToJsonObject(...)
contains any anonymous type as a value (structure does not matter)
Run with ModelContextProtocol 0.5.0-preview.1
Observe serialization failure
Minimal conceptual example:
var dict = new Dictionary<string, object?>
{
["data"] = new { X = 1.0, Y = 2.0 }
};
AIContentExtensions.ToJsonObject(dict);
Expected behavior
ToJsonObject should be able to serialize dictionaries whose values are anonymous types, producing a valid JsonObject / JsonNode.
At minimum, anonymous types should be handled as plain object graphs rather than requiring source-generated JsonTypeInfo, which cannot be provided for anonymous types.
This is especially important because:
These anonymous types are generated internally by Semantic Kernel
Application developers cannot intercept or replace them
Logs
System.NotSupportedException: JsonTypeInfo metadata for type
'<>f__AnonymousType0`5[...]' was not provided by TypeInfoResolver
of type 'ModelContextProtocol.McpJsonUtilities+JsonContext'
at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType
at System.Text.Json.JsonSerializer.SerializeToNode
at ModelContextProtocol.AIContentExtensions.ToJsonObject
at ModelContextProtocol.Server.McpServer.SampleAsync(...)
Additional context
In 0.4.0-preview.3:
stdio transport worked
web transport failed (likely due to different serialization pipelines)
In 0.5.0-preview.1:
Both stdio and web transports fail
Indicates a unified pipeline that now enforces the same resolver behavior
Request / suggestion:
Enhance ModelContextProtocol.AIContentExtensions.ToJsonObject to support
dictionary values containing anonymous types, either by:
Falling back to a non–source-generated resolver for such values, or
Detecting anonymous types and serializing them via a compatible JsonSerializerOptions
This would restore compatibility with Semantic Kernel sampling scenarios
where developers cannot control the generated object shapes.