modelcontextprotocol / modelcontextprotocol/csharp-sdk
Serialization exception when invoking a tool with an array parameter
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.5k
- Forks
- 814
- Avg merge
- 9d 19h
- Merged PRs (30d)
- 4
Description
Describe the bug
Invoking a tool which accept an array fails with a serialization error on Native AOT.
To Reproduce
Call a tool which accepts an array as a parameter, for example read_multiple_files from https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem. I used the MS extensions AI integration and used FunctionInvokingChatClient.
Expected behavior
I would expect the default serializer context to provide List<object>.
Logs
System.NotSupportedException: JsonTypeInfo metadata for type 'System.Collections.Generic.List`1[System.Object]' was not provided by TypeInfoResolver of type '[ModelContextProtocol.McpJsonUtilities+JsonContext, Microsoft.Extensions.AI.AIJsonUtilities+JsonContext]'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoForRootType(Type type, Boolean fallBackToNearestAncestorType)
at System.Text.Json.JsonSerializerOptions.TryGetPolymorphicTypeInfoForRootType(Object rootValue, JsonTypeInfo& polymorphicTypeInfo)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
at System.Text.Json.JsonSerializer.WriteElement[TValue](TValue& value, JsonTypeInfo`1 jsonTypeInfo)
at System.Text.Json.JsonSerializer.SerializeToElement[TValue](TValue value, JsonTypeInfo`1 jsonTypeInfo)
Additional context
I think the problem is caused by the fact the parameter value gets deserialized as List<object> by MS Extensions AI.
I know this can likely be worked around by manually providing the List<object> serialization metadata or even by converting the value to some known type, but I thought reporting this may be a good idea.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Native AOT failure with FunctionInvokingChatClient and the read_multiple_files tool, then trace how its array parameter becomes List before System.Text.Json serialization. Check the serializer context used for tool arguments and add coverage showing that an array parameter can be serialized successfully under Native AOT.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100