Skip to content

Studio: MCP agents upload datasets, load what they trained, and download models ahead of time - #14107

Merged
danielhanchen merged 202 commits into
unslothai:mainfrom
Etherll:fix/inline-mcp-train-to-use-ace31b0b542c
Oct 19, 2026
Merged

danielhanchen merged 202 commits into
unslothai:mainfrom
Etherll:fix/inline-mcp-train-to-use-ace31b0b542c

Conversation

@Etherll

@Etherll Etherll commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Includes #14088, #14089 and #14090; review the last 24 commits (7cbecec..HEAD: git log --no-merges 06a089d2d1..HEAD), leaving out the merges of #14090's branch.

Summary

Over MCP, an agent could start training in Unsloth Studio, but it could not upload its own data, load the model it had trained, or download the next model ahead of time. This PR adds dataset uploads, loading a checkpoint or export by name, a resolved validate_only echo, TTS fine-tuning guidance, and a download_model tool backed by Unsloth Studio's download manager.

Changes

Datasets

  • datasets(action="upload", filename, data_base64 | rows) sends a file to /api/hub/datasets/upload. It returns a ref: handle for config.local_datasets, never the stored path the route returns.
  • datasets(action="upload_images", name, images, captions) sends captioned images to the diffusion dataset route. Every image gets a fresh file stem, so a later upload never replaces an earlier one. Images past Unsloth Studio's upload limit are refused before anything is sent, with a note to split them across calls. It returns the dataset name that start_training(kind="diffusion") takes as data_dir.

Loading what you trained

  • load_model(checkpoint="<run>" | "<run>/checkpoint-N") resolves the name through checkpoints.resolve. load_model(export="<name>") resolves it under the exports folder, and variant picks a GGUF export's quantization.
  • Both load through a minted ref: handle and report the name back, never a path or the handle. evicted_slots entries get their names back the same way, and so does the get_job(kind="load") result of a load that outlasted wait_s. unload_model(model=<that name>) finds it.
  • is_lora follows the run's PEFT type, passed through Studio: MCP agents choose how and where a model loads, preview the load, and queue videos #14090's is_lora kwarg. The load route detects LoRA itself, so a full fine-tune is not forced to load as LoRA.
  • A GGUF export loads as its folder plus the quantization in its file name, or the variant passed, so device="cpu" places it as GGUF. An export whose file name has no quantization needs variant before it can be placed. A checkpoint is refused for device="cpu".
  • checkpoint and export are refused for kind image, video and stt.
  • Unsloth Studio serves a trained load under its ref: handle, so the field text says to chat with it by leaving chat's model unset.

start_training

  • validate_only now returns the resolved config, with every path and secret dropped.
  • Refusals name the model as the caller sent it, never the host path a ref: handle resolves to.
  • It runs the trainer's audio-type detection. GGUF, audio.cpp and native TTS ids are refused, with the list of trainable bases. It sets is_dataset_audio for codec TTS: Orpheus/SNAC, CSM, Spark/BiCodec and OuteTTS/DAC. A real start runs the same check, so it refuses the same models and sends is_dataset_audio for codec TTS.
  • The docstring names the trainable TTS bases: unsloth/orpheus-3b-0.1-ft, sesame/csm-1b, unsloth/Spark-TTS-0.5B and unsloth/Llama-OuteTTS-1.0-1B. It covers:
    • the columns (audio + text, optional speaker_id) and the custom_format_mapping roles;
    • CUDA only;
    • that GGUF/audio.cpp TTS (Qwen3-TTS, Breeze) can't be trained;
    • the one-GPU order main uses: training unloads image and video models, and unloads chat or TTS only when they don't fit next to it. A 409 comes back while inference is running. Then export → load_model(checkpoint/export) → generate_audio.
  • Agents follow training with get_job(kind="training", wait_s=50) instead of studio_status.

Prefetch

  • ALLOWED_PREFIXES gains exactly /api/hub/download. That covers /download, /download-status, /download-progress and /download/cancel, and nothing else under /api/hub.
  • download_model(model, kind, variant?) starts a download without loading anything. GGUF repos send gguf_variant, image and video send their download-plan entries with scope_id="diffusion", a native TTS or audio model sends its audio download-plan entries (companion codec repos included) with scope_id="audio", and STT reuses download_stt. Image and video downloads take the same options load_model takes (memory_mode, speed_mode, transformer_quant, text_encoder_quant, h3_task), so the plan stages the files the load will read.
  • The id carries each start's generation, so cancel(kind="model_download", id) (through /download/cancel) stops only the run download_model started. get_job(kind="download", id) reads /download-status and works with wait_s.
  • An image or video download that fails part-way stops the parts it already started. A cancel stops every part before it reports a refusal.
  • repo:variant shorthand downloads and waits on that variant. A native speech prefetch under kind="tts" or "audio" serves a load of either kind.
  • K16 guard 2: load_model waits for a running Unsloth Studio download of the same model or variant before Studio: MCP agents choose how and where a model loads, preview the load, and queue videos #14090's partial-variant refusal. The wait runs inside the load job (Studio: MCP agents choose how and where a model loads, preview the load, and queue videos #14090 now runs every pre-load read there), so it outlives wait_s: the call returns loading with a job id, get_job(kind="load") names the download id while it waits, and the load starts once the download ends. The wait is capped at an hour, after which the load fails and names the download id. A managed download is waited on, never refused. An attach to an in-flight load (guard 1) still comes first, and an unmanaged partial variant is still refused.
  • New outputs: JobStatus.download and DownloadRef.

studio/MCP.md gets the download_model row and updated datasets and start_training rows.

Testing

New tests: test_mcp_tools_datasets_upload.py, test_mcp_tools_datasets_images.py, test_mcp_tools_training_validate.py, test_mcp_tools_download.py, test_mcp_train_to_use.py and test_mcp_k16_guard_order.py. test_mcp_server.py, test_mcp_forward.py, test_mcp_no_host_paths.py, test_mcp_tool_contract.py and the cancel, jobs, placement, training and video tests were updated. #14090's allowlist test now expects the whole download manager to be forwarded. In #14090's test_mcp_tools_plan_load.py, the fake download-status now reports only the partial Q8_0 as running: with guard 2, a load_model waits on any running managed download, so a world where every download is running made those loads wait out wait_s.

cd studio/backend
python -m pytest tests/test_mcp_*.py tests/test_gpu_arbiter.py tests/test_multi_model_always.py tests/test_multi_model_settings.py -q

Result on this head: 1,840 passed, 3 skipped.

GPU run on the head before the last two own commits (one device, real Unsloth Studio on its own port, 91 MCP calls):

  • Train to use:
    • datasets(upload) with 40 rows returned a ref:, and validate_only echoed the config with no paths.
    • unsloth/Qwen3-0.6B trained for 10 steps (~57 s), followed with get_job(kind="training", wait_s).
    • load_model(checkpoint=<run>) loaded it in 26 s, reported the name only, and chat answered. checkpoint-5 also loaded.
    • export_model(format="lora"), then get_job(kind="export"), then load_model(export="<run>/lora") and chat worked.
    • unload_model(kind="all") returned GPU memory to baseline.
  • TTS: MOSS-TTS-Nano and Qwen3-TTS GGUF were refused with the trainable list. Orpheus validate_only set is_dataset_audio.
  • Prefetch ("plan a film for a 24 GB card and prefetch everything", GPU size stubbed): zero calls went around MCP.
    • download_model worked for Qwen3-4B-GGUF UD-Q4_K_XL, Z-Image-Turbo and Wan2.2-TI2V-5B GGUF (already on disk, so they came back completed), and audio.cpp TTS.
    • FLUX.2-klein and LTX-2.3 (69 GB) were cancelled while running with cancel(kind="model_download").
    • A load_model during a running download returned loading with a job id and later finished.

No UI change.

Not in this PR

  • The INSTRUCTIONS prefetch sentence waits for the MCP errors PR (Studio: MCP errors, cancels and chat controls agents can act on #14081), which owns INSTRUCTIONS.
  • After a checkpoint or export load, studio_status and unload_model(kind="all") show its ref: handle (opaque, not a path) instead of its name. That code is owned elsewhere; follow-up.
  • GGUF and diffusion downloads report no percent, because gguf-download-progress is left off the allowlist on purpose.
  • A merged or LoRA export whose save_directory has only one level is not found by name. Use "<run>/<name>".
  • Backend TTS refusals and an audio dataset builder are deferred.

Follow-ups

  • A safetensors repo whose id contains "gguf" is treated as GGUF by download_model (no variant listed, so it is refused). This is the same name-based rule Studio: MCP agents choose how and where a model loads, preview the load, and queue videos #14090's is_gguf uses for loads, so prefetch and load agree. Moving both to the listing signal is a follow-up.
  • A get_job(kind="load") read in the same event-loop turn a trained load finishes can still show its ref: handle, and a cancelled trained load names the handle. The fix belongs in the shared load job runner.

Etherll and others added 30 commits October 17, 2026 13:03
…_datasets

Adds the MCP output classes this PR uses (DatasetUpload, ImageDatasetUpload,
DownloadRef, JobStatus.download, TrainingStarted.config, LocalDataset.ref).
The upload route returns the raw stored path to API-key callers, so the tool
mints the ref itself and never echoes the path.
…rainable TTS

validate_only now runs the trainer's audio-type detection, refuses GGUF,
audio.cpp and native text-to-speech ids, sets is_dataset_audio for codec TTS
(Orpheus/SNAC, CSM, Spark/BiCodec, OuteTTS/DAC), and echoes the validated config
with every path and secret dropped. The start_training docstring names the
trainable TTS bases, their columns and the one-GPU order.
…aset

Sends captioned images to the diffusion dataset route with fresh file stems,
so a later upload never replaces an earlier image, and returns the dataset name
start_training kind "diffusion" takes as data_dir, never its path.
Adds exactly /api/hub/download to the MCP allowlist (download, download-status,
download-progress and download/cancel; nothing else under /api/hub) and a
model_downloads module that stages GGUF, safetensors and diffusion downloads,
follows and stops them by one id, and waits for a managed download before a
load (K16 guard 2). The tools wire it up once the parent PR's load changes merge.
… load unloads

- get_job takes wait_s (0-240 s): it re-reads the job every 2 s, reports
  progress, and returns when the job finishes, its status or phase changes,
  or the wait runs out, with poll_after_s for a job still running. New kinds:
  training and diffusion_training (run folder and checkpoint names once
  completed) and load.
- load_model takes wait_s (unset: 240 s). The load runs as a job in a new
  studio_mcp/job_store.py, so a client that stops waiting never stops it; an
  early return gives status "loading" and a job_id for get_job(kind="load"),
  and a repeat call follows the load in flight.
- Loads report evicted_slots ({kind, model}) from the load answer's
  evicted_slots, else its evicted ids placed by a resident snapshot, else a
  before/after diff of the chat, image and video slots. unload_model echoes
  the kind asked for.
- export_jobs.py is a thin wrapper over the job store.
…ad, and queue videos

- load_model: image/video memory knobs (memory_mode, speed_mode, transformer_quant,
  text_encoder_quant, h3_task) and llm/tts/audio placement (device, alongside,
  kv_cache_type, gpu_layers); placement echoed on LoadResult and the image/video slots.
- plan_load previews variants, download bytes, residents and what a load would unload.
- A GGUF variant only partly on disk, and not downloading under Unsloth Studio, is refused.
- unload_model(kind="all") frees every chat, speech, image, video and STT model.
- generate_video queues a clip Unsloth Studio is too busy for and submits it in order.
download_model stages a model in Unsloth Studio's download manager without
loading it; get_job(kind="download") follows it and cancel(kind="model_download")
stops it by the id it returns. load_model takes a training checkpoint or an
export by name, loads it by a minted ref: handle, and reports the name back,
never a path or the handle. is_lora follows the run's PEFT type (the load route
detects LoRA itself), and checkpoint/export are refused for image, video and stt.
… arbiter that tracks residents

Stores the mode in the owner's database with a bool-first migration, extends
/api/settings/multi-model, and gives acquire_for a policy, an eviction report
and per-owner residents. Adds the response fields, the MCP mode read, the
padded-error headers and the structured out_of_memory check.
… failed read, and keep media evictions

- A chat load started elsewhere is followed only when no variant is asked
  for (the status names only the repo), and a repeat call follows a load
  only with the same variant and settings.
- Following a load skips a failed status read instead of failing.
- A route's chat-only evicted list no longer hides an image or video model
  the load also unloaded; evicted is built from evicted_slots.
- Progress keeps its last fraction through a step that reports none.
…o-use-ace31b0b542c

# Conflicts:
#	studio/backend/studio_mcp/tools/cancel.py
#	studio/backend/studio_mcp/tools/jobs.py
#	studio/backend/studio_mcp/tools/models.py
#	studio/backend/tests/test_mcp_no_host_paths.py
…eckpoints as LoRA

K16 guard 2: before M7's partial-variant refusal, load_model waits up to
240 s on an Unsloth Studio download of the same model or variant, then reports
it with the id get_job(kind="download") follows; a managed download is waited
on, never refused. Checkpoint and export loads skip both guards and pass is_lora
from the run's PEFT type. The LLM load poll no longer asks the Hub about a ref:
handle. Adds the download_model row to MCP.md.
- plan_load treats another variant of a resident GGUF repo as a new load.
- plan_load reports 0 bytes when nothing is left to download.
- unload_model(kind="all") also unloads models held behind the active one.
- A media GGUF that an image or video load is already downloading is not refused.
- The video queue keeps a clip queued through a network error and ignores stale clip times.
…o-use-ace31b0b542c

# Conflicts:
#	studio/backend/studio_mcp/tools/jobs.py
#	studio/backend/studio_mcp/tools/models.py
…load-placement

# Conflicts:
#	studio/backend/studio_mcp/loading.py
…heckpoints

Now that get_job waits on training and loads, start_training points agents at
get_job(kind="training", wait_s=50) instead of studio_status. A checkpoint or
export load names any evicted_slots entry back from its ref: handle, the download
wait skips ref: handles, get_job(kind="download") has a poll_after_s, and the
K16 tests cover attach before the download wait before the partial refusal.
…o-use-ace31b0b542c

# Conflicts:
#	studio/backend/studio_mcp/loading.py
…d report every model a load unloads

Always is enforced in the arbiter for chat and media, at the primary-replace
guard (same repo allowed), in the kept-slot retry loop and before the H3
image unload. The refusal answers 409 with X-Unsloth-Refusal everywhere, the
OpenAI envelope on /v1 and auto-switch, and the existing 503 on /p. Chat,
image and video loads list what they unloaded in evicted_slots, and callers
that assumed one GPU owner now ask holds().
…xport

Unsloth Studio serves a checkpoint under its ref: handle, so chat(model=<name>)
answers from the active model with a mismatch note. The checkpoint and export
fields now say to chat with model unset; the GPU run hit this.
danielhanchen and others added 12 commits October 18, 2026 10:31
# Conflicts:
#	studio/backend/core/inference/gpu_arbiter.py
@danielhanchen danielhanchen added >3000# new-feature# regresses# security# Moves a security boundary: sandbox, allowlist, permission or network guard labels Oct 18, 2026
# Conflicts:
#	studio/backend/routes/inference.py
# Conflicts:
#	studio/MCP.md
#	studio/backend/studio_mcp/loading.py
#	studio/backend/studio_mcp/tools/jobs.py
#	studio/backend/studio_mcp/tools/models.py
# Conflicts:
#	studio/backend/tests/test_mcp_tools_loading.py
# Conflicts:
#	studio/MCP.md
#	studio/backend/studio_mcp/outputs.py
#	studio/backend/studio_mcp/tools/jobs.py
#	studio/backend/studio_mcp/tools/models.py
#	studio/backend/studio_mcp/tools/video.py
#	studio/backend/tests/test_mcp_server.py
#	studio/backend/tests/test_mcp_tools_status.py
# Conflicts:
#	studio/MCP.md
#	studio/backend/studio_mcp/tools/cancel.py
#	studio/backend/studio_mcp/tools/models.py
#	studio/backend/tests/test_mcp_no_host_paths.py
#	studio/backend/tests/test_mcp_tools_cancel.py
@danielhanchen
danielhanchen merged commit 21f02a8 into unslothai:main Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature# regresses# security# Moves a security boundary: sandbox, allowlist, permission or network guard >3000#

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants