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: do not let the default tool policy reject every plain Anthropic message (#8023)

Daniel Han6d0ac7c865af
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index fa641038b2d..a86e59b963c 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -15130,6 +15130,26 @@ _STUDIO_ANTHROPIC_TOOL_ALIASES = {
_ANTHROPIC_UNPROMPTED_SAFE_TOOLS = frozenset({"web_search", "search_knowledge_base"})
+def _anthropic_selects_server_tools(
+ payload, requested_studio_tools: set[str], has_client_tool: bool
+) -> bool:
+ """Whether THIS request asked Unsloth to run its own tools on the Messages channel.
+
+ A process-wide ``--enable-tools`` is a default for ordinary chat, not a selection: reading
+ it as one made the permission gate reject every plain request on a default server, and
+ routing on it entered the local tool loop with terminal/python and nothing to confirm
+ them. So only an explicit ask counts -- ``enable_tools``/``mcp_enabled``, or an Anthropic
+ server-tool type in ``tools`` -- while ``--disable-tools`` and an explicit
+ ``enable_tools: false`` still veto both, and a client-tool catalog is never stolen.
+ """
+ if has_client_tool or _effective_enable_tools(payload) is False:
+ return False
+ # enable_tools only, not the OpenAI path's mcp_enabled: this model is extra="allow", so
+ # that key does arrive, but nothing here loads MCP schemas. Treating it as intent would
+ # answer an MCP-only request with ALL_TOOLS' built-ins, or reject it for holding terminal.
+ return payload.enable_tools is True or bool(requested_studio_tools)
+
+
def _anthropic_requested_studio_tools(tools: Optional[list]) -> set[str]:
requested: set[str] = set()
for tool in tools or []:
@@ -15618,10 +15638,12 @@ async def anthropic_messages(
# chat. It must not steal an explicit Anthropic client-tool catalog (Claude
# Code's Write/Edit/Bash tools) and turn it into Unsloth's local tool loop.
# An explicit per-request server-tool ask was rejected as mixed mode above.
- _enable_pre = False if _has_client_tool else _effective_enable_tools(payload)
- _server_tools_requested_pre = (
- _enable_pre or (_enable_pre is None and bool(requested_studio_tools))
- ) and not _anthropic_request_has_image(payload)
+ _selects_server_tools = _anthropic_selects_server_tools(
+ payload, requested_studio_tools, _has_client_tool
+ )
+ _server_tools_requested_pre = _selects_server_tools and not _anthropic_request_has_image(
+ payload
+ )
if _server_tools_requested_pre:
from core.inference.tools import ALL_TOOLS as _ALL_TOOLS_PRE
@@ -15746,13 +15768,10 @@ async def anthropic_messages(
# An Anthropic server-tool declaration implies server-tool mode, but only
# when tools aren't explicitly disabled (CLI --disable-tools or per-request
- # enable_tools=false). Explicit False always wins.
- _enable = False if _has_client_tool else _effective_enable_tools(payload)
- server_tools = (
- (_enable or (_enable is None and bool(requested_studio_tools)))
- and llama_backend.supports_tools
- and not _has_image
- )
+ # enable_tools=false). Explicit False always wins. Same predicate as the
+ # permission gate above: deciding "did this request select server tools"
+ # twice is what let the gate reject requests the router then served.
+ server_tools = _selects_server_tools and llama_backend.supports_tools and not _has_image
client_tools = (
not server_tools
and len(openai_client_tools) > 0
diff --git a/studio/backend/run.py b/studio/backend/run.py
index 54c7b0378d5..c9bfda62dac 100644
--- a/studio/backend/run.py
+++ b/studio/backend/run.py
@@ -2266,7 +2266,9 @@ def _build_arg_parser():
action = "store_true",
default = None,
help = "Force server-side tools (web search, code execution) on for "
- "every request. Default: on for every bind, per-request setting honored.",
+ "every request. Default: on for every bind, per-request setting honored. "
+ "/v1/messages takes the on direction per request (enable_tools) because it has "
+ "no confirmation channel; the off direction still applies everywhere.",
)
parser.add_argument(
"--disable-tools",
diff --git a/studio/backend/state/tool_policy.py b/studio/backend/state/tool_policy.py
index e0792321f93..de770155e17 100644
--- a/studio/backend/state/tool_policy.py
+++ b/studio/backend/state/tool_policy.py
@@ -6,8 +6,10 @@
Set by `unsloth run` at startup; consulted by the inference route gates.
None -> no CLI override (default). Per-request `enable_tools` is honored.
- True -> CLI forced tools on for every request.
- False -> CLI forced tools off for every request.
+ True -> CLI forced tools on for every request. Not on /v1/messages: that channel
+ cannot present a confirmation prompt, so it takes the on direction from the
+ request itself (see _anthropic_selects_server_tools).
+ False -> CLI forced tools off for every request, /v1/messages included.
"""
import contextvars
diff --git a/studio/backend/tests/test_anthropic_messages.py b/studio/backend/tests/test_anthropic_messages.py
index 9c6bf5f8aa5..cc1e536558c 100644
--- a/studio/backend/tests/test_anthropic_messages.py
+++ b/studio/backend/tests/test_anthropic_messages.py
@@ -2065,6 +2065,70 @@ class TestAnthropicMessagesToolRouting:
_drive(anthropic_messages(payload, request = None, current_subject = "t"))
assert backend.calls[0][0] == "tools"
+ def test_the_process_tool_default_alone_is_not_a_server_tool_selection(self, monkeypatch):
+ """`unsloth studio run` resolves the policy to on unless --disable-tools. Reading that
+ as "this request selected server tools" rejected every plain Messages request on a
+ default server, and routing on it ran the local tool loop with terminal/python and no
+ way to confirm them. A default is not a selection, in either direction."""
+ import routes.inference as inf_mod
+ from fastapi.responses import JSONResponse
+
+ backend = _mock_backend(monkeypatch)
+
+ async def _passthrough(*args, **kwargs):
+ return JSONResponse({"type": "message", "content": []})
+
+ monkeypatch.setattr(inf_mod, "_anthropic_passthrough_non_streaming", _passthrough)
+ set_tool_policy(True)
+
+ _drive(anthropic_messages(_basic_payload(), request = None, current_subject = "t"))
+ assert backend.calls, "a plain chat request must still be served"
+ path, kwargs = backend.calls[0]
+ assert path == "plain", (
+ "a tool-free request took the server-tool loop on the process default alone, so "
+ "the model can call terminal/python with no confirmation channel"
+ )
+ assert not kwargs.get("tools")
+
+ # An explicit ask still routes to the loop, with the mode that permits it.
+ backend = _mock_backend(monkeypatch)
+ _drive(
+ anthropic_messages(
+ _basic_payload(enable_tools = True, permission_mode = "off"),
+ request = None,
+ current_subject = "t",
+ )
+ )
+ assert backend.calls[0][0] == "tools"
+
+ # mcp_enabled is an ask on the OpenAI routes, which wire MCP discovery. This one does
+ # not, and the request model is extra="allow" so the key does arrive: honouring it
+ # would answer an MCP-only request with ALL_TOOLS' terminal/python under an MCP name.
+ reset_tool_policy()
+ backend = _mock_backend(monkeypatch)
+ _drive(
+ anthropic_messages(
+ _basic_payload(mcp_enabled = True, permission_mode = "off"),
+ request = None,
+ current_subject = "t",
+ )
+ )
+ assert backend.calls[0][0] == "plain"
+ assert not backend.calls[0][1].get("tools")
+
+ # The same default must not stop gating a request that does ask for server tools.
+ for fields in (
+ {"enable_tools": True},
+ {"enable_tools": True, "permission_mode": "ask"},
+ {"tools": [{"type": "terminal", "name": "terminal"}]},
+ ):
+ with pytest.raises(HTTPException) as exc:
+ _drive(
+ anthropic_messages(_basic_payload(**fields), request = None, current_subject = "t")
+ )
+ assert exc.value.status_code == 400
+ assert "no confirmation channel" in exc.value.detail["error"]["message"]
+
def test_render_html_gated_for_server_tools(self, monkeypatch):
# render_html is no longer unconditionally safe: a networked canvas prompts
# in auto and this channel cannot present that gate, so selecting it under
diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.py
index f011494d5de..55457827e22 100644
--- a/unsloth_cli/commands/studio.py
+++ b/unsloth_cli/commands/studio.py
@@ -1351,7 +1351,9 @@ def studio_default(
None,
"--enable-tools/--disable-tools",
help = "Force server-side tools (web search, code execution) on or off for "
- "every request. Default: on for every bind, with the per-chat UI toggle honored.",
+ "every request. Default: on for every bind, with the per-chat UI toggle honored. "
+ "/v1/messages takes the on direction per request (enable_tools) because it has no "
+ "confirmation channel; the off direction still applies everywhere.",
),
disable_dns_pinning: bool = typer.Option(
False,
@@ -1844,7 +1846,9 @@ def run(
rich_help_panel = _RUN_PANEL_TOOLS,
help = (
"Force server-side tools (web search, code execution) on or off for "
- "every request. Default: on for every bind."
+ "every request. Default: on for every bind. /v1/messages takes the on "
+ "direction per request (enable_tools) because it has no confirmation "
+ "channel; the off direction still applies everywhere."
),
),
disable_dns_pinning: bool = typer.Option(