Skip to main content

qualia_mcp/tools/
llm.rs

1//! MCP tools for Local LLM GGUF Autoregressive Inference and Chat.
2//!
3//! Migrated from legacy `mcp_stub_impls.rs` to dedicated library module.
4//! Designed according to `AGENTS.md` ยง0-B (<500 lines).
5
6use crate::types::{hex_decode, parse_tool_args, qualia_storage_path, McpSystemError};
7use serde_json::{json, Value};
8
9/// Helper to serialize a single `NQuin` to JSON.
10fn quin_to_json(q: &qualia_core_db::NQuin) -> Value {
11    json!({
12        "subject": q.subject,
13        "predicate": q.predicate,
14        "object": q.object,
15        "context": q.context,
16        "metadata": q.metadata,
17        "parity": q.parity,
18    })
19}
20
21/// `llm_infer` tool โ€” runs local GGUF model autoregressive inference.
22pub fn llm_infer(args: &[u8]) -> Result<String, McpSystemError> {
23    let v = parse_tool_args(args)?;
24    let prompt = v.get("prompt").and_then(Value::as_str).unwrap_or("");
25    if prompt.is_empty() {
26        return Err(McpSystemError::InvalidParameters);
27    }
28    let graph_context = v.get("graph_context").and_then(Value::as_str).unwrap_or("");
29    let model_path = v
30        .get("model_path")
31        .and_then(Value::as_str)
32        .map(str::to_string)
33        .or_else(|| qualia_core_db::resident_model::resident_gguf_path())
34        .or_else(|| std::env::var("QUALIA_ACTIVE_GGUF").ok())
35        .unwrap_or_default();
36
37    if model_path.is_empty() {
38        return Err(McpSystemError::InvalidParameters);
39    }
40
41    let sampler_cbor = v.get("sampler_cbor").and_then(Value::as_str).unwrap_or("");
42    let sampler_cfg = if sampler_cbor.is_empty() {
43        None
44    } else {
45        hex_decode(sampler_cbor)
46            .ok()
47            .as_deref()
48            .and_then(qualia_core_db::sampler::SamplerConfig::from_cbor)
49            .filter(|c| !c.is_greedy())
50    };
51    let sampler_installed = sampler_cfg.is_some();
52    let agent = qualia_core_db::llm_agent::LocalLlmAgent::new("did:qualia:mcp-agent", &model_path);
53    qualia_core_db::mcp::inference_session::bind(&agent)
54        .map_err(McpSystemError::ExecutionFailed)?;
55    if sampler_installed {
56        qualia_core_db::llm_bench::set_sampler_config(sampler_cfg);
57    }
58
59    let (text, provenance, tokens, semantic_quin) =
60        agent.infer_local_model_streaming(prompt, graph_context, None::<fn(String)>);
61
62    if sampler_installed {
63        qualia_core_db::llm_bench::set_sampler_config(None);
64    }
65
66    let payload = json!({
67        "text": text,
68        "tokensGenerated": tokens,
69        "provenanceHashes": provenance,
70        "semanticQuin": semantic_quin.map(|q| quin_to_json(&q)),
71        "modelPath": model_path,
72        "sampled": sampler_installed,
73    });
74    serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError)
75}
76
77/// `llm_chat` tool โ€” multi-turn chat completion via the local GGUF inference engine.
78pub fn llm_chat(args: &[u8]) -> Result<String, McpSystemError> {
79    let v = parse_tool_args(args)?;
80    let messages = v
81        .get("messages")
82        .and_then(Value::as_array)
83        .ok_or(McpSystemError::InvalidParameters)?;
84    if messages.is_empty() {
85        return Err(McpSystemError::InvalidParameters);
86    }
87    let mut prompt = String::new();
88    for msg in messages {
89        let role = msg.get("role").and_then(Value::as_str).unwrap_or("user");
90        let content = msg.get("content").and_then(Value::as_str).unwrap_or("");
91        prompt.push_str(role);
92        prompt.push_str(": ");
93        prompt.push_str(content);
94        prompt.push('\n');
95    }
96    let chat_args = serde_json::to_vec(&json!({
97        "prompt": prompt,
98        "model_path": v.get("model_path").and_then(Value::as_str).unwrap_or(""),
99        "graph_context": v.get("graph_context").and_then(Value::as_str).unwrap_or(""),
100    }))
101    .map_err(|_| McpSystemError::ParseError)?;
102    llm_infer(&chat_args)
103}
104
105/// `list_models` tool โ€” discovers GGUF models under local storage and reports any resident mounted model.
106pub fn list_models(_args: &[u8]) -> Result<String, McpSystemError> {
107    let models_dir = qualia_storage_path().join("Models");
108    let mut discovered = Vec::new();
109    if models_dir.is_dir() {
110        if let Ok(entries) = std::fs::read_dir(&models_dir) {
111            for entry in entries.filter_map(Result::ok) {
112                let path = entry.path();
113                if path.extension().and_then(|s| s.to_str()) == Some("gguf") {
114                    discovered.push(json!({
115                        "path": path.display().to_string(),
116                        "name": path.file_name().and_then(|n| n.to_str()).unwrap_or(""),
117                        "source": "storage",
118                    }));
119                }
120            }
121        }
122    }
123
124    let resident = match (
125        qualia_core_db::resident_model::resident_model_id(),
126        qualia_core_db::resident_model::resident_gguf_path(),
127    ) {
128        (Some(id), path) => Some(json!({
129            "modelId": id,
130            "path": path,
131            "source": "resident",
132        })),
133        _ => None,
134    };
135
136    let payload = json!({
137        "modelsDir": models_dir.display().to_string(),
138        "discovered": discovered,
139        "resident": resident,
140    });
141    serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError)
142}