Skip to content

Optional parameters not working #357

Closed

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
         }**,
     .......

Activity

  1. 1357310795 commented on Apr 26, 2025

    @1357310795
    Contributor

    See #354
    you can use McpServerTool.Create to pass AIJsonSchemaCreateOptions now.

    although this cannot yet be achieved using the most commonly used WithTool

    my workaround is duplicating WithTool/WithToolsFromAssembly into project and apply some minor adjustments

    https://github.com/SJTU-Geek/sjtu-mcp-server/blob/a9d8b6ef3f0ff3ea8af229976f0f0b983694bc34/SJTUGeek.MCP.Server/Extensions/McpScriptBuilderExtensions.cs#L55

  2. jason-lee-x commented on Apr 28, 2025

    @jason-lee-x

    That was really helpful!
    Thanks a lot.


    Hoping the WithToolsFromCurrentAssembly can be parts of SDK.

  3. stephentoub commented on Apr 28, 2025

    @stephentoub
    Contributor
  4. 1357310795 commented on Apr 28, 2025

    @1357310795
    Contributor

    Passing AIJsonSchemaCreateOptions into WithTool/WithToolsFromAssembly maybe need a more flexible way.

    Perhaps there are more parameters that need to be passed in the future

  5. eiriktsarpalis commented on Apr 28, 2025

    @eiriktsarpalis
    Member

    We recently merged this PR in Microsoft.Extensions.AI that among other things changes the default value of RequireAllProperties to false. The issue should resolve itself once the MCP sdk references an updated version of MEAI.

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