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: keep deferred MCP tools in place when a listed tool runs again (#17326)

oobaboogaf12cd4b8fcb2
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 69977bc2160..fa8be61e064 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -12522,9 +12522,11 @@ _MCP_LISTING_THREAD: ContextVar = ContextVar("mcp_listing_thread", default = Non
_MCP_LISTING_QUERY: ContextVar = ContextVar("mcp_listing_query", default = "")
_MCP_LISTING_FORCED: ContextVar = ContextVar("mcp_listing_forced", default = None)
_MCP_LISTING_IMAGE: ContextVar = ContextVar("mcp_listing_image", default = False)
+_MCP_LISTING_REMEMBER: ContextVar = ContextVar("mcp_listing_remember", default = True)
# (account, thread) -> MCP tools the thread ran while they were deferred, oldest first.
_MCP_LOADED_THREADS: "OrderedDict[tuple, list[str]]" = OrderedDict()
-_MCP_RANKED_THREADS: "OrderedDict[tuple, list[str]]" = OrderedDict()
+# (account, thread) -> (held ranked tools, the last listing's order).
+_MCP_HELD_THREADS: "OrderedDict[tuple, tuple[list[str], list[str]]]" = OrderedDict()
_MCP_LOADED_LOCK = threading.Lock()
# Digest of the catalog's searchable text -> its in-memory FTS5 index, rebuilt only when that text changes.
_MCP_SEARCH_INDEXES: "OrderedDict[str, sqlite3.Connection]" = OrderedDict()
@@ -12537,11 +12539,13 @@ def set_mcp_listing_context_tokens(
messages = None,
tool_choice = None,
mcp_image = None,
+ remember = True,
) -> None:
"""The local window the next MCP listing in this context is sized against; unset lists every tool in full.
``thread_id`` keeps the tools a chat ran listed; ``messages`` ranks the deferred ones listed up front; a
``tool_choice`` forcing a tool keeps that one listed, and an attached image (in the message or, sent to MCP tools
- only, as ``mcp_image``) keeps the tools mapped to take it listed."""
+ only, as ``mcp_image``) keeps the tools mapped to take it listed. ``remember = False`` (token counts) reads the
+ held listing without replacing it."""
from core.inference.chat_template_helpers import forced_tool_name, last_user_text # noqa: PLC0415
valid = isinstance(context_tokens, int) and context_tokens > 0
@@ -12550,6 +12554,7 @@ def set_mcp_listing_context_tokens(
dicts = [m.model_dump() if hasattr(m, "model_dump") else m for m in messages or []]
_MCP_LISTING_QUERY.set(last_user_text(dicts) if valid else "")
_MCP_LISTING_FORCED.set(forced_tool_name(tool_choice))
+ _MCP_LISTING_REMEMBER.set(bool(remember))
last_user = next(
(m for m in reversed(dicts) if isinstance(m, dict) and m.get("role") == "user"), None
)
@@ -12751,23 +12756,24 @@ def mcp_record_loaded_tools(thread_id, names) -> None:
_MCP_LOADED_THREADS.popitem(last = False)
-def _mcp_held_ranking(by_name: dict) -> list[str]:
+def _mcp_held(by_name: dict) -> tuple[list[str], list[str]]:
key = _mcp_thread_key()
if key is None:
- return []
+ return [], []
with _MCP_LOADED_LOCK:
- return [n for n in _MCP_RANKED_THREADS.get(key, ()) if n in by_name]
+ ranked, listed = _MCP_HELD_THREADS.get(key, ((), ()))
+ return [n for n in ranked if n in by_name], list(listed)
-def _mcp_hold_ranking(names: list[str]) -> None:
+def _mcp_hold(ranked: list[str], listed: list[str]) -> None:
key = _mcp_thread_key()
- if key is None:
+ if key is None or not _MCP_LISTING_REMEMBER.get():
return
with _MCP_LOADED_LOCK:
- _MCP_RANKED_THREADS.pop(key, None)
- _MCP_RANKED_THREADS[key] = names
- while len(_MCP_RANKED_THREADS) > _MCP_LOADED_THREADS_MAX:
- _MCP_RANKED_THREADS.popitem(last = False)
+ _MCP_HELD_THREADS.pop(key, None)
+ _MCP_HELD_THREADS[key] = (ranked, listed)
+ while len(_MCP_HELD_THREADS) > _MCP_LOADED_THREADS_MAX:
+ _MCP_HELD_THREADS.popitem(last = False)
def _mcp_listing(listed: list[tuple[dict, list[dict], list[dict]]]) -> list[dict]:
@@ -12837,7 +12843,7 @@ def _mcp_listing(listed: list[tuple[dict, list[dict], list[dict]]]) -> list[dict
left -= cost
return chosen
- held = _mcp_held_ranking(by_name)
+ held, listed_before = _mcp_held(by_name)
chosen = filled(held)
held = [name for name in held if by_name[name] in chosen]
if ranked and (not held or by_name[ranked[0]] not in chosen):
@@ -12845,8 +12851,11 @@ def _mcp_listing(listed: list[tuple[dict, list[dict], list[dict]]]) -> list[dict
# A best match too big to list is not worth dropping the held tools for.
if not held or by_name[ranked[0]] in fresh:
chosen, held = fresh, [name for name in ranked if by_name[name] in fresh]
- _mcp_hold_ranking(held)
- return chosen
+ # Listed tools keep their places, new ones go last: a reorder in the tool block reprocesses the whole chat.
+ names = [spec["function"]["name"] for spec in chosen[1:]]
+ order = list(dict.fromkeys([name for name in listed_before if name in names] + names))
+ _mcp_hold(held, order)
+ return chosen[:1] + [by_name[name] for name in order]
def _cached_mcp_catalog() -> list[dict]:
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index f1f99811fd2..55bb17c4e54 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -42577,6 +42577,7 @@ async def _mlx_count_chat_tokens(
payload.messages,
getattr(payload, "tool_choice", None),
getattr(payload, "mcp_image", None),
+ remember = False,
)
# A database read, so off the loop; guarded like the GGUF count, or an interleaving edit pairs a new row with a stale schema
@@ -43022,6 +43023,7 @@ async def chat_count_tokens(
payload.messages,
getattr(payload, "tool_choice", None),
getattr(payload, "mcp_image", None),
+ remember = False,
)
from core.inference.mcp_client import mcp_server_snapshot_guard
diff --git a/studio/backend/tests/test_mcp_servers.py b/studio/backend/tests/test_mcp_servers.py
index bf117a78c31..0d370f9bb8b 100644
--- a/studio/backend/tests/test_mcp_servers.py
+++ b/studio/backend/tests/test_mcp_servers.py
@@ -2172,7 +2172,7 @@ def _cache_server_tools(tmp_path, monkeypatch, tools):
_reset_db(tmp_path, monkeypatch)
monkeypatch.setattr(mcp_client, "_tool_cache", {})
monkeypatch.setattr(tools_mod, "_MCP_LOADED_THREADS", OrderedDict())
- monkeypatch.setattr(tools_mod, "_MCP_RANKED_THREADS", OrderedDict())
+ monkeypatch.setattr(tools_mod, "_MCP_HELD_THREADS", OrderedDict())
mcp_servers_db.create_server(id = "srv1", display_name = "A", url = "https://a.example/mcp")
mcp_client.cache_tools("srv1", tools)
@@ -2500,6 +2500,60 @@ def test_mcp_listing_reranks_when_no_held_tool_still_fits(tmp_path, monkeypatch,
assert _thread_listing(tools_mod, searching, chat) == second
+def test_mcp_listing_keeps_its_order_when_a_listed_tool_runs(tmp_path, monkeypatch, searching):
+ from core.inference import tools as tools_mod
+
+ _cache_server_tools(tmp_path, monkeypatch, _NOTES)
+ ping, create, move = (f"mcp__srv1__{name}" for name in ("ping", "create-pages", "move-pages"))
+ names = [spec["function"]["name"] for spec in tools_mod._cached_mcp_catalog()]
+ monkeypatch.setattr(
+ tools_mod, "_mcp_ranked", lambda specs, query: ([names.index(ping), names.index(create)], 0)
+ )
+ chat = [{"role": "user", "content": "first"}]
+ first = _thread_listing(tools_mod, searching, chat)
+ assert _names(first) == ["mcp_tool_search", ping, create]
+
+ for ran in ([create], [ping], [create, ping]):
+ tools_mod.mcp_record_loaded_tools("thread-1", ran)
+ assert _thread_listing(tools_mod, searching, chat) == first
+
+ tools_mod.mcp_record_loaded_tools("thread-1", [move])
+ assert _names(_thread_listing(tools_mod, searching, chat)) == [*_names(first), move]
+
+
+def test_mcp_listing_token_count_leaves_the_held_listing_alone(tmp_path, monkeypatch, searching):
+ import contextvars
+
+ from core.inference import tools as tools_mod
+
+ _cache_server_tools(tmp_path, monkeypatch, _NOTES)
+ a, b, c = (f"mcp__srv1__{name}" for name in ("ping", "create-pages", "move-pages"))
+ names = [spec["function"]["name"] for spec in tools_mod._cached_mcp_catalog()]
+ rankings = {"ab": [a, b], "cb": [c, b]}
+ monkeypatch.setattr(
+ tools_mod,
+ "_mcp_ranked",
+ lambda specs, query: ([names.index(name) for name in rankings[query]], 0),
+ )
+
+ def count(messages):
+ ctx = contextvars.copy_context()
+ ctx.run(
+ tools_mod.set_mcp_listing_context_tokens,
+ searching,
+ "thread-1",
+ messages,
+ remember = False,
+ )
+ return ctx.run(lambda: _names(tools_mod.cached_mcp_tools()[0]))
+
+ chat = [{"role": "user", "content": "ab"}]
+ first = _thread_listing(tools_mod, searching, chat)
+ assert _names(first) == ["mcp_tool_search", a, b]
+ assert count([{"role": "user", "content": "cb"}]) == ["mcp_tool_search", b, c]
+ assert _thread_listing(tools_mod, searching, chat) == first
+
+
def test_mcp_listing_holds_when_the_best_match_cannot_fit(tmp_path, monkeypatch, searching):
from core.inference import tools as tools_mod