Skip to content

Prompt parameters of type bool fail when the client sends "true" or "false" #2040

Backlog

Description

@Ahmed-Manai

Describe the bug

A prompt with a bool parameter fails when the client sends the argument as a string, which is how the spec defines prompt arguments (arguments: { [key: string]: string }). The client only gets Request failed (remote): An error occurred.

Numbers sent as strings work, because McpJsonUtilities.DefaultOptions sets JsonNumberHandling.AllowReadingFromString. System.Text.Json has no equivalent for booleans, so "true" can't be read into a bool. Resource templates don't have this problem: AIFunctionMcpServerResource converts each URI value with bool.Parse, int.Parse and so on.

To reproduce

[McpServerPromptType]
public sealed class Prompts
{
    [McpServerPrompt(Name = "count_prompt")]
    public static string CountPrompt(int count) => $"count={count}";

    [McpServerPrompt(Name = "bool_prompt")]
    public static string BoolPrompt(bool formal) => $"formal={formal}";
}
await client.GetPromptAsync("count_prompt", new Dictionary<string, object?> { ["count"] = "5" });
// count=5

await client.GetPromptAsync("bool_prompt", new Dictionary<string, object?> { ["formal"] = "true" });
// McpProtocolException: Request failed (remote): An error occurred.

Same with bool?. Tested with ModelContextProtocol 2.4.0 and with main (90476da).

Expected behavior

"true" and "false" bind to bool and bool? prompt parameters, like numeric strings already bind to numeric parameters. A value that isn't a boolean gives an error that names the parameter.

Possible fix

Add a parameter binder for bool and bool? in AIFunctionMcpServerPrompt.CreateAIFunctionFactoryOptions, next to the existing service binders, that accepts JSON true/false and the strings "true"/"false". Other types don't need it: enums, dates, Guid and TimeSpan are already read from strings by their JSON converters. I have a PR with tests ready.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions