ToolSchemaExt

Trait ToolSchemaExt 

Source
pub trait ToolSchemaExt {
    // Required method
    fn from_schemars(schema: Schema) -> Result<Self, String>
       where Self: Sized;
}
Expand description

Extension trait for ToolSchema to support schemars conversion

This trait is automatically implemented for ToolSchema, providing the from_schemars() method for converting schemars schemas to MCP format.

Required Methods§

Source

fn from_schemars(schema: Schema) -> Result<Self, String>
where Self: Sized,

Convert a schemars JSON Schema to MCP ToolSchema

This enables auto-generating tool output schemas from Rust types using the schemars crate’s JsonSchema derive macro.

§Arguments
  • schema - A schemars Schema generated via schema_for!()
§Returns
  • Ok(ToolSchema) - Successfully converted schema
  • Err(String) - Conversion error message
§Example
use turul_mcp_builders::ToolSchemaExt;
use turul_mcp_protocol::ToolSchema;
use schemars::{JsonSchema, schema_for};
use serde::Serialize;
use std::sync::OnceLock;

#[derive(Serialize, JsonSchema)]
struct Output {
    result: f64,
    timestamp: String,
}

// In your HasOutputSchema implementation:
fn get_output_schema() -> &'static ToolSchema {
    static SCHEMA: OnceLock<ToolSchema> = OnceLock::new();
    SCHEMA.get_or_init(|| {
        let json_schema = schema_for!(Output);
        ToolSchema::from_schemars(json_schema)
            .expect("Valid schema")
    })
}

Implementors§