Skip to content

Optional parameters not working #357

@rlacaci

Description

@rlacaci

Bug Description
I'm doing simple SDK example in order to learn how to use it for my application. One think that I need and I've not seen in any examples is about optional parameters in a function. I tried to simulate it with string? date

 [McpServerTool, Description("Gets the weather")]
 public string GetWeather([Description("City code")] string cityCode, [Description("Date in yyyy-mm-dd format")] string? date = null)
 {

but when using from MCP Client both parameters appears like required in the function definition.

I've been debugging and investigating a possible solution and the reason seems to be due to default JsonSchemaCreateOptions.RequireAllProperties that is true so regardless of the parameter is or not optional it's always required.

I was able to make it works adding a JsonSchemaCreateOptions in CreateAIFunctionFactoryOptions in AIFunctionMcpServerTool.cs but I'm not familiar with the source code and could be an invalid workaround with side effects (although it works perfectly for me)

 private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
     MethodInfo method, McpServerToolCreateOptions? options) =>
     new()
     {
         Name = options?.Name ?? method.GetCustomAttribute<McpServerToolAttribute>()?.Name,
         Description = options?.Description,
         MarshalResult = static (result, _, cancellationToken) => new ValueTask<object?>(result),
         SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions,
         **JsonSchemaCreateOptions=new AIJsonSchemaCreateOptions()
         {
             RequireAllProperties=false
         }**,
     .......

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