Skip to content

Serialization exception when invoking a tool with an array parameter #1846

Description

@KubaZ2

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.

Activity

  1. luisangelrod commented on Aug 29, 2026

    @luisangelrod
    Contributor

    I reproduced this on current main using the existing Native AOT test app. Passing a List<object> argument to a tool that accepts string[] fails in McpClient.ToArgumentsDictionary with the reported missing-metadata exception.

    I have a focused local fix that adds List<object> to McpJsonUtilities’ source-generated context and extends the AOT app to invoke an array-parameter tool, so CI exercises the path in Debug and Release. The native test fails before the metadata addition and passes afterward.

    I’d like to take this issue and open a PR with that scope if the direction looks right.

    Note

    This comment was prepared with AI assistance.

  2. kondv commented on Sep 8, 2026

    @kondv

    @luisangelrod reproduced this and described the same fix shape here on 29 August. I opened #1857 nine days later when no PR had appeared — happy to close it if you would rather land yours.

    For the record, #1857 is the List<object> registration in McpJsonUtilities.JsonContext plus array coverage in the Native AOT compatibility app. A/B on the current head, both arms natively compiled and run from the published executable: with only the McpJsonUtilities.cs line reverted it throws JsonTypeInfo metadata for type 'System.Collections.Generic.List\1[System.Object]' was not provided, and with the fix it prints Success!for both a populated and an empty list. The scalarEcho` tool passes in both arms, so the failure is specific to the array shape rather than to AOT tool invocation generally.

    Update: #1857 is closed and replaced by #1872, which is the same change rebased onto current main. Same offer stands.

    — kondv's Copilot 🤖

  3. kondv commented on Sep 12, 2026

    @kondv

    PR is up: #1872, rebased onto current main (cb3e6e87). Same scope as my earlier comment — the List<object> registration in McpJsonUtilities.JsonContext, plus a Join(string[] items) tool in the Native AOT compatibility app invoked with a populated and an empty list so make test-aot covers it.

    Re-ran the A/B on the rebased branch, both arms natively compiled and run from the published executable:

    Build Result
    With the fix Success!, exit 0
    With only the McpJsonUtilities.cs line reverted NotSupportedException on List\1[System.Object]`, exit non-zero

    @luisangelrod the offer from before still stands — happy to close #1872 if you would rather land yours.

    — kondv's Copilot 🤖

  4. luisangelrod commented on Oct 2, 2026

    @luisangelrod
    Contributor

    Thanks for following through and for checking with me. Please continue with #1872; your PR covers the same fix and Native AOT regression scope I had validated, so opening another would only duplicate work. I will step aside and follow the review.

    Note

    This response was prepared with AI assistance.

  5. halter73 commented on Oct 6, 2026

    @halter73
    Contributor

    Thanks for tracing this through Ollama and adding the regression. I’m closing this because List<object> is Ollama’s representation of a JSON array, not a type MCP or FunctionInvokingChatClient requires. We shouldn’t add each provider’s runtime argument types to the SDK’s default AOT context. Applications needing this representation can register it in a custom context and chain resolvers, then pass those options to ListToolsAsync via RequestOptions.JsonSerializerOptions. Ollama could also preserve arrays as JsonElement, which the MCP client already accepts.

  6. girishkvs commented on Oct 6, 2026

    @girishkvs

    @KubaZ2, PR #1872 was closed by @halter73 because List<object> is how Ollama represents JSON arrays, not a type MCP requires. He suggested a custom serializer context instead. I've only observed this with Ollama; have you seen it with any other packages?

    — girishkvs's Copilot 🤖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions