Architecture & Latency
How ochreml mcp achieves zero-dependency JSON-RPC 2.0 over stdio, shares core logic with the CLI via _ops.py, and benchmarks on CPU and CUDA.
ochreml mcp is designed to feel like a native, instant local tool rather than a heavyweight Python service (^▽^). Three architectural choices make this possible:
- Zero-dependency JSON-RPC 2.0 over
stdio(python/ochreml/_mcp.py) - Shared core operations engine (
python/ochreml/_ops.py) - PEP 562 lazy imports (
python/ochreml/__init__.py)
Zero-Dependency stdio Transport
Most Python MCP servers pull in mcp, pydantic, anyio, starlette, and httpx—adding dozens of transitive packages and hundreds of milliseconds of import latency before the first JSON-RPC frame is even parsed.
ochreml._mcp implements the MCP 2024-11-05 framing loop directly with Python's standard library (json, os, sys, pathlib):
MCP Client (Claude / Cursor / Windsurf / Antigravity)
│ newline-delimited JSON-RPC 2.0 (stdin)
▼
ochreml._mcp.run_stdio_server(root_dir)
├── OchreMCPServer._resolve_contained() # --root / OCHREML_MCP_ROOT + .dit guardrails
├── OchreMCPServer.handle_request() # initialize, ping, tools/*, resources/*
└── ochreml._ops.op_*() # shared ML, tabular, metrics & .dit engine
│ compact JSON-RPC 2.0 frames (stdout) # diagnostic logs -> stderr if OCHREML_DEBUG=1
▼
MCP ClientBecause pyproject.toml declares dependencies = [], ochreml mcp works in any Python 3.9+ environment where ochreml is installed—including ephemeral uvx --from ochreml ochreml mcp invocations—with zero extra packages to download.
Shared _ops.py Engine
Both ochreml._cli (ochreml <subcommand>) and ochreml._mcp (ochreml mcp) are thin transport adapters over the same internal engine, python/ochreml/_ops.py:
Shared Operation (_ops.py) | CLI Subcommand | MCP Tool / Resource |
|---|---|---|
op_device_info() | ochreml info, ochreml doctor | ochreml_device_info |
op_dataset_profile() | ochreml profile | ochreml_dataset_profile |
op_inspect_dit() | ochreml inspect | ochreml_inspect_dit |
op_verify_dit() | ochreml verify | ochreml_verify_dit |
op_train() | ochreml train | ochreml_train |
op_predict() | ochreml predict | ochreml_predict |
op_eval() | ochreml eval | ochreml_eval |
op_compare() | ochreml compare | ochreml_compare |
op_bench() | ochreml bench | ochreml_bench |
op_catalog() | — | ochreml://catalog/estimators, ochreml://catalog/nn |
Every tabular loader (CSV/TSV via stdlib csv, optional Parquet via pyarrow/polars/pandas), pure-Python metric (accuracy, f1_macro, log_loss, r2, rmse, mae, mse), streaming partial_fit loop, and .dit validation rule lives once in _ops.py. Results produced by an AI agent over MCP are bit-for-bit identical to running the equivalent CLI command locally.
PEP 562 Lazy Loading & Benchmark Latencies
ochreml/__init__.py resolves submodules and estimator classes lazily via __getattr__ (PEP 562) and never eagerly imports _ops, _cli, _mcp, or unused estimator modules. When an MCP server starts, it imports only the core package and loads specific Rust-backed estimators on demand when a tool calls them.
The tables below report measured median latencies from the OchreML v0.3.7a1 startup, CLI, and MCP benchmark suites on a 4-core CPU host (cpu_4c) and a dual-GPU CUDA host (cuda_2gpu).
MCP & CLI End-to-End Latencies
| Workload | cpu_4c Median (ms) | cuda_2gpu Median (ms) | Status |
|---|---|---|---|
mcp: 6-step JSON-RPC roundtrip | 1002.07 | 1201.37 | PASS |
cli: profile --json | 191.57 | 139.90 | PASS |
cli: train Ridge | 327.43 | 236.80 | PASS |
cli: inspect .dit | 311.68 | 241.46 | PASS |
cli: verify .dit | 319.94 | 234.37 | PASS |
cli: predict .dit | 317.16 | 244.91 | PASS |
cli: eval .dit | 308.95 | 239.21 | PASS |
cli: compare (cv=3) | 335.45 | 276.95 | PASS |
cli: info --json | 1942.93 | 2890.92 | PASS |
Cold Import Startup Comparison
| Import Statement | cpu_4c Median (ms) | cpu_4c Loaded Modules | cuda_2gpu Median (ms) | cuda_2gpu Loaded Modules |
|---|---|---|---|---|
import ochreml | 0.85 | 200 | 0.61 | 204 |
from ochreml.linear_model import Ridge | 103.44 | 300 | 86.11 | 304 |
import ochreml.nn | 106.26 | 300 | 86.45 | 304 |
import sklearn | 1202.19 | 1384 | 987.45 | 1388 |
from sklearn.linear_model import Ridge | 1316.47 | 1483 | 1098.61 | 1487 |
import torch | 1708.71 | 1137 | 1497.92 | 1147 |
Key Takeaways
- Sub-millisecond base import:
import ochremlcompletes in 0.85 ms (cpu_4c) and 0.61 ms (cuda_2gpu), loading ~200 modules versus >1,100–1,400 modules fortorchandsklearn. - Amortized multi-step workflows: A full 6-step MCP JSON-RPC roundtrip completes in 1.00 s on CPU and 1.20 s on dual-GPU CUDA—faster than executing 6 separate CLI subprocesses because the persistent
stdioprocess pays Python startup and Rust extension initialization only once(*≧ω≦*). - Why
cli: info --jsontakes longer: Probing all hardware backends (cuda,wgpu,tpu) initializes driver/adapter discovery; subsequent training and inference calls in a long-lived MCP session avoid repeating interpreter startup.