Implements outputSchema validation #566
Open
+125
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #532
Implements validation to enforce MCP specification requirement that tool
outputSchemamust have a root type of"object". Tools using structured output (Json<T>) where T is a primitive type will fail at compile time with clear error messages.Motivation and Context
As reported in issue #532, the MCP specification requires tool
outputSchemato have a root type of"object"(see MCP Tool Schema). The Rust SDK did not validate this, allowing spec-violating schemas to be generated.The latest spec published on Nov 25 states this more clearly:
How Has This Been Tested?
Noticed the
subtool in the calculator example in the repo doesn't comply with the MCP spec, which causes an issue with MCP Insepctor.rust-sdk/examples/servers/src/common/calculator.rs
Lines 46 to 49 in 94428d5
Now that the output schema validation is in place, the server panics during startup:
➜ servers git:(outputschema-validation) ✗ cargo run -p mcp-server-examples --example servers_calculator_stdio Compiling mcp-server-examples v0.1.5 (/Users/dale/work/rust-sdk/examples/servers) Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.31s Running `/Users/dale/work/rust-sdk/target/debug/examples/servers_calculator_stdio` 2025-11-29T18:50:44.901451Z INFO servers_calculator_stdio: Starting Calculator MCP server thread 'main' panicked at examples/servers/src/common/calculator.rs:46:5: Invalid output schema for Json<i32>: MCP specification requires tool outputSchema to have root type 'object', but found 'integer'. note: run with `RUST_BACKTRACE=1` environment variable to display a backtraceAfter fixing the calculator example code so that the
subtool returns unstructured output just like thesumtool, the server starts up without any issues and works well with the MCP Inspector.➜ servers git:(outputschema-validation) ✗ cargo run -p mcp-server-examples --example servers_calculator_stdio Compiling mcp-server-examples v0.1.5 (/Users/dale/work/rust-sdk/examples/servers) Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.41s Running `/Users/dale/work/rust-sdk/target/debug/examples/servers_calculator_stdio` 2025-11-29T19:05:00.312995Z INFO servers_calculator_stdio: Starting Calculator MCP serverAs suggested in issue #532, wrapping the primitive type also works as expected.
Breaking Changes
Existing valid code works unchanged:
i32,String) with no output stream continues to work<Json<i32>,<Json<String>) fails to compileTypes of changes
Checklist
Additional context
The TypeScript SDK also enforces this requirement in the Tool schema definition here: