BREP_mcp 0.1.0

The BREP MCP automation server: hosts the BREP CAD app headlessly under egui_kittest and serves the BREP_mcp_core Model Context Protocol surface over stdio or streamable HTTP, so an agent can drive the CAD app with no display attached. The same server the app embeds behind `brep-app --mcp`.
Documentation
//! The headless host: the whole `BrepApp` inside `egui_kittest::Harness` on a
//! wgpu device, stepped on demand on a dedicated thread (spec §3.2; measured
//! in §12). No window, no display. Answers the `brep_mcp_core` host protocol;
//! the app's own envelope and reply types are converted at the boundary.
use brep_app::app::BrepApp;
use brep_app::automation::command::{Envelope as AppEnvelope, Reply as AppReply};
use brep_app::automation::AppOptions;
use brep_mcp_core::host::{HostConfig, HostHandle, HostInfo, HostRequest, Reply};
use egui_kittest::Harness;
use serde_json::Value;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

pub struct HeadlessConfig {
    pub width: f32,
    pub height: f32,
    pub ppp: f32,
    pub seed: bool,
    /// The session's private store directory (spec §8).
    pub store_dir: PathBuf,
    /// Frames to step while waiting for one command's reply before giving up.
    pub max_steps: u32,
}

impl Default for HeadlessConfig {
    fn default() -> Self {
        Self { width: 1400.0, height: 960.0, ppp: 1.0, seed: true, store_dir: std::env::temp_dir().join("brep-mcp-store"), max_steps: 600 }
    }
}

impl From<HostConfig> for HeadlessConfig {
    fn from(c: HostConfig) -> Self {
        Self { width: c.width, height: c.height, ppp: c.ppp, seed: c.seed, store_dir: c.store_dir, ..Self::default() }
    }
}

/// The app's reply in the server's shape: the same JSON, the blob beside it.
fn convert(mut r: AppReply) -> Reply {
    let blob = r.blob.take();
    let (id, frame) = (r.id, r.frame);
    match serde_json::to_value(&r).map_err(|e| e.to_string()).and_then(|v| Reply::from_app(v, blob)) {
        Ok(reply) => reply,
        Err(e) => Reply::err(id, frame, e),
    }
}

/// The last panic message, captured by the hook the host installs.
static LAST_PANIC: Mutex<Option<String>> = Mutex::new(None);

fn install_panic_hook() {
    static ONCE: std::sync::Once = std::sync::Once::new();
    ONCE.call_once(|| {
        let previous = std::panic::take_hook();
        std::panic::set_hook(Box::new(move |info| {
            let msg = info
                .payload()
                .downcast_ref::<String>()
                .cloned()
                .or_else(|| info.payload().downcast_ref::<&str>().map(|s| s.to_string()))
                .unwrap_or_else(|| "panic".into());
            let loc = info.location().map(|l| format!(" at {}:{}", l.file(), l.line())).unwrap_or_default();
            *LAST_PANIC.lock().unwrap() = Some(format!("{msg}{loc}"));
            previous(info);
        }));
    });
}

fn take_panic() -> Option<String> {
    LAST_PANIC.lock().unwrap().take()
}

/// Spawn the host thread; returns once the app is built and its seed history
/// has settled, or with the build error.
pub fn spawn(cfg: impl Into<HeadlessConfig>) -> Result<HostHandle, String> {
    let cfg: HeadlessConfig = cfg.into();
    install_panic_hook();
    // Publishers are off in a plain native run; a host is what turns them on.
    brep_app::automation::registry::set_enabled(true);
    let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<HostRequest>();
    let (ready_tx, ready_rx) = std::sync::mpsc::channel::<Result<HostInfo, String>>();
    let join = std::thread::Builder::new()
        .name("brep-mcp-headless".into())
        .spawn(move || {
            let built = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| build(&cfg)));
            let (mut harness, info) = match built {
                Ok(Ok(v)) => v,
                Ok(Err(e)) => {
                    let _ = ready_tx.send(Err(e));
                    return;
                }
                Err(_) => {
                    let _ = ready_tx.send(Err(format!("the app panicked while starting: {}", take_panic().unwrap_or_default())));
                    return;
                }
            };
            let queue = harness.state().automation().clone();
            let ppp = cfg.ppp;
            // Let the seed history land before the first command.
            settle(&mut harness, &queue, ppp, Duration::from_secs(20));
            let _ = ready_tx.send(Ok(info));

            while let Some(req) = rx.blocking_recv() {
                match req {
                    HostRequest::Stop => break,
                    HostRequest::Command { envelope, reply } => {
                        let id = envelope.id;
                        let rx_reply = queue.submit(AppEnvelope { id, cmd: envelope.cmd, args: envelope.args });
                        let mut steps = 0u32;
                        let result = loop {
                            if let Ok(r) = rx_reply.try_recv() {
                                break convert(r);
                            }
                            if queue.is_poisoned() {
                                break Reply::err(id, queue.frame(), "session poisoned");
                            }
                            if steps >= cfg.max_steps {
                                break Reply::err(id, queue.frame(), format!("no reply after {} frames", cfg.max_steps));
                            }
                            if let Err(why) = step(&mut harness, &queue, ppp) {
                                queue.poison(why.clone());
                                break Reply::err(id, queue.frame(), why);
                            }
                            steps += 1;
                            if steps > 2 {
                                std::thread::sleep(Duration::from_millis(2));
                            }
                        };
                        let _ = reply.send(result);
                    }
                    HostRequest::Screenshot { region, reply } => {
                        let result = (|| {
                            step(&mut harness, &queue, ppp)?;
                            let image = harness.render().map_err(|e| format!("headless render: {e}"))?;
                            let view = harness.state().view_rect();
                            let region: brep_app::automation::cmd_capture::Region =
                                serde_json::from_value(region).map_err(|e| format!("region: {e}"))?;
                            let color = egui::ColorImage {
                                size: [image.width() as usize, image.height() as usize],
                                source_size: egui::vec2(image.width() as f32, image.height() as f32),
                                pixels: image
                                    .pixels()
                                    .map(|p| egui::Color32::from_rgba_premultiplied(p.0[0], p.0[1], p.0[2], p.0[3]))
                                    .collect(),
                            };
                            brep_app::automation::cmd_capture::encode_capture(&color, ppp, view, &region)
                        })();
                        let _ = reply.send(result);
                    }
                }
            }
        })
        .map_err(|e| format!("spawn host thread: {e}"))?;
    match ready_rx.recv() {
        Ok(Ok(info)) => Ok(HostHandle::new(tx, info, join)),
        Ok(Err(e)) => Err(e),
        Err(_) => Err("the host thread ended before the app was ready".into()),
    }
}

type App = Harness<'static, BrepApp>;

fn build(cfg: &HeadlessConfig) -> Result<(App, HostInfo), String> {
    std::fs::create_dir_all(&cfg.store_dir).map_err(|e| format!("store dir {}: {e}", cfg.store_dir.display()))?;
    let store_dir = cfg.store_dir.clone();
    let seed = cfg.seed;
    let adapter = Arc::new(Mutex::new(String::new()));
    let adapter_out = adapter.clone();
    let harness = Harness::builder()
        .with_size(egui::vec2(cfg.width, cfg.height))
        .with_pixels_per_point(cfg.ppp)
        .with_step_dt(1.0 / 60.0)
        .wgpu()
        .build_eframe(move |cc| {
            brep_app::fonts::install(&cc.egui_ctx);
            let store = brep_app::store::native_store_at(store_dir.clone());
            let app = BrepApp::new_with(cc, AppOptions { store: Some(store), seed })
                .expect("BrepApp::new_with under kittest");
            // The app's OWN diagnostics, not a second reading of the same
            // adapter: `HostInfo.adapter`, the `diagnostics` command and the
            // Info window must all name the same device (brep_app::diagnostics).
            *adapter_out.lock().unwrap() = app.diagnostics().adapter_line();
            app
        });
    let info = HostInfo {
        backend: "headless",
        adapter: adapter.lock().unwrap().clone(),
        platform: std::env::consts::OS,
        width: cfg.width,
        height: cfg.height,
        ppp: cfg.ppp,
    };
    Ok((harness, info))
}

/// One frame: drain input into the harness's RawInput, step, check for a panic.
fn step(harness: &mut App, queue: &Arc<brep_app::automation::queue::AutomationQueue>, ppp: f32) -> Result<(), String> {
    let frame = harness.ctx.cumulative_frame_nr();
    let view = harness.state().view_rect();
    queue.drain_input(harness.input_mut(), frame, ppp, view);
    let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| harness.step()));
    match r {
        Ok(()) => Ok(()),
        Err(_) => Err(format!("the app panicked: {}", take_panic().unwrap_or_else(|| "(no message)".into()))),
    }
}

/// Step until the runner is idle (plus two settle frames) or the deadline.
fn settle(harness: &mut App, queue: &Arc<brep_app::automation::queue::AutomationQueue>, ppp: f32, limit: Duration) {
    let start = Instant::now();
    let mut idle_frames = 0;
    while start.elapsed() < limit {
        if step(harness, queue, ppp).is_err() {
            return;
        }
        let idle = {
            let e = harness.state().docs_engine();
            !(e.run_pending() || e.queries_pending() || e.mesh_imports_pending() || e.step_probes_pending())
        };
        if idle {
            idle_frames += 1;
            if idle_frames >= 2 {
                return;
            }
        } else {
            idle_frames = 0;
            std::thread::sleep(Duration::from_millis(8));
        }
    }
}

#[allow(dead_code)]
fn _value(_: Value) {}