Skip to content

Allowed to set a custom JsonSchemaGeneratorOptions for MCP tool schema #543

Description

@fvdcx

Say I have such a tool input object class

    public class TemporalData
    {
        [JsonPropertyName("dateTimeValue")]
        public DateTime DateTimeValue { get; set; }

        [JsonPropertyName("dateTimeOffsetValue")]
        public DateTimeOffset DateTimeOffsetValue { get; set; }

        [JsonPropertyName("timeSpanValue")]
        public TimeSpan TimeSpanValue { get; set; }
    }

Its corresponding input schema (auto-generated by sdk 0.3.0, no hack here)is

{
        "name": "test",
        "description": "Test tool that accepts a TemporalData object with DateTime, DateTimeOffset, and TimeSpan properties.",
        "inputSchema": {
          "type": "object",
          "properties": {
            "input": {
              "description": "A TemporalData object with temporal properties",
              "type": "object",
              "properties": {
                "dateTimeValue": {
                  "type": "string"
                },
                "dateTimeOffsetValue": {
                  "type": "string"
                },
                "timeSpanValue": {
                  "$comment": "Represents a System.TimeSpan value.",
                  "type": "string"
                }
              }
            }
          },
          "required": [
            "input"
          ]
        }
      }

Three date time related types are there

  • DateTime - dateTimeValue
  • DateTimeOffSet - dateTimeOffsetValue
  • TimeSpan - timeSpanValue
    As you can see, only TimeSpan type has been added a comment "$comment": "Represents a System.TimeSpan value.", automatically.

However, it's super important for AI agent to understand the field type especically when the type isn't supported in JSON natively like datetime, so sdk should provide a way to allow me to set a JsonSchemaGeneratorOptions to add a TypeMapping,

options.TypeMappings.Add(
  new JsonSchemaTypeMapping<DateTime>((_, schema) =>
  {
    schema.Type    = JsonSchemaType.String;
    schema.Format  = "date-time";
    schema.Comment = "Represents a System.DateTime";
    return true;
  })
);

Then for DateTime type in inputSchema, it'll be like

"dateTimeValue": {
  "$comment": "Represents a System.DateTime",
  "type": "string",
  "format": "date-time"
}

Let me know if this makes sense

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions