Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Bind true and false prompt arguments to bool parameters - #2041

Open
Ahmed-Manai wants to merge 3 commits into
modelcontextprotocol:mainfrom
Ahmed-Manai:prompt-bool-arguments
Open

Ahmed-Manai wants to merge 3 commits into
modelcontextprotocol:mainfrom
Ahmed-Manai:prompt-bool-arguments

Conversation

@Ahmed-Manai

@Ahmed-Manai Ahmed-Manai commented Nov 25, 2026 •

Copy link
Copy Markdown

Prompt arguments are strings on the wire, so clients send booleans as "true" or "false". Numeric strings already bind to numeric parameters through JsonNumberHandling.AllowReadingFromString, but System.Text.Json has no equivalent for booleans, so a bool prompt parameter failed with An error occurred.

This adds a parameter binder for bool and bool? prompt parameters in AIFunctionMcpServerPrompt:

  • JSON true/false and the strings "true"/"false" (any casing) are accepted.
  • Arguments are looked up by the parameter's schema name, so [AIParameterName] is honored. A missing argument uses the effective default value ([DefaultValue] or the C# default), or throws McpProtocolException with InvalidParams (-32602) naming the parameter if it's required.
  • null binds to bool?. For bool, null and any other value throw McpProtocolException with InvalidParams naming the parameter, so the client sees the message instead of An error occurred.

Only booleans get a custom binder. Enums, dates, Guid and TimeSpan are already read from strings by their JSON converters, and going through them keeps attributes such as [JsonStringEnumMemberName] working.

Tests

New tests in McpServerPromptTests for 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 in McpServerPromptBooleanArgumentTests checking that the client receives -32602 with the parameter name. They fail without the change.

On net10.0, all prompt tests pass (138), and the whole ModelContextProtocol.Tests project passes except two DockerEverythingServerTests, which also fail on main in my environment.

Fixes #2040

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".
@halter73 halter73 self-assigned this Nov 25, 2026

@sivaraman-krishnan sivaraman-krishnan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 src change 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 -32603 with "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),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants