Skip to content

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.

CLI: doctor --mcp sends the API key only to a Studio that proves its identity (#14178)

Daniel Han0c237639f642
diff --git a/unsloth_cli/commands/mcp.py b/unsloth_cli/commands/mcp.py
index e8478c2a986..e1244cc5382 100644
--- a/unsloth_cli/commands/mcp.py
+++ b/unsloth_cli/commands/mcp.py
@@ -29,6 +29,7 @@ from unsloth_cli._inference import (
find_studio_server,
is_loopback_url,
urlopen_no_redirect,
+ verify_studio_identity,
)
from unsloth_cli.commands.start import (
_codex_source_home,
@@ -501,6 +502,12 @@ def gather_mcp_facts(env_var: str = "UNSLOTH_API_KEY", timeout: float = 5.0) ->
# Like the other auto flows, the key only goes to loopback or an explicit UNSLOTH_STUDIO_URL.
if key and not (is_loopback_url(facts.base) or os.environ.get("UNSLOTH_STUDIO_URL")):
facts.key, key = "unsent", None
+ elif (
+ key and not os.environ.get("UNSLOTH_STUDIO_URL") and not verify_studio_identity(facts.base)
+ ):
+ # A found port answering /api/health is not proof it is Unsloth Studio: another local user can
+ # listen there. Same check `unsloth chat` and `unsloth start` make before sending a secret.
+ facts.key, key = "unverified", None
elif key:
status, detail = _probe(f"{facts.base}/api/inference/loaded-models", key, timeout, None)
if status is not None and 200 <= status < 300:
@@ -528,6 +535,8 @@ def mcp_verdict(facts: McpFacts) -> Tuple[bool, List[str], List[str]]:
key_hints = {
"missing": f"Set {var} to an Unsloth Studio API key (Settings → API).",
"unsent": f"{facts.base} is not on this machine; set UNSLOTH_STUDIO_URL to test the key.",
+ "unverified": f"{facts.base} did not prove it is this machine's Unsloth Studio, so {var} "
+ "was not sent. If it is (for example through an SSH tunnel), set UNSLOTH_STUDIO_URL.",
"invalid": f"Unsloth Studio refused the key in {var}. Create one in Settings → API.",
"error": f"Unsloth Studio failed while checking the key: {facts.key_detail}",
}
@@ -564,6 +573,7 @@ def mcp_doctor(env_var: str = "UNSLOTH_API_KEY", timeout: float = 5.0) -> int:
"invalid": "refused",
"error": "not checked",
"unsent": "not sent",
+ "unverified": "not sent",
}
if facts.base and facts.key in checked:
key += f", {checked[facts.key]}"
diff --git a/unsloth_cli/tests/test_doctor_mcp.py b/unsloth_cli/tests/test_doctor_mcp.py
index d14ea97c8ad..fdc06915a30 100644
--- a/unsloth_cli/tests/test_doctor_mcp.py
+++ b/unsloth_cli/tests/test_doctor_mcp.py
@@ -53,6 +53,7 @@ def _facts(**kw):
(_facts(mcp = "refused", mcp_detail = "Workflow keys cannot use"), "Workflow keys cannot use"),
(_facts(mcp = "error", mcp_detail = "timed out"), "timed out"),
(_facts(key = "unsent"), "UNSLOTH_STUDIO_URL"),
+ (_facts(key = "unverified"), "did not prove it is this machine's Unsloth Studio"),
],
)
def test_verdict_problems(facts, needle):
@@ -90,10 +91,12 @@ def _stub_http(
monkeypatch,
answers,
base = "http://127.0.0.1:8888",
+ verified = True,
):
"""answers: path suffix -> status. Records (url, Authorization header)."""
seen = []
monkeypatch.setattr(mcp_mod, "find_studio_server", lambda *a, **k: base)
+ monkeypatch.setattr(mcp_mod, "verify_studio_identity", lambda *a, **k: verified, raising = False)
def opener(request, timeout):
seen.append((request.full_url, request.get_header("Authorization")))
@@ -250,6 +253,7 @@ def test_probe_errors_never_quote_the_key(env, monkeypatch):
# http.client quotes a refused header value, so a key with a newline lands in the error.
monkeypatch.setenv("UNSLOTH_API_KEY", CANARY + "\r\n")
monkeypatch.setattr(mcp_mod, "find_studio_server", lambda *a, **k: "http://127.0.0.1:9")
+ monkeypatch.setattr(mcp_mod, "verify_studio_identity", lambda *a, **k: True, raising = False)
facts = mcp_mod.gather_mcp_facts(timeout = 1.0)
assert facts.key == "error"
assert "LEAKCANARY" not in facts.key_detail, facts.key_detail
@@ -259,6 +263,7 @@ def test_probe_errors_never_quote_the_key(env, monkeypatch):
def test_probes_send_the_cli_user_agent(env, monkeypatch):
agents = []
monkeypatch.setattr(mcp_mod, "find_studio_server", lambda *a, **k: "http://127.0.0.1:8888")
+ monkeypatch.setattr(mcp_mod, "verify_studio_identity", lambda *a, **k: True, raising = False)
def opener(request, timeout):
agents.append(request.get_header("User-agent"))
@@ -267,3 +272,16 @@ def test_probes_send_the_cli_user_agent(env, monkeypatch):
monkeypatch.setattr(mcp_mod, "urlopen_no_redirect", opener)
mcp_mod.gather_mcp_facts()
assert agents == ["unsloth-cli"] * 2
+
+
+def test_gather_never_sends_the_key_to_an_unverified_loopback_server(env, monkeypatch):
+ # Anything answering /api/health on the found port could be another local user's listener.
+ seen = _stub_http(monkeypatch, {"/mcp/": 401}, verified = False)
+ facts = mcp_mod.gather_mcp_facts()
+ assert facts.key == "unverified"
+ assert seen and all(auth is None for _, auth in seen)
+ # An explicit UNSLOTH_STUDIO_URL is the user naming the server, as for the other commands.
+ monkeypatch.setenv("UNSLOTH_STUDIO_URL", "http://127.0.0.1:8888")
+ seen = _stub_http(monkeypatch, {}, verified = False)
+ assert mcp_mod.gather_mcp_facts().key == "valid"
+ assert seen[0][1] == f"Bearer {CANARY}"