Skip to Content
OchreML

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:

  1. Zero-dependency JSON-RPC 2.0 over stdio (python/ochreml/_mcp.py)
  2. Shared core operations engine (python/ochreml/_ops.py)
  3. 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 Client

Because 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 SubcommandMCP Tool / Resource
op_device_info()ochreml info, ochreml doctorochreml_device_info
op_dataset_profile()ochreml profileochreml_dataset_profile
op_inspect_dit()ochreml inspectochreml_inspect_dit
op_verify_dit()ochreml verifyochreml_verify_dit
op_train()ochreml trainochreml_train
op_predict()ochreml predictochreml_predict
op_eval()ochreml evalochreml_eval
op_compare()ochreml compareochreml_compare
op_bench()ochreml benchochreml_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

Workloadcpu_4c Median (ms)cuda_2gpu Median (ms)Status
mcp: 6-step JSON-RPC roundtrip1002.071201.37PASS
cli: profile --json191.57139.90PASS
cli: train Ridge327.43236.80PASS
cli: inspect .dit311.68241.46PASS
cli: verify .dit319.94234.37PASS
cli: predict .dit317.16244.91PASS
cli: eval .dit308.95239.21PASS
cli: compare (cv=3)335.45276.95PASS
cli: info --json1942.932890.92PASS

Cold Import Startup Comparison

Import Statementcpu_4c Median (ms)cpu_4c Loaded Modulescuda_2gpu Median (ms)cuda_2gpu Loaded Modules
import ochreml0.852000.61204
from ochreml.linear_model import Ridge103.4430086.11304
import ochreml.nn106.2630086.45304
import sklearn1202.191384987.451388
from sklearn.linear_model import Ridge1316.4714831098.611487
import torch1708.7111371497.921147

Key Takeaways

  • Sub-millisecond base import: import ochreml completes in 0.85 ms (cpu_4c) and 0.61 ms (cuda_2gpu), loading ~200 modules versus >1,100–1,400 modules for torch and sklearn.
  • 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 stdio process pays Python startup and Rust extension initialization only once (*≧ω≦*).
  • Why cli: info --json takes 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.

On this page