Skip to content

Studio: let chat call Unsloth Studio's own MCP tools - #14349

Closed
Etherll wants to merge 83 commits into
unslothai:mainfrom
Etherll:studio/chat-unsloth-studio-mcp
Closed

Etherll wants to merge 83 commits into
unslothai:mainfrom
Etherll:studio/chat-unsloth-studio-mcp

Conversation

@Etherll

@Etherll Etherll commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #14348 ("Studio: add a Settings > MCP tab with per-tool switches"), so this branch includes it. Review only the changes on top of #14348, and merge #14348 first.

Summary

Adds Unsloth Studio's own MCP server to the chat MCP dropdown. "Unsloth Studio" is now the first preset in that dropdown. Ticking it lets the chat model call Unsloth Studio's MCP tools in process, with no network listener and no API key to paste.

  • The chat gets only the tools the owner left on in Settings > MCP, minus chat, load_model, unload_model, cancel, start_training and export_model, which would evict or re-enter the chat that calls them or outlive the call. Those six show "Not in chat" in Settings > MCP > Tools (CHAT_DENYLIST in mcp_server.py, chat field on the tool settings response).
  • It works whether or not external Agent access is on. The external /mcp gate is unchanged: it still returns 404 while off and still refuses internal keys.
  • Each tool call runs as the chat's own account with a per-call internal API key ("Unsloth Studio chat tools"). The key expires in at most 6 h and is deleted when the call ends, including on cancel, timeout and error (core/inference/studio_tools_mcp.py). A password reset since the chat request signed in stops new keys from being created.
  • Host file paths are refused for these calls, even for the owner, so a prompt-injected model cannot read host files.
  • Only a UI session can add the reserved row or repoint a row to or from it. API keys get 403 on create, import and URL edits (routes/mcp_servers.py).
  • Media links in tool results come back as origin-relative text (/api/...), so they open from whatever address the browser uses.
  • A chat video request that would have to wait behind another video is told to retry instead of being queued.
  • The row's tools are re-listed on every send, so owner toggles in Settings > MCP apply on the next message.
  • studio_mcp/caller.py falls back to a caller bound in a ContextVar when there is no HTTP request. A real HTTP request always uses its own scope, so /mcp never sees the bound caller.

Known limits:

  • API-key /v1 chats can use the row once a UI session has turned it on (same as the stdio and Blender rows), and their inner tool calls do not show in the API monitor.

Testing

Backend, from studio/backend (on the final head the MCP, chat bridge and auth suites pass. The only failures, in files such as test_mcp_images.py, fail the same way on main in this environment):

python -m pytest tests/test_studio_tools_chat.py tests/test_mcp_servers.py tests/test_mcp_gate.py tests/test_mcp_forward.py tests/test_mcp_no_host_paths.py tests/test_mcp_http_sessions.py tests/test_systemone_connections.py tests/test_research_internal_key_monitor.py tests/test_mcp_resources.py tests/test_mcp_access_route.py tests/test_mcp_access_setting.py tests/test_mcp_server.py tests/test_mcp_prompts.py

Frontend: typecheck clean, i18n:check:strict passes, the MCP and settings tests pass, build and bundle:check within budget (+233 B raw / +15 B transfer, and the budget in scripts/check-bundle-budget.ts is raised by that).

Negative control: on main the new tests fail (no studio_tools_mcp module, API-key guard missing). With caller.py reverted, 11 tests fail with "No active HTTP request found".

Manual: ran Unsloth Studio from this branch with Qwen3-4B-Instruct-2507 GGUF on one GPU, ticked "Unsloth Studio" in the chat MCP dropdown and asked what is loaded. The chat called studio_status ("Used tool: Unsloth Studio · studio_status") and answered correctly. No key row was left after the call.

Screenshots

Before is #14348's head (9a51d6d), since this PR is stacked on it; after is this branch at 801ae88. Both are separate installs. After, the seven tools the chat cannot call (chat, load_model, unload_model, cancel, start_training, export_model, run_recipe) read "Not in chat" in Settings > MCP, and the reserved Unsloth Studio row lists the other 16 tools in process. Before, the same row lists nothing.

Settings > MCP before and after: tools the chat cannot call are marked Not in chat

Checked end to end on a Colab A100 with Qwen3-4B-Instruct-2507 GGUF: the chat called studio_status through the row and named the loaded model, turning studio_status off in Settings > MCP removed it from the row, and no per-call key row was left after either call.

Studio UI regression check

Base 9c30ea884 vs head db8548862. Functional: NO_REGRESSION, UI coverage 88%, 3 visual change(s), 2 text-only change(s). Only steps whose screenshots differ are shown.

crawl/r_images
crawl/r_images

desktop/s05_route_images
desktop/s05_route_images

desktop/s06_route_studio
Facts: path: {'before': '/studio', 'after': '/chat'}
desktop/s06_route_studio

Step Text / structure change
crawl/r_data_recipes
desktop/s01_boot {'removed': [], 'added': []}

Unsloth Studio's MCP controls move to their own owner-only Settings > MCP tab:
the Agent access switch and per-agent setup snippet, plus a Tools list with
one switch per tool. A turned-off tool is hidden from tools/list and its calls
are refused with [tool_disabled]; prompts that read a turned-off tool are
hidden too. New tools start on. The Agents and API keys tabs show the access
status with a button that opens the MCP tab.

GET /api/settings/mcp-access gains tools[{name, area, enabled}]; PUT also
takes disabled_tools. The old {enabled} body still works, and
UNSLOTH_STUDIO_ENABLE_MCP now locks only the switch, not the tool list.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-28T00:38:31.084363Z 8929fe8 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Etherll

Etherll commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

Ticking "Unsloth Studio" in the chat MCP dropdown now lets the chat model use Unsloth Studio's own tools, limited to the ones turned on in Settings > MCP. Six tools that would unload, restart, cancel or re-enter the chat show "Not in chat" and are never offered.

What changed

  • Chat image and audio generation reported an error after the work had already run, because their links were relative. Those links now come back as text the chat can show.
  • Each chat tool call left an inactive API key row behind. The per-call key is now deleted when the call ends.
  • Chat-started exports could outlive their per-call key and fail. export_model is no longer offered to chat, and a chat video request that would have to wait behind another video now says to retry instead of queueing.
  • Behind a proxy that serves Unsloth Studio under a path prefix, chat media links dropped that prefix. They now keep it.
  • A chat whose session was ended by a password reset could still create tool keys. Creating one now checks that the session is still valid.

The chat bridge, MCP and auth tests pass on 629da331, apart from test files that fail the same way on main in this environment. In a browser with a small GGUF model, a chat with the row ticked called studio_status and answered correctly, and no key row was left after the call. CI was still running when this was posted.

Chat MCP dropdown and Settings > MCP > Tools before and after
Before: #14348 head 07546e7a. After: ceecaa31 (the later commit on 629da331 changes only how the backend builds links).

The chat's Unsloth Studio row revokes its per-call key when the tool call
returns. export_model keeps working in a background job after that, so a
chat-started export failed with 401; it also loads a checkpoint that can
evict the chat's model, so it joins the chat denylist. The video queue
posts a queued clip later with the caller's token, so a chat call that
finds another video running is now told to retry instead of being queued.
…relative

A chat request that a password reset revoked could still mint keys for its
remaining Unsloth Studio tool calls. The request's credential generation now
rides on its account context, and the per-call mint passes it as expect_gen,
so a rotated credential refuses the call like any other new key.

Chat tool media links were built on 127.0.0.1, which a browser on another
machine (LAN, tunnel, proxy) cannot open. The chat's callers now get
origin-relative links that resolve against the address the user reached
Unsloth Studio on.
…o the MCP snippet

A read of the MCP settings that overlapped a write was reported as every tool
off, so agents briefly saw an empty tools/list and the Tools list could save
that view back. The read now retries, and the settings API sends tools: null
when it really cannot read, which hides the list instead of showing it all off.

A key created on Settings > API fills the MCP tab's setup snippet again, kept
in memory until the key card is dismissed or Settings closes. The Unsloth
Studio MCP row moves to the end of the Agents tab.
…hat-unsloth-studio-mcp

# Conflicts:
#	studio/backend/routes/settings.py
A chat call has no outer request, so its media links are origin-relative, and
MCP's ResourceLink rejects a relative URI: every chat generate_image and
generate_audio call reported an error after the work had already run. Those
links now come back as text; external /mcp callers still get ResourceLinks.

The bridge also deleted nothing: each chat tool call left an inactive key row
in auth.db. The per-call key row and its account copy are now deleted when the
call ends.
…cp-settings-tab

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
…unsloth into studio/chat-unsloth-studio-mcp

# Conflicts:
#	studio/backend/auth/authentication.py
#	studio/backend/utils/account_context.py
#	studio/frontend/scripts/check-bundle-budget.ts
Behind a proxy that serves Unsloth Studio under a path such as /studio, chat
media links came back as /api/... and missed the prefix. Each request's
root_path is now remembered for the chat's tool calls, so their links read
/studio/api/....
…cp-settings-tab

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
…unsloth into studio/chat-unsloth-studio-mcp

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
@NilayYadav NilayYadav self-assigned this Oct 20, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Failed to sample tokens
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
…unsloth into r14349

# Conflicts:
#	studio/backend/auth/storage.py
#	studio/frontend/scripts/check-bundle-budget.ts
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 23, 2026
# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 23, 2026
… dialog

Drops the separate Settings > MCP tab so the MCP name stays free for a
future tab that manages MCP servers. The Agent access (MCP) card now ends
the Agents page: the on/off switch, a Tools row ("20 of 23 on") that opens
a dialog, and the setup snippet. The dialog groups the tools (Models,
Generate, Training and export, Data and library, Jobs) with one-line rows
whose name toggles the switch, a switch per group, and Turn all on. Every
change sends only the tools it flips as tool_states, so the server keeps
merging concurrent toggles. The API tab keeps a one-line status row that
jumps to the card.
…io/mcp-settings-tab

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
#	studio/frontend/src/features/settings/components/mcp-access-section.tsx
#	studio/frontend/src/features/settings/tabs/api-keys-tab.tsx
#	studio/frontend/tests/mcp-access-section.test.ts
@Etherll

Etherll commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator Author

Replaced by #15631 (stacked on #15629), with the same final code as one commit. Daniel's and Nilay's fixes from this PR are included.

@Etherll Etherll closed this Oct 28, 2026
danielhanchen added a commit that referenced this pull request Nov 8, 2026
Replaces #14349, with the same final code as one commit. It is stacked
on #15629, so merge that first. Until then this diff also shows #15629's
changes, and only the last commit is new. It includes the fixes Daniel
and Nilay made on #14349, and both are credited as co-authors.

## Summary

This PR adds an **Unsloth** preset at the top of the chat MCP dropdown.
Ticking it lets the chat model call Unsloth Studio's own MCP tools in
process, with no API key to paste and no network listener.

![Chat MCP
dropdown](https://raw.githubusercontent.com/Etherll/unsloth/ui-evidence/chat-unsloth-tools-15155c54/b-chat-mcp-menu.png)

- **Which tools the chat gets:** only the tools the owner left on in
Settings > Agents > Choose tools, minus the ones that would evict or
re-enter the chat that calls them: `chat`, `load_model`, `unload_model`,
`cancel`, `start_training`, `export_model` and `run_recipe`
(`CHAT_DENYLIST` in `mcp_server.py`). The tools dialog names those once,
in a note under the groups.
- **Toggles apply right away:** the row's tools are re-listed on every
send, so a change in Settings applies to the next message.
- **External agent access is unaffected:** it works whether or not
"Agent access (MCP)" is on, and `/mcp` still returns 404 while that is
off.

![Tools dialog
note](https://raw.githubusercontent.com/Etherll/unsloth/ui-evidence/chat-unsloth-tools-15155c54/b-note-crop.png)

## Safety

- **Per-call key:** each tool call runs as the chat's own account, with
an internal API key minted for that call. The key is deleted when the
call ends, including on Stop, timeout and error.
- **No host files:** host file paths are refused for these calls, even
for the owner.
- **Who can turn it on:** only a signed-in UI session can add the row or
point another row at it. API keys and keyless callers get 403, and
keyless callers never see the row.
- **Stop:** a chat Stop also stops the image, audio or video job a tool
call started.
- **Model switches:** a chat tool call never switches the loaded model.
- **Media links:** links open through the user's session, so they work
in the desktop app and behind a proxy path prefix.

## Testing

```bash
cd studio/backend
python -m pytest tests/test_studio_tools_chat.py tests/test_mcp_*.py tests/test_settings*.py tests/test_systemone_connections.py tests/test_research_internal_key_monitor.py tests/test_fine_tuned_model_serving.py tests/test_openai_tool_passthrough.py
cd ../frontend
npm run typecheck && npm run i18n:check:strict && npm test && npm run build && npm run bundle:check
```

- **Backend:** 2,237 passed, plus 499 in the model-switch and
tool-passthrough files. The same 3 `test_mcp_images.py` tests fail here
and on main.
- **Frontend:** typecheck clean, `i18n:check:strict` passes, and the
settings, MCP and chat-composer tests pass (871).
- **Bundle:** within budget. The budget grows by +1,363 B raw / +366 B
transfer over #15629, from the chat preset and the note.

I also ran Unsloth Studio from this branch to check the dropdown and the
dialog note.

<!-- studio-regress:start -->
### Studio UI regression check
Base `81dc73ac4` vs head `fe799ee49`. Functional: **NO_REGRESSION**, UI
coverage 83%, 1 visual change(s), 5 text-only change(s). Only steps
whose screenshots differ are shown.

Note: in this run diffusion_video failed once on head inside the
regression harness's own fixture copy (a temp-dir race between the two
arms, fixed in the harness), before any request reached Unsloth. A rerun
of that journey on both arms at the same head gave 6/6 identical steps;
its results are used here.

**settings/s06_tab_api**:  | reproduced on confirm (VISUAL_DIFF)

![settings/s06_tab_api](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/15631/settings_s06_tab_api-78bbeb6f21de.png)

| Step | Text / structure change |
|---|---|
| `settings/s10_tab_agents` | {'removed': [], 'added': ['Unsloth MCP',
'Agent access (MCP)', 'Let coding agents […] use Unsloth over MCP.', '23
of 23 on', 'Choose t |
| `parallel/stop_training` | {'removed': ['65/4.1k', 'Three fruits are
named:', '#ctrl button:Name three fruits.', '#ctrl button:Context usage:
65 of 4.1k tokens'], 'added': ['Name three an |
| `crawl/r_projects` |  |
| `crawl/r_export` |  |
| `crawl/r_data_recipes` |  |
<!-- studio-regress:end -->


### Chat MCP preset: before and after this layer

BEFORE is #15629's head (`069a35528`), AFTER is this PR's head
(`fe799ee49`), so only this layer's change shows; this is not the PR's
own merge base.

The chat's MCP menu now offers **Unsloth** first, which answers with
Unsloth's own tools in process:

![Chat MCP menu, before and
after](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/15631/q_00-9d621edce6c6.png)

The tools dialog in Settings > Agents ends with the tools a chat never
gets ("The chat never gets Load model, Unload model, Plan a load, Chat,
Run Data Recipe, Start training, Export model, and Cancel work."):

![Unsloth MCP tools dialog, before and
after](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/15631/q_01-31bf4d099ec3.png)

---------

Co-authored-by: Daniel Han <[email protected]>
Co-authored-by: Nilay Yadav <[email protected]>
Co-authored-by: Etherl <[email protected]>
Co-authored-by: LeoBorcherding <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants