Skip to main content

JsonSchema

Derive Macro JsonSchema 

Source
#[derive(JsonSchema)]
{
    // Attributes available to this derive:
    #[json_schema]
}
Expand description

Derives JSON Schema for a type.

Used for generating input schemas for tools. Generates a json_schema() method that returns the JSON Schema representation of the type.

§Example

use fastmcp_rust::JsonSchema;

#[derive(JsonSchema)]
struct MyToolInput {
    /// The name of the person
    name: String,
    /// Optional age
    age: Option<u32>,
    /// List of tags
    tags: Vec<String>,
}

// Generated schema:
// {
//   "type": "object",
//   "properties": {
//     "name": { "type": "string", "description": "The name of the person" },
//     "age": { "type": "integer", "description": "Optional age" },
//     "tags": { "type": "array", "items": { "type": "string" }, "description": "List of tags" }
//   },
//   "required": ["name", "tags"]
// }

§Supported Types

  • String, &str"string"
  • i8..i128, u8..u128, isize, usize"integer"
  • f32, f64"number"
  • bool"boolean"
  • Option<T> → schema for T, field not required
  • Vec<T>"array" with items schema
  • HashMap<String, T>"object" with additionalProperties
  • Other types → "object" (custom types should derive JsonSchema)

§Attributes

  • #[json_schema(rename = "...")] - Rename the field in the schema
  • #[json_schema(skip)] - Skip this field
  • #[json_schema(flatten)] - Flatten nested object properties