Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Keep Codex MCP servers when running unsloth start codex (#15452)
Nilayd0349f034ba9
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.pyindex a813c01e28c..c9b7b82e695 100644--- a/unsloth_cli/commands/start.py+++ b/unsloth_cli/commands/start.py@@ -3378,17 +3378,226 @@ def _codex_model_catalog(model: dict, visibility: str = "none") -> dict:return {"models": [entry]}+_TOML_KEY_PART = re.compile(+ r"""[ \t]*(?:"((?:[^"\\]|\\.)*)"|'([^']*)'|([A-Za-z0-9_-]+))[ \t]*([.\]])"""+)+++_TOML_STRING = re.compile(r'"(?:[^"\\]|\\.)*"|\'[^\']*\'')+++def _toml_table_path(chunk: str) -> Optional[tuple]:+ """Key path of the [table] or [[array]] header opening chunk, whatever its quoting or comment."""+ line = chunk.lstrip(" \t")+ if not line.startswith("["):+ return None+ parts, pos = [], 2 if line.startswith("[[") else 1+ while True:+ m = _TOML_KEY_PART.match(line, pos)+ if m is None:+ return None+ parts.append(next(g for g in m.groups()[:3] if g is not None))+ pos = m.end()+ if m.group(4) == "]":+ return tuple(parts)+++def _toml_chunks(text: str) -> list:+ """Split before each table header; a "[" line inside a string or an array is not one."""+ chunks, current, delim, depth = [], "", None, 0+ for line in text.splitlines(True):+ if delim is None and depth == 0 and line.lstrip(" \t").startswith("["):+ chunks.append(current)+ current = line+ continue+ current += line+ i = 0+ while i < len(line):+ if delim is not None:+ if line.startswith(delim, i):+ # Up to two quotes may end the string just before its closing delimiter.+ run = len(line) - i - len(line[i:].lstrip(delim[0]))+ delim, i = None, i + min(run, 5)+ else:+ i += 2 if delim == '"""' and line[i] == "\\" else 1+ continue+ c = line[i]+ if c == "#":+ break+ if line.startswith(('"""', "'''"), i):+ delim, i = line[i : i + 3], i + 3+ continue+ if c in "\"'":+ m = _TOML_STRING.match(line, i)+ i = m.end() if m else len(line)+ continue+ depth += (c in "[{") - (c in "]}")+ i += 1+ chunks.append(current)+ return chunks+++def _toml_loads():+ try:+ import tomllib # novermin+ except ImportError:+ try:+ import tomli as tomllib+ except ImportError:+ return None+ return tomllib.loads+++def _toml_parses(text: str) -> Optional[bool]:+ loads = _toml_loads()+ if loads is None:+ return None+ try:+ loads(text)+ except ValueError:+ return False+ return True+++def _toml_key(key: str) -> str:+ return key if re.fullmatch(r"[A-Za-z0-9_-]+", key) else _toml_value(key)+++def _toml_value(value) -> str:+ if isinstance(value, bool):+ return "true" if value else "false"+ if isinstance(value, str):+ # JSON string escapes are TOML's, except that TOML also forbids a raw DEL.+ return json.dumps(value, ensure_ascii = False).replace("\x7f", "\\u007f")+ if isinstance(value, float) and value != value:+ return "nan"+ if isinstance(value, float) and value in (float("inf"), float("-inf")):+ return "inf" if value > 0 else "-inf"+ if isinstance(value, (int, float)):+ return repr(value)+ if isinstance(value, list):+ return "[" + ", ".join(_toml_value(v) for v in value) + "]"+ if isinstance(value, dict):+ return (+ "{ " + ", ".join(f"{_toml_key(k)} = {_toml_value(v)}" for k, v in value.items()) + " }"+ )+ return value.isoformat() # TOML dates and times+++_CODEX_MCP_MANIFEST = "unsloth_mcp_servers.json"+++def _codex_mcp_server(chunk: str) -> Optional[tuple]:+ # The server a [mcp_servers.NAME...] table belongs to: owning NAME owns its subtables. A bare+ # [mcp_servers] table can mix servers from two homes, so it is never owned as a whole.+ path = _toml_table_path(chunk)+ return path[:2] if path and len(path) > 1 and path[0] == "mcp_servers" else None+++def _codex_mcp_manifest(home: Path) -> tuple:+ """(home it was built from, the user's own home at the end of that chain, inherited servers)."""+ try:+ state = json.loads((home / _CODEX_MCP_MANIFEST).read_text(encoding = "utf-8"))+ inherited = {tuple(path)[:2] for path in state["tables"]}+ source, origin = state.get("source"), state.get("origin")+ except (OSError, ValueError, TypeError, KeyError, AttributeError):+ return None, None, set()+ source = source if isinstance(source, str) else None+ return source, origin if isinstance(origin, str) else source, inherited+++def _codex_mcp_followed(home: Path) -> Optional[Path]:+ # A source that was itself a temporary session home is gone once that session exits.+ source, origin, _ = _codex_mcp_manifest(home)+ for candidate in (source, origin):+ if candidate and Path(candidate).is_dir():+ return Path(candidate)+ return None+++def _codex_mcp_tables(source: Path, seen: frozenset) -> list:+ """MCP tables in source's config; a home Unsloth built trades its inherited ones for its origin's."""+ try:+ text = (source / "config.toml").read_text(encoding = "utf-8-sig")+ except OSError:+ return []+ loads = _toml_loads()+ try:+ # Parsed, so root dotted keys, inline tables and a bare [mcp_servers] all count.+ servers = loads(text).get("mcp_servers") if loads else None+ except ValueError:+ loads = (+ None # TOML 1.1, which Codex reads and tomllib does not: copy the tables as written.+ )+ if loads is None:+ tables = [c for c in _toml_chunks(text) if _codex_mcp_server(c)]+ else:+ tables = [+ f"[mcp_servers.{_toml_key(name)}]\n"+ + "".join(f"{_toml_key(k)} = {_toml_value(v)}\n" for k, v in spec.items())+ + "\n"+ for name, spec in (servers.items() if isinstance(servers, dict) else ())+ if isinstance(spec, dict)+ ]+ _, _, inherited = _codex_mcp_manifest(source)+ upstream_home = _codex_mcp_followed(source)+ if upstream_home is not None and str(upstream_home.resolve(strict = False)) not in seen:+ upstream = _codex_mcp_tables(+ upstream_home, seen | {str(upstream_home.resolve(strict = False))}+ )+ # The origin wins a name clash, as it does when that saved home is synced.+ taken = inherited | {_codex_mcp_server(c) for c in upstream}+ tables = [c for c in tables if _codex_mcp_server(c) not in taken] + upstream+ return tables+++def _with_user_codex_mcp_servers(text: str, home: Path) -> str:+ _, _, carried = _codex_mcp_manifest(home)+ source = _codex_source_home()+ home_key = home.resolve(strict = False)+ if source.resolve(strict = False) == home_key:+ # The printed recipe exports CODEX_HOME=home: keep reading the home it was built from.+ followed = _codex_mcp_followed(home)+ if followed is not None and followed.resolve(strict = False) != home_key:+ source = followed+ else:+ source = _codex_source_home(ignore_configured = True)+ source_key = str(source.resolve(strict = False))+ tables = _codex_mcp_tables(source, frozenset({str(home_key), source_key}))+ # Only replace what came from the user's config; servers added in this home stay.+ servers = {_codex_mcp_server(c) for c in tables}+ carried |= servers+ out = "".join(c for c in _toml_chunks(text) if _codex_mcp_server(c) not in carried)+ if tables:+ out = out.rstrip("\n") + "\n\n" + "\n\n".join(c.rstrip("\n") for c in tables) + "\n"+ if _toml_parses(out) is False:+ return text+ _, origin, _ = _codex_mcp_manifest(source)+ # Written even when empty: a recipe shell's CODEX_HOME=home must still find a custom origin.+ (home / _CODEX_MCP_MANIFEST).write_text(+ json.dumps(+ {"source": source_key, "origin": origin or source_key, "tables": sorted(servers)}+ )+ + "\n",+ encoding = "utf-8",+ )+ return out++def write_codex_config(base: str,model: dict,home: Path,reasoning_effort: Optional[str] = None,+ keep_mcp_servers: bool = False,) -> None:home.mkdir(parents = True, exist_ok = True)config = home / "config.toml"existing = config.read_text(encoding = "utf-8") if config.exists() else ""merged = _merge_codex_config(existing, base)+ if keep_mcp_servers:+ merged = _with_user_codex_mcp_servers(merged, home)if merged != existing:config.write_text(merged, encoding = "utf-8")typer.echo(f"Updated {config}")@@ -7469,7 +7678,7 @@ def codex(*ctx.args,]with _session_config("codex", launch, persist = persist) as home:- write_codex_config(base, entry, home, codex_effort)+ write_codex_config(base, entry, home, codex_effort, keep_mcp_servers = True)env = {_CODEX_ENV_KEY: key, "CODEX_HOME": str(home)}_run(base,diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.pyindex 4a6c9bc5467..36604cc08cf 100644--- a/unsloth_cli/tests/test_start.py+++ b/unsloth_cli/tests/test_start.py@@ -2763,6 +2763,272 @@ def test_connect_codex_no_launch(fake_studio, tmp_path):assert (home / "unsloth_api.config.toml").exists()+def test_codex_keeps_the_users_mcp_servers(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ user_config = source_home / "config.toml"+ user_config.write_text(+ 'model = "cloud-model"\n\n'+ '[mcp_servers.weather]\ncommand = "python3"\nargs = ["weather.py"]\n\n'+ '[mcp_servers.weather.env]\nUNITS = "metric"\n\n'+ '[mcp_servers.docs]\nurl = "http://127.0.0.1:9000/mcp"\n'+ )+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ home = tmp_path / "agents" / "codex"++ result = CliRunner().invoke(start.start_app, ["codex", "--no-launch"])+ assert result.exit_code == 0, result.output+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {+ "weather": {"command": "python3", "args": ["weather.py"], "env": {"UNITS": "metric"}},+ "docs": {"url": "http://127.0.0.1:9000/mcp"},+ }+ assert "model" not in config+ assert config["model_providers"]["unsloth_api"]["base_url"].endswith("/v1")++ user_config.write_text('[mcp_servers.docs]\nurl = "http://127.0.0.1:9000/mcp"\n')+ result = CliRunner().invoke(start.start_app, ["codex", "--no-launch"])+ assert result.exit_code == 0, result.output+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {"docs": {"url": "http://127.0.0.1:9000/mcp"}}+ assert user_config.read_text() == '[mcp_servers.docs]\nurl = "http://127.0.0.1:9000/mcp"\n'+++def test_codex_keeps_mcp_servers_added_in_the_saved_home(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ user_home = tmp_path / "user-home"+ (user_home / ".codex").mkdir(parents = True)+ (user_home / ".codex" / "config.toml").write_text('[mcp_servers.docs]\nurl = "http://d/mcp"\n')+ monkeypatch.setenv("HOME", str(user_home))+ monkeypatch.setenv("USERPROFILE", str(user_home))+ monkeypatch.delenv("CODEX_HOME", raising = False)+ home = tmp_path / "agents" / "codex"++ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ with (home / "config.toml").open("a") as f:+ f.write('\n[mcp_servers.local]\ncommand = "local-server"\n')+ # The shell that ran the recipe has CODEX_HOME set to the saved home.+ monkeypatch.setenv("CODEX_HOME", str(home))+ result = CliRunner().invoke(start.start_app, ["codex", "--no-launch"])+ assert result.exit_code == 0, result.output+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {+ "docs": {"url": "http://d/mcp"},+ "local": {"command": "local-server"},+ }++ (user_home / ".codex" / "config.toml").write_text("")+ monkeypatch.delenv("CODEX_HOME")+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {"local": {"command": "local-server"}}+++def test_codex_mcp_servers_follow_a_custom_codex_home_through_the_recipe(+ fake_studio, tmp_path, monkeypatch+):+ tomllib = pytest.importorskip("tomllib")+ user_home = tmp_path / "user-home"+ (user_home / ".codex").mkdir(parents = True)+ monkeypatch.setenv("HOME", str(user_home))+ monkeypatch.setenv("USERPROFILE", str(user_home))+ custom = tmp_path / "custom-codex"+ custom.mkdir()+ (custom / "config.toml").write_text('[mcp_servers.custom]\nurl = "http://c/mcp"\n')+ monkeypatch.setenv("CODEX_HOME", str(custom))+ home = tmp_path / "agents" / "codex"++ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ monkeypatch.setenv("CODEX_HOME", str(home))+ result = CliRunner().invoke(start.start_app, ["codex", "--no-launch"])+ assert result.exit_code == 0, result.output+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {"custom": {"url": "http://c/mcp"}}+++def test_codex_drops_a_removed_server_with_subtables_added_in_the_saved_home(+ fake_studio, tmp_path, monkeypatch+):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ (source_home / "config.toml").write_text('[mcp_servers.docs]\nurl = "http://d/mcp"\n')+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ home = tmp_path / "agents" / "codex"++ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ with (home / "config.toml").open("a") as f:+ f.write('\n[mcp_servers.docs.env]\nUNITS = "metric"\n')+ f.write('\n[[mcp_servers.docs.env_vars]]\nname = "TOKEN"\nsource = "local"\n')+ (source_home / "config.toml").write_text('[mcp_servers.keep]\nurl = "http://k/mcp"\n')+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {"keep": {"url": "http://k/mcp"}}+++def test_codex_session_from_a_saved_home_reads_its_origin(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ (source_home / "config.toml").write_text('[mcp_servers.docs]\nurl = "http://d/mcp"\n')+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ saved = tmp_path / "agents" / "codex"+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ with (saved / "config.toml").open("a") as f:+ f.write('\n[mcp_servers.local]\ncommand = "local-server"')++ (source_home / "config.toml").write_text('[mcp_servers.fresh]\nurl = "http://f/mcp"\n')+ monkeypatch.setenv("CODEX_HOME", str(saved))+ session = tmp_path / "session"+ start.write_codex_config("http://h", {"id": "m"}, session, keep_mcp_servers = True)+ config = tomllib.loads((session / "config.toml").read_text())+ assert config["mcp_servers"] == {+ "local": {"command": "local-server"},+ "fresh": {"url": "http://f/mcp"},+ }++ (source_home / "config.toml").write_text(+ '[mcp_servers.fresh]\nurl = "http://f/mcp"\n\n[mcp_servers.local]\nurl = "http://l/mcp"\n'+ )+ start.write_codex_config("http://h", {"id": "m"}, session, keep_mcp_servers = True)+ config = tomllib.loads((session / "config.toml").read_text())+ assert config["mcp_servers"] == {+ "local": {"url": "http://l/mcp"},+ "fresh": {"url": "http://f/mcp"},+ }+++def test_codex_carries_mcp_servers_written_as_root_keys(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ (source_home / "config.toml").write_text(+ 'mcp_servers.docs.command = "python3"\n'+ 'mcp_servers.docs.env = { "API KEY" = "x" }\n'+ 'mcp_servers.notes = { url = "http://n/mcp", tools = { search = { enabled = false } } }\n'+ )+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ home = tmp_path / "agents" / "codex"++ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ config = tomllib.loads((home / "config.toml").read_text())+ assert (+ config["mcp_servers"]+ == tomllib.loads((source_home / "config.toml").read_text())["mcp_servers"]+ )+++def test_codex_reads_mcp_servers_from_a_config_with_a_bom(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ (source_home / "config.toml").write_bytes(+ b'\xef\xbb\xbf[mcp_servers.docs]\nurl = "http://d/mcp"\n'+ )+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ home = tmp_path / "agents" / "codex"++ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {"docs": {"url": "http://d/mcp"}}+++def test_toml_chunks_split_only_at_real_table_headers():+ text = (+ 'model = "m"\n'+ '[mcp_servers.notes]\nargs = ["-c", """echo "hi""""]\nm = [\n[1],\n]\ns = """\n[fake]\n"""\n'+ "[model_providers.unsloth_api]\nname = 'x'\n"+ )+ chunks = start._toml_chunks(text)+ assert "".join(chunks) == text+ assert [start._toml_table_path(c) for c in chunks] == [+ None,+ ("mcp_servers", "notes"),+ ("model_providers", "unsloth_api"),+ ]+++def test_codex_keeps_saved_servers_when_the_source_is_newer_toml(+ fake_studio, tmp_path, monkeypatch+):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ (source_home / "config.toml").write_text('[mcp_servers.docs]\nurl = "http://d/mcp"\n')+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ home = tmp_path / "agents" / "codex"+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0++ # A multi-line inline table is TOML 1.1: Codex reads it, tomllib does not.+ (source_home / "config.toml").write_text(+ '[mcp_servers.docs]\nurl = "http://d/mcp"\nenv = {\n UNITS = "metric",\n}\n'+ )+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {"docs": {"url": "http://d/mcp"}}+++def test_codex_saved_home_outlives_the_session_it_was_made_in(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ (source_home / "config.toml").write_text('[mcp_servers.docs]\nurl = "http://d/mcp"\n')+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ session = tmp_path / "session"+ start.write_codex_config("http://h", {"id": "m"}, session, keep_mcp_servers = True)++ monkeypatch.setenv("CODEX_HOME", str(session))+ saved = tmp_path / "agents" / "codex"+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ shutil.rmtree(session)+ monkeypatch.setenv("CODEX_HOME", str(saved))+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ config = tomllib.loads((saved / "config.toml").read_text())+ assert config["mcp_servers"] == {"docs": {"url": "http://d/mcp"}}+++def test_codex_remembers_an_empty_custom_codex_home(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ user_home = tmp_path / "user-home"+ (user_home / ".codex").mkdir(parents = True)+ (user_home / ".codex" / "config.toml").write_text('[mcp_servers.unrelated]\nurl = "http://u"\n')+ monkeypatch.setenv("HOME", str(user_home))+ monkeypatch.setenv("USERPROFILE", str(user_home))+ custom = tmp_path / "custom-codex"+ custom.mkdir()+ monkeypatch.setenv("CODEX_HOME", str(custom))+ home = tmp_path / "agents" / "codex"++ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ (custom / "config.toml").write_text('[mcp_servers.fresh]\nurl = "http://f/mcp"\n')+ monkeypatch.setenv("CODEX_HOME", str(home))+ assert CliRunner().invoke(start.start_app, ["codex", "--no-launch"]).exit_code == 0+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["mcp_servers"] == {"fresh": {"url": "http://f/mcp"}}+++def test_codex_carries_mcp_servers_with_multiline_values(fake_studio, tmp_path, monkeypatch):+ tomllib = pytest.importorskip("tomllib")+ source_home = tmp_path / "user-codex"+ source_home.mkdir()+ (source_home / "config.toml").write_text(+ '[mcp_servers.notes]\ncommand = "notes"\nargs = ["--banner", """\n[welcome]\nhi"""]\n\n'+ '[mcp_servers.docs]\nurl = "http://d/mcp"\n'+ )+ monkeypatch.setenv("CODEX_HOME", str(source_home))+ home = tmp_path / "agents" / "codex"++ result = CliRunner().invoke(start.start_app, ["codex", "--no-launch"])+ assert result.exit_code == 0, result.output+ config = tomllib.loads((home / "config.toml").read_text())+ assert config["model_providers"]["unsloth_api"]["base_url"].endswith("/v1")+ assert config["mcp_servers"] == {+ "notes": {"command": "notes", "args": ["--banner", "[welcome]\nhi"]},+ "docs": {"url": "http://d/mcp"},+ }++def test_connect_codex_as_subagent_preserves_cloud_parent(fake_studio, tmp_path, monkeypatch):monkeypatch.setattr(start, "_codex_supports_model_catalog", lambda: True)source_home = tmp_path / "user-codex"