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§
Sourcefn from_schemars(schema: Schema) -> Result<Self, String>where
Self: Sized,
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 viaschema_for!()
§Returns
Ok(ToolSchema)- Successfully converted schemaErr(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")
})
}