Skip to main content

render_json_schema

Function render_json_schema 

Source
pub fn render_json_schema(command: &Command) -> Result<String, Error>
Expand description

Generate a JSON Schema (draft-07) describing the inputs for a command.

The schema object is suitable for use in agent tool definitions (e.g. OpenAI function calling, Anthropic tool use, MCP tool input schemas).

Arguments appear as required string properties (with "required" if marked so). Flags with crate::model::Flag::takes_value become string properties; boolean flags become boolean properties.

ยงExamples

let cmd = Command::builder("deploy")
    .summary("Deploy a service")
    .argument(Argument::builder("env").required().description("Target environment").build().unwrap())
    .flag(Flag::builder("dry-run").description("Simulate only").build().unwrap())
    .flag(Flag::builder("strategy")
        .takes_value()
        .choices(["rolling", "blue-green"])
        .description("Rollout strategy")
        .build().unwrap())
    .build().unwrap();

let schema = render_json_schema(&cmd).unwrap();
let v: serde_json::Value = serde_json::from_str(&schema).unwrap();
assert_eq!(v["title"], "deploy");
assert_eq!(v["properties"]["env"]["type"], "string");
assert_eq!(v["properties"]["dry-run"]["type"], "boolean");
let strats = v["properties"]["strategy"]["enum"].as_array().unwrap();
assert_eq!(strats.len(), 2);