Skip to content

[server] Fix exclusive numeric bounds in generated tool schemas - #772

Open
bbdu3 wants to merge 1 commit into
modelcontextprotocol:mainfrom
bbdu3:fix/schema-exclusive-bounds
Open

bbdu3 wants to merge 1 commit into
modelcontextprotocol:mainfrom
bbdu3:fix/schema-exclusive-bounds

Conversation

@bbdu3

@bbdu3 bbdu3 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds support for JSON Schema 2020-12 numeric values for exclusiveMinimum and exclusiveMaximum, while maintaining backward compatibility with existing boolean configurations.

Fixes #754.

Previously:

#[Schema(minimum: 0, exclusiveMinimum: true)]

produced:

{"minimum": 0, "exclusiveMinimum": true}

It now produces:

{"minimum": 0, "exclusiveMinimum": 0}

Direct numeric configuration is also supported:

#[Schema(exclusiveMinimum: 0, exclusiveMaximum: 5)]

Motivation and Context

MCP defaults to JSON Schema 2020-12 when a schema does not declare another dialect. In this dialect, both exclusive-bound keywords require numeric values.

The SDK previously declared these attribute settings as booleans and emitted them unchanged. Strict validators such as Ajv 2020 reject the resulting schemas, including configurations using false. The SDK’s default Opis validator accepts the legacy boolean form through a compatibility option, which masked the interoperability problem.

Changes

  • Allow int|float|bool|null for both exclusive-bound settings.
  • Normalize legacy boolean settings after method-level and parameter-level configurations have been merged:
    • true becomes the corresponding numeric minimum or maximum.
    • false removes the exclusive-bound keyword while retaining any inclusive bound.
    • Numeric values remain unchanged, including zero, negative values and fractions.
  • Preserve configuration precedence: bounds can be inherited from method-level settings, and parameter-level flags can override them.
  • Raise an SDK InvalidArgumentException during schema generation when true has no corresponding numeric bound after merging.
  • Preserve complete definition overrides without converting their contents.
  • Update the existing numeric-constraint test, add 15 regression cases, and document the supported forms and compatibility implications.

Testing

  • PHP 8.3.36: 552 relevant unit tests passed, covering capabilities, discovery, tool-call handling and server builder behavior.
  • PHP 8.5.13 on Windows: 40 integer-argument integration tests passed.
  • Real STDIO verification: both 2025-11-25 and 2026-07-28 completed 32 valid/invalid tool calls across numeric, legacy, inclusive and inherited bounds.
  • Ajv 2020 strict validation: all eight schemas received through those connections compiled successfully; 80 boundary checks passed.
  • PHPStan, PHP CS Fixer, strict documentation build and git diff --check: passed.

Compatibility

Existing boolean configurations with valid corresponding bounds remain supported.

Two compatibility implications are recorded in the changelog:

  • A true flag without a corresponding numeric bound now fails during schema generation instead of being silently ignored.
  • Subclasses redeclaring either public exclusive-bound property must match its widened int|float|bool|null type.

Signed-off-by: bbdu3 <ergouyang854@gmail.com>

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.

[Server] #[Schema(exclusiveMinimum: true)] is invalid under JSON Schema 2020-12

1 participant