Repository navigation
Bind true and false prompt arguments to bool parameters - #2041
Ahmed-Manai wants to merge 3 commits into
Conversation
Prompt arguments are strings on the wire. Numeric strings already bind to numeric parameters, but System.Text.Json has no equivalent for booleans, so bool and bool? prompt parameters failed when the client sent "true" or "false".
sivaraman-krishnan
left a comment
There was a problem hiding this comment.
Thanks for the clear issue and the focused fix, @Ahmed-Manai! Special-casing bool makes sense to me, since it's the one common scalar System.Text.Json can't read from a string. The PR description explains well why numbers, enums and the other types don't need it.
What I checked locally:
- The solution builds cleanly, and the prompt tests pass on net10.0.
- With the
srcchange reverted, 6 of the new tests fail, so they really cover the fix. - In a quick client/server run,
"TRUE"binds correctly. An invalid value like"yes"reaches the client as-32603with "An error occurred.", so the message naming the parameter only appears in the server logs (inline note below).
A small FYI: McpServerPromptTests is skipped on .NET Framework on Windows (#587), so the new tests won't run on net472 in CI. That's existing behavior, not something this PR needs to change.
| JsonElement { ValueKind: JsonValueKind.String } element when bool.TryParse(element.GetString(), out bool parsed) => parsed, | ||
| null when isNullable => null, | ||
| JsonElement { ValueKind: JsonValueKind.Null } when isNullable => null, | ||
| _ => throw new ArgumentException($"The value for the parameter '{pi.Name}' must be true or false.", pi.Name), |
There was a problem hiding this comment.
Would it make sense to throw McpProtocolException(..., McpErrorCode.InvalidParams) here, and for the missing-value case at line 126? McpSessionHandler only passes the message of an McpException back to the client. Other exceptions become -32603 with "An error occurred.", so a client sending "formal": "yes" still gets the opaque error described in #2040. The prompts spec also lists -32602 (Invalid params) for missing required arguments. Other parameter types already report missing arguments this way, though, so this could reasonably be a follow-up if you'd like to keep the PR focused.
There was a problem hiding this comment.
Thanks for checking it end to end! Good point about the error code. I've switched both the missing-value and invalid-value cases to McpProtocolException with InvalidParams, so the client now gets -32602 with the message naming the parameter, and I added client/server tests for it. And thanks for the note on #587.
There was a problem hiding this comment.
Thanks, @Ahmed-Manai, this looks good to me. Both cases now throw McpProtocolException with InvalidParams, which McpSessionHandler passes through, so the client gets -32602 and the parameter name. Optional bool and bool? parameters still fall back to their defaults first. One small thing: the PR description still mentions ArgumentException, so it may be worth updating.
McpSessionHandler only sends the message of an McpException back to the client, so the ArgumentException from the bool binder reached the client as -32603 with "An error occurred.". Throw McpProtocolException with InvalidParams instead, as the prompts spec lists -32602 for invalid or missing arguments.
|
|
||
| private static object? BindBooleanParameter(ParameterInfo pi, AIFunctionArguments args) | ||
| { | ||
| if (!args.TryGetValue(pi.Name!, out object? value)) |
There was a problem hiding this comment.
Could we look up the parameter's schema name here, including [AIParameterName], instead of pi.Name? A renamed boolean parameter is advertised under the schema name, so this lookup would fail for otherwise valid calls.
There was a problem hiding this comment.
Good catch, thanks! The binder now looks the argument up by the schema name, honoring [AIParameterName] the same way AIFunctionFactory does, and the error messages use that name too. Added a test with a renamed parameter.
| { | ||
| if (!args.TryGetValue(pi.Name!, out object? value)) | ||
| { | ||
| return pi.HasDefaultValue ? pi.DefaultValue : |
There was a problem hiding this comment.
Could we use the effective default value here? pi.HasDefaultValue misses [DefaultValue], so an argument marked optional in the prompt schema can still be rejected as missing.
There was a problem hiding this comment.
Done: it now uses the effective default value, checking [DefaultValue] first, then the C# default, then an optional parameter, which matches how the schema marks the argument as optional. Added a test for a [DefaultValue(true)] parameter whose argument is left out.
The bool binder looked the argument up by the C# parameter name and only used a C# default value. Use the name and default the prompt schema advertises, as AIFunctionFactory does: a parameter renamed with [AIParameterName] binds under its schema name, and [DefaultValue] supplies the value of a missing argument.
Prompt arguments are strings on the wire, so clients send booleans as
"true"or"false". Numeric strings already bind to numeric parameters throughJsonNumberHandling.AllowReadingFromString, but System.Text.Json has no equivalent for booleans, so aboolprompt parameter failed withAn error occurred.This adds a parameter binder for
boolandbool?prompt parameters inAIFunctionMcpServerPrompt:true/falseand the strings"true"/"false"(any casing) are accepted.[AIParameterName]is honored. A missing argument uses the effective default value ([DefaultValue]or the C# default), or throwsMcpProtocolExceptionwithInvalidParams(-32602) naming the parameter if it's required.nullbinds tobool?. Forbool,nulland any other value throwMcpProtocolExceptionwithInvalidParamsnaming the parameter, so the client sees the message instead ofAn error occurred.Only booleans get a custom binder. Enums, dates,
GuidandTimeSpanare already read from strings by their JSON converters, and going through them keeps attributes such as[JsonStringEnumMemberName]working.Tests
New tests in
McpServerPromptTestsfor string and JSON booleans,bool?, a default value, a[DefaultValue]default, a parameter renamed with[AIParameterName], invalid values and a missing required argument, and client/server tests inMcpServerPromptBooleanArgumentTestschecking that the client receives-32602with the parameter name. They fail without the change.On net10.0, all prompt tests pass (138), and the whole
ModelContextProtocol.Testsproject passes except twoDockerEverythingServerTests, which also fail onmainin my environment.Fixes #2040