|
| 1 | +"""omc-substrate MCP server — expose the OMC kernel as MCP tools. |
| 2 | +
|
| 3 | +Lets any MCP-aware LLM (Claude, Cursor, Cline, etc.) use the |
| 4 | +canonical-hash content-addressed store as a memory/compression |
| 5 | +layer. No retraining required — the LLM just calls these tools. |
| 6 | +
|
| 7 | +Tools exposed: |
| 8 | +
|
| 9 | + omc_store(content, kind="prose") -> hex_hash |
| 10 | + Store arbitrary content addressed by canonical hash. |
| 11 | + kind ∈ {omc_fn, json, prose, blob}. |
| 12 | +
|
| 13 | + omc_lookup(hex_hash) -> content | None |
| 14 | + Retrieve stored content by canonical hash. |
| 15 | +
|
| 16 | + omc_canonicalize(content, kind="prose") -> {hash, canonical} |
| 17 | + Compute the canonical hash WITHOUT storing. Useful for |
| 18 | + client-side dedup checks. |
| 19 | +
|
| 20 | + omc_stat(hex_hash) -> metadata dict |
| 21 | + Return the sidecar metadata (kind, attractor, distance, |
| 22 | + bytes, origin_file) for a stored entry. |
| 23 | +
|
| 24 | + omc_list() -> [{hash, fn_name, bytes}, ...] |
| 25 | + Enumerate all stored entries. |
| 26 | +
|
| 27 | + omc_compress(content, every_n=3) -> codec_payload |
| 28 | + Apply the substrate codec (sampled-token compression). |
| 29 | + For OMC code; for prose use omc_store + return hex_hash |
| 30 | + as the reference. |
| 31 | +
|
| 32 | +The server shells out to the `omc-kernel` Rust binary so the |
| 33 | +backing store is shared with any other process using it (CLI |
| 34 | +commands, other agents, etc.). |
| 35 | +""" |
| 36 | + |
| 37 | +from __future__ import annotations |
| 38 | + |
| 39 | +import json |
| 40 | +import os |
| 41 | +import shutil |
| 42 | +import subprocess |
| 43 | +import sys |
| 44 | +import tempfile |
| 45 | +from pathlib import Path |
| 46 | +from typing import Any |
| 47 | + |
| 48 | +from mcp.server.fastmcp import FastMCP |
| 49 | + |
| 50 | + |
| 51 | +def find_kernel_binary() -> str | None: |
| 52 | + """Locate the omc-kernel binary. Search: |
| 53 | + 1. OMC_KERNEL_BIN env (explicit override) |
| 54 | + 2. PATH |
| 55 | + 3. ./target/release/omc-kernel (when run from repo root) |
| 56 | + """ |
| 57 | + explicit = os.environ.get("OMC_KERNEL_BIN") |
| 58 | + if explicit and Path(explicit).is_file(): |
| 59 | + return explicit |
| 60 | + found = shutil.which("omc-kernel") |
| 61 | + if found: |
| 62 | + return found |
| 63 | + cwd = Path.cwd() / "target" / "release" / "omc-kernel" |
| 64 | + if cwd.is_file(): |
| 65 | + return str(cwd) |
| 66 | + return None |
| 67 | + |
| 68 | + |
| 69 | +KERNEL = find_kernel_binary() |
| 70 | +if not KERNEL: |
| 71 | + print( |
| 72 | + "omc-substrate MCP server: omc-kernel binary not found. " |
| 73 | + "Set OMC_KERNEL_BIN or run from a directory with target/release/omc-kernel.", |
| 74 | + file=sys.stderr, |
| 75 | + ) |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | + |
| 79 | +def _kernel(args: list[str], stdin: str | None = None) -> subprocess.CompletedProcess[str]: |
| 80 | + """Run the omc-kernel binary with given args. Capture stdout + stderr.""" |
| 81 | + return subprocess.run( |
| 82 | + [KERNEL, *args], |
| 83 | + input=stdin, |
| 84 | + capture_output=True, |
| 85 | + text=True, |
| 86 | + check=False, |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +mcp = FastMCP("omc-substrate") |
| 91 | + |
| 92 | + |
| 93 | +# ----- Pure implementations (callable directly for tests) ----- |
| 94 | + |
| 95 | + |
| 96 | +def _impl_store(content: str, kind: str = "prose") -> str: |
| 97 | + """Store arbitrary content in the substrate-keyed kernel. |
| 98 | + Returns the canonical hex hash that addresses the stored entry. |
| 99 | +
|
| 100 | + kind selects the canonicalizer: |
| 101 | + omc_fn — alpha-rename-invariant OMC canonical form |
| 102 | + json — recursive key-sort |
| 103 | + prose — raw bytes (default) |
| 104 | + blob — alias for prose |
| 105 | + """ |
| 106 | + with tempfile.NamedTemporaryFile( |
| 107 | + mode="w", suffix=".tmp", delete=False, dir=tempfile.gettempdir() |
| 108 | + ) as f: |
| 109 | + f.write(content) |
| 110 | + tmp_path = f.name |
| 111 | + try: |
| 112 | + r = _kernel(["put", tmp_path, "--kind", kind]) |
| 113 | + if r.returncode != 0: |
| 114 | + raise RuntimeError( |
| 115 | + f"omc-kernel put failed (rc={r.returncode}): {r.stderr.strip()}" |
| 116 | + ) |
| 117 | + # Kernel writes the hex hash to stdout on success. |
| 118 | + return r.stdout.strip() |
| 119 | + finally: |
| 120 | + os.unlink(tmp_path) |
| 121 | + |
| 122 | + |
| 123 | +def _impl_lookup(hex_hash: str) -> str | None: |
| 124 | + """Retrieve stored content by canonical hex hash. |
| 125 | + Returns the content string, or None if no entry exists. |
| 126 | + """ |
| 127 | + r = _kernel(["fetch", hex_hash]) |
| 128 | + if r.returncode != 0: |
| 129 | + return None |
| 130 | + return r.stdout |
| 131 | + |
| 132 | + |
| 133 | +def _impl_stat(hex_hash: str) -> dict[str, Any]: |
| 134 | + """Return sidecar metadata for a stored entry: kind, attractor, |
| 135 | + attractor_distance, source_bytes, canonical_bytes, origin_file. |
| 136 | + """ |
| 137 | + r = _kernel(["stat", hex_hash]) |
| 138 | + if r.returncode != 0: |
| 139 | + return {"error": r.stderr.strip(), "found": False} |
| 140 | + try: |
| 141 | + return json.loads(r.stdout) |
| 142 | + except json.JSONDecodeError as e: |
| 143 | + return {"error": f"could not parse stat output: {e}", "raw": r.stdout} |
| 144 | + |
| 145 | + |
| 146 | +def _impl_list() -> list[dict[str, Any]]: |
| 147 | + """List all stored entries: their canonical hash, fn name (or |
| 148 | + first-line summary for non-fn content), and byte size. |
| 149 | + """ |
| 150 | + r = _kernel(["ls"]) |
| 151 | + if r.returncode != 0: |
| 152 | + return [{"error": r.stderr.strip()}] |
| 153 | + # Parse `omc-kernel ls` output. Format: |
| 154 | + # N fn(s) in store at /path |
| 155 | + # canonical-hash bytes fn |
| 156 | + # <hash> <bytes> fn <name> |
| 157 | + lines = r.stdout.splitlines() |
| 158 | + out: list[dict[str, Any]] = [] |
| 159 | + for ln in lines[2:]: # skip "N fn(s)..." header + column header |
| 160 | + parts = ln.split(None, 2) |
| 161 | + if len(parts) < 3: |
| 162 | + continue |
| 163 | + hash_hex, bytes_s, rest = parts[0], parts[1], parts[2] |
| 164 | + try: |
| 165 | + n_bytes = int(bytes_s) |
| 166 | + except ValueError: |
| 167 | + continue |
| 168 | + # rest is "fn NAME" — strip the leading "fn ". |
| 169 | + name = rest[3:] if rest.startswith("fn ") else rest |
| 170 | + out.append({"hash": hash_hex, "bytes": n_bytes, "name": name}) |
| 171 | + return out |
| 172 | + |
| 173 | + |
| 174 | +def _impl_canonicalize(content: str, kind: str = "prose") -> dict[str, Any]: |
| 175 | + """Compute the canonical hash WITHOUT storing. |
| 176 | + Useful when a client wants to check 'do I already have this?' |
| 177 | + before paying the store cost. Returns {hash, kind, addressing}. |
| 178 | + """ |
| 179 | + # The kernel doesn't have a `hash-only` mode yet, so we cheat: put, |
| 180 | + # then check whether the entry already existed via the stderr line. |
| 181 | + # The hash is the same whether the entry is new or pre-existing. |
| 182 | + with tempfile.NamedTemporaryFile( |
| 183 | + mode="w", suffix=".tmp", delete=False, dir=tempfile.gettempdir() |
| 184 | + ) as f: |
| 185 | + f.write(content) |
| 186 | + tmp_path = f.name |
| 187 | + try: |
| 188 | + r = _kernel(["put", tmp_path, "--kind", kind]) |
| 189 | + hash_hex = r.stdout.strip() if r.returncode == 0 else None |
| 190 | + was_new = "stored" in (r.stderr or "") |
| 191 | + return { |
| 192 | + "hash": hash_hex, |
| 193 | + "kind": kind, |
| 194 | + "was_new": was_new, |
| 195 | + "ok": r.returncode == 0, |
| 196 | + } |
| 197 | + finally: |
| 198 | + os.unlink(tmp_path) |
| 199 | + |
| 200 | + |
| 201 | +def _impl_compress(content: str, every_n: int = 3) -> dict[str, Any]: |
| 202 | + """Apply the substrate codec (sampled-token compression). |
| 203 | + Returns a dict with the codec payload + canonical hash for |
| 204 | + library-lookup recovery on the receiver side. |
| 205 | +
|
| 206 | + Best for OMC source code; for arbitrary prose, the wire-byte |
| 207 | + win only appears at payloads >~500 B with every_n >= 8. |
| 208 | + """ |
| 209 | + # The kernel binary doesn't expose codec_encode directly; for now |
| 210 | + # the cleanest path is to ask the OMC interpreter via stdin. If |
| 211 | + # we hit OMC_KERNEL_BIN's sibling binary, use it. |
| 212 | + omc = ( |
| 213 | + shutil.which("omnimcode-standalone") |
| 214 | + or (Path(KERNEL).parent / "omnimcode-standalone").as_posix() |
| 215 | + ) |
| 216 | + if not Path(omc).is_file(): |
| 217 | + return { |
| 218 | + "error": "omnimcode-standalone binary not found; cannot run codec", |
| 219 | + "hint": "build with `cargo build --release -p omnimcode-cli`", |
| 220 | + } |
| 221 | + program = f""" |
| 222 | +fn main() {{ |
| 223 | + h content = read_file("{0}"); |
| 224 | + h codec = omc_codec_encode(content, {every_n}); |
| 225 | + print(json_stringify(codec)); |
| 226 | +}} |
| 227 | +main(); |
| 228 | +""".strip() |
| 229 | + with tempfile.NamedTemporaryFile( |
| 230 | + mode="w", suffix=".tmp", delete=False, dir=tempfile.gettempdir() |
| 231 | + ) as f: |
| 232 | + f.write(content) |
| 233 | + content_tmp = f.name |
| 234 | + with tempfile.NamedTemporaryFile( |
| 235 | + mode="w", suffix=".omc", delete=False, dir=tempfile.gettempdir() |
| 236 | + ) as f: |
| 237 | + f.write(program.format(content_tmp)) |
| 238 | + prog_tmp = f.name |
| 239 | + try: |
| 240 | + r = subprocess.run( |
| 241 | + [omc, prog_tmp], |
| 242 | + capture_output=True, |
| 243 | + text=True, |
| 244 | + check=False, |
| 245 | + env={**os.environ, "PYO3_USE_ABI3_FORWARD_COMPATIBILITY": "1"}, |
| 246 | + ) |
| 247 | + if r.returncode != 0: |
| 248 | + return {"error": r.stderr.strip(), "rc": r.returncode} |
| 249 | + try: |
| 250 | + return json.loads(r.stdout.strip()) |
| 251 | + except json.JSONDecodeError as e: |
| 252 | + return {"error": f"parse failed: {e}", "raw": r.stdout} |
| 253 | + finally: |
| 254 | + for p in (content_tmp, prog_tmp): |
| 255 | + try: |
| 256 | + os.unlink(p) |
| 257 | + except OSError: |
| 258 | + pass |
| 259 | + |
| 260 | + |
| 261 | +# ----- MCP tool registrations (thin wrappers over _impl_*) ----- |
| 262 | + |
| 263 | + |
| 264 | +@mcp.tool() |
| 265 | +def omc_store(content: str, kind: str = "prose") -> str: |
| 266 | + """Store arbitrary content in the substrate-keyed kernel. |
| 267 | + Returns the canonical hex hash that addresses the stored entry. |
| 268 | + kind ∈ {omc_fn, json, prose, blob}. |
| 269 | + """ |
| 270 | + return _impl_store(content, kind) |
| 271 | + |
| 272 | + |
| 273 | +@mcp.tool() |
| 274 | +def omc_lookup(hex_hash: str) -> str | None: |
| 275 | + """Retrieve stored content by canonical hex hash. None on miss.""" |
| 276 | + return _impl_lookup(hex_hash) |
| 277 | + |
| 278 | + |
| 279 | +@mcp.tool() |
| 280 | +def omc_stat(hex_hash: str) -> dict[str, Any]: |
| 281 | + """Sidecar metadata: kind, attractor, distance, bytes, origin.""" |
| 282 | + return _impl_stat(hex_hash) |
| 283 | + |
| 284 | + |
| 285 | +@mcp.tool() |
| 286 | +def omc_list() -> list[dict[str, Any]]: |
| 287 | + """Enumerate all stored entries.""" |
| 288 | + return _impl_list() |
| 289 | + |
| 290 | + |
| 291 | +@mcp.tool() |
| 292 | +def omc_canonicalize(content: str, kind: str = "prose") -> dict[str, Any]: |
| 293 | + """Compute the canonical hash without storing — dedup-check.""" |
| 294 | + return _impl_canonicalize(content, kind) |
| 295 | + |
| 296 | + |
| 297 | +@mcp.tool() |
| 298 | +def omc_compress(content: str, every_n: int = 3) -> dict[str, Any]: |
| 299 | + """Apply substrate codec for OMC source code.""" |
| 300 | + return _impl_compress(content, every_n) |
| 301 | + |
| 302 | + |
| 303 | +if __name__ == "__main__": |
| 304 | + mcp.run() |
0 commit comments