Skip to main content

Module schema

Module schema 

Source
Expand description

Utilities for normalizing JSON Schema values produced by the #[command] macro into language-agnostic JSON Schema suitable for MCP tool schemas and remote GET /cmd.json-style consumers.

The Rust schemars crate emits several Rust/OpenAPI-flavored fields that are NOT part of the JSON Schema standard and that generic consumers — including the TypeScript implementation — do not expect:

  • $schema — the draft URL. Informational only; dropped.
  • title — defaults to the Rust type name (e.g. "BinaryOpReq", "int64"). Dropped so the wire payload is free of Rust identifiers.
  • format on numeric types (int64, int32, uint64, float, double, etc.) — these are OpenAPI extensions, not standard JSON Schema format values. format is only meaningful on type: "string" (e.g. "date-time", "email", "uri", "uuid"), so we strip it everywhere except on string schemas.

The normalizer also adds additionalProperties: false to any object-typed schema that doesn’t already specify one, so the advertised schema reflects the fact that every request/response type in Rust is strict by construction (extra JSON fields would fail at serde_json::from_value regardless).

Recursion walks every nested schema — including properties, items, definitions / $defs, and oneOf / anyOf / allOf — so the transformation is applied uniformly.

Users who hand-implement Command::schema can call normalize_schema on their manual output to get the same shape the macro produces.

Functions§

normalize_schema
Rewrites a JSON Schema value in place: strips title and $schema recursively, drops format on non-string schemas, and adds additionalProperties: false to every object schema that doesn’t already declare one.