Skip to main content

qualia_core_db/mcp/
inference_session.rs

1//! Local host delegation to the MCP inference surface. Tool arguments cannot
2//! install, select or replace this authenticated authority.
3use crate::llm_agent::{native_request::NativeProductSession, LocalLlmAgent};
4use std::sync::{Arc, Mutex, OnceLock};
5
6static SESSION: OnceLock<Mutex<Option<Arc<NativeProductSession>>>> = OnceLock::new();
7
8/// Privileged in-process setup only; never register this as an MCP tool.
9pub fn configure(session: Arc<NativeProductSession>) -> Result<(), String> {
10    session.bind().map_err(|e| e.to_string())?;
11    *SESSION.get_or_init(|| Mutex::new(None)).lock()
12        .map_err(|_| "MCP inference session unavailable")? = Some(session);
13    Ok(())
14}
15
16pub fn disconnect() {
17    if let Some(slot) = SESSION.get() {
18        *slot.lock().unwrap_or_else(|e| e.into_inner()) = None;
19    }
20}
21
22/// Every tool invocation obtains a fresh, owner-validated single-use request.
23pub fn bind(agent: &LocalLlmAgent) -> Result<(), String> {
24    let session = SESSION.get().ok_or("Native host must bind MCP inference authority")?
25        .lock().map_err(|_| "MCP inference session unavailable")?
26        .clone().ok_or("Native host must bind MCP inference authority")?;
27    agent.bind_native_governed_request(session.bind().map_err(|e| e.to_string())?)
28        .map_err(|e| e.to_string())
29}