Skip to content

Optional parameters not working #357

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")]publicstringGetWeather([Description("City code")]stringcityCode,[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)

privatestaticAIFunctionFactoryOptionsCreateAIFunctionFactoryOptions(MethodInfomethod,McpServerToolCreateOptions?options)=>new(){Name=options?.Name??method.GetCustomAttribute<McpServerToolAttribute>()?.Name,Description=options?.Description,MarshalResult=static(result,_,cancellationToken)=>newValueTask<object?>(result),SerializerOptions=options?.SerializerOptions??McpJsonUtilities.DefaultOptions,**JsonSchemaCreateOptions=newAIJsonSchemaCreateOptions(){RequireAllProperties=false}**,
.......

Activity

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