Skip to main content

aprender_mcp/tools/
validate.rs

1//! `apr.validate` — M2 subprocess wrapper over `apr validate <model> --json`.
2//!
3//! This was the first M2 tool shipped and established the subprocess pattern
4//! every M2/M3 `apr.*` wrapper follows: spawn `apr <subcommand> --json`,
5//! capture stdout, pass through to the MCP client as a single text content
6//! block. Non-zero exit maps to `isError: true` with stderr attached. All 7
7//! M2 wrappers (`apr.validate`, `apr.tensors`, `apr.bench`, `apr.qa`,
8//! `apr.trace`, `apr.run`, `apr.serve`) and the M3 addition `apr.finetune`
9//! now ship on this pattern.
10
11#![allow(clippy::disallowed_methods)] // serde_json::json! macro expands to .unwrap() internally
12
13use crate::tools::subprocess::run_apr;
14use crate::types::{InputSchema, ToolCallResult, ToolDefinition};
15
16/// Tool name registered with MCP clients.
17pub const NAME: &str = "apr.validate";
18
19/// Return the MCP tool definition for `apr.validate`.
20///
21/// FALSIFY-MCP-008: the `inputSchema` is parsed from the build-time codegen
22/// constant `crate::schemas::APR_VALIDATE_SCHEMA`, which `build.rs` emits from
23/// `contracts/apr-mcp-tool-schemas-v1.yaml`. The contract is the single
24/// source of truth — the live `tools/list` response and the YAML must agree
25/// byte-for-byte after JSON canonicalization (asserted by
26/// `tests/falsify_mcp_008.rs`).
27#[must_use]
28pub fn validate_tool_definition() -> ToolDefinition {
29    let input_schema: InputSchema = serde_json::from_str(crate::schemas::APR_VALIDATE_SCHEMA)
30        .expect(
31            "FALSIFY-MCP-008: apr.validate codegen constant must parse as InputSchema; \
32             regenerate by editing contracts/apr-mcp-tool-schemas-v1.yaml and rebuilding",
33        );
34    ToolDefinition {
35        name: NAME.to_string(),
36        description: crate::schemas::APR_VALIDATE_DESCRIPTION.to_string(),
37        input_schema,
38    }
39}
40
41/// Execute `apr.validate` by spawning `apr validate <model_path> --json`.
42#[must_use]
43pub fn call(args: &serde_json::Value) -> ToolCallResult {
44    let Some(model_path) = args.get("model_path").and_then(|v| v.as_str()) else {
45        return ToolCallResult::error("Missing required argument: model_path");
46    };
47    run_apr(&["validate", model_path, "--json"])
48}
49
50/// HELIX-IDEA-002 — unified-signature shim for the inventory dispatcher.
51pub fn dispatch(
52    args: &serde_json::Value,
53    _cancel: &std::sync::mpsc::Receiver<()>,
54    _sink: Option<&crate::server::NotificationSink>,
55    _token: Option<serde_json::Value>,
56) -> ToolCallResult {
57    call(args)
58}
59
60crate::register_mcp_tool!(
61    name: NAME,
62    definition: validate_tool_definition,
63    dispatch: dispatch,
64);
65
66#[cfg(test)]
67#[allow(clippy::disallowed_methods)] // serde_json::json! expands to code that hits unwrap()
68mod tests {
69    use super::*;
70
71    #[test]
72    fn definition_has_correct_name_and_required_field() {
73        let def = validate_tool_definition();
74        assert_eq!(def.name, "apr.validate");
75        assert_eq!(def.input_schema.schema_type, "object");
76        assert_eq!(def.input_schema.required, vec!["model_path".to_string()]);
77        assert!(def.input_schema.properties.contains_key("model_path"));
78    }
79
80    #[test]
81    fn missing_model_path_returns_error() {
82        let result = call(&serde_json::json!({}));
83        assert_eq!(result.is_error, Some(true));
84        assert!(result.content[0].text.contains("model_path"));
85    }
86
87    #[test]
88    fn nonstring_model_path_returns_error() {
89        let result = call(&serde_json::json!({ "model_path": 42 }));
90        assert_eq!(result.is_error, Some(true));
91    }
92}