Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

tools/strata_mcp.py: an MCP server so an AI assistant can install, start, check and stop Strata (#133)

Niko1221885e52052b63
diff --git a/.gitignore b/.gitignore
index 26885bfe4..ca841628b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@ td.log
/strata-*.json
/strata-*.log
/build-strata.bat
+/.strata-mcp/
# image encoder (setup.py --vision): build output and the generated build script
build-vision*/
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 08737ec2c..82a6e4fdb 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -5,7 +5,8 @@ New here? Start with the [README](../README.md) - it has everything you need to
> **On this page:** [Speed](#speed-measured) · [Other GPUs](#other-gpus-estimated) · [Which model?](#which-model) ·
> [Requirements](#before-you-start) · [Windows](#windows) · [Linux](#linux) · [API](#using-it) ·
-> [MCP tools](#tools-from-mcp-servers) · [Images](#images-vision) ·
+> [MCP tools](#tools-from-mcp-servers) · [MCP server](#manage-strata-from-your-ai-assistant-mcp-server) ·
+> [Images](#images-vision) ·
> [Troubleshooting](#troubleshooting) · [How it works](#how-it-works)
---
@@ -609,6 +610,29 @@ unmeasured: all the runs above are text.
---
+## Manage Strata from your AI assistant (MCP server)
+
+`tools/strata_mcp.py` is an MCP server for Claude Code, Claude Desktop, Cursor, VS Code, Codex and other assistants.
+Once it is added, you can ask your assistant "install Strata for this PC", "start Strata" or "is Strata running?".
+In Claude Code, add it with:
+
+```bash
+claude mcp add strata -- python C:\Users\you\Strata\tools\strata_mcp.py
+```
+
+It has eight tools: status (the running model, what is installed, the hardware, a recommended size), the model
+list, install, start, stop, logs, a speed test, and connection settings for other apps.
+
+Install runs `setup.py` with `--yes` in the background. Before it downloads anything, it shows the plan and waits
+for your OK. Start and stop work like the run scripts and the server's own unload. The MCP server only ends
+processes it started itself. It uses only Python's standard library, so it works before `.venv` exists.
+
+The config snippets for every client, the tool arguments and the safety rules are in
+[docs/MCP_SERVER.md](MCP_SERVER.md). This is the opposite direction from
+[Tools from MCP servers](#tools-from-mcp-servers) above, where the Strata model calls *your* MCP tools.
+
+---
+
## Images (vision)
The model has a vision encoder: [`mmproj-Qwen3.8-Flash-Next-BF16.gguf`](https://huggingface.co/ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF)
diff --git a/docs/MCP_SERVER.md b/docs/MCP_SERVER.md
new file mode 100644
index 000000000..b72c659c2
--- /dev/null
+++ b/docs/MCP_SERVER.md
@@ -0,0 +1,205 @@
+# Strata's MCP server: let your AI assistant install and run Strata
+
+`tools/strata_mcp.py` is an [MCP](https://modelcontextprotocol.io) server for the AI assistant you already use
+(Claude Code, Claude Desktop, Cursor, VS Code, Codex ...). Add it once, then ask in plain words:
+
+- "Install Strata for this PC."
+- "Start Strata." / "Stop Strata."
+- "Is Strata running? How much VRAM does it use?"
+- "Which Strata model fits my PC?"
+- "Strata didn't start - what does the log say?"
+- "How fast is Strata on this PC?" (a short speed test)
+- "How do I connect Open WebUI / the OpenAI SDK / Claude Code to Strata?"
+
+The assistant calls the server's tools. The tools run Strata's own setup and start the model the way the start
+scripts do. It is one Python file that uses only the standard library, so it works before setup has created
+`.venv`. It needs Python 3.10 or newer, 64-bit on Windows.
+
+It is not the same thing as [Tools from MCP servers](DETAILS.md#tools-from-mcp-servers). That feature
+(`serve/mcp.py`) lets the *Strata model* call tools from your MCP servers in its chat page. This page is the other
+direction: *your assistant* manages Strata.
+
+## Add it
+
+You need a copy of Strata: the zip from GitHub, or `git clone https://github.com/Niko1221/Strata`. Below,
+`C:\Users\you\Strata` (Windows) and `/home/you/Strata` (Linux) stand for that folder. Use `python3` on Linux. On
+Windows, use `py` if `python` is not on your PATH.
+
+### Claude Code
+
+```bash
+claude mcp add strata -- python C:\Users\you\Strata\tools\strata_mcp.py # Windows
+claude mcp add strata -- python3 /home/you/Strata/tools/strata_mcp.py # Linux
+```
+
+Add `--scope user` to have it in every project. `/mcp` in Claude Code shows that it is connected.
+
+### Claude Desktop
+
+Open Settings > Developer > Edit Config (`claude_desktop_config.json`) and add:
+
+```json
+{
+ "mcpServers": {
+ "strata": {
+ "command": "python",
+ "args": ["C:\\Users\\you\\Strata\\tools\\strata_mcp.py"]
+ }
+ }
+}
+```
+
+On Linux, use `"command": "python3"` and `"args": ["/home/you/Strata/tools/strata_mcp.py"]`. Restart Claude
+Desktop after the change.
+
+### Cursor
+
+Use `~/.cursor/mcp.json` for every project, or `.cursor/mcp.json` in one project. The format is the same as
+Claude Desktop's:
+
+```json
+{
+ "mcpServers": {
+ "strata": {
+ "command": "python",
+ "args": ["C:\\Users\\you\\Strata\\tools\\strata_mcp.py"]
+ }
+ }
+}
+```
+
+### VS Code (GitHub Copilot agent mode)
+
+Use `.vscode/mcp.json` in a workspace, or run "MCP: Open User Configuration" from the command palette:
+
+```json
+{
+ "servers": {
+ "strata": {
+ "type": "stdio",
+ "command": "python",
+ "args": ["C:\\Users\\you\\Strata\\tools\\strata_mcp.py"]
+ }
+ }
+}
+```
+
+On Linux, use `"command": "python3"` and `"args": ["/home/you/Strata/tools/strata_mcp.py"]`.
+
+### Codex CLI
+
+In `~/.codex/config.toml`:
+
+```toml
+[mcp_servers.strata]
+command = "python"
+args = ['C:\Users\you\Strata\tools\strata_mcp.py'] # Linux: command = "python3", args = ["/home/you/Strata/tools/strata_mcp.py"]
+tool_timeout_sec = 180 # strata_stop / strata_benchmark can take longer than 60 s
+```
+
+### Any other MCP client
+
+The command is `python <Strata folder>/tools/strata_mcp.py`. The transport is stdio. It speaks MCP revision
+2025-06-18, and also 2025-03-26 and 2024-11-05.
+
+`--root <folder>` manages a Strata folder other than the one the file is in. For example:
+`python D:\tools\strata_mcp.py --root D:\Strata`.
+
+## The tools
+
+| Tool | What it does | Arguments (all optional) |
+| --- | --- | --- |
+| `strata_status` | Whether a model is running: the model, engine version, context, busy or idle, VRAM and RAM in use. What is installed: models, start scripts, engine version, data folder, downloads. An install in progress. This PC's hardware: OS, CPU, RAM, GPUs, page file, free disk. The model and context setup recommends for this PC. | `port`, `hardware` (true/false) |
+| `strata_models` | Every family and size setup offers, with download size, RAM needed, whether it fits this PC, and whether it is installed. Also the context lengths and the recommendation. | - |
+| `strata_install` | Runs setup without questions, in the background (see below). Without `confirm: true` it only returns the plan. A second call shows the progress. `cancel: true` stops the install. | `family` (qwen, swift, coder, unsloth), `model` (Q2_0, IQ2_XS, IQ3_XXS, IQ3_S, IQ1_M, UD-Q4_K_XL), `context` (8192 ... 524288), `vision` (no, yes, gpu, cpu), `kv` (int8, q4_0, k8v4), `gpu` (a number) or `gpus` ("0,2", "all"), `backend` (auto, cuda, hip), `low_ram`, `port`, `data_dir`, `confirm`, `cancel` |
+| `strata_start` | Starts an installed model in the background and waits until it answers. If it is not ready after `wait_seconds` (default 50), it returns "loading". | `model` (an id from `strata_status`, e.g. `q2_0`, `coder-iq1_m`; default: the most recently used), `port`, `gpu`, `wait_seconds` (0-600) |
+| `strata_stop` | Unloads the model through the server's own API, then ends the server it started. | `port`, `force` |
+| `strata_logs` | The last lines of the server log, the engine log or the setup log. | `source` (server, engine, setup), `model`, `lines` (1-500) |
+| `strata_benchmark` | Sends a short fixed request (greedy, thinking off) and reports output and prompt tokens/s. | `port`, `max_tokens` (16-512) |
+| `strata_connect_info` | The OpenAI and Anthropic base URLs, the model id, whether a key is needed, and settings for Claude Code, Codex, the OpenAI SDK, curl, Cursor, Continue and Open WebUI. | `port` |
+
+Every result is JSON with a one-line `summary`. It is sent both as text and as MCP structured content.
+
+## How it works
+
+**Install.** `strata_install` uses setup's own logic and tables (it imports `setup.py`'s `MODELS`, `FAMILIES`,
+`CONTEXTS` and RAM checks) to fill in what you left out:
+
+- the size setup itself would pick: IQ3_XXS from 60 GB of RAM, Q2_0 from about 44 GB, the Coder from about 28 GB,
+ and the Coder in the low-RAM mode below that when the GPU makes up for the RAM;
+- the context setup recommends for the GPU's VRAM;
+- images off.
+
+The first call returns the plan: the download size (58-111 GB), the free disk space, the RAM check, and the exact
+setup command. The assistant should show you the plan and call again with `confirm: true` once you agree.
+
+It then runs the same steps `START-HERE.bat` / `setup.sh` run:
+
+1. It creates `.venv` with the Python it runs on, if `.venv` is missing.
+2. It runs `setup.py --yes --no-start --family ... --model ... --context ... --vision ...` as a background process.
+ The output goes to `.strata-mcp/setup.log` in the Strata folder.
+
+The tool returns at once. Call it again to see the step (1-7), the download percentage, or setup's error message.
+Everything setup has finished is skipped on the next run, and downloads resume. If the AI app closes, the install
+keeps running. The install is refused while a model this server started is running, because setup may replace the
+engine files that model uses.
+
+**Start.** `strata_start` runs the same command as the `run-<model>` script, `serve/server.py --engine strata
+--config strata-<model>.json --port N`, with the venv's Python, without `--open` (no browser) and without a console
+window. The server's output goes to `.strata-mcp/server.log`. The tool then polls `GET /health` until the model
+answers. Loading takes 1-3 minutes. If the server ends while loading, the tool returns the end of its log. The model
+keeps running after the AI app closes, until `strata_stop`. If the AI app runs its tools inside a Windows job that
+does not allow processes to leave it, the model ends together with the app.
+
+Two cases are refused:
+
+- a model is already running: the tool returns it and starts nothing;
+- another program uses the port: the tool says so.
+
+**Stop.** `strata_stop` first sends `POST /unload`. The server sends the engine `QUIT`, and the engine frees its
+VRAM and pinned RAM itself, as when you close the window. Then the server ends:
+
+- Linux: SIGTERM, which runs the server's own Ctrl+C path.
+- Windows: the process ends. Its job object ends the vision encoder and the MCP servers it started.
+
+While a request is running, the tool refuses unless you pass `force: true`.
+
+**Status.** The tool reads `GET /health`, `/v1/status`, `/status` and, on older servers, `/metrics` on 127.0.0.1.
+If a config of the folder sets an `api_key`, the tool sends that key with its own requests. It never shows the key.
+
+## Safety
+
+- **No shell, no free paths.** Every argument is checked against a fixed schema and against setup's own choices. An
+ unknown argument, a model or family setup does not offer, or a `gpus` value other than digits and commas is
+ refused before anything runs. Commands are passed as argument lists; nothing goes through a shell.
+- **Paths.** The only path argument is `data_dir`. It must be one of:
+ - the current Strata data folder;
+ - a folder inside the Strata folder;
+ - a new absolute folder whose name starts with `Strata` (e.g. `D:\Strata-data`), whose parent exists, and which is
+ not in a system folder.
+
+ `..`, relative paths and network (UNC) paths are refused. Logs are read only from `.strata-mcp/` and from the
+ engine log a config names, if that log is inside the Strata folder or the data folder.
+- **Only its own processes.** The server records the process id *and start time* of every process it starts, in
+ `.strata-mcp/*.json`. It only ends a process when both match, so a process id the OS has given to another program
+ is never touched. A Strata started some other way (its window, a run script, a service) is only asked over its
+ API to unload the model. To end that server, close its window.
+- **Big downloads need a yes.** `strata_install` does nothing until it is called with `confirm: true`. The plan
+ shows the download size and the free space first, and the install is refused if the disk is too small.
+- **Local only.** The server checks `127.0.0.1` and never changes the network settings: it does not install with
+ `--host 0.0.0.0` or an API key. To share Strata on your network, run setup yourself (see
+ [Using it](DETAILS.md#using-it)). Experimental options are left off too: the speed projection, rope scaling past
+ 262K and `--build`. Run setup by hand for those.
+
+## Troubleshooting
+
+- **The assistant does not see the tools.** Run the command by hand:
+ `python C:\Users\you\Strata\tools\strata_mcp.py`. It should print `[strata-mcp] ready (Strata folder ...)` to
+ stderr and wait for input (Ctrl+C ends it). If it reports `not a Strata folder`, the path is wrong or `--root` is
+ needed. Python older than 3.10 is refused.
+- **The install failed.** Ask the assistant for the setup log (`strata_logs` with `source: setup`), or open
+ `.strata-mcp/setup.log`. Setup's `[X]` line says what to fix. Then install again: every finished step is skipped.
+- **The model does not start.** Get the server log with `strata_logs`, and the engine log with `source: engine`.
+ [Troubleshooting](DETAILS.md#troubleshooting) has the usual causes.
+- **Tests.** `python -m unittest tools/test_strata_mcp.py` runs the tests with a fake setup and server. They do not
+ download anything or use the GPU.
diff --git a/tools/strata_mcp.py b/tools/strata_mcp.py
new file mode 100644
index 000000000..811c9bcf5
--- /dev/null
+++ b/tools/strata_mcp.py
@@ -0,0 +1,1725 @@
+#!/usr/bin/env python3
+"""tools/strata_mcp.py - an MCP server that lets an AI assistant install, start, check and stop Strata.
+
+ claude mcp add strata -- python /path/to/Strata/tools/strata_mcp.py
+
+Add it to Claude Code, Claude Desktop, Cursor, VS Code, Codex ... (docs/MCP_SERVER.md has every client's snippet) and
+ask your AI "install Strata for this PC", "start Strata", "is Strata running?". It speaks MCP (JSON-RPC 2.0, one
+message per line) over stdin/stdout, uses the Python standard library only (it runs before setup made .venv; Python
+3.10 or newer), and offers a fixed set of tools: strata_status, strata_models, strata_install, strata_start,
+strata_stop, strata_logs, strata_benchmark, strata_connect_info.
+
+Safety: no tool takes a shell command or a free path. Every argument is checked against setup.py's own choices
+(models, families, contexts ...); the only path argument (install's data_dir) must be the Strata data folder, a folder
+inside the Strata folder, or a new folder named Strata* outside the system folders. Setup and the model run as
+background processes this server starts itself; it keeps their process id and start time in .strata-mcp/ and only
+ever stops a process it started (a Strata started elsewhere, e.g. from its own window, is only asked over its API to
+unload the model).
+
+Not to be confused with serve/mcp.py: that is Strata's MCP *client* (the model calling your tools from the chat page).
+This file is an MCP *server* for the AI assistant you already use.
+"""
+from __future__ import annotations
+
+import argparse
+import importlib.util
+import json
+import os
+import platform
+import re
+import shutil
+import signal
+import subprocess
+import sys
+import threading
+import time
+import traceback
+import urllib.error
+import urllib.request
+from pathlib import Path
+
+PROTOCOL = "2025-06-18"
+PROTOCOLS = ("2025-06-18", "2025-03-26", "2024-11-05") # the revisions this server answers in
+VERSION = "0.1.0"
+WIN = os.name == "nt"
+DEFAULT_ROOT = Path(__file__).resolve().parent.parent
+DEFAULT_PORT = 8080
+STATE_DIR = ".strata-mcp" # in the Strata folder (git-ignored)
+
+# What setup.py offers, used only when setup.py cannot be imported (the numbers are setup's MODELS / FAMILIES /
+# CONTEXTS; tools/test_strata_mcp.py checks they still match).
+FALLBACK_MODELS = {
+ "Q2_0": {"about": "2-bit, the fastest", "download_gb": 66.4, "ram_gb": 48, "arena_gb": 34.0, "families": ("qwen",)},
+ "IQ2_XS": {"about": "2-bit i-quant, a little better quality, close in speed", "download_gb": 68.0, "ram_gb": 48,
+ "arena_gb": 35.5},
+ "IQ3_XXS": {"about": "3-bit i-quant, better quality, slower (more CPU work per token)", "download_gb": 75.8,
+ "ram_gb": 60, "arena_gb": 42.9},
+ "IQ3_S": {"about": "3.5-bit i-quant, the best quality (matches the full model), the slowest; needs a 64 GB PC "
+ "with little else running", "download_gb": 83.6, "ram_gb": 62, "arena_gb": 50.3,
+ "families": ("qwen",)},
+ "IQ1_M": {"about": "the Coder's only size: half the experts, stored like IQ3_S (3.5 bits)", "download_gb": 58.4,
+ "ram_gb": 32, "arena_gb": 23.4, "families": ("coder",)},
+ "UD-Q4_K_XL": {"about": "4-bit (Unsloth Dynamic), EXPERIMENTAL: the best quality, but most experts come from the "
+ "SSD on a 64 GB PC (7-8.5 tokens/s measured)", "download_gb": 111.3, "ram_gb": 48,
+ "arena_gb": 77.0, "families": ("unsloth",), "budget": True},
+}
+FALLBACK_FAMILIES = {
+ "qwen": {"title": "Qwen3.8-Flash-Next", "about": "the original model", "tag": ""},
+ "swift": {"title": "Swift 1.5", "about": "thinks much shorter (-63% thinking tokens, 1.8x sooner answers by its "
+ "authors' numbers)", "tag": "swift-"},
+ "coder": {"title": "Qwen3.8-Flash-Next Coder", "about": "half the experts (code, tools, images kept): needs ~32 GB "
+ "of RAM, faster; weaker outside coding", "tag": "coder-"},
+ "unsloth": {"title": "Qwen3.8-Flash-Next (Unsloth)", "about": "4-bit, 111 GB download, most experts read from the "
+ "SSD: slow (7-8.5 tokens/s on a 64 GB PC)",
+ "tag": "unsloth-", "experimental": True, "vision": False},
+}
+FALLBACK_CONTEXTS = [8192, 32768, 65536, 131072, 262144, 393216, 524288]
+BENCH_PROMPT = ("Write a short story (about 300 words) about a lighthouse keeper who finds a message in a bottle. "
+ "Plain prose, no title.")
+
+if WIN:
+ import ctypes
+ from ctypes import wintypes
+
+ _k32 = ctypes.WinDLL("kernel32", use_last_error=True)
+ _k32.OpenProcess.restype = wintypes.HANDLE
+ _k32.OpenProcess.argtypes = (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD)
+ _k32.GetProcessTimes.argtypes = (wintypes.HANDLE, *(ctypes.POINTER(wintypes.FILETIME),) * 4)
+ _k32.GetExitCodeProcess.argtypes = (wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD))
+ _k32.TerminateProcess.argtypes = (wintypes.HANDLE, wintypes.UINT)
+ _k32.WaitForSingleObject.argtypes = (wintypes.HANDLE, wintypes.DWORD)
+ _k32.CloseHandle.argtypes = (wintypes.HANDLE,)
+ _QUERY, _TERMINATE, _SYNCHRONIZE, _STILL_ACTIVE = 0x1000, 0x0001, 0x00100000, 259
+ CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW, CREATE_BREAKAWAY_FROM_JOB = 0x200, 0x08000000, 0x01000000
+
+
+def log(msg: str) -> None:
+ """Diagnostics go to stderr: stdout carries only the protocol."""
+ try:
+ sys.stderr.write(f"[strata-mcp] {msg}\n")
+ sys.stderr.flush()
+ except (OSError, ValueError):
+ pass
+
+
+# ------------------------------------------------------------------------------------------------ processes
+def proc_identity(pid) -> str | None:
+ """A token for a RUNNING process: its start time (so a reused process id is never mistaken for ours), or None
+ when no such process runs."""
+ try:
+ pid = int(pid)
+ except (TypeError, ValueError):
+ return None
+ if pid <= 0:
+ return None
+ if WIN:
+ h = _k32.OpenProcess(_QUERY, False, pid)
+ if not h:
+ return None
+ try:
+ code = wintypes.DWORD()
+ if not _k32.GetExitCodeProcess(h, ctypes.byref(code)) or code.value != _STILL_ACTIVE:
+ return None
+ c, e, k, u = (wintypes.FILETIME() for _ in range(4))
+ if not _k32.GetProcessTimes(h, ctypes.byref(c), ctypes.byref(e), ctypes.byref(k), ctypes.byref(u)):
+ return None
+ return f"win:{(c.dwHighDateTime << 32) | c.dwLowDateTime}"
+ finally:
+ _k32.CloseHandle(h)
+ stat = Path(f"/proc/{pid}/stat")
+ if Path("/proc/self/stat").exists():
+ try:
+ fields = stat.read_text().rsplit(")", 1)[1].split()
+ except (OSError, IndexError):
+ return None
+ if fields[0] in ("Z", "X"): # a zombie has ended
+ return None
+ return f"linux:{fields[19]}" # field 22 of stat: the start time in clock ticks
+ try: # elsewhere (no /proc): alive, without a start time
+ os.kill(pid, 0)
+ return "pid"
+ except OSError:
+ return None
+
+
+def proc_alive(pid, ident) -> bool:
+ return bool(ident) and proc_identity(pid) == ident
+
+
+def proc_terminate(pid, ident) -> bool:
+ """Ask a process WE started to end (POSIX: SIGTERM, the server's clean path; Windows: TerminateProcess). Never
+ touches a process whose start time differs from the one recorded."""
+ if not proc_alive(pid, ident):
+ return False
+ if WIN:
+ h = _k32.OpenProcess(_TERMINATE | _QUERY, False, int(pid))
+ if not h:
+ return False
+ try:
+ return bool(_k32.TerminateProcess(h, 1))
+ finally:
+ _k32.CloseHandle(h)
+ try:
+ os.kill(int(pid), signal.SIGTERM)
+ return True
+ except OSError:
+ return False
+
+
+def proc_kill_tree(pid, ident) -> bool:
+ """End a process WE started together with the processes it started (setup's pip / cmake, the engine)."""
+ if not proc_alive(pid, ident):
+ return False
+ if WIN:
+ r = subprocess.run(["taskkill", "/PID", str(int(pid)), "/T", "/F"], capture_output=True,
+ creationflags=CREATE_NO_WINDOW)
+ return r.returncode == 0
+ try:
+ os.killpg(int(pid), signal.SIGKILL) # started as a session leader: its group is its own
+ return True
+ except OSError:
+ try:
+ os.kill(int(pid), signal.SIGKILL)
+ return True
+ except OSError:
+ return False
+
+
+def end_group_leftovers(pgid) -> None:
+ """POSIX: after the server WE started (a session leader) is gone, end what is left of its process group - an
+ engine it started but could not close (e.g. stopped while loading, before its own SIGTERM handler existed).
+ While any member lives, the group id cannot belong to anybody else."""
+ if WIN or not pgid:
+ return
+ for sig, wait in ((signal.SIGTERM, 20.0), (signal.SIGKILL, 5.0)):
+ try:
+ os.killpg(int(pgid), sig)
+ except OSError:
+ return # no such group left
+ end = time.time() + wait
+ while time.time() < end:
+ try:
+ os.killpg(int(pgid), 0)
+ except OSError:
+ return
+ time.sleep(0.25)
+
+
+def wait_gone(pid, ident, seconds: float) -> bool:
+ end = time.time() + seconds
+ while time.time() < end:
+ if not proc_alive(pid, ident):
+ return True
+ time.sleep(0.25)
+ return not proc_alive(pid, ident)
+
+
+def spawn_detached(cmd: list, cwd: Path, log_path: Path, env: dict | None = None) -> subprocess.Popen:
+ """Start a background process whose output goes to log_path. It keeps running when the AI client (and this
+ server) ends: its own process group / session, no console window on Windows."""
+ log_path.parent.mkdir(parents=True, exist_ok=True)
+ with open(log_path, "ab") as logf:
+ kw = dict(cwd=str(cwd), stdin=subprocess.DEVNULL, stdout=logf, stderr=subprocess.STDOUT,
+ env=env, close_fds=True)
+ if WIN:
+ flags = CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW
+ try: # out of the AI client's job, if it lets us
+ return subprocess.Popen(cmd, creationflags=flags | CREATE_BREAKAWAY_FROM_JOB, **kw)
+ except OSError:
+ return subprocess.Popen(cmd, creationflags=flags, **kw)
+ return subprocess.Popen(cmd, start_new_session=True, **kw)
+
+
+def run_quiet(cmd, timeout=30) -> str:
+ """A command's stdout ('' when it is missing or fails), without a console window flashing up on Windows."""
+ try:
+ return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, stdin=subprocess.DEVNULL,
+ creationflags=CREATE_NO_WINDOW if WIN else 0).stdout or ""
+ except (OSError, subprocess.SubprocessError, ValueError):
+ return ""
+
+
+def read_tail(path: Path, lines: int, max_bytes: int = 512 * 1024) -> list[str]:
+ """The last lines of a log; a progress line rewritten with \\r counts as its last version."""
+ try:
+ with open(path, "rb") as f:
+ f.seek(0, os.SEEK_END)
+ size = f.tell()
+ f.seek(max(0, size - max_bytes))
+ data = f.read().decode("utf-8", "replace")
+ except OSError:
+ return []
+ out = []
+ for raw in data.split("\n"):
+ part = [p for p in raw.split("\r") if p.strip()]
+ if part:
+ out.append(part[-1].rstrip())
+ return out[-lines:]
+
+
+def dir_size_gb(path: Path, limit_files: int = 20000) -> float:
+ total, n = 0, 0
+ for base, _dirs, files in os.walk(path):
+ for f in files:
+ try:
+ total += os.stat(os.path.join(base, f)).st_size
+ except OSError:
+ pass
+ n += 1
+ if n >= limit_files:
+ return round(total / 1e9, 1)
+ return round(total / 1e9, 1)
+
+
+# ------------------------------------------------------------------------------------------------ HTTP
+_OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({})) # localhost: never through a proxy
+
+
+def http_json(url: str, method="GET", body=None, api_key=None, timeout=3.0):
+ """(status, JSON or None); status 0 when nothing answers."""
+ data = json.dumps(body).encode() if body is not None else (b"" if method == "POST" else None)
+ req = urllib.request.Request(url, data=data, method=method, headers={"Content-Type": "application/json"})
+ if api_key:
+ req.add_header("Authorization", f"Bearer {api_key}")
+ try:
+ with _OPENER.open(req, timeout=timeout) as r:
+ raw = r.read()
+ status = r.status
+ except urllib.error.HTTPError as e:
+ raw, status = e.read(), e.code
+ except (OSError, ValueError):
+ return 0, None
+ try:
+ return status, json.loads(raw or b"null")
+ except ValueError:
+ return status, None
+
+
+def port_free(port: int, host="127.0.0.1") -> bool:
+ import socket
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
+ s.bind((host, port))
+ return True
+ except OSError:
+ return False
+ finally:
+ s.close()
+
+
+# ------------------------------------------------------------------------------------------------ validation
+class ToolError(Exception):
+ """A clear message for the AI (and its user): the tool result is an error, the server goes on."""
+
+
+def check_schema(schema: dict, args) -> dict:
+ """The tools' JSON schemas, enforced: object, known properties only, types, enums, ranges, patterns."""
+ if args is None:
+ args = {}
+ if not isinstance(args, dict):
+ raise ToolError("arguments must be a JSON object")
+ props = schema.get("properties", {})
+ for k in args:
+ if k not in props:
+ raise ToolError(f"unknown argument {k!r}; this tool takes: {', '.join(props) or 'no arguments'}")
+ for k in schema.get("required", []):
+ if k not in args:
+ raise ToolError(f"missing argument {k!r}")
+ for k, v in args.items():
+ p = props[k]
+ t = p.get("type")
+ if t == "integer" and (isinstance(v, bool) or not isinstance(v, int)):
+ if isinstance(v, float) and v.is_integer():
+ v = args[k] = int(v)
+ elif isinstance(v, str) and re.fullmatch(r"\d{1,7}", v.strip()):
+ v = args[k] = int(v.strip()) # some clients send numbers as strings
+ else:
+ raise ToolError(f"{k} must be a whole number")
+ if t == "boolean" and not isinstance(v, bool):
+ if v in ("true", "false"):
+ v = args[k] = v == "true"
+ else:
+ raise ToolError(f"{k} must be true or false")
+ if t == "string":
+ if not isinstance(v, str):
+ raise ToolError(f"{k} must be a string")
+ if len(v) > p.get("maxLength", 300) or "\x00" in v or "\n" in v or "\r" in v:
+ raise ToolError(f"{k} is too long or contains control characters")
+ if "pattern" in p and not re.fullmatch(p["pattern"], v):
+ raise ToolError(f"{k}={v!r} is not valid ({p.get('description', p['pattern'])})")
+ if "enum" in p and v not in p["enum"]:
+ raise ToolError(f"{k} must be one of: {', '.join(map(str, p['enum']))} (got {v!r})")
+ if t == "integer":
+ if "minimum" in p and v < p["minimum"] or "maximum" in p and v > p["maximum"]:
+ raise ToolError(f"{k} must be between {p.get('minimum')} and {p.get('maximum')} (got {v})")
+ return args
+
+
+def is_inside(path: Path, root: Path) -> bool:
+ try:
+ path.relative_to(root)
+ return True
+ except ValueError:
+ return False
+
+
+def norm(p) -> Path:
+ """A path to compare: absolute, links and Windows' short names (AI-SER~1) resolved, case folded on Windows."""
+ return Path(os.path.normcase(os.path.realpath(os.path.abspath(str(p)))))
+
+
+def system_folders() -> list[Path]:
+ if WIN:
+ env = [os.environ.get(k) for k in ("SystemRoot", "windir", "ProgramFiles", "ProgramFiles(x86)", "ProgramData",
+ "ProgramW6432")]
+ return [norm(p) for p in env if p]
+ return [norm(p) for p in ("/bin", "/boot", "/dev", "/etc", "/lib", "/lib32", "/lib64", "/proc", "/run", "/sbin",
+ "/sys", "/usr", "/var", "/snap")]
+
+
+# ------------------------------------------------------------------------------------------------ Strata
+class Strata:
+ """One Strata folder: what is installed there, the PC, and the processes this server started."""
+
+ def __init__(self, root: Path | str = DEFAULT_ROOT):
+ self.root = Path(root).resolve()
+ self.state_dir = self.root / STATE_DIR
+ self.lock = threading.Lock() # install / start / stop one at a time
+ self.children: list[subprocess.Popen] = [] # reaped now and then (no zombies on POSIX)
+ self._setup = None
+ self._setup_tried = False
+ self._hw_cache = None
+ self.python_override: str | None = None # tests: the interpreter that runs setup / the server
+ self.health_timeout = 2.0
+
+ # ---- setup.py's own tables and logic
+ def setup_module(self):
+ """setup.py imported as a module (its tables and its pure checks), or None. Its command runner is replaced
+ by one without console windows; nothing of its install path runs."""
+ if not self._setup_tried:
+ self._setup_tried = True
+ try:
+ spec = importlib.util.spec_from_file_location("strata_setup_for_mcp", self.root / "setup.py")
+ mod = importlib.util.module_from_spec(spec)
+ real = sys.stdout
+ sys.stdout = sys.stderr # nothing it prints may reach the protocol stream
+ try:
+ spec.loader.exec_module(mod)
+ finally:
+ sys.stdout = real
+ if all(hasattr(mod, a) for a in ("MODELS", "FAMILIES", "CONTEXTS")):
+ mod.out = lambda cmd: run_quiet(cmd, 60)
+ self._setup = mod
+ except (Exception, SystemExit) as e: # noqa: BLE001 - any failure: the built-in tables
+ log(f"setup.py could not be imported ({e}); using the built-in model table")
+ return self._setup
+
+ def tables(self):
+ s = self.setup_module()
+ if s is not None:
+ return s.MODELS, s.FAMILIES, list(s.CONTEXTS), "setup.py"
+ return FALLBACK_MODELS, FALLBACK_FAMILIES, FALLBACK_CONTEXTS, "built-in table (setup.py not importable)"
+
+ @staticmethod
+ def sizes_of(models, family) -> list:
+ return [m for m in models if family in models[m].get("families", ("qwen", "swift"))]
+
+ # ---- paths
+ def settings_path(self) -> Path:
+ if WIN:
+ return Path(os.environ.get("APPDATA") or Path.home() / "AppData" / "Roaming") / "Strata" / "settings.json"
+ return Path(os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config") / "strata" / "settings.json"
+
+ def data_dir(self) -> Path:
+ try:
+ s = json.loads(self.settings_path().read_text(encoding="utf-8"))
+ except (OSError, ValueError):
+ s = {}
+ d = s.get("data_dir") if isinstance(s, dict) else None
+ return Path(d) if d else self.root.parent / "Strata-data"
+
+ def allowed_roots(self) -> list[Path]:
+ return [norm(self.root), norm(self.data_dir())]
+
+ def allowed(self, p) -> bool:
+ p = norm(p)
+ return any(is_inside(p, r) for r in self.allowed_roots())
+
+ def venv_python(self) -> Path:
+ return self.root / ".venv" / ("Scripts/python.exe" if WIN else "bin/python")
+
+ def run_python(self) -> str:
+ return self.python_override or str(self.venv_python())
+
+ def state(self, name: str) -> dict:
+ try:
+ d = json.loads((self.state_dir / f"{name}.json").read_text(encoding="utf-8"))
+ return d if isinstance(d, dict) else {}
+ except (OSError, ValueError):
+ return {}
+
+ def save_state(self, name: str, d: dict | None) -> None:
+ self.state_dir.mkdir(parents=True, exist_ok=True)
+ f = self.state_dir / f"{name}.json"
+ if d is None:
+ f.unlink(missing_ok=True)
+ else:
+ tmp = f.with_suffix(".tmp")
+ tmp.write_text(json.dumps(d, indent=1), encoding="utf-8")
+ os.replace(tmp, f)
+
+ def reap(self):
+ self.children = [p for p in self.children if p.poll() is None]
+
+ # ---- what is installed
+ def configs(self) -> list[Path]:
+ return sorted(self.root.glob("strata-*.json"), key=lambda p: p.stat().st_mtime, reverse=True)
+
+ @staticmethod
+ def read_config(path: Path) -> dict:
+ try:
+ cfg = json.loads(path.read_text(encoding="utf-8-sig"))
+ return cfg if isinstance(cfg, dict) else {}
+ except (OSError, ValueError):
+ return {}
+
+ def describe_config(self, path: Path) -> dict:
+ cfg = self.read_config(path)
+ a = cfg.get("args") if isinstance(cfg.get("args"), list) else []
+
+ def val(k):
+ return a[a.index(k) + 1] if k in a and a.index(k) + 1 < len(a) else None
+ tag = path.stem[len("strata-"):]
+ missing = [p for p in [cfg.get("exe"), *[x for x in a if isinstance(x, str) and x.endswith(".gguf")]]
+ if not p or not Path(p).exists()]
+ script = self.root / f"run-{tag}.{'bat' if WIN else 'sh'}"
+ vis = cfg.get("vision")
+ return {"model": tag, "config": path.name, "model_name": cfg.get("model_name"),
+ "context": int(val("--max-context")) if str(val("--max-context") or "").isdigit() else None,
+ "kv": val("--kv"), "images": ("gpu" if vis.get("gpu") else "cpu") if isinstance(vis, dict) else "off",
+ "port": cfg.get("port", DEFAULT_PORT), "host": cfg.get("host", "127.0.0.1"),
+ "gpu": cfg.get("gpu"), "api_key_set": bool(cfg.get("api_key")),
+ "low_ram": "--resident-experts" in a or "--mmap-experts" in a,
+ "start_script": script.name if script.exists() else None,
+ "ready": not missing and bool(cfg), "missing_files": missing[:3],
+ "last_used": time.strftime("%Y-%m-%d %H:%M", time.localtime(path.stat().st_mtime))}
+
+ def find_config(self, model: str | None) -> Path:
+ have = self.configs()
+ if not have:
+ raise ToolError("no model is installed in " + str(self.root) + " yet: install one first (strata_install)")
+ if not model:
+ return have[0] # the most recently used, as setup starts it
+ m = model.strip().lower()
+ exact = [c for c in have if c.stem[len("strata-"):] == m]
+ if exact:
+ return exact[0]
+ by_name = [c for c in have if str(self.read_config(c).get("model_name", "")).lower() == m]
+ if by_name:
+ return by_name[0]
+ ends = [c for c in have if c.stem.endswith("-" + m)]
+ if len(ends) == 1:
+ return ends[0]
+ raise ToolError(f"no installed model {model!r}; installed: " +
+ ", ".join(c.stem[len("strata-"):] for c in have))
+
+ def engine_info(self) -> dict:
+ for d in ("engine",):
+ try:
+ meta = json.loads((self.root / d / "BUILD.json").read_text())
+ return {"folder": d, "version": meta.get("version"), "source": meta.get("source"),
+ "backend": meta.get("backend")}
+ except (OSError, ValueError):
+ continue
+ return {"folder": None, "version": None}
+
+ def installed(self) -> dict:
+ data = self.data_dir()
+ items = {}
+ for sub in ("models", "packs"):
+ for base in dict.fromkeys([data, self.root]):
+ d = base / sub
+ if d.is_dir():
+ for c in sorted(d.iterdir()):
+ if c.is_dir():
+ ggufs = list(c.glob("*.gguf"))
+ entry = {"folder": str(c), "size_gb": dir_size_gb(c)}
+ if sub == "models":
+ entry["complete"] = bool(ggufs) and all(
+ g.with_name(g.name + ".done").exists() for g in ggufs)
+ entry["downloading"] = bool(list(c.glob("*.part")))
+ items.setdefault(sub, []).append(entry)
+ mtp = data / "mtp" / "rt" / "experts.bin"
+ return {"strata_folder": str(self.root), "data_folder": str(data), "data_folder_exists": data.is_dir(),
+ "python_env": self.venv_python().exists(), "engine": self.engine_info(),
+ "models": [self.describe_config(c) for c in self.configs()],
+ "downloads": items.get("models", []), "prepared": items.get("packs", []),
+ "draft_layer": mtp.exists()}
+
+ # ---- the PC
+ def hardware(self, fresh=False) -> dict:
+ if self._hw_cache and not fresh and time.time() - self._hw_cache[0] < 600:
+ hw = dict(self._hw_cache[1])
+ else:
+ hw = self._hardware()
+ self._hw_cache = (time.time(), hw)
+ hw = dict(hw)
+ hw["disk_free_gb"] = self.disk_free(self.data_dir())
+ return hw
+
+ @staticmethod
+ def disk_free(p: Path) -> float | None:
+ p = Path(p)
+ while not p.exists() and p.parent != p:
+ p = p.parent
+ try:
+ return round(shutil.disk_usage(p).free / 1e9, 1)
+ except OSError:
+ return None
+
+ def _hardware(self) -> dict:
+ S = self.setup_module()
+ hw = {"os": f"{platform.system()} {platform.release()}", "os_version": platform.version(),
+ "machine": platform.machine(), "python": platform.python_version()}
+ if not WIN and Path("/proc/version").exists():
+ try:
+ hw["wsl"] = "microsoft" in Path("/proc/version").read_text().lower()
+ except OSError:
+ pass
+ try:
+ hw["ram_gb"] = round(S.ram_gb(), 1) if S else round(own_ram_gb(), 1)
+ except Exception: # noqa: BLE001
+ hw["ram_gb"] = round(own_ram_gb(), 1)
+ if S and WIN:
+ try:
+ hw["page_file_gb"] = round(S.page_file_gb(), 1)
+ except Exception: # noqa: BLE001
+ pass
+ try:
+ if S:
+ name, avx2, avx512 = S.cpu_info()
+ else:
+ name, avx2, avx512 = platform.processor() or "unknown CPU", None, None
+ hw["cpu"] = {"name": name, "cores": os.cpu_count(), "avx2": avx2, "avx512": avx512}
+ except Exception: # noqa: BLE001
+ hw["cpu"] = {"name": platform.processor(), "cores": os.cpu_count()}
+ nv = []
+ try:
+ nv = S.gpus() if S else own_nvidia_gpus()
+ except Exception: # noqa: BLE001
+ nv = own_nvidia_gpus()
+ for g in nv:
+ g["vendor"] = "nvidia"
+ g["vram_gb"] = round(g["vram_gb"], 1)
+ try:
+ g["problem"] = S.gpu_problem(g) if S else (None if int(g["arch"]) >= 75 else "older than RTX 20")
+ except Exception: # noqa: BLE001
+ g["problem"] = None
+ g["usable"] = g["problem"] is None
+ amd = []
+ if not WIN:
+ try:
+ amd = S.amd_gpus() if S else []
+ for g in amd:
+ g["vendor"] = "amd"
+ g["vram_gb"] = round(g["vram_gb"], 1)
+ g["problem"] = S.amd_problem(g)
+ g["usable"] = g["problem"] is None
+ except Exception: # noqa: BLE001
+ amd = []
+ if not amd and shutil.which("rocm-smi"):
+ amd = rocm_smi_gpus()
+ hw["gpus"] = nv + amd
+ if WIN:
+ hw["display_adapters"] = windows_video_controllers()
+ hw["nvidia_smi"] = bool(shutil.which("nvidia-smi"))
+ return hw
+
+ def recommend(self, hw: dict) -> dict:
+ """setup.py's own rules: the size it preselects (IQ3_XXS from 60 GB of RAM, else the first, Q2_0), the
+ Coder below the full model's RAM (setup's RAM table), the low-RAM mode when the GPU makes up for the RAM, and
+ the context it preselects for the card's VRAM (32K under 14 GB, 64K under 20 GB, else 128K)."""
+ models, families, _ctx, _src = self.tables()
+ S = self.setup_module()
+ usable = [g for g in hw.get("gpus", []) if g.get("usable")]
+ ram = hw.get("ram_gb") or 0
+ if not usable:
+ why = ("no NVIDIA RTX 20-series-or-newer GPU found (nvidia-smi did not list one)"
+ + ("; AMD cards run on Linux only" if WIN and any(
+ "amd" in str(a.get("name", "")).lower() or "radeon" in str(a.get("name", "")).lower()
+ for a in hw.get("display_adapters", [])) else ""))
+ return {"family": None, "model": None, "why": why}
+ best = max(usable, key=lambda g: (round(g["vram_gb"]), -g.get("index", 0)))
+ vram = best["vram_gb"]
+ backend = "hip" if best.get("vendor") == "amd" else "cuda"
+
+ def low_fits(m):
+ try:
+ return S.low_ram_fits(m, ram, vram) if S else ram - 6 + max(0.0, vram - 5) >= models[m]["arena_gb"]
+ except Exception: # noqa: BLE001
+ return False
+ notes = []
+ if ram >= 60:
+ fam, model, why = "qwen", "IQ3_XXS", f"{ram:.0f} GB of RAM: setup's own pick from 60 GB (better quality)"
+ elif ram >= models["Q2_0"]["ram_gb"] - 4:
+ fam, model, why = "qwen", "Q2_0", f"{ram:.0f} GB of RAM: the full model's fastest size fits"
+ elif ram >= models["IQ1_M"]["ram_gb"] - 4:
+ fam, model, why = "coder", "IQ1_M", (f"{ram:.0f} GB of RAM: the full model needs ~48 GB; the Coder "
+ "(half the experts, best for code) needs ~32 GB")
+ elif low_fits("IQ1_M"):
+ fam, model, why = "coder", "IQ1_M", (f"{ram:.0f} GB of RAM is below the Coder's 32 GB, but the GPU's "
+ f"{vram:.0f} GB make up for it (setup's low-RAM mode: slower)")
+ else:
+ return {"family": None, "model": None, "why": f"{ram:.0f} GB of RAM: Strata needs 32 GB or more "
+ "(the smallest model, the Coder, keeps ~23 GB of experts in "
+ "RAM)"}
+ ctx = 32768 if vram < 14 else 65536 if vram < 20 else 131072
+ if vram < 11:
+ notes.append("less than 12 GB of VRAM: it runs, but slowly (most experts stay on the CPU)")
+ if hw.get("cpu", {}).get("avx2") is False:
+ return {"family": None, "model": None, "why": "this CPU has no AVX2; Strata needs at least AVX2"}
+ if backend == "hip":
+ notes.append("AMD (experimental, Linux): the engine is compiled during setup; no images")
+ cmd = (("START-HERE.bat --setup" if WIN else "./setup.sh --setup") +
+ f" --yes --family {fam} --model {model} --context {ctx}" + (" --backend hip" if backend == "hip" else ""))
+ return {"family": fam, "model": model, "title": families[fam]["title"] + " " + model, "context": ctx,
+ "backend": backend, "gpu": {k: best.get(k) for k in ("index", "name", "vram_gb")},
+ "download_gb": models[model]["download_gb"], "why": why, "notes": notes, "setup_command": cmd}
+
+ def model_table(self, hw: dict | None) -> list:
+ models, families, _ctx, _src = self.tables()
+ S = self.setup_module()
+ ram = (hw or {}).get("ram_gb")
+ usable = [g for g in (hw or {}).get("gpus", []) if g.get("usable")]
+ vram = max((g["vram_gb"] for g in usable), default=0.0)
+ have = {c.stem[len("strata-"):] for c in self.configs()}
+ out = []
+ for f, fd in families.items():
+ sizes = []
+ for m in self.sizes_of(models, f):
+ d = models[m]
+ tag = (fd.get("tag", "") + m).lower()
+ verdict = None
+ if ram is not None:
+ verdict = "fits" if ram >= d["ram_gb"] else "tight" if ram >= d["ram_gb"] - 8 else "does not fit"
+ try:
+ if d.get("budget"):
+ verdict = ("experimental: part of its experts in RAM, the rest read from the SSD"
+ if ram >= d["ram_gb"] else "does not fit")
+ elif S and S.low_ram_needed(m, ram) and S.low_ram_fits(m, ram, vram):
+ verdict = "fits in the low-RAM mode (slower: the GPU holds part of the experts)"
+ except Exception: # noqa: BLE001
+ pass
+ sizes.append({"model": m, "about": d["about"], "download_gb": d["download_gb"],
+ "ram_needed_gb": d["ram_gb"], "experts_gb": d.get("arena_gb"),
+ "on_this_pc": verdict, "installed": tag in have, "id": tag})
+ out.append({"family": f, "title": fd["title"], "about": fd.get("about"),
+ "experimental": bool(fd.get("experimental")), "images": fd.get("vision", True) is not False,
+ "sizes": sizes})
+ return out
+
+ # ---- the running server
+ def ports(self, port: int | None) -> list[int]:
+ if port:
+ return [port]
+ ps = [self.state("server").get("port")] + [self.describe_config(c)["port"] for c in self.configs()]
+ return list(dict.fromkeys(int(p) for p in [*ps, DEFAULT_PORT] if isinstance(p, int)))
+
+ def api_key_for(self, port: int) -> str | None:
+ """The key a config of this folder set for that port (used for our own requests; never shown)."""
+ for c in self.configs():
+ cfg = self.read_config(c)
+ if cfg.get("api_key") and int(cfg.get("port", DEFAULT_PORT)) == port:
+ return cfg["api_key"]
+ for c in self.configs():
+ if self.read_config(c).get("api_key"):
+ return self.read_config(c)["api_key"]
+ return None
+
+ def probe(self, port: int, deep=True) -> dict | None:
+ """What answers on 127.0.0.1:port: a Strata server's facts, {"other": True} for something else, None for
+ nothing."""
+ base = f"http://127.0.0.1:{port}"
+ st, health = http_json(base + "/health", timeout=self.health_timeout)
+ if st == 0:
+ return None
+ if st != 200 or not isinstance(health, dict) or health.get("service") != "strata":
+ return {"port": port, "other": True, "note": "something else answers on this port (not a Strata server)"}
+ info = {"port": port, "url": base, "model": health.get("model"), "context": health.get("max_context"),
+ "images": health.get("images"), "loaded": health.get("loaded"),
+ "api_key_required": bool(health.get("api_key"))}
+ if not deep:
+ return info
+ key = self.api_key_for(port)
+ st, v1 = http_json(base + "/v1/status", api_key=key, timeout=self.health_timeout)
+ if st == 200 and isinstance(v1, dict):
+ info["engine_version"] = v1.get("engine")
+ info["uptime_s"] = v1.get("uptime_s")
+ info["requests_served"] = (v1.get("activity") or {}).get("requests")
+ m = v1.get("machine") or {}
+ if m.get("gpu"):
+ info["gpu"] = m["gpu"]
+ if m.get("ram"):
+ info["ram"] = m["ram"]
+ if v1.get("last_timings"):
+ lt = v1["last_timings"]
+ info["last_request"] = {k: lt.get(k) for k in ("prompt_n", "prompt_per_second", "predicted_n",
+ "predicted_per_second") if k in lt}
+ elif st == 401:
+ info["note"] = "the server wants an API key this folder's configs do not have"
+ st, s = http_json(base + "/status", api_key=key, timeout=self.health_timeout)
+ if st == 200 and isinstance(s, dict):
+ info["busy"] = bool(s.get("busy"))
+ info["activity"] = {k: s.get(k) for k in ("phase", "generated", "tokens_per_s", "queued", "elapsed_s")
+ if s.get(k) is not None}
+ if "gpu" not in info: # older servers: the Monitor's numbers
+ st, met = http_json(base + "/metrics", api_key=key, timeout=self.health_timeout)
+ if st == 200 and isinstance(met, dict):
+ hwn = met.get("hardware") or {}
+ if hwn.get("gpu_mem_total"):
+ info["gpu"] = {"used_mib": round(hwn.get("gpu_mem_used", 0) / 2**20),
+ "total_mib": round(hwn["gpu_mem_total"] / 2**20)}
+ if hwn.get("ram_total"):
+ info["ram"] = {"used_gib": round(hwn.get("ram_used", 0) / 2**30, 1),
+ "total_gib": round(hwn["ram_total"] / 2**30, 1)}
+ info["engine_version"] = (met.get("engine") or {}).get("version")
+ return info
+
+ def tracked_server(self) -> dict | None:
+ """The server this MCP server started, while it still runs (else its record is cleared)."""
+ s = self.state("server")
+ if not s:
+ return None
+ if proc_alive(s.get("pid"), s.get("ident")):
+ return s
+ return {**s, "ended": True}
+
+ # ---- install job
+ def install_job(self) -> dict | None:
+ j = self.state("install")
+ if not j:
+ return None
+ res = self.state("install-result")
+ running = proc_alive(j.get("pid"), j.get("ident"))
+ log_path = Path(j.get("log", ""))
+ tail = read_tail(log_path, 400) if log_path.name else []
+ prog = install_progress(tail)
+ out = {"running": running, "started": j.get("started"), "model": j.get("model"), "family": j.get("family"),
+ "log": str(log_path), **prog}
+ if not running:
+ if res.get("job") == j.get("job") and "exit_code" in res:
+ out["exit_code"] = res["exit_code"]
+ out["finished"] = res.get("ended")
+ out["result"] = "installed" if res["exit_code"] == 0 else "failed"
+ else:
+ out["result"] = "ended unexpectedly (no exit code recorded)"
+ else:
+ out["result"] = "running"
+ out["last_lines"] = tail[-12:]
+ return out
+
+
+def install_progress(lines: list[str]) -> dict:
+ """setup's progress from its output: the step, a download's percentage, a failure."""
+ step, title, dl, err = None, None, None, None
+ for i, line in enumerate(lines):
+ m = re.search(r"=== Step (\d+): (.+?) ===", line)
+ if m:
+ step, title, err = int(m.group(1)), m.group(2), None
+ m = re.match(r"\s*(\S.*?): +([\d.]+) / ([\d.]+) GB \((\d+)%\)", line)
+ if m:
+ dl = {"file": m.group(1), "done_gb": float(m.group(2)), "total_gb": float(m.group(3)),
+ "percent": int(m.group(4))}
+ if line.strip().startswith("[X]"):
+ err = line.strip()[3:].strip()
+ if i + 1 < len(lines) and lines[i + 1].startswith(" "):
+ err += " - " + lines[i + 1].strip()
+ out = {"step": f"{step}/7: {title}" if step else None}
+ if dl:
+ out["download"] = dl
+ if err:
+ out["error"] = err
+ return out
+
+
+def own_ram_gb() -> float:
+ if WIN:
+ class MS(ctypes.Structure):
+ _fields_ = [("dwLength", ctypes.c_ulong), ("dwMemoryLoad", ctypes.c_ulong),
+ *((n, ctypes.c_ulonglong) for n in ("tp", "ap", "tpf", "apf", "tv", "av", "aev"))]
+ m = MS()
+ m.dwLength = ctypes.sizeof(MS)
+ ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(m))
+ return m.tp / 2**30
+ try:
+ for line in open("/proc/meminfo"):
+ if line.startswith("MemTotal"):
+ return int(line.split()[1]) * 1024 / 2**30
+ except OSError:
+ pass
+ return 0.0
+
+
+def own_nvidia_gpus() -> list:
+ s = run_quiet(["nvidia-smi", "--query-gpu=index,name,memory.total,compute_cap,driver_version",
+ "--format=csv,noheader,nounits"])
+ found = []
+ for line in s.strip().splitlines():
+ try:
+ idx, name, mem, cc, drv = [x.strip() for x in line.split(",")]
+ found.append({"index": int(idx), "name": name, "vram_gb": float(mem) / 1024.0,
+ "arch": cc.replace(".", ""), "driver": drv})
+ except ValueError:
+ continue
+ return found
+
+
+def windows_video_controllers() -> list:
+ s = run_quiet(["powershell", "-NoProfile", "-NonInteractive", "-Command",
+ "Get-CimInstance Win32_VideoController | Select-Object Name,DriverVersion | ConvertTo-Json -Compress"])
+ try:
+ d = json.loads(s) if s.strip() else []
+ except ValueError:
+ return []
+ d = [d] if isinstance(d, dict) else d
+ return [{"name": x.get("Name"), "driver": x.get("DriverVersion")} for x in d if isinstance(x, dict)]
+
+
+def rocm_smi_gpus() -> list:
+ s = run_quiet(["rocm-smi", "--showproductname", "--showmeminfo", "vram", "--json"])
+ try:
+ d = json.loads(s)
+ except ValueError:
+ return []
+ out = []
+ for i, (card, v) in enumerate(sorted(d.items())):
+ if not isinstance(v, dict) or not card.lower().startswith("card"):
+ continue
+ total = next((v[k] for k in v if "total" in k.lower() and "vram" in k.lower()), 0)
+ try:
+ vram = int(total) / 2**30
+ except (TypeError, ValueError):
+ vram = 0.0
+ out.append({"index": i, "name": v.get("Card Series") or v.get("Card SKU") or card, "vram_gb": round(vram, 1),
+ "vendor": "amd", "usable": None, "problem": "listed by rocm-smi (setup checks the card itself)"})
+ return out
+
+
+# ------------------------------------------------------------------------------------------------ the tools
+GPUS_PATTERN = r"all|\d{1,2}(,\d{1,2}){1,7}"
+MODEL_ID_PATTERN = r"[A-Za-z0-9][A-Za-z0-9_.-]{0,48}"
+
+
+def tool_defs(models: dict, families: dict, contexts: list) -> list:
+ port = {"type": "integer", "minimum": 1024, "maximum": 65535,
+ "description": "the server's port (default: the installed model's port, usually 8080)"}
+ installed_model = {"type": "string", "pattern": MODEL_ID_PATTERN, "maxLength": 49,
+ "description": "an installed model's id as strata_status lists it (e.g. 'q2_0', "
+ "'coder-iq1_m', 'swift-iq3_xxs'); default: the most recently used one"}
+ ro = {"readOnlyHint": True, "destructiveHint": False, "openWorldHint": False}
+ return [
+ {"name": "strata_status", "title": "Strata status",
+ "description": "Is Strata (the local AI model server) running, and what is installed? Reports the running "
+ "server (model, engine version, context, busy or idle, VRAM/RAM in use), the installed models "
+ "and data folder, an install in progress, this PC's hardware (OS, CPU, RAM, GPUs, free disk) "
+ "and the model size setup recommends for this PC. Read-only; call it first.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {
+ "port": port,
+ "hardware": {"type": "boolean", "description": "include the hardware check and the recommendation "
+ "(default true; takes a second or two)"}}},
+ "annotations": ro},
+ {"name": "strata_models", "title": "Strata models",
+ "description": "The models and sizes Strata's setup offers (families: qwen = Qwen3.8-Flash-Next, swift = "
+ "Swift 1.5, coder = the Coder, unsloth = experimental 4-bit), with download size, RAM needed, "
+ "whether each fits this PC, which are installed, the context lengths, and the recommendation. "
+ "Read-only.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {}},
+ "annotations": ro},
+ {"name": "strata_install", "title": "Install Strata",
+ "description": "Install a Strata model on this PC by running Strata's setup non-interactively in the "
+ "background (Python packages, the engine, a 58-111 GB model download, preparation, start "
+ "script). Without confirm=true it only returns the plan (what would be downloaded, disk "
+ "space): show it to the user and call again with confirm=true once they agree. Returns at "
+ "once; call it again (no arguments needed) to see the progress, or with cancel=true to stop "
+ "it (it resumes later). Defaults: the recommended model and context for this PC, images off. "
+ "After it finishes, start the model with strata_start.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {
+ "family": {"type": "string", "enum": list(families),
+ "description": "qwen = Qwen3.8-Flash-Next (the original), swift = Swift 1.5 (thinks "
+ "shorter), coder = the Coder (half the experts, ~32 GB RAM, for code), "
+ "unsloth = 4-bit EXPERIMENTAL (slow)"},
+ "model": {"type": "string", "enum": list(models),
+ "description": "the size (quantization): see strata_models"},
+ "context": {"type": "integer", "enum": contexts,
+ "description": "context length in tokens (more needs more VRAM)"},
+ "vision": {"type": "string", "enum": ["no", "yes", "gpu", "cpu"],
+ "description": "let the model read images (yes = the encoder on the GPU; ~0.9 GB more "
+ "download); default no"},
+ "kv": {"type": "string", "enum": ["int8", "q4_0", "k8v4"],
+ "description": "KV cache precision above 8K context (default int8)"},
+ "gpu": {"type": "integer", "minimum": 0, "maximum": 63,
+ "description": "one GPU, as nvidia-smi numbers them (default: the one with the most VRAM)"},
+ "gpus": {"type": "string", "pattern": GPUS_PATTERN, "maxLength": 40,
+ "description": "several GPUs sharing the model: '0,2' or 'all' (experimental)"},
+ "backend": {"type": "string", "enum": ["auto", "cuda", "hip"],
+ "description": "auto (default), cuda = NVIDIA, hip = AMD on Linux (experimental)"},
+ "low_ram": {"type": "string", "enum": ["auto", "on", "off", "resident", "mmap"],
+ "description": "setup's low-RAM mode (default auto)"},
+ "port": {**port, "description": "the port the model will listen on (default 8080)"},
+ "data_dir": {"type": "string", "maxLength": 260,
+ "description": "where the model files go (default: the Strata-data folder next to the "
+ "Strata folder). Only the current data folder, a folder inside the Strata "
+ "folder, or a new absolute path whose last folder is named Strata... "
+ "(e.g. D:\\Strata-data) outside system folders"},
+ "confirm": {"type": "boolean", "description": "true = really start the install (after the user agreed "
+ "to the plan and the download size)"},
+ "cancel": {"type": "boolean", "description": "true = stop the install in progress"}}},
+ "annotations": {"readOnlyHint": False, "destructiveHint": False, "idempotentHint": True,
+ "openWorldHint": True}},
+ {"name": "strata_start", "title": "Start Strata",
+ "description": "Start an installed Strata model in the background (as its start script does, without "
+ "opening a browser) and wait until it answers. Loading takes 1-3 minutes and the PC can be "
+ "slow meanwhile; if it is not ready within wait_seconds this returns 'loading' - check again "
+ "with strata_status. The model keeps running until strata_stop.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {
+ "model": installed_model, "port": port,
+ "gpu": {"type": "integer", "minimum": 0, "maximum": 63,
+ "description": "run on this GPU for this start (as nvidia-smi numbers them)"},
+ "wait_seconds": {"type": "integer", "minimum": 0, "maximum": 600,
+ "description": "how long to wait for it to be ready (default 50: some AI apps end "
+ "a tool call after 60 s)"}}},
+ "annotations": {"readOnlyHint": False, "destructiveHint": False, "idempotentHint": True,
+ "openWorldHint": False}},
+ {"name": "strata_stop", "title": "Stop Strata",
+ "description": "Stop the Strata model: the engine is unloaded through the server's own API (it frees its "
+ "VRAM and RAM itself), then the server this tool started is ended. A Strata started "
+ "elsewhere (e.g. its own window) is only unloaded - close its window to end it. Refuses while "
+ "a request is running unless force=true.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {
+ "port": port,
+ "force": {"type": "boolean", "description": "stop even while a request is running"}}},
+ "annotations": {"readOnlyHint": False, "destructiveHint": True, "idempotentHint": True,
+ "openWorldHint": False}},
+ {"name": "strata_logs", "title": "Strata logs",
+ "description": "The last lines of a Strata log: 'server' (the model server this tool started: loading, "
+ "requests, errors), 'engine' (the engine's own log for a model), 'setup' (the install). "
+ "Default: the setup log while an install runs, else the server log. Read-only.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {
+ "source": {"type": "string", "enum": ["server", "engine", "setup"]},
+ "model": installed_model,
+ "lines": {"type": "integer", "minimum": 1, "maximum": 500, "description": "default 80"}}},
+ "annotations": ro},
+ {"name": "strata_benchmark", "title": "Strata speed test",
+ "description": "A short fixed request (greedy, thinking off) through the running Strata server; reports "
+ "the output speed (tokens/s) and prompt speed. Takes 10-60 s. Refuses while the model is "
+ "busy.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {
+ "port": port,
+ "max_tokens": {"type": "integer", "minimum": 16, "maximum": 512, "description": "default 200"}}},
+ "annotations": {"readOnlyHint": True, "destructiveHint": False, "openWorldHint": False}},
+ {"name": "strata_connect_info", "title": "Connect to Strata",
+ "description": "How to point other apps at the local Strata server: the OpenAI and Anthropic base URLs, "
+ "the model id, whether an API key is needed, and ready-to-paste settings for Claude Code, "
+ "Codex CLI, the OpenAI SDK, curl, Cursor/Continue/Open WebUI. Read-only.",
+ "inputSchema": {"type": "object", "additionalProperties": False, "properties": {"port": port}},
+ "annotations": ro},
+ ]
+
+
+class Tools:
+ def __init__(self, strata: Strata):
+ self.s = strata
+
+ def defs(self) -> list:
+ return tool_defs(*self.s.tables()[:3])
+
+ def call(self, name: str, args) -> dict:
+ defs = {d["name"]: d for d in self.defs()}
+ if name not in defs:
+ raise KeyError(name)
+ args = check_schema(defs[name]["inputSchema"], args)
+ return getattr(self, name)(**args)
+
+ # ---- strata_status
+ def strata_status(self, port=None, hardware=True) -> dict:
+ s = self.s
+ s.reap()
+ servers = []
+ for p in s.ports(port):
+ info = s.probe(p)
+ if info is not None:
+ servers.append(info)
+ tracked = s.tracked_server()
+ out = {}
+ running = [x for x in servers if not x.get("other")]
+ if tracked and not tracked.get("ended"):
+ mine = next((x for x in running if x["port"] == tracked["port"]), None)
+ if mine:
+ mine["started_by"] = f"this MCP server (pid {tracked['pid']})"
+ else:
+ out["loading"] = {"model": tracked.get("model"), "port": tracked["port"], "pid": tracked["pid"],
+ "since": tracked.get("started"),
+ "last_log_line": (read_tail(Path(tracked.get("log", "")), 1) or [None])[-1]}
+ elif tracked and tracked.get("ended"):
+ out["last_start"] = {"model": tracked.get("model"), "ended": True,
+ "last_log_lines": read_tail(Path(tracked.get("log", "")), 6)}
+ for x in running:
+ x.setdefault("started_by", "outside this MCP server (its own window, a start script or a service)")
+ out["running"] = running
+ others = [x for x in servers if x.get("other")]
+ if others:
+ out["ports_used_by_other_programs"] = others
+ if running:
+ r = running[0]
+ summary = (f"Strata is running on port {r['port']}: {r.get('model')}"
+ + (", busy" if r.get("busy") else ", idle")
+ + ("" if r.get("loaded", True) else " (model unloaded; the next request loads it)"))
+ elif "loading" in out:
+ summary = f"Strata is loading {out['loading']['model']} (port {out['loading']['port']})"
+ else:
+ summary = "Strata is not running"
+ inst = s.installed()
+ job = s.install_job()
+ if job:
+ out["install"] = job
+ if job["running"]:
+ summary += f"; an install is running ({job.get('step') or 'starting'})"
+ if not inst["models"]:
+ summary += "; no model is installed yet"
+ else:
+ summary += "; installed: " + ", ".join(m["model"] for m in inst["models"])
+ out["installed"] = inst
+ if hardware:
+ hw = s.hardware()
+ out["hardware"] = hw
+ out["recommendation"] = s.recommend(hw)
+ return {"summary": summary, **out}
+
+ # ---- strata_models
+ def strata_models(self) -> dict:
+ s = self.s
+ hw = s.hardware()
+ _m, _f, contexts, src = s.tables()
+ rec = s.recommend(hw)
+ return {"summary": "Model families and sizes setup offers" + (
+ f"; recommended for this PC: {rec['title']}, {rec['context'] // 1024}K context"
+ if rec.get("model") else f"; no model fits this PC: {rec['why']}"),
+ "families": s.model_table(hw), "contexts": contexts,
+ "context_note": "longer contexts need more VRAM (fewer experts fit on the GPU); past 262144 setup adds "
+ "rope scaling (experimental)",
+ "recommendation": rec, "source": src,
+ "this_pc": {"ram_gb": hw.get("ram_gb"),
+ "gpus": [{k: g.get(k) for k in ("index", "name", "vram_gb", "usable")}
+ for g in hw.get("gpus", [])]}}
+
+ # ---- strata_install
+ def check_data_dir(self, raw: str) -> Path:
+ s = self.s
+ if raw.startswith("\\\\") or raw.startswith("//"):
+ raise ToolError("data_dir: network (UNC) paths are not allowed; use a local drive")
+ p = Path(os.path.expandvars(raw)).expanduser()
+ if not p.is_absolute():
+ raise ToolError("data_dir must be an absolute path (e.g. D:\\Strata-data or /mnt/big/Strata-data)")
+ if ".." in p.parts:
+ raise ToolError("data_dir must not contain '..'")
+ n = norm(p)
+ current = norm(s.data_dir())
+ if n == current or is_inside(n, norm(s.root)) or n == norm(s.root.parent / "Strata-data"):
+ pass
+ else:
+ if not p.name.lower().startswith("strata"):
+ raise ToolError(f"data_dir {raw!r}: a new data folder must be named Strata... (e.g. "
+ f"{Path(p.anchor or '/') / 'Strata-data'}), so that no other folder is filled with "
+ "model files")
+ if not p.parent.is_dir():
+ raise ToolError(f"data_dir {raw!r}: its parent folder {p.parent} does not exist")
+ if any(n == f or is_inside(n, f) for f in system_folders()):
+ raise ToolError(f"data_dir {raw!r} is inside a system folder")
+ if p.exists() and not p.is_dir():
+ raise ToolError(f"data_dir {raw!r} is a file")
+ return p
+
+ def strata_install(self, family=None, model=None, context=None, vision=None, kv=None, gpu=None, gpus=None,
+ backend=None, low_ram=None, port=None, data_dir=None, confirm=False, cancel=False) -> dict:
+ s = self.s
+ with s.lock:
+ job = s.install_job()
+ if cancel:
+ if not job or not job["running"]:
+ return {"summary": "no install is running", "install": job}
+ j = s.state("install")
+ res = s.state("install-result")
+ child = res.get("child_pid") if res.get("job") == j.get("job") else None
+ if child:
+ proc_kill_tree(child, res.get("child_ident"))
+ proc_kill_tree(j["pid"], j["ident"])
+ wait_gone(j["pid"], j["ident"], 15)
+ return {"summary": "the install was stopped; calling strata_install again resumes it (downloads "
+ "continue where they stopped)", "install": s.install_job()}
+ if job and job["running"]:
+ return {"summary": f"an install is running: {job.get('step') or 'starting'}"
+ + (f", downloading {job['download']['file']} {job['download']['percent']}%"
+ if job.get("download") else ""),
+ "install": job, "follow": "call strata_install again for progress, or strata_logs "
+ "source=setup for the full output"}
+ given = [x for x in (family, model, context, vision, kv, gpu, gpus, backend, low_ram, port, data_dir)
+ if x is not None]
+ if job and not given and not confirm:
+ return {"summary": f"the last install {job['result']}" + (f": {job['error']}" if job.get("error") else ""),
+ "install": job,
+ "next": "strata_start" if job.get("exit_code") == 0 else
+ "fix the problem above and call strata_install again with confirm=true (finished "
+ "steps are skipped)"}
+ plan, args = self.install_plan(family, model, context, vision, kv, gpu, gpus, backend, low_ram, port,
+ data_dir)
+ tracked = s.tracked_server()
+ if tracked and not tracked.get("ended"):
+ raise ToolError("Strata is running (started by strata_start): stop it first with strata_stop - setup "
+ "may update the engine files it uses")
+ if not confirm:
+ return {"summary": f"Plan: install {plan['title']} ({plan['download_gb']:.0f} GB download"
+ f"{' at most; part of it is already here' if plan['partly_downloaded'] else ''}). "
+ "Nothing was done yet: ask the user, then call strata_install again with the "
+ "same arguments and confirm=true.", "plan": plan}
+ if plan["disk_short"]:
+ raise ToolError(plan["disk_short"])
+ return self.launch_install(plan, args)
+
+ def install_plan(self, family, model, context, vision, kv, gpu, gpus, backend, low_ram, port, data_dir):
+ s = self.s
+ models, families, contexts, _src = s.tables()
+ if gpu is not None and gpus is not None:
+ raise ToolError("give gpu (one card) or gpus (several), not both")
+ if backend == "hip" and WIN:
+ raise ToolError("the AMD (hip) backend runs on Linux only; on Windows Strata needs an NVIDIA RTX 20 "
+ "series or newer card")
+ if sys.version_info < (3, 10) or (WIN and sys.maxsize <= 2**32):
+ raise ToolError("this Python is too old or 32-bit: Strata's setup needs 64-bit Python 3.10+; run "
+ + ("START-HERE.bat" if WIN else "./setup.sh") + " once instead (it installs Python)")
+ hw = s.hardware()
+ rec = s.recommend(hw)
+ if model is None and family is None:
+ if not rec.get("model"):
+ raise ToolError("no model was given and none fits this PC: " + rec["why"])
+ family, model = rec["family"], rec["model"]
+ context = context or rec["context"]
+ elif model is None:
+ sizes = s.sizes_of(models, family)
+ model = rec["model"] if rec.get("family") == family else ("IQ3_XXS" if "IQ3_XXS" in sizes and
+ hw.get("ram_gb", 0) >= 60 else sizes[0])
+ elif family is None:
+ fams = [f for f in families if model in s.sizes_of(models, f)]
+ family = "qwen" if "qwen" in fams else fams[0]
+ sizes = s.sizes_of(models, family)
+ if model not in sizes:
+ raise ToolError(f"{families[family]['title']} has no {model}; its sizes: {', '.join(sizes)}")
+ if context is None:
+ usable = [g for g in hw.get("gpus", []) if g.get("usable")]
+ vram = max((g["vram_gb"] for g in usable), default=12)
+ context = 32768 if vram < 14 else 65536 if vram < 20 else 131072
+ if models[model].get("budget"):
+ context = 8192 if vram < 14 else 32768
+ if vision in ("yes", "gpu", "cpu") and families[family].get("vision") is False:
+ raise ToolError(f"images are not available with {families[family]['title']} yet: use vision=no")
+ if vision is not None and backend == "hip" and vision != "no":
+ raise ToolError("images are not available on the AMD backend yet: use vision=no")
+ target = self.check_data_dir(data_dir) if data_dir else s.data_dir()
+ tag = (families[family].get("tag", "") + model)
+ have_dir = target / "models" / tag
+ partly = have_dir.is_dir() and any(have_dir.glob("*.gguf*"))
+ need = (8 if partly else models[model]["download_gb"] + 8) + (1 if vision in ("yes", "gpu", "cpu") else 0)
+ free = s.disk_free(target)
+ short = None
+ if free is not None and free < need:
+ short = (f"not enough free disk space for {model}: about {need:.0f} GB needed at {target}, "
+ f"{free:.0f} GB free; free some space or choose data_dir on a bigger drive (e.g. D:\\Strata-data)")
+ args = ["--yes", "--no-start", "--family", family, "--model", model, "--context", str(context),
+ "--vision", vision or "no"]
+ if family in ("qwen", "coder"):
+ args += ["--experimental-speed-projection", "off"]
+ for flag, v in (("--kv", kv), ("--gpu", gpu), ("--gpus", gpus), ("--low-ram", low_ram), ("--port", port),
+ ("--data-dir", str(target) if data_dir else None)):
+ if v is not None:
+ args += [flag, str(v)]
+ if backend in ("cuda", "hip"):
+ args += ["--backend", backend]
+ fit = None
+ ram = hw.get("ram_gb")
+ if ram is not None:
+ need_ram = models[model]["ram_gb"]
+ fit = "fits" if ram >= need_ram else f"needs ~{need_ram} GB of RAM, this PC has {ram:.0f} GB"
+ installed = (s.root / f"strata-{tag.lower()}.json").exists()
+ plan = {"title": f"{families[family]['title']} {model}", "family": family, "model": model,
+ "context": context, "images": vision or "no", "download_gb": models[model]["download_gb"],
+ "partly_downloaded": partly, "ram": fit, "data_folder": str(target), "disk_free_gb": free,
+ "disk_needed_gb": round(need), "disk_short": short, "already_installed": installed,
+ "setup_command": ("START-HERE.bat " if WIN else "./setup.sh ") + " ".join(args),
+ "takes": "about 20-90 minutes, mostly the download; the PC stays usable",
+ "recommended_for_this_pc": rec if rec.get("model") else None}
+ if installed:
+ plan["note"] = ("this model is already installed: running setup again updates the engine and applies "
+ "the options (finished steps are skipped)")
+ if families[family].get("license"):
+ plan["license"] = families[family]["license"]
+ if families[family].get("experimental"):
+ plan["experimental"] = True
+ return plan, args
+
+ def launch_install(self, plan: dict, args: list) -> dict:
+ s = self.s
+ s.state_dir.mkdir(parents=True, exist_ok=True)
+ job_id = f"{int(time.time())}-{os.getpid()}"
+ log_path = s.state_dir / "setup.log"
+ with open(log_path, "a", encoding="utf-8") as f:
+ f.write(f"\n===== strata-mcp: install {plan['title']} at {time.strftime('%Y-%m-%d %H:%M:%S')} =====\n")
+ spec = {"job": job_id, "root": str(s.root), "python": s.run_python(), "base_python": sys.executable,
+ "create_venv": s.python_override is None and not s.venv_python().exists(),
+ "args": args, "result": str(s.state_dir / "install-result.json")}
+ spec_path = s.state_dir / "install-job.json"
+ spec_path.write_text(json.dumps(spec, indent=1), encoding="utf-8")
+ s.save_state("install-result", None)
+ env = dict(os.environ, PYTHONUNBUFFERED="1", PYTHONIOENCODING="utf-8")
+ p = spawn_detached([sys.executable, str(Path(__file__).resolve()), "--install-job", str(spec_path)],
+ s.root, log_path, env)
+ s.children.append(p)
+ time.sleep(0.2)
+ ident = proc_identity(p.pid)
+ if ident is None and p.poll() and s.state("install-result").get("job") != job_id:
+ raise ToolError("setup did not start: " + " | ".join(read_tail(log_path, 5)))
+ s.save_state("install", {"job": job_id, "pid": p.pid, "ident": ident, "log": str(log_path),
+ "model": plan["model"], "family": plan["family"],
+ "started": time.strftime("%Y-%m-%d %H:%M:%S"), "args": args})
+ return {"summary": f"Install of {plan['title']} started in the background (about 20-90 minutes, mostly the "
+ f"{plan['download_gb']:.0f} GB download). Call strata_install again to see the progress; "
+ "when it is done, start it with strata_start.",
+ "plan": plan, "log": str(log_path), "pid": p.pid}
+
+ # ---- strata_start
+ def strata_start(self, model=None, port=None, gpu=None, wait_seconds=50) -> dict:
+ s = self.s
+ with s.lock:
+ job = s.install_job()
+ if job and job["running"]:
+ raise ToolError("an install is still running (strata_install shows its progress); start the model "
+ "when it is done")
+ cfg_path = s.find_config(model)
+ desc = s.describe_config(cfg_path)
+ if not desc["ready"]:
+ raise ToolError(f"{cfg_path.name} refers to missing files ({', '.join(desc['missing_files'])}): run "
+ "the install again (strata_install with confirm=true) to repair it")
+ port = port or desc["port"] or DEFAULT_PORT
+ tracked = s.tracked_server()
+ if tracked and not tracked.get("ended"):
+ info = s.probe(tracked["port"])
+ return {"summary": f"Strata is already {'running' if info else 'loading'} ({tracked.get('model')}, "
+ f"port {tracked['port']}, started by this MCP server)"
+ + ("" if tracked.get("model") == desc["model"] else
+ ": stop it first (strata_stop) to start another model"),
+ "server": info}
+ info = s.probe(port, deep=False)
+ if info and not info.get("other"):
+ return {"summary": f"a Strata server already runs on port {port} ({info.get('model')}), started "
+ "outside this MCP server; stop it (close its window) or use another port",
+ "server": info}
+ if info or not port_free(port):
+ raise ToolError(f"port {port} is used by another program; choose another port (port=...)")
+ py = s.run_python()
+ if not Path(py).exists():
+ raise ToolError("Strata's Python environment (.venv) is missing: run the install again")
+ cmd = [py, str(s.root / "serve" / "server.py"), "--engine", "strata", "--config", str(cfg_path),
+ "--port", str(port)]
+ if gpu is not None:
+ cmd += ["--gpu", str(gpu)]
+ log_path = s.state_dir / "server.log"
+ s.state_dir.mkdir(parents=True, exist_ok=True)
+ with open(log_path, "a", encoding="utf-8") as f:
+ f.write(f"\n===== strata-mcp: start {desc['model']} on port {port} at "
+ f"{time.strftime('%Y-%m-%d %H:%M:%S')} =====\n")
+ env = dict(os.environ, PYTHONUNBUFFERED="1", PYTHONIOENCODING="utf-8")
+ try:
+ os.utime(cfg_path) # the most recently used model, as setup's start marks it
+ except OSError:
+ pass
+ p = spawn_detached(cmd, s.root, log_path, env)
+ s.children.append(p)
+ time.sleep(0.1)
+ ident = proc_identity(p.pid)
+ s.save_state("server", {"pid": p.pid, "ident": ident, "port": port, "model": desc["model"],
+ "config": cfg_path.name, "log": str(log_path),
+ "started": time.strftime("%Y-%m-%d %H:%M:%S")})
+ return self.wait_ready(p, ident, port, desc, wait_seconds, log_path)
+
+ def wait_ready(self, p, ident, port, desc, wait_seconds, log_path) -> dict:
+ s = self.s
+ end = time.time() + wait_seconds
+ while True:
+ if p.poll() is not None or not proc_alive(p.pid, ident):
+ tail = read_tail(log_path, 15)
+ end_group_leftovers(p.pid)
+ s.save_state("server", None)
+ raise ToolError(f"Strata stopped while starting (exit code {p.poll()}). Its last output:\n"
+ + "\n".join(tail))
+ info = s.probe(port)
+ if info and not info.get("other"):
+ return {"summary": f"Strata is running: {info.get('model')} on http://127.0.0.1:{port}/v1 "
+ "(OpenAI and Anthropic APIs; chat page at http://127.0.0.1:" + str(port) + "/)",
+ "server": {**info, "pid": p.pid}}
+ if time.time() >= end:
+ return {"summary": f"Strata is loading {desc['model']} (this takes 1-3 minutes; the PC may be slow "
+ "meanwhile). Check with strata_status in a minute.",
+ "loading": {"pid": p.pid, "port": port,
+ "last_log_line": (read_tail(log_path, 1) or [None])[-1]}}
+ time.sleep(1.0)
+
+ # ---- strata_stop
+ def strata_stop(self, port=None, force=False) -> dict:
+ s = self.s
+ with s.lock:
+ tracked = s.tracked_server()
+ if tracked and tracked.get("ended"):
+ s.save_state("server", None)
+ tracked = None
+ if tracked:
+ port = tracked["port"]
+ key = s.api_key_for(port)
+ st, body = http_json(f"http://127.0.0.1:{port}/status", api_key=key, timeout=3)
+ if st == 200 and isinstance(body, dict) and body.get("busy") and not force:
+ raise ToolError("a request is running on Strata right now; wait for it, or call strata_stop "
+ "with force=true to stop anyway")
+ st, body = http_json(f"http://127.0.0.1:{port}/unload", "POST", api_key=key, timeout=120)
+ unloaded = (body or {}).get("status") if isinstance(body, dict) else None
+ if st == 409 and not force:
+ raise ToolError("Strata is busy (a request is running or queued); call again with force=true "
+ "to stop anyway")
+ proc_terminate(tracked["pid"], tracked["ident"]) # POSIX: SIGTERM = the server's own Ctrl+C path
+ clean = wait_gone(tracked["pid"], tracked["ident"], 30)
+ if not clean:
+ proc_kill_tree(tracked["pid"], tracked["ident"])
+ wait_gone(tracked["pid"], tracked["ident"], 15)
+ s.reap()
+ gone = not proc_alive(tracked["pid"], tracked["ident"])
+ if gone:
+ end_group_leftovers(tracked["pid"])
+ s.save_state("server", None)
+ return {"summary": "Strata stopped" + ("" if gone else " - but its process still runs; see strata_logs")
+ + (" (the engine unloaded itself first)" if unloaded == "unloaded" else ""),
+ "model": tracked.get("model"), "port": port, "engine_unloaded": unloaded,
+ "ended_cleanly": clean}
+ for p in s.ports(port):
+ info = s.probe(p, deep=False)
+ if info and not info.get("other"):
+ key = s.api_key_for(p)
+ st, body = http_json(f"http://127.0.0.1:{p}/unload", "POST", api_key=key, timeout=120)
+ r = body.get("status") if isinstance(body, dict) else None
+ if st == 409:
+ raise ToolError("Strata is busy (a request is running); try again when it is done")
+ if st == 401:
+ raise ToolError("Strata (started outside this MCP server) wants an API key this folder's "
+ "configs do not have; close its window to stop it")
+ return {"summary": f"Strata on port {p} was started outside this MCP server (its own window or a "
+ f"service), so only its model was unloaded ({r}): its VRAM and RAM are free, "
+ "but the server still listens and loads the model again on the next request. "
+ "Close its window to end it completely.",
+ "port": p, "engine_unloaded": r}
+ return {"summary": "Strata is not running"}
+
+ # ---- strata_logs
+ def strata_logs(self, source=None, model=None, lines=80) -> dict:
+ s = self.s
+ if source is None:
+ job = s.install_job()
+ source = "setup" if job and job["running"] else "server"
+ if source == "setup":
+ path = s.state_dir / "setup.log"
+ elif source == "server":
+ path = s.state_dir / "server.log"
+ else:
+ cfg = s.find_config(model or (s.state("server").get("model")))
+ raw = s.read_config(cfg).get("log")
+ if not raw:
+ raise ToolError(f"{cfg.name} names no engine log")
+ path = Path(raw)
+ if not s.allowed(path):
+ raise ToolError(f"{cfg.name}'s log {raw} is outside the Strata folder and its data folder: not shown")
+ if not path.exists():
+ return {"summary": f"no {source} log yet ({path})", "log": str(path), "lines": []}
+ tail = read_tail(path, lines)
+ return {"summary": f"last {len(tail)} lines of the {source} log", "log": str(path), "lines": tail}
+
+ # ---- strata_benchmark
+ def strata_benchmark(self, port=None, max_tokens=200) -> dict:
+ s = self.s
+ target = None
+ for p in s.ports(port):
+ info = s.probe(p, deep=False)
+ if info and not info.get("other"):
+ target = info
+ break
+ if target is None:
+ raise ToolError("Strata is not running: start it first (strata_start)")
+ p = target["port"]
+ key = s.api_key_for(p)
+ st, stat = http_json(f"http://127.0.0.1:{p}/status", api_key=key, timeout=3)
+ if st == 200 and isinstance(stat, dict) and (stat.get("busy") or stat.get("queued")):
+ raise ToolError("Strata is busy with a request; run the speed test when it is idle")
+ body = {"model": target.get("model") or "strata", "max_tokens": max_tokens, "temperature": 0,
+ "reasoning_effort": "none", "stream": False,
+ "messages": [{"role": "user", "content": BENCH_PROMPT}]}
+ t0 = time.time()
+ st, r = http_json(f"http://127.0.0.1:{p}/v1/chat/completions", "POST", body, api_key=key, timeout=600)
+ wall = time.time() - t0
+ if st != 200 or not isinstance(r, dict):
+ msg = (r or {}).get("error", {}).get("message") if isinstance(r, dict) else None
+ raise ToolError(f"the request failed (HTTP {st}): {msg or 'no answer'}")
+ usage, tm = r.get("usage") or {}, r.get("timings") or {}
+ n = usage.get("completion_tokens") or tm.get("predicted_n") or 0
+ out = {"model": r.get("model"), "port": p, "prompt_tokens": usage.get("prompt_tokens"),
+ "output_tokens": n, "wall_s": round(wall, 2),
+ "output_tok_s": round(tm["predicted_per_second"], 1) if tm.get("predicted_per_second") else None,
+ "prompt_tok_s": round(tm["prompt_per_second"], 1) if tm.get("prompt_per_second") else None,
+ "wall_tok_s": round(n / wall, 1) if wall > 0 and n else None,
+ "finish_reason": ((r.get("choices") or [{}])[0]).get("finish_reason"),
+ "settings": f"greedy (temperature 0), thinking off, max_tokens {max_tokens}, a fixed story prompt"}
+ speed = out["output_tok_s"] or out["wall_tok_s"]
+ out["summary"] = (f"{n} tokens at {speed} tokens/s" + (f" (prompt {out['prompt_tok_s']} tokens/s)"
+ if out["prompt_tok_s"] else "")
+ + f", {wall:.1f} s in total")
+ out["note"] = ("output speed depends on the context length, the GPU's free VRAM and what else the PC does; "
+ "the first request after a start is slower")
+ return out
+
+ # ---- strata_connect_info
+ def strata_connect_info(self, port=None) -> dict:
+ s = self.s
+ target = None
+ for p in s.ports(port):
+ info = s.probe(p, deep=False)
+ if info and not info.get("other"):
+ target = info
+ break
+ if port is None:
+ cfgs = s.configs()
+ port = target["port"] if target else (s.describe_config(cfgs[0])["port"] if cfgs else DEFAULT_PORT)
+ model = (target or {}).get("model") or "strata"
+ key_needed = (target or {}).get("api_key_required")
+ if key_needed is None:
+ key_needed = any(s.describe_config(c)["api_key_set"] for c in s.configs())
+ key = "<your api_key from strata-<model>.json>" if key_needed else "none"
+ base = f"http://127.0.0.1:{port}"
+ return {
+ "summary": (f"Strata {'runs' if target else 'will run'} at {base}: OpenAI base URL {base}/v1, Anthropic "
+ f"base URL {base}" + (" (an API key is required)" if key_needed else " (any API key works)")),
+ "running": bool(target), "openai_base_url": f"{base}/v1", "anthropic_base_url": base,
+ "anthropic_messages_endpoint": f"{base}/v1/messages", "chat_page": base + "/", "model": model,
+ "api_key": ("required: the \"api_key\" value in the Strata folder's strata-<model>.json"
+ if key_needed else "not required (send any value)"),
+ "examples": {
+ "claude_code": {"env": {"ANTHROPIC_BASE_URL": base, "ANTHROPIC_AUTH_TOKEN": key,
+ "ANTHROPIC_MODEL": "claude-sonnet-4-5"},
+ "note": "Claude Code needs a Claude model name it knows; Strata ignores the name"},
+ "codex_cli": {"file": "~/.codex/config.toml",
+ "toml": f'model = "{model}"\nmodel_provider = "strata"\n\n[model_providers.strata]\n'
+ f'name = "Strata"\nbase_url = "{base}/v1"\nwire_api = "chat"'},
+ "openai_python": (f'from openai import OpenAI\nclient = OpenAI(base_url="{base}/v1", api_key="{key}")\n'
+ f'r = client.chat.completions.create(model="{model}", messages=[{{"role": "user", '
+ f'"content": "Hello!"}}])\nprint(r.choices[0].message.content)'),
+ "curl": (f"curl {base}/v1/chat/completions -H \"Content-Type: application/json\" "
+ + (f"-H \"Authorization: Bearer {key}\" " if key_needed else "")
+ + f"-d '{{\"model\": \"{model}\", \"messages\": [{{\"role\": \"user\", \"content\": "
+ "\"Hello!\"}]}'"),
+ "openai_compatible_apps": {"base_url": f"{base}/v1", "api_key": key, "model": model,
+ "apps": "Cursor (Settings > Models > OpenAI base URL override), "
+ "Continue, Open WebUI, LM Studio clients, aider "
+ f"(--openai-api-base {base}/v1)"}},
+ "other_devices": "the server listens on this PC only; to reach it from your network, run setup with "
+ "--host 0.0.0.0 --api-key <secret> (docs/DETAILS.md, 'Using it')"}
+
+
+# ------------------------------------------------------------------------------------------------ MCP over stdio
+INSTRUCTIONS = ("Strata runs a large AI model (Qwen3.8-Flash-Next, a mixture of experts) locally on this PC's GPU and "
+ "RAM, with an OpenAI- and Anthropic-compatible server on http://127.0.0.1:8080. Call strata_status "
+ "first. To install: strata_install (it returns a plan; get the user's OK for the 58-111 GB download, "
+ "then call it with confirm=true; it runs in the background - call it again for progress). Then "
+ "strata_start, strata_stop, strata_logs for problems, strata_connect_info to point other apps at it.")
+
+
+class McpServer:
+ def __init__(self, strata: Strata, out=None):
+ self.strata = strata
+ self.tools = Tools(strata)
+ self.out = out
+ self.write_lock = threading.Lock()
+ self.protocol = PROTOCOL
+
+ def send(self, msg: dict) -> None:
+ data = (json.dumps(msg, ensure_ascii=False) + "\n").encode("utf-8")
+ with self.write_lock:
+ self.out.write(data)
+ self.out.flush()
+
+ @staticmethod
+ def result(rid, result):
+ return {"jsonrpc": "2.0", "id": rid, "result": result}
+
+ @staticmethod
+ def error(rid, code, message):
+ return {"jsonrpc": "2.0", "id": rid, "error": {"code": code, "message": message}}
+
+ def handle(self, msg) -> dict | None:
+ """One JSON-RPC message -> its response (None for a notification or a response)."""
+ if not isinstance(msg, dict) or msg.get("jsonrpc") != "2.0":
+ return self.error(msg.get("id") if isinstance(msg, dict) else None, -32600, "invalid request")
+ if "method" not in msg:
+ return None # a response to something we never ask: ignored
+ method, rid, params = msg["method"], msg.get("id"), msg.get("params") or {}
+ if "id" not in msg:
+ return None # notifications (initialized, cancelled ...): nothing to say
+ if not isinstance(params, dict):
+ return self.error(rid, -32602, "params must be an object")
+ if method == "initialize":
+ asked = str(params.get("protocolVersion") or PROTOCOL)
+ self.protocol = asked if asked in PROTOCOLS else PROTOCOL
+ return self.result(rid, {"protocolVersion": self.protocol,
+ "capabilities": {"tools": {"listChanged": False}},
+ "serverInfo": {"name": "strata", "title": "Strata", "version": VERSION},
+ "instructions": INSTRUCTIONS})
+ if method == "ping":
+ return self.result(rid, {})
+ if method == "tools/list":
+ return self.result(rid, {"tools": self.tools.defs()})
+ if method == "tools/call":
+ name = params.get("name")
+ try:
+ res = self.tools.call(name, params.get("arguments"))
+ return self.result(rid, self.content(res, error=False))
+ except KeyError:
+ return self.error(rid, -32602, f"unknown tool: {name}")
+ except ToolError as e:
+ return self.result(rid, self.content({"error": str(e)}, error=True))
+ except Exception as e: # noqa: BLE001 - a bug must not end the server
+ log("tool failed:\n" + traceback.format_exc())
+ return self.result(rid, self.content({"error": f"internal error in {name}: {e}"}, error=True))
+ if method in ("resources/list", "prompts/list"):
+ return self.result(rid, {method.split("/")[0]: []})
+ return self.error(rid, -32601, f"method not found: {method}")
+
+ def content(self, res: dict, error: bool) -> dict:
+ text = json.dumps(res, indent=1, ensure_ascii=False)
+ out = {"content": [{"type": "text", "text": text}], "isError": error}
+ if self.protocol >= "2025-06-18":
+ out["structuredContent"] = res
+ return out
+
+ def serve(self, inp) -> None:
+ """Read JSON-RPC lines until stdin closes; tool calls run in their own threads (a start waits for the model
+ while pings and status calls are still answered)."""
+ threads = []
+ for raw in inp:
+ line = raw.decode("utf-8", "replace").strip() if isinstance(raw, bytes) else raw.strip()
+ if not line:
+ continue
+ try:
+ msg = json.loads(line)
+ except ValueError:
+ self.send(self.error(None, -32700, "parse error"))
+ continue
+ batch = isinstance(msg, list)
+ if batch or (isinstance(msg, dict) and msg.get("method") == "tools/call"):
+ t = threading.Thread(target=self._run, args=(msg,), daemon=True)
+ t.start()
+ threads.append(t)
+ threads = [x for x in threads if x.is_alive()]
+ else:
+ self._run(msg)
+ for t in threads:
+ t.join(timeout=5)
+
+ def _run(self, msg) -> None:
+ if isinstance(msg, list):
+ out = [r for r in (self.handle(m) for m in msg) if r is not None]
+ if out:
+ data = (json.dumps(out, ensure_ascii=False) + "\n").encode("utf-8")
+ with self.write_lock:
+ self.out.write(data)
+ self.out.flush()
+ return
+ r = self.handle(msg)
+ if r is not None:
+ self.send(r)
+
+
+# ------------------------------------------------------------------------------------------------ the install job
+def install_job(spec_path: str) -> int:
+ """The background half of strata_install (this file started again with --install-job): make .venv like
+ START-HERE.bat / setup.sh do when it is missing, run setup, record its exit code."""
+ spec = json.loads(Path(spec_path).read_text(encoding="utf-8"))
+ root = Path(spec["root"])
+ result = Path(spec["result"])
+
+ def record(**kw):
+ try:
+ old = json.loads(result.read_text(encoding="utf-8")) if result.exists() else {}
+ except (OSError, ValueError):
+ old = {}
+ tmp = result.with_suffix(".tmp")
+ tmp.write_text(json.dumps({**old, "job": spec["job"], **kw}, indent=1), encoding="utf-8")
+ os.replace(tmp, result)
+
+ py = Path(spec["python"])
+ try:
+ if spec.get("create_venv"):
+ venv = root / ".venv"
+ if py.exists() and subprocess.run([str(py), "-m", "pip", "--version"], capture_output=True).returncode:
+ print("strata-mcp: .venv has no pip (an earlier run stopped half-way): making it again", flush=True)
+ shutil.rmtree(venv, ignore_errors=True)
+ if not py.exists():
+ print(f"strata-mcp: creating Strata's Python environment in {venv} ...", flush=True)
+ r = subprocess.run([spec["base_python"], "-m", "venv", str(venv)])
+ if r.returncode or not py.exists():
+ print("\n [X] could not create the Python environment (.venv)", flush=True)
+ print(" Linux: install python3-venv (sudo apt install python3-venv) or run ./setup.sh "
+ "once in a terminal", flush=True)
+ record(exit_code=1, ended=time.strftime("%Y-%m-%d %H:%M:%S"))
+ return 1
+ cmd = [str(py), str(root / "setup.py"), *spec["args"]]
+ print("strata-mcp: running " + " ".join(cmd), flush=True)
+ env = dict(os.environ, PYTHONUNBUFFERED="1", PYTHONIOENCODING="utf-8")
+ p = subprocess.Popen(cmd, cwd=str(root), stdin=subprocess.DEVNULL, env=env)
+ record(child_pid=p.pid, child_ident=proc_identity(p.pid))
+ rc = p.wait()
+ except Exception as e: # noqa: BLE001
+ print(f"\n [X] strata-mcp could not run setup: {e}", flush=True)
+ rc = 1
+ print(f"strata-mcp: setup ended with exit code {rc}", flush=True)
+ record(exit_code=rc, ended=time.strftime("%Y-%m-%d %H:%M:%S"))
+ return rc
+
+
+def main(argv=None) -> int:
+ ap = argparse.ArgumentParser(description="Strata's MCP server (stdio): install, start, check and stop Strata "
+ "from an AI assistant. See docs/MCP_SERVER.md.")
+ ap.add_argument("--root", help="the Strata folder (default: the one this file is in)")
+ ap.add_argument("--install-job", help=argparse.SUPPRESS)
+ a = ap.parse_args(argv)
+ if a.install_job:
+ return install_job(a.install_job)
+ if sys.version_info < (3, 10):
+ log("Python 3.10 or newer is needed")
+ return 2
+ root = Path(a.root).expanduser().resolve() if a.root else DEFAULT_ROOT
+ if not (root / "setup.py").is_file() or not (root / "serve" / "server.py").is_file():
+ log(f"{root} is not a Strata folder (no setup.py / serve/server.py)")
+ return 2
+ out = sys.stdout.buffer
+ sys.stdout = sys.stderr # stray prints must never corrupt the protocol stream
+ server = McpServer(Strata(root), out)
+ log(f"ready (Strata folder {root})")
+ try:
+ server.serve(sys.stdin.buffer)
+ except KeyboardInterrupt:
+ pass
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tools/test_strata_mcp.py b/tools/test_strata_mcp.py
new file mode 100644
index 000000000..051380501
--- /dev/null
+++ b/tools/test_strata_mcp.py
@@ -0,0 +1,570 @@
+"""tools/test_strata_mcp.py - tests for tools/strata_mcp.py (Strata's MCP server), standard library only.
+
+ python -m unittest tools/test_strata_mcp.py -v
+
+Setup and the model server are fakes (a temporary Strata folder with a stand-in setup.py and serve/server.py), so
+nothing is downloaded and no GPU is used: the JSON-RPC handshake, argument checks, install / start / stop with real
+background processes, status against a fake HTTP server, and that the built-in model table still matches setup.py.
+"""
+from __future__ import annotations
+
+import io
+import json
+import os
+import socket
+import subprocess
+import sys
+import tempfile
+import threading
+import time
+import unittest
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from pathlib import Path
+
+HERE = Path(__file__).resolve().parent
+sys.path.insert(0, str(HERE))
+import strata_mcp as M # noqa: E402
+
+FAKE_SETUP = r'''
+import json, sys, time
+from pathlib import Path
+def main():
+ root = Path(__file__).resolve().parent
+ (root / "setup-args.json").write_text(json.dumps(sys.argv[1:]))
+ print("Strata - fake setup", flush=True)
+ print("\n=== Step 1: checking your PC ===", flush=True)
+ print(" [ok] GPU: fake", flush=True)
+ print("\n=== Step 5: downloading Qwen3.8-Flash-Next Q2_0 ===", flush=True)
+ for pct in (10, 20, 30):
+ print(f"\r model.gguf: {pct * 0.5:.2f} / 50.00 GB ({pct}%) ", end="", flush=True)
+ print(flush=True)
+ if (root / "slow").exists():
+ end = time.time() + 30
+ while not (root / "go").exists() and time.time() < end:
+ time.sleep(0.1)
+ if (root / "fail").exists():
+ print("\n [X] not enough free disk space", flush=True)
+ print(" use --models-dir on a bigger drive", flush=True)
+ sys.exit(1)
+ a = sys.argv
+ model = a[a.index("--model") + 1].lower()
+ cfg = {"exe": sys.executable, "args": ["--max-context", a[a.index("--context") + 1]], "port": 8080,
+ "model_name": "qwen3.8-flash-next-" + model, "log": str(root / f"strata-{model}.log")}
+ (root / f"strata-{model}.json").write_text(json.dumps(cfg))
+ print("All set.", flush=True)
+
+
+if __name__ == "__main__":
+ main()
+'''
+
+FAKE_SERVER = r'''
+import json, os, signal, sys, time
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+from pathlib import Path
+a = sys.argv
+port = int(a[a.index("--port") + 1])
+cfg = json.loads(Path(a[a.index("--config") + 1]).read_text())
+root = Path(__file__).resolve().parent.parent
+print("loading the model (fake) ...", flush=True)
+if cfg.get("crash"):
+ print("engine failed: out of memory (fake)", flush=True)
+ sys.exit(3)
+time.sleep(cfg.get("delay", 0))
+state = {"loaded": True}
+class H(BaseHTTPRequestHandler):
+ def log_message(self, *x):
+ pass
+ def js(self, code, obj):
+ b = json.dumps(obj).encode()
+ self.send_response(code)
+ self.send_header("Content-Type", "application/json")
+ self.send_header("Content-Length", str(len(b)))
+ self.end_headers()
+ self.wfile.write(b)
+ def do_GET(self):
+ p = self.path.split("?")[0]
+ if p == "/health":
+ self.js(200, {"status": "ok", "service": "strata", "model": cfg["model_name"], "max_context": 32768,
+ "images": False, "api_key": False, "loaded": state["loaded"]})
+ elif p == "/status":
+ self.js(200, {"busy": bool(cfg.get("busy")), "queued": 0})
+ elif p == "/v1/status":
+ self.js(200, {"service": "strata", "engine": "0.1.32", "uptime_s": 5, "activity": {"requests": 0},
+ "machine": {"gpu": {"name": "Fake GPU", "used_mib": 11000, "total_mib": 12227},
+ "ram": {"used_gib": 40.0, "total_gib": 63.1}}})
+ else:
+ self.js(404, {"error": {"message": "not found"}})
+ def do_POST(self):
+ n = int(self.headers.get("Content-Length") or 0)
+ body = json.loads(self.rfile.read(n) or b"{}")
+ p = self.path.split("?")[0]
+ if p == "/unload":
+ state["loaded"] = False
+ with open(root / "unloaded.txt", "a") as f:
+ f.write(str(port) + "\n")
+ self.js(200, {"status": "unloaded"})
+ elif p == "/v1/chat/completions":
+ with open(root / "bench-request.json", "w") as f:
+ json.dump(body, f)
+ self.js(200, {"model": cfg["model_name"], "choices": [{"message": {"content": "x"}, "finish_reason": "length"}],
+ "usage": {"prompt_tokens": 30, "completion_tokens": body.get("max_tokens", 1)},
+ "timings": {"predicted_per_second": 61.5, "prompt_per_second": 400.0}})
+ else:
+ self.js(404, {})
+httpd = ThreadingHTTPServer(("127.0.0.1", port), H)
+def term(*_):
+ print("[strata] stopped (fake)", flush=True)
+ os._exit(0)
+signal.signal(signal.SIGTERM, term)
+print(f"ready: http://127.0.0.1:{port}/v1", flush=True)
+httpd.serve_forever()
+'''
+
+
+def free_port() -> int:
+ s = socket.socket()
+ s.bind(("127.0.0.1", 0))
+ p = s.getsockname()[1]
+ s.close()
+ return p
+
+
+def wait_for(cond, seconds=30.0):
+ end = time.time() + seconds
+ while time.time() < end:
+ if cond():
+ return True
+ time.sleep(0.1)
+ return cond()
+
+
+FAKE_HW = {"os": "TestOS 1", "ram_gb": 64.0, "cpu": {"name": "Fake CPU", "cores": 12, "avx2": True, "avx512": True},
+ "gpus": [{"index": 0, "name": "NVIDIA GeForce RTX 5070", "vram_gb": 11.9, "arch": "120", "vendor": "nvidia",
+ "problem": None, "usable": True}]}
+
+
+class FakeRoot(unittest.TestCase):
+ """A temporary Strata folder: fake setup.py and serve/server.py, its own settings file (data folder)."""
+
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ base = Path(self.tmp.name)
+ self.root = base / "Strata"
+ (self.root / "serve").mkdir(parents=True)
+ (self.root / "setup.py").write_text(FAKE_SETUP)
+ (self.root / "serve" / "server.py").write_text(FAKE_SERVER)
+ self.env_backup = {k: os.environ.get(k) for k in ("APPDATA", "XDG_CONFIG_HOME")}
+ os.environ["APPDATA"] = os.environ["XDG_CONFIG_HOME"] = str(base / "config")
+ self.strata = M.Strata(self.root)
+ self.strata.python_override = sys.executable
+ self.strata.health_timeout = 1.0
+ self.strata._hardware = lambda: dict(FAKE_HW)
+ self.strata.disk_free = lambda p: 1000.0
+ self.tools = M.Tools(self.strata)
+ self.extra = []
+
+ def tearDown(self):
+ for p in self.extra:
+ if p.poll() is None:
+ p.kill()
+ p.wait(10)
+ srv = self.strata.state("server")
+ if srv and M.proc_alive(srv.get("pid"), srv.get("ident")):
+ M.proc_kill_tree(srv["pid"], srv["ident"])
+ M.wait_gone(srv["pid"], srv["ident"], 10)
+ job = self.strata.state("install")
+ if job and M.proc_alive(job.get("pid"), job.get("ident")):
+ M.proc_kill_tree(job["pid"], job["ident"])
+ M.wait_gone(job["pid"], job["ident"], 10)
+ for p in self.strata.children:
+ try:
+ p.wait(5)
+ except subprocess.TimeoutExpired:
+ pass
+ for k, v in self.env_backup.items():
+ if v is None:
+ os.environ.pop(k, None)
+ else:
+ os.environ[k] = v
+ for _ in range(20): # Windows: a log may still be closing
+ try:
+ self.tmp.cleanup()
+ break
+ except OSError:
+ time.sleep(0.25)
+
+ def call(self, name, args=None):
+ """A tools/call through the JSON-RPC layer: (result dict, isError)."""
+ srv = M.McpServer(self.strata, io.BytesIO())
+ r = srv.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
+ "params": {"name": name, "arguments": args or {}}})
+ res = r["result"]
+ return json.loads(res["content"][0]["text"]), res["isError"]
+
+ def write_config(self, tag="q2_0", port=8080, **extra):
+ cfg = {"exe": sys.executable, "args": ["--max-context", "32768"], "port": port,
+ "model_name": f"qwen3.8-flash-next-{tag}", "log": str(self.root / f"strata-{tag}.log"), **extra}
+ (self.root / f"strata-{tag}.json").write_text(json.dumps(cfg))
+ (self.root / f"strata-{tag}.log").write_text("engine line 1\nengine line 2\n")
+ return cfg
+
+
+# ------------------------------------------------------------------------------------------------ the protocol
+class Protocol(FakeRoot):
+ def run_lines(self, msgs):
+ inp = io.BytesIO(("\n".join(m if isinstance(m, str) else json.dumps(m) for m in msgs) + "\n").encode())
+ out = io.BytesIO()
+ M.McpServer(self.strata, out).serve(inp)
+ return {m.get("id"): m for m in (json.loads(x) for x in out.getvalue().decode().splitlines())}
+
+ def test_handshake_list_call(self):
+ r = self.run_lines([
+ {"jsonrpc": "2.0", "id": 1, "method": "initialize",
+ "params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "t", "version": "1"}}},
+ {"jsonrpc": "2.0", "method": "notifications/initialized"},
+ {"jsonrpc": "2.0", "id": 2, "method": "tools/list"},
+ {"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "strata_models", "arguments": {}}},
+ {"jsonrpc": "2.0", "id": 4, "method": "ping"},
+ ])
+ init = r[1]["result"]
+ self.assertEqual(init["protocolVersion"], "2025-06-18")
+ self.assertIn("tools", init["capabilities"])
+ self.assertEqual(init["serverInfo"]["name"], "strata")
+ names = [t["name"] for t in r[2]["result"]["tools"]]
+ self.assertEqual(names, ["strata_status", "strata_models", "strata_install", "strata_start", "strata_stop",
+ "strata_logs", "strata_benchmark", "strata_connect_info"])
+ for t in r[2]["result"]["tools"]:
+ self.assertEqual(t["inputSchema"]["type"], "object")
+ self.assertFalse(t["inputSchema"]["additionalProperties"])
+ self.assertGreater(len(t["description"]), 60)
+ call = r[3]["result"]
+ self.assertFalse(call["isError"])
+ self.assertIn("families", call["structuredContent"])
+ self.assertEqual(json.loads(call["content"][0]["text"])["recommendation"]["model"], "IQ3_XXS")
+ self.assertEqual(r[4]["result"], {})
+ self.assertNotIn(None, r) # the notification got no answer
+
+ def test_older_protocol_and_errors(self):
+ r = self.run_lines([
+ {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05"}},
+ "{not json",
+ {"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "rm_rf", "arguments": {}}},
+ {"jsonrpc": "2.0", "id": 3, "method": "no/such"},
+ {"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "strata_logs", "arguments": []}},
+ ])
+ self.assertEqual(r[1]["result"]["protocolVersion"], "2024-11-05")
+ self.assertEqual(r[None]["error"]["code"], -32700)
+ self.assertEqual(r[2]["error"]["code"], -32602)
+ self.assertEqual(r[3]["error"]["code"], -32601)
+ self.assertTrue(r[4]["result"]["isError"])
+ self.assertNotIn("structuredContent", r[4]["result"]) # 2024-11-05 has no structured results
+
+ def test_real_stdio_process(self):
+ """The script itself over a pipe: stdout carries only JSON-RPC lines."""
+ msgs = [{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}},
+ {"jsonrpc": "2.0", "id": 2, "method": "tools/list"},
+ {"jsonrpc": "2.0", "id": 3, "method": "tools/call",
+ "params": {"name": "strata_logs", "arguments": {"source": "setup"}}}]
+ p = subprocess.run([sys.executable, str(HERE / "strata_mcp.py"), "--root", str(self.root)],
+ input="\n".join(json.dumps(m) for m in msgs) + "\n", capture_output=True, text=True,
+ timeout=60, env=dict(os.environ))
+ lines = [json.loads(x) for x in p.stdout.splitlines()]
+ self.assertEqual(sorted(m["id"] for m in lines), [1, 2, 3])
+ self.assertEqual(len(next(m for m in lines if m["id"] == 2)["result"]["tools"]), 8)
+ self.assertIn("ready", p.stderr)
+
+ def test_not_a_strata_folder(self):
+ p = subprocess.run([sys.executable, str(HERE / "strata_mcp.py"), "--root", self.tmp.name], input="",
+ capture_output=True, text=True, timeout=60)
+ self.assertEqual(p.returncode, 2)
+
+
+# ------------------------------------------------------------------------------------------------ validation
+class Validation(FakeRoot):
+ def assertRejected(self, tool, args, text):
+ res, err = self.call(tool, args)
+ self.assertTrue(err, f"{tool} {args} was accepted: {res}")
+ self.assertIn(text, res["error"])
+
+ def test_install_arguments(self):
+ self.assertRejected("strata_install", {"model": "Q9_X"}, "model must be one of")
+ self.assertRejected("strata_install", {"family": "llama"}, "family must be one of")
+ self.assertRejected("strata_install", {"context": 12345}, "context must be one of")
+ self.assertRejected("strata_install", {"gpus": "0;rm -rf /"}, "gpus=")
+ self.assertRejected("strata_install", {"gpus": "0 && calc"}, "gpus=")
+ self.assertRejected("strata_install", {"gpu": -1}, "between")
+ self.assertRejected("strata_install", {"gpu": "1; calc"}, "whole number")
+ self.assertRejected("strata_install", {"gpu": 0, "gpus": "0,1"}, "not both")
+ self.assertRejected("strata_install", {"port": 80}, "between")
+ self.assertRejected("strata_install", {"backend": "rocm"}, "backend must be one of")
+ self.assertRejected("strata_install", {"command": "setup.py --build"}, "unknown argument")
+ self.assertRejected("strata_install", {"family": "swift", "model": "IQ3_S"}, "has no IQ3_S")
+ self.assertRejected("strata_install", {"family": "unsloth", "vision": "yes"}, "images are not available")
+ self.assertRejected("strata_install", {"vision": "yes\nrm"}, "control characters")
+
+ def test_data_dir_paths(self):
+ self.assertRejected("strata_install", {"data_dir": "models"}, "absolute")
+ self.assertRejected("strata_install", {"data_dir": str(self.root.parent / "x" / ".." / "Strata-x")}, "'..'")
+ self.assertRejected("strata_install", {"data_dir": str(self.root.parent / "Documents")}, "named Strata")
+ self.assertRejected("strata_install", {"data_dir": str(self.root.parent / "nope" / "Strata-data")},
+ "does not exist")
+ self.assertRejected("strata_install", {"data_dir": "\\\\server\\share\\Strata-data"}, "network")
+ sysdir = (os.environ.get("SystemRoot", "C:\\Windows") + "\\Strata-data") if M.WIN else "/etc/Strata-data"
+ self.assertRejected("strata_install", {"data_dir": sysdir}, "system folder")
+ ok = [self.root.parent / "Strata-data", self.root / "models-here", self.root.parent / "Strata-big"]
+ for d in ok:
+ self.assertEqual(M.norm(self.tools.check_data_dir(str(d))), M.norm(d))
+
+ def test_other_tools_arguments(self):
+ self.assertRejected("strata_start", {"model": "../../etc/passwd"}, "not valid")
+ self.assertRejected("strata_start", {"model": "q2_0"}, "no model is installed")
+ self.write_config("q2_0")
+ self.assertRejected("strata_start", {"model": "iq3_s"}, "no installed model 'iq3_s'")
+ self.assertRejected("strata_start", {"wait_seconds": 100000}, "between")
+ self.assertRejected("strata_logs", {"lines": 0}, "between")
+ self.assertRejected("strata_logs", {"source": "/etc/passwd"}, "source must be one of")
+ self.assertRejected("strata_benchmark", {"max_tokens": 100000}, "between")
+ self.assertRejected("strata_stop", {"force": "yes"}, "true or false")
+
+ def test_engine_log_outside_is_not_read(self):
+ self.write_config("q2_0", log=str(Path(self.tmp.name).parent / "elsewhere.log"))
+ res, err = self.call("strata_logs", {"source": "engine", "model": "q2_0"})
+ self.assertTrue(err)
+ self.assertIn("outside the Strata folder", res["error"])
+ self.write_config("q2_0")
+ res, err = self.call("strata_logs", {"source": "engine"})
+ self.assertFalse(err)
+ self.assertEqual(res["lines"], ["engine line 1", "engine line 2"])
+
+
+# ------------------------------------------------------------------------------------------------ install
+class Install(FakeRoot):
+ def wait_job(self):
+ self.assertTrue(wait_for(lambda: not self.strata.install_job()["running"], 60))
+
+ def test_plan_then_install(self):
+ res, err = self.call("strata_install", {})
+ self.assertFalse(err)
+ self.assertIn("Nothing was done yet", res["summary"])
+ self.assertEqual((res["plan"]["family"], res["plan"]["model"], res["plan"]["context"]), ("qwen", "IQ3_XXS", 32768))
+ self.assertFalse((self.root / "setup-args.json").exists())
+ self.assertIsNone(self.strata.install_job())
+
+ res, err = self.call("strata_install", {"model": "Q2_0", "context": 65536, "vision": "no", "confirm": True})
+ self.assertFalse(err, res)
+ self.assertIn("started in the background", res["summary"])
+ self.wait_job()
+ args = json.loads((self.root / "setup-args.json").read_text())
+ self.assertEqual(args[:2], ["--yes", "--no-start"])
+ for pair in (["--family", "qwen"], ["--model", "Q2_0"], ["--context", "65536"], ["--vision", "no"]):
+ i = args.index(pair[0])
+ self.assertEqual(args[i:i + 2], pair)
+ res, err = self.call("strata_install", {})
+ self.assertEqual(res["install"]["result"], "installed")
+ self.assertEqual(res["install"]["exit_code"], 0)
+ self.assertEqual(res["next"], "strata_start")
+ st, _ = self.call("strata_status", {"hardware": False})
+ self.assertEqual([m["model"] for m in st["installed"]["models"]], ["q2_0"])
+
+ def test_progress_while_running_and_cancel(self):
+ (self.root / "slow").write_text("")
+ res, err = self.call("strata_install", {"family": "coder", "confirm": True})
+ self.assertFalse(err, res)
+ self.assertTrue(wait_for(lambda: (self.strata.install_job() or {}).get("download") is not None, 30))
+ res, err = self.call("strata_install", {"model": "IQ3_S", "confirm": True}) # a second call: progress only
+ self.assertFalse(err)
+ self.assertIn("an install is running", res["summary"])
+ self.assertEqual(res["install"]["step"], "5/7: downloading Qwen3.8-Flash-Next Q2_0")
+ self.assertEqual(res["install"]["download"]["percent"], 30)
+ res, err = self.call("strata_start", {})
+ self.assertTrue(err)
+ self.assertIn("install is still running", res["error"])
+ res, err = self.call("strata_install", {"cancel": True})
+ self.assertIn("stopped", res["summary"])
+ self.assertFalse(self.strata.install_job()["running"])
+
+ def test_failure_is_reported(self):
+ (self.root / "fail").write_text("")
+ self.call("strata_install", {"model": "Q2_0", "confirm": True})
+ self.wait_job()
+ res, _ = self.call("strata_install", {})
+ self.assertEqual(res["install"]["result"], "failed")
+ self.assertIn("not enough free disk space - use --models-dir on a bigger drive", res["install"]["error"])
+
+ def test_disk_space_is_checked(self):
+ self.strata.disk_free = lambda p: 10.0
+ res, err = self.call("strata_install", {"model": "Q2_0"})
+ self.assertIn("not enough free disk space", res["plan"]["disk_short"])
+ res, err = self.call("strata_install", {"model": "Q2_0", "confirm": True})
+ self.assertTrue(err)
+ self.assertIsNone(self.strata.install_job())
+
+
+# ------------------------------------------------------------------------------------------------ start / stop / status
+class StartStop(FakeRoot):
+ def test_start_status_bench_stop(self):
+ port = free_port()
+ self.write_config("q2_0", port=port, delay=1.5)
+ res, err = self.call("strata_start", {"wait_seconds": 30})
+ self.assertFalse(err, res)
+ self.assertIn("Strata is running", res["summary"])
+ pid = res["server"]["pid"]
+
+ st, _ = self.call("strata_status", {"hardware": False})
+ self.assertIn(f"running on port {port}", st["summary"])
+ srv = st["running"][0]
+ self.assertEqual(srv["engine_version"], "0.1.32")
+ self.assertEqual(srv["gpu"]["total_mib"], 12227)
+ self.assertIn("this MCP server", srv["started_by"])
+
+ res, err = self.call("strata_start", {}) # already running: nothing new
+ self.assertIn("already running", res["summary"])
+
+ b, err = self.call("strata_benchmark", {"max_tokens": 64})
+ self.assertFalse(err, b)
+ self.assertEqual(b["output_tok_s"], 61.5)
+ sent = json.loads((self.root / "bench-request.json").read_text())
+ self.assertEqual((sent["temperature"], sent["max_tokens"], sent["reasoning_effort"]), (0, 64, "none"))
+
+ c, _ = self.call("strata_connect_info", {})
+ self.assertEqual(c["openai_base_url"], f"http://127.0.0.1:{port}/v1")
+
+ logs, _ = self.call("strata_logs", {"source": "server"})
+ self.assertTrue(any(line.startswith("ready:") for line in logs["lines"]))
+
+ res, err = self.call("strata_stop", {})
+ self.assertFalse(err, res)
+ self.assertIn("Strata stopped", res["summary"])
+ self.assertEqual(res["engine_unloaded"], "unloaded")
+ self.assertEqual((self.root / "unloaded.txt").read_text().split(), [str(port)])
+ ident = M.proc_identity(pid)
+ self.assertIsNone(ident)
+ st, _ = self.call("strata_status", {"hardware": False})
+ self.assertEqual(st["running"], [])
+ res, _ = self.call("strata_stop", {})
+ self.assertEqual(res["summary"], "Strata is not running")
+
+ def test_start_reports_a_crash(self):
+ self.write_config("q2_0", port=free_port(), crash=True)
+ res, err = self.call("strata_start", {"wait_seconds": 30})
+ self.assertTrue(err)
+ self.assertIn("out of memory (fake)", res["error"])
+ self.assertEqual(self.strata.state("server"), {})
+
+ def test_loading_then_ready(self):
+ port = free_port()
+ self.write_config("q2_0", port=port, delay=3)
+ res, err = self.call("strata_start", {"wait_seconds": 0})
+ self.assertIn("loading", res["summary"])
+ st, _ = self.call("strata_status", {"hardware": False})
+ self.assertIn("loading", st["summary"])
+ self.assertTrue(wait_for(lambda: self.strata.probe(port) is not None, 30))
+
+ def test_stop_while_busy_needs_force(self):
+ port = free_port()
+ self.write_config("q2_0", port=port, busy=True)
+ self.call("strata_start", {"wait_seconds": 30})
+ res, err = self.call("strata_stop", {})
+ self.assertTrue(err)
+ self.assertIn("force=true", res["error"])
+ res, err = self.call("strata_stop", {"force": True})
+ self.assertFalse(err, res)
+
+ def test_external_server_is_only_unloaded(self):
+ port = free_port()
+ self.write_config("q2_0", port=port)
+ p = subprocess.Popen([sys.executable, str(self.root / "serve" / "server.py"), "--config",
+ str(self.root / "strata-q2_0.json"), "--port", str(port)],
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ self.extra.append(p)
+ self.assertTrue(wait_for(lambda: self.strata.probe(port) is not None, 30))
+ st, _ = self.call("strata_status", {"hardware": False})
+ self.assertIn("outside this MCP server", st["running"][0]["started_by"])
+ res, err = self.call("strata_start", {})
+ self.assertIn("started outside this MCP server", res["summary"])
+ res, err = self.call("strata_stop", {})
+ self.assertFalse(err, res)
+ self.assertIn("only its model was unloaded", res["summary"])
+ time.sleep(0.5)
+ self.assertIsNone(p.poll()) # the server itself was not touched
+
+ def test_never_kills_an_unrelated_process(self):
+ other = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"])
+ self.extra.append(other)
+ # a stale record: this process id, but another start time (a reused id)
+ self.strata.save_state("server", {"pid": other.pid, "ident": "win:1" if M.WIN else "linux:1",
+ "port": free_port(), "model": "q2_0"})
+ res, _ = self.call("strata_stop", {})
+ self.assertEqual(res["summary"], "Strata is not running")
+ self.assertFalse(M.proc_terminate(other.pid, "linux:1"))
+ time.sleep(0.3)
+ self.assertIsNone(other.poll())
+
+ def test_status_sees_other_programs_on_the_port(self):
+ class H(BaseHTTPRequestHandler):
+ def do_GET(self):
+ self.send_response(404)
+ self.end_headers()
+
+ def log_message(self, *a):
+ pass
+ httpd = HTTPServer(("127.0.0.1", 0), H)
+ port = httpd.server_address[1]
+ threading.Thread(target=httpd.serve_forever, daemon=True).start()
+ try:
+ self.write_config("q2_0", port=port)
+ st, _ = self.call("strata_status", {"hardware": False})
+ self.assertEqual(st["running"], [])
+ self.assertEqual(st["ports_used_by_other_programs"][0]["port"], port)
+ res, err = self.call("strata_start", {})
+ self.assertTrue(err)
+ self.assertIn("used by another program", res["error"])
+ finally:
+ httpd.shutdown()
+ httpd.server_close()
+
+
+# ------------------------------------------------------------------------------------------------ helpers
+class Helpers(unittest.TestCase):
+ def test_progress_from_setup_output(self):
+ with tempfile.TemporaryDirectory() as d:
+ f = Path(d) / "setup.log"
+ f.write_text("=== Step 4: the Strata engine ===\n [ok] engine\n=== Step 5: downloading X ===\n"
+ "\r a.gguf: 1.00 / 50.00 GB (2%) \r a.gguf: 25.00 / 50.00 GB (50%) \n")
+ p = M.install_progress(M.read_tail(f, 50))
+ self.assertEqual(p["step"], "5/7: downloading X")
+ self.assertEqual(p["download"], {"file": "a.gguf", "done_gb": 25.0, "total_gb": 50.0, "percent": 50})
+ self.assertNotIn("error", p)
+
+ def test_builtin_table_matches_setup(self):
+ s = M.Strata(HERE.parent)
+ S = s.setup_module()
+ self.assertIsNotNone(S, "the repository's setup.py did not import")
+ self.assertEqual(list(S.MODELS), list(M.FALLBACK_MODELS))
+ for m, d in S.MODELS.items():
+ for k in ("download_gb", "ram_gb", "arena_gb"):
+ self.assertEqual(d[k], M.FALLBACK_MODELS[m][k], f"{m} {k}")
+ self.assertEqual(d.get("families", ("qwen", "swift")), M.FALLBACK_MODELS[m].get("families", ("qwen", "swift")))
+ self.assertEqual(list(S.FAMILIES), list(M.FALLBACK_FAMILIES))
+ for f, d in S.FAMILIES.items():
+ self.assertEqual(d["tag"], M.FALLBACK_FAMILIES[f]["tag"])
+ self.assertEqual(list(S.CONTEXTS), M.FALLBACK_CONTEXTS)
+
+ def test_recommendation_follows_ram(self):
+ s = M.Strata(HERE.parent)
+ gpu = [{"index": 0, "name": "x", "vram_gb": 24.0, "usable": True, "vendor": "nvidia"}]
+ cases = {96: ("qwen", "IQ3_XXS", 131072), 48: ("qwen", "Q2_0", 131072), 32: ("coder", "IQ1_M", 131072)}
+ for ram, want in cases.items():
+ r = s.recommend({"ram_gb": ram, "gpus": gpu, "cpu": {"avx2": True}})
+ self.assertEqual((r["family"], r["model"], r["context"]), want, ram)
+ r = s.recommend({"ram_gb": 16, "gpus": [{**gpu[0], "vram_gb": 8.0}], "cpu": {"avx2": True}})
+ self.assertIsNone(r["model"])
+ r = s.recommend({"ram_gb": 64, "gpus": [], "cpu": {"avx2": True}})
+ self.assertIsNone(r["model"])
+ r = s.recommend({"ram_gb": 64, "gpus": [{**gpu[0], "vram_gb": 12.0}], "cpu": {"avx2": True}})
+ self.assertEqual(r["context"], 32768)
+
+
+if __name__ == "__main__":
+ unittest.main()