Skip to content

Studio: do not let the default tool policy reject every plain Anthropic message - #8023

Merged
danielhanchen merged 4 commits into
mainfrom
fix/anthropic-tool-default-gate
Aug 6, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
fix/anthropic-tool-default-gate

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

On a default unsloth studio run server, every ordinary POST /v1/messages answers 400:

permission_mode 'ask' has no confirmation channel for Anthropic Messages server tools,
and 'auto' (or the omitted default) cannot gate a local 'terminal'/'python' tool here;
set 'off' or 'full'.

Two changes met:

  • ce193c243 made the server-side tool policy default to on for every bind (resolve_tool_policy: return True if flag is None else flag), so unsloth studio run calls set_tool_policy(True) unless --disable-tools is passed.
  • #7079 added a pre-switch permission gate that reads the resolved policy as "this request selected server tools". For a tool-free request it therefore collected the whole catalog, found terminal in it, saw no permission_mode, and rejected.

A process-wide default is not a per-request selection. The gate now fires on an explicit ask only: enable_tools / mcp_enabled, or an Anthropic server-tool type in tools. --disable-tools still vetoes both forms.

What still rejects

Verified by driving the real route with set_tool_policy(True), the default a server actually runs with:

request before after
plain chat, no tools, no mode 400 200
enable_tools: true, mode omitted 400 400
enable_tools: true, permission_mode: ask 400 400
{"type": "terminal"}, mode omitted or auto 400 400
{"type": "terminal"}, permission_mode: full runs runs
web_search only, permission_mode: ask 400 400
{"type": "terminal"} under --disable-tools runs runs
render_html under ask/auto/omitted 400 400

Why CI never saw it

The four end-to-end cases that cover this path (test_anthropic_basic, test_anthropic_streaming, test_anthropic_with_tools, test_anthropic_tool_choice_any) live in tests/test_studio_api.py, which studio-backend-ci.yml ignores because it needs a live model and a ~2 GB GGUF download. test_the_process_tool_default_alone_is_not_a_server_tool_selection now pins the case in the unit suite, where it fails before this change and passes after.

tests/test_anthropic_messages.py  tests/test_openai_tool_passthrough.py
tests/test_tool_policy_gates.py   tests/test_tool_policy_state.py
tests/test_secure_tools_execute.py tests/test_sf_client_tools_passthrough.py
tests/test_mcp_stdio_pr5863.py
-> 510 passed

What this removes

Two behaviours change beyond the rejection, both deliberate:

  • --enable-tools no longer enables server tools on /v1/messages. The policy is veto-only on that channel now: the off direction still applies everywhere, the on direction has to come from the request. This is not a state the flag could reach anyway, because resolve_tool_policy is return True if flag is None else flag, so the explicit flag and the bare default store the same True. /v1/chat/completions is untouched and still forces the loop on. The CLI help and the tool_policy docstring have been corrected to say so.
  • A policy-only default no longer routes into the server-tool loop. Lifting the gate off the default fixed the rejection but left the execution it existed to stop: a tool-free request took the tools path with python/terminal/render_html and confirm_tool_calls unset. Both the gate and the router now call one predicate, _anthropic_selects_server_tools, since deciding "did this request select server tools" twice is what let them disagree.

mcp_enabled is deliberately not an ask here. It is one on the OpenAI routes because those consult get_enabled_mcp_tools; this route has no MCP discovery, so honouring it would answer an MCP-only request with the built-in terminal/python. The request model is extra = "allow", so the key does arrive and this is not theoretical.

Still reachable, unchanged from main

{"enable_tools": true, "permission_mode": "off"} hands the full catalog to the loop, and anthropic_messages never passes confirm_tool_calls, whose callee defaults it to False -- so needs_confirm is False on this channel in every mode. The sandbox stays on unless full or bypass_permissions. That is pre-existing and out of scope here, but it is the reason the on direction is per-request rather than a server default.

`unsloth studio run` resolves the server-side tool policy to on unless --disable-tools
(resolve_tool_policy: "tools default on for every bind", ce193c2). The permission gate added
in #7079 read that resolved policy as "this request selected server tools", so for an ordinary
tool-free POST /v1/messages it collected the whole catalog, found terminal in it, saw no
permission_mode, and returned 400. Every plain Anthropic chat request against a default server
answered:

    permission_mode 'ask' has no confirmation channel for Anthropic Messages server tools

A process-wide default is not a per-request selection. The gate now fires on an explicit ask
only, enable_tools/mcp_enabled or an Anthropic server-tool type in `tools`, and --disable-tools
still vetoes both. Everything the gate rejected because the request asked for something it
cannot confirm still rejects: enable_tools with ask or an omitted mode, a bare terminal/python
tool type, render_html, and ask for a safe-only selection.

Nothing caught it because the four e2e cases that exercise this path live in
tests/test_studio_api.py, which studio-backend-ci.yml ignores: they need a live model and a
2 GB GGUF download.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bb7b29507a

ℹ️ 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".

Comment thread studio/backend/routes/inference.py Outdated
) and not _anthropic_request_has_image(payload)
not _has_client_tool
and _policy_pre is not False
and (_asked_pre or bool(requested_studio_tools))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep gating tools enabled by the process policy

When the process tool policy is True—the default for unsloth studio run—a tool-free request makes this condition false and skips the permission rejection, but the later routing still resolves _effective_enable_tools(payload) to true (inference.py:15759-15764) and enters the server-tool loop with the full catalog, including terminal and python. That call does not enable confirm_tool_calls, so an omitted permission mode cannot pause model-emitted local calls for approval. Ordinary /v1/messages requests can therefore execute server tools without a confirmation channel; either retain the gate for policy-enabled tool routing or ensure policy alone does not enter the server-tool loop.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and reproduced: with the process policy at its unsloth studio run default, a tool-free request took the tools path with ['python', 'render_html', 'search_knowledge_base', 'terminal', 'web_search'] and confirm_tool_calls=None. Lifting the gate off the default fixed the rejection and left exactly the execution the gate existed to stop.

Fixed in d47a686 by your second suggestion: the routing no longer enters the loop on the policy alone. Both sites now call one predicate, _anthropic_selects_server_tools, since deciding "did this request select server tools" twice is what let the gate and the router disagree in the first place.

plain, policy default-on   -> plain path, tools=[]
enable_tools=true, off     -> tools path, full catalog
enable_tools=true, omitted -> HTTP 400
terminal type, full        -> tools path, tools=['terminal']
terminal type, omitted     -> HTTP 400

The test now asserts the path rather than that a response came back, and fails on the old routing with "a tool-free request took the server-tool loop on the process default alone". 592 passed across the tool-policy suites.

Lifting the gate off the process default fixed the rejection but not its twin: the tool
routing below decides server-tool mode from `_effective_enable_tools(payload)` too, so a
tool-free request on a default server still entered the local tool loop with the full
catalog, python/terminal/render_html included, and confirm_tool_calls unset. That is the
execution this channel has no way to confirm, which is what the gate existed to stop.

Deciding "did this request select server tools" twice is what let the two disagree, so both
now call `_anthropic_selects_server_tools`. A tool-free request takes the plain chat path,
an explicit enable_tools/mcp_enabled/server-tool type still routes to the loop under a mode
that permits it, and --disable-tools or enable_tools=false still vetoes.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d47a686dbe

ℹ️ 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".

Comment thread studio/backend/routes/inference.py Outdated
"""
if has_client_tool or _effective_enable_tools(payload) is False:
return False
asked = payload.enable_tools is True or bool(getattr(payload, "mcp_enabled", False))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor the forced-on tool policy for Anthropic requests

When the server is launched with unsloth studio run --enable-tools, the launcher stores True as the process policy, which is documented as forcing tools on for every request (unsloth_cli/_tool_policy.py:31-35 and studio/backend/state/tool_policy.py:8-10). This predicate only treats False specially and then requires a second per-request tool signal, so an ordinary /v1/messages request now takes the plain path even under the explicit forced-on flag. Distinguish the default-on state from an explicit forced-on policy, or otherwise preserve the flag's documented behavior while avoiding the default permission rejection.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The divergence from the documented flag is real, but restoring the behaviour is not available: honouring it reinstates the outage rather than forcing tools on.

resolve_tool_policy is return True if flag is None else flag, so --enable-tools and no flag at all both store the same True. There is no explicit-forced-on state to distinguish today. And if there were, a plain request on such a server would select terminal with permission_mode omitted, which is exactly the condition the gate above rejects, so the flag would produce a 400 rather than tools. The only way to get tools out of it is to read the flag as consent to skip confirmation, which its documentation does not claim and which bypass_permissions already exists to express.

So the flag's stated meaning and the permission gate are in direct conflict on this channel, and that predates this PR: since #7079 an --enable-tools server has answered 400 to every plain Messages request, so nothing working is being taken away here.

I have made the documentation match instead, in f372686: both CLI help strings and the tool_policy docstring now say the on direction comes from the request on /v1/messages because it has no confirmation channel, while the off direction still applies everywhere. /v1/chat/completions is unchanged and still forces the loop on, which I have left alone rather than widen this PR.

Comment thread studio/backend/routes/inference.py Outdated
"""
if has_client_tool or _effective_enable_tools(payload) is False:
return False
asked = payload.enable_tools is True or bool(getattr(payload, "mcp_enabled", False))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Resolve MCP-only requests to MCP tools

When the process policy is unset, this newly makes mcp_enabled: true enter the Anthropic server-tool path, but that path later calls _select_anthropic_server_tools(ALL_TOOLS, ...) and never loads enabled MCP schemas. Consequently an MCP-only request with the default permission mode is rejected because terminal/python are considered selected, while permission_mode: off or full exposes those built-ins instead of the requested MCP tools. The regular tool route explicitly handles this case by starting with no built-ins and appending get_enabled_mcp_tools() (inference.py:2790-2812); the Anthropic route needs equivalent MCP-aware selection before treating this flag as server-tool intent.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and my assumption that the field was inert was wrong. AnthropicMessagesRequest sets model_config = {"extra": "allow"}, and pydantic serves extras through BaseModel.__getattr__, so mcp_enabled: true does arrive:

extra stored: {'mcp_enabled': True}
getattr mcp_enabled: True

On a stock server with no CLI flags, that request went from the plain path on main to the tool loop holding ['python', 'render_html', 'search_knowledge_base', 'terminal', 'web_search'] on this branch, and anthropic_messages contains no MCP discovery at all, so an MCP-only caller got built-ins under an MCP-shaped name.

Dropped in f372686. It is an ask on the OpenAI routes because those consult get_enabled_mcp_tools; mirroring it here created the divergence rather than preventing it. Before this branch the key did nothing on this route, so dropping it restores that, and the test now pins it.

…g stops

AnthropicMessagesRequest is extra="allow", so `mcp_enabled: true` really does arrive and
pydantic serves it through __getattr__: reading it as intent routed such a request into a
loop holding ALL_TOOLS' terminal/python and not one MCP tool, since nothing on this route
loads MCP schemas. It is an ask on the OpenAI routes because those wire MCP discovery.
Before this branch it did nothing here, so dropping it restores that.

The CLI help and the tool_policy docstring said --enable-tools forces tools on for every
request. On /v1/messages it no longer does, deliberately: that channel has no confirmation
prompt, so the on direction has to come from the request. The off direction still applies
everywhere. Documented rather than left to be discovered.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: f3726860e2

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 6d0ac7c into main Aug 6, 2026
14 of 154 checks passed
@danielhanchen
danielhanchen deleted the fix/anthropic-tool-default-gate branch August 6, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant