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: let an MCP call run again after other work changed state (#13287)

Daniel Han0006f0e8f73c
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 0ea20519050..4046d013f3e 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -478,8 +478,8 @@ from core.inference.mcp_images import (
)
from core.inference.tool_loop_controller import (
_WORKSPACE_READ_TOOLS,
- _WORKSPACE_TOOLS,
ToolLoopController,
+ _reruns_after_new_work,
append_deferred_nudges,
awaiting_approval_status,
deferred_nudge_text,
@@ -39730,7 +39730,7 @@ class LlamaCppBackend:
for _tc in tool_calls if deduplicate_tool_calls else ():
_fn = _tc.get("function", {}) or {}
_key = (_fn.get("name", ""), str(_fn.get("arguments", "")))
- if _fn.get("name") in _WORKSPACE_TOOLS:
+ if _reruns_after_new_work(_fn.get("name")):
if _key == _last_workspace_key:
continue
if _key in _seen_keys:
diff --git a/studio/backend/core/inference/safetensors_agentic.py b/studio/backend/core/inference/safetensors_agentic.py
index b4a351c0f53..50f7839758d 100644
--- a/studio/backend/core/inference/safetensors_agentic.py
+++ b/studio/backend/core/inference/safetensors_agentic.py
@@ -63,8 +63,8 @@ from core.inference.mcp_images import (
)
from core.inference.tool_loop_controller import (
_WORKSPACE_READ_TOOLS,
- _WORKSPACE_TOOLS,
ToolLoopController,
+ _reruns_after_new_work,
append_deferred_nudges,
awaiting_approval_status,
coerce_tool_arguments,
@@ -1433,7 +1433,7 @@ def run_safetensors_tool_loop(
for _tc in tool_calls if deduplicate_tool_calls else ():
_fn = _tc.get("function", {}) or {}
_key = (_fn.get("name", ""), str(_fn.get("arguments", "")))
- if _fn.get("name") in _WORKSPACE_TOOLS:
+ if _reruns_after_new_work(_fn.get("name")):
if _key == last_workspace_key:
continue
if _key in seen_keys:
diff --git a/studio/backend/core/inference/tool_loop_controller.py b/studio/backend/core/inference/tool_loop_controller.py
index 0b0482cee93..95cd1d11946 100644
--- a/studio/backend/core/inference/tool_loop_controller.py
+++ b/studio/backend/core/inference/tool_loop_controller.py
@@ -995,6 +995,11 @@ _WORKSPACE_READ_TOOLS = frozenset({"view_image"})
_WORKSPACE_TOOLS = _SANDBOX_TOOLS | {"edit_file"} | _WORKSPACE_READ_TOOLS
+def _reruns_after_new_work(tool_name: Any) -> bool:
+ """An MCP tool can change state too (`list, create, list`), so it follows the workspace rerun rule (#10348)."""
+ return tool_name in _WORKSPACE_TOOLS or str(tool_name or "").startswith(_MCP_TOOL_PREFIX)
+
+
# `sk-unsloth-` + 32 hex (auth/storage.py), cached in the clear so the CLI can reuse it. Masked on
# the way to the model, which is where it would leave the machine. The mask carries neither prefix,
# so re-running is a no-op.
@@ -1308,7 +1313,7 @@ class ToolLoopController:
# One rerun per piece of NEW work, not per call: `read, edit, read, edit` would
# otherwise apply the edit twice. Here as well as in the prefilters, which a
# structured batch skips. A failed command can still have written, so it counts too.
- if decision.tool_name in _WORKSPACE_TOOLS:
+ if _reruns_after_new_work(decision.tool_name):
if (
decision.tool_name not in _WORKSPACE_READ_TOOLS
and decision.key not in self._workspace_ran
@@ -1318,7 +1323,7 @@ class ToolLoopController:
stale = {
key
for key in self._successful_keys
- if key.partition(":")[0] in _WORKSPACE_TOOLS
+ if _reruns_after_new_work(key.partition(":")[0])
and self._workspace_novel_at.get(key, 0) < self._workspace_novel
}
self._successful_keys -= stale
diff --git a/studio/backend/tests/test_llama_cpp_tool_loop.py b/studio/backend/tests/test_llama_cpp_tool_loop.py
index 44c251c6091..d01b66f4e47 100644
--- a/studio/backend/tests/test_llama_cpp_tool_loop.py
+++ b/studio/backend/tests/test_llama_cpp_tool_loop.py
@@ -6910,6 +6910,36 @@ def test_textual_every_independent_edit_gets_its_own_verification_rerun(monkeypa
assert calls == ["terminal", "edit_file", "terminal", "edit_file", "terminal"]
+def test_textual_mcp_listing_runs_again_after_an_mcp_write(monkeypatch):
+ """#10348: `list, create, list` on an MCP server re-lists instead of dropping the call."""
+ name = "mcp__docs__manager"
+ listing = '<tool_call>{"name":"%s","arguments":{"action":"list"}}</tool_call>' % name
+ create = '<tool_call>{"name":"%s","arguments":{"action":"create"}}</tool_call>' % name
+ backend, _ = _backend_and_payloads(
+ monkeypatch,
+ [
+ [_sse({"content": listing + create + listing}), _done()],
+ [_sse({"content": "Done."}), _done()],
+ ],
+ )
+ calls = []
+ results = iter(["[]", "created", "[a]"])
+
+ def execute(name, arguments, **kwargs):
+ calls.append(arguments.get("action"))
+ return next(results)
+
+ monkeypatch.setattr("core.inference.tools.execute_tool", execute)
+ list(
+ backend.generate_chat_completion_with_tools(
+ messages = [{"role": "user", "content": "Create a doc"}],
+ tools = [{"type": "function", "function": {"name": name}}],
+ max_tool_iterations = 3,
+ )
+ )
+ assert calls == ["list", "create", "list"]
+
+
def _structured_batch(spec: list) -> list:
"""One assistant turn carrying `spec` as parallel structured tool_calls."""
frames = [
diff --git a/studio/backend/tests/test_safetensors_tool_loop.py b/studio/backend/tests/test_safetensors_tool_loop.py
index 5279c85aad8..c4d598d88bd 100644
--- a/studio/backend/tests/test_safetensors_tool_loop.py
+++ b/studio/backend/tests/test_safetensors_tool_loop.py
@@ -5842,6 +5842,29 @@ def test_an_alternating_workspace_block_does_not_crowd_out_a_later_tool():
]
+def test_mcp_listing_runs_again_after_an_mcp_write():
+ """#10348: `list, create, list` on an MCP server re-lists instead of dropping the call."""
+ name = "mcp__docs__manager"
+ listing = '<tool_call>{"name":"%s","arguments":{"action":"list"}}</tool_call>' % name
+ create = '<tool_call>{"name":"%s","arguments":{"action":"create"}}</tool_call>' % name
+ turns = iter([listing + create + listing, "Done."])
+
+ def single_turn(messages, **kwargs):
+ yield next(turns)
+
+ executor = FakeExecuteTool(["[]", "created", "[a]"])
+ _collect_events(
+ run_safetensors_tool_loop(
+ single_turn = single_turn,
+ messages = [{"role": "user", "content": "Create a doc"}],
+ tools = [{"type": "function", "function": {"name": name}}],
+ execute_tool = executor,
+ max_tool_iterations = 3,
+ )
+ )
+ assert [args.get("action") for _, args in executor.calls] == ["list", "create", "list"]
+
+
def test_every_independent_edit_gets_its_own_verification_rerun():
"""Two edit-and-verify cycles in one turn: the test after the second edit must run."""
test = '<tool_call>{"name":"terminal","arguments":{"command":"pytest -q"}}</tool_call>'
diff --git a/studio/backend/tests/test_tool_loop_controller.py b/studio/backend/tests/test_tool_loop_controller.py
index 8b4ec86abf1..32f923da732 100644
--- a/studio/backend/tests/test_tool_loop_controller.py
+++ b/studio/backend/tests/test_tool_loop_controller.py
@@ -364,6 +364,23 @@ def test_a_repeating_workspace_block_stops_replaying_itself():
assert controller.prepare_call(edit).action == "duplicate"
+def test_mcp_call_can_run_again_after_another_mcp_call_changed_state():
+ """#10348: `list, create, list` on an MCP server must re-list, not answer from a stale duplicate."""
+ name = "mcp__docs__unified_document_manager"
+ controller = ToolLoopController(tools = [_tool(name)])
+ listing = _call(name, {"action": "list"})
+ create = _call(name, {"action": "create", "name": "a.md"})
+
+ controller.record_result(controller.prepare_call(listing), "[]")
+ assert controller.prepare_call(listing).action == "duplicate"
+ controller.record_result(controller.prepare_call(create), "created a.md")
+ relist = controller.prepare_call(listing)
+ assert relist.action == "execute"
+ controller.record_result(relist, '["a.md"]')
+ assert controller.prepare_call(listing).action == "duplicate"
+ assert controller.prepare_call(create).action == "duplicate"
+
+
def test_failed_call_does_not_block_retry():
controller = ToolLoopController(tools = [_tool("web_search")])
first = controller.prepare_call(_call("web_search", {"query": "gpu prices"}))