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.

Studio MCP: never run server-side tools from the chat tool (#13605)

Daniel Han01e0b8c8f4b2
diff --git a/studio/backend/studio_mcp/tools/text.py b/studio/backend/studio_mcp/tools/text.py
index 3f5c8c8de04..62b2e7be6dd 100644
--- a/studio/backend/studio_mcp/tools/text.py
+++ b/studio/backend/studio_mcp/tools/text.py
@@ -12,6 +12,7 @@ from typing import Annotated, Any, Literal, Optional, Union
from fastmcp.exceptions import ToolError
from pydantic import BaseModel, ConfigDict, Field
+from state.tool_policy import tools_force_disabled
from studio_mcp.caller import current_caller
from studio_mcp.errors import raise_for_route
from studio_mcp.forward import forward
@@ -89,13 +90,20 @@ async def chat(
"model": model or "default",
"messages": turns,
"stream": False,
+ # `unsloth studio run` turns tools on for an omitted field; MCP chat never runs them.
+ "enable_tools": False,
# Per-run id (the route scopes it to the caller's account), so only a cancel aimed at this chat stops it.
"cancel_id": cancel_id,
**present(max_tokens = max_tokens, temperature = temperature),
}
- payload = await route_json(
- "POST", "/v1/chat/completions", json_body = body, hint_if = (NOT_LOADED, {400: LOAD_CHAT_HINT})
- )
+ # Beats a CLI --enable-tools too; forward's task copies this context into the route.
+ with tools_force_disabled():
+ payload = await route_json(
+ "POST",
+ "/v1/chat/completions",
+ json_body = body,
+ hint_if = (NOT_LOADED, {400: LOAD_CHAT_HINT}),
+ )
choices = payload.get("choices") if isinstance(payload, dict) else None
if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict):
raise ToolError("Unsloth Studio returned no reply")
diff --git a/studio/backend/tests/test_mcp_tools_text.py b/studio/backend/tests/test_mcp_tools_text.py
index b6d9af9da21..1574ad7b07f 100644
--- a/studio/backend/tests/test_mcp_tools_text.py
+++ b/studio/backend/tests/test_mcp_tools_text.py
@@ -97,6 +97,24 @@ def test_a_prompt_becomes_one_user_turn(monkeypatch):
assert body["cancel_id"].startswith("mcp-") and len(body["cancel_id"]) > 20
+def test_chat_never_runs_server_side_tools_whatever_the_launcher_says(monkeypatch):
+ from state import tool_policy
+
+ # `unsloth studio run` default on, plus a CLI --enable-tools override on top.
+ monkeypatch.setattr(tool_policy, "_tool_policy_default", True)
+ monkeypatch.setattr(tool_policy, "_tool_policy", True)
+ seen = []
+
+ def reply(request, body):
+ seen.append((tool_policy.get_tool_policy(), tool_policy.get_tool_policy_default()))
+ return COMPLETION
+
+ _result, studio = _chat(monkeypatch, {"prompt": "hi"}, reply = reply)
+ assert seen == [(False, False)]
+ assert bodies(studio, "/v1/chat/completions")[0]["enable_tools"] is False
+ assert tool_policy.get_tool_policy() is True
+
+
def test_a_caller_chosen_cancel_id_is_sent_so_another_call_can_stop_the_reply(monkeypatch):
result, studio = _chat(monkeypatch, {"prompt": "a", "cancel_id": "agent-run-0001"})
assert bodies(studio, "/v1/chat/completions")[0]["cancel_id"] == "agent-run-0001"