Skip to content

Problem with deserialization of Response Error property #512

@keiserS

Description

@keiserS

Bug Report

Overview

When calling tools and a tool throws an exception the Error property is not being deserialized properly.

To Reproduce

Steps to reproduce the behavior:
MCP tool throws the exception like:

        // Check if the client supports elicitation
        if (server.ClientCapabilities?.Elicitation == null)
        {
            // fail the tool call
            throw new McpException("Client does not support elicitation");
        }

in the client error thrown in response deserialization method:

  internal static T Deserialize<T>(this HttpResponseMessage response, JsonNode jNode, OpenAIClient client)
  {
      T result;
      try
      {
          result = jNode.Deserialize<T>(OpenAIClient.JsonSerializationOptions);  // <<<< trows exception
      }
      catch (Exception e)
      {
          Console.WriteLine($"Failed to parse {typeof(T).Name} -> {jNode.ToJsonString(DebugJsonOptions)}\n{e}");
          throw;
      }

      if (result is BaseResponse resultResponse)
      {
          resultResponse.SetResponseData(response.Headers, client);
      }

      return result;
  }

Exception:
{"The JSON value could not be converted to System.String. Path: $.error | LineNumber: 0 | BytePositionInLine: 205."} Cannot get the value of a token type 'StartObject' as a string.

FIX:

    /// <summary>
    /// An invocation of a tool on an MCP server.
    /// </summary>
    public sealed class MCPToolCall : BaseResponse, IResponseItem
    {...
        /// <summary>
        /// The error from the tool call, if any.
        /// </summary>
        [JsonInclude]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        [JsonPropertyName("error")]
        public JsonNode Error { get; private set; }   /// <<< change prop type
    }

this will at the end work for (This will deserialize):

  • null
  • a string
  • an object
  • an array

No further changes required.

Metadata

Metadata

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