MCP chat: at the tool-round limit the model gets one final reply with no tools from the results already found, instead of the turn ending without an answer (PR #3741 by i-Vladimir-i)
This commit is contained in:
1 parent
dc1fd367d3
commit
e0469a585f
4 files changed
+106
-12
No files matched your search
+2
-1
@@ -769,7 +769,8 @@ after a change.
|
||||
transport is not supported). `"disabled": true` leaves an entry out.
|
||||
- Servers start with Strata. The server window says what each offers or why it did not start. The Monitor tab lists them, and
|
||||
the Sampling drawer has **Use tools from MCP servers** (on by default). A server that stops later is started again at its next call.
|
||||
- In the chat each call shows as a small block. The model reads the result and goes on, up to `max_rounds` calls in a row. A tool
|
||||
- In the chat each call shows as a small block. The model reads the result and goes on, up to `max_rounds` rounds of calls (one round can hold several calls). At the limit
|
||||
the model gets one final reply with no tools, using the results already found, so the answer is not cut off. A tool
|
||||
that fails or takes longer than `timeout_s` gives the model an `error: ...` result. Results longer than `max_result_chars` are
|
||||
cut with a note. Stop stops a running tool too.
|
||||
- Only the chat page uses them. API clients see the API exactly as before and keep their own tools. A request to
|
||||
|
||||
+14
-5
@@ -5719,13 +5719,15 @@ def run_with_mcp(svc: Service, hub, messages, tools, kw, ids, thinking, max_new,
|
||||
"""Service.run with the MCP tools executed here: the model writes a call to an MCP tool, the server runs it, adds
|
||||
the call and its result to the conversation and lets the model continue - up to `max_rounds` times. Yields what
|
||||
Service.run yields (text, thinking, the request's own tool calls) plus ("mcp", {...}) for the tool activity, and
|
||||
one ("done", ...) at the very end with the output tokens of every round.
|
||||
one ("done", ...) at the very end with the output tokens of every round. After the last tool round, one final
|
||||
reply uses the results already obtained, with no tools offered or executed.
|
||||
|
||||
`mcp_names`: the MCP tools this request offered; any other call is one of the request's own tools and ends the
|
||||
turn as always (the client answers it). MCP calls written in the same answer are then not run (their results
|
||||
could not reach the model before the client's)."""
|
||||
max_rounds = int(hub.settings["max_rounds"])
|
||||
total, rounds, done = 0, 0, None
|
||||
finalizing = False
|
||||
messages = list(messages)
|
||||
while True:
|
||||
text, reasoning, calls, own_calls = [], [], [], 0
|
||||
@@ -5749,10 +5751,7 @@ def run_with_mcp(svc: Service, hub, messages, tools, kw, ids, thinking, max_new,
|
||||
reasoning.append(ev.text)
|
||||
yield kind, x
|
||||
total += done["completion_tokens"]
|
||||
run_them = calls and not own_calls and done["finish"] == "stop" and not cancel.is_set()
|
||||
if run_them and rounds >= max_rounds:
|
||||
yield "mcp", {"event": "limit", "max_rounds": max_rounds}
|
||||
run_them = False
|
||||
run_them = calls and not own_calls and not finalizing and done["finish"] == "stop" and not cancel.is_set()
|
||||
if not run_them:
|
||||
for c in calls: # announced, never run: close them in the client's view
|
||||
yield "mcp", {"event": "result", "id": c.id, "ok": False, "skipped": True, "text": "not run",
|
||||
@@ -5797,6 +5796,16 @@ def run_with_mcp(svc: Service, hub, messages, tools, kw, ids, thinking, max_new,
|
||||
**({"reasoning_content": "".join(reasoning).strip()} if reasoning else {}),
|
||||
"tool_calls": [{"function": {"name": c.name, "arguments": c.arguments}} for c in calls]})
|
||||
messages += [{"role": "tool", "content": r} for r in results]
|
||||
if rounds >= max_rounds:
|
||||
finalizing = True
|
||||
tools = None
|
||||
system_end = next((index for index, message in enumerate(messages)
|
||||
if message.get("role") not in ("system", "developer")), len(messages))
|
||||
messages.insert(system_end, {"role": "system", "content":
|
||||
f"The tool-use limit of {max_rounds} rounds has been reached. No tools are available for this final "
|
||||
"reply. Answer the user's original request in their language using the results already obtained. "
|
||||
"Explain any gaps in the available evidence. Do not request or emit further tool calls."})
|
||||
yield "mcp", {"event": "limit", "max_rounds": max_rounds}
|
||||
try:
|
||||
ids, thinking, max_new = svc.prepare(messages, tools, kw, max_req, cancel=cancel)
|
||||
except ClientGone: # #1999: the client left while a picture was prepared
|
||||
|
||||
+89
-5
@@ -365,14 +365,98 @@ class ToolLoop(unittest.TestCase):
|
||||
self.assertNotIn("y" * 501, second)
|
||||
|
||||
def test_max_rounds(self):
|
||||
self.start(call_script("fake__echo", text="again")) # the model never stops calling
|
||||
self.start(call_script("fake__echo", text="first result"),
|
||||
call_script("fake__echo", text="second result"), "</think>\n\nSummary of both results.")
|
||||
code, text = self.post({"strata_mcp": True})
|
||||
mcp = [c["strata_mcp"] for c in self.chunks(text) if "strata_mcp" in c]
|
||||
self.assertEqual(len(self.engine.prompts), 3) # 2 rounds of tools, then the limit
|
||||
self.assertEqual(code, 200, text)
|
||||
chunks = self.chunks(text)
|
||||
mcp = [chunk["strata_mcp"] for chunk in chunks if "strata_mcp" in chunk]
|
||||
self.assertEqual(len(self.engine.prompts), 3)
|
||||
self.assertEqual(sum(m["event"] == "call" for m in mcp), 2)
|
||||
self.assertIn({"event": "limit", "max_rounds": 2}, mcp)
|
||||
self.assertTrue(mcp[-1].get("skipped"))
|
||||
self.assertEqual(self.chunks(text)[-1]["choices"][0]["finish_reason"], "stop")
|
||||
self.assertFalse(any(m.get("skipped") for m in mcp))
|
||||
self.assertIn("Summary of both results.", "".join(chunk["choices"][0]["delta"].get("content", "")
|
||||
for chunk in chunks))
|
||||
final_prompt = self.engine.prompt_text(2)
|
||||
self.assertIn("<tool_response>\nfirst result\n</tool_response>", final_prompt)
|
||||
self.assertIn("<tool_response>\nsecond result\n</tool_response>", final_prompt)
|
||||
self.assertIn("No tools are available for this final reply", final_prompt)
|
||||
self.assertNotIn("<tools>", final_prompt)
|
||||
self.assertEqual(chunks[-1]["choices"][0]["finish_reason"], "stop")
|
||||
self.assertEqual(chunks[-1]["usage"]["prompt_tokens"], len(self.engine.prompts[2]))
|
||||
|
||||
def test_max_rounds_non_stream_with_existing_system(self):
|
||||
self.hub.settings["max_rounds"] = 1
|
||||
self.start(call_script("fake__add", a=2, b=3), "</think>\n\nThe result is 5.")
|
||||
code, text = self.post({"strata_mcp": True, "messages": [
|
||||
{"role": "system", "content": "Keep answers concise."},
|
||||
{"role": "developer", "content": "Use the provided evidence."},
|
||||
{"role": "user", "content": "Add 2 and 3."}]}, stream=False)
|
||||
self.assertEqual(code, 200, text)
|
||||
response = json.loads(text)
|
||||
self.assertIn("The result is 5.", response["choices"][0]["message"]["content"])
|
||||
final_prompt = self.engine.prompt_text(1)
|
||||
self.assertIn("Keep answers concise.", final_prompt)
|
||||
self.assertIn("Use the provided evidence.", final_prompt)
|
||||
self.assertIn("<tool_response>\n5\n</tool_response>", final_prompt)
|
||||
self.assertNotIn("<tools>", final_prompt)
|
||||
|
||||
def test_max_rounds_counts_rounds_with_multiple_calls(self):
|
||||
self.hub.settings["max_rounds"] = 1
|
||||
self.start(call_script("fake__echo", text="source A") + "\n" +
|
||||
call_script("fake__echo", text="source B"), "</think>\n\nBoth sources agree.")
|
||||
code, text = self.post({"strata_mcp": True})
|
||||
self.assertEqual(code, 200, text)
|
||||
chunks = self.chunks(text)
|
||||
mcp = [chunk["strata_mcp"] for chunk in chunks if "strata_mcp" in chunk]
|
||||
calls = [event for event in mcp if event["event"] == "call"]
|
||||
self.assertEqual([event["round"] for event in calls], [1, 1])
|
||||
self.assertEqual(len(self.engine.prompts), 2)
|
||||
self.assertIn("<tool_response>\nsource A\n</tool_response>", self.engine.prompt_text(1))
|
||||
self.assertIn("<tool_response>\nsource B\n</tool_response>", self.engine.prompt_text(1))
|
||||
self.assertIn("Both sources agree.", "".join(chunk["choices"][0]["delta"].get("content", "")
|
||||
for chunk in chunks))
|
||||
|
||||
def test_sixteen_rounds_then_final_answer(self):
|
||||
self.hub.settings["max_rounds"] = 16
|
||||
scripts = [call_script("fake__echo", text=f"source {index}") for index in range(16)]
|
||||
self.start(*scripts, "</think>\n\nSummary after sixteen rounds.")
|
||||
code, text = self.post({"strata_mcp": True})
|
||||
self.assertEqual(code, 200, text)
|
||||
chunks = self.chunks(text)
|
||||
mcp = [chunk["strata_mcp"] for chunk in chunks if "strata_mcp" in chunk]
|
||||
self.assertEqual(sum(event["event"] == "call" for event in mcp), 16)
|
||||
self.assertIn({"event": "limit", "max_rounds": 16}, mcp)
|
||||
self.assertEqual(len(self.engine.prompts), 17)
|
||||
final_prompt = self.engine.prompt_text(16)
|
||||
self.assertIn("<tool_response>\nsource 0\n</tool_response>", final_prompt)
|
||||
self.assertIn("<tool_response>\nsource 15\n</tool_response>", final_prompt)
|
||||
self.assertNotIn("<tools>", final_prompt)
|
||||
self.assertIn("Summary after sixteen rounds.", "".join(chunk["choices"][0]["delta"].get("content", "")
|
||||
for chunk in chunks))
|
||||
|
||||
def test_max_rounds_preserves_errors_and_disables_client_tools(self):
|
||||
self.hub.settings["max_rounds"] = 1
|
||||
own = [{"type": "function", "function": {"name": "lookup", "parameters": {"type": "object"}}}]
|
||||
self.start(call_script("fake__fail"), "</think>\n\nThe source failed; here is what is known.")
|
||||
code, text = self.post({"strata_mcp": True, "tools": own})
|
||||
self.assertEqual(code, 200, text)
|
||||
chunks = self.chunks(text)
|
||||
self.assertFalse(any(chunk["choices"][0]["delta"].get("tool_calls") for chunk in chunks))
|
||||
self.assertIn("error: it failed on purpose", self.engine.prompt_text(1))
|
||||
self.assertNotIn("<tools>", self.engine.prompt_text(1))
|
||||
self.assertIn("here is what is known", "".join(chunk["choices"][0]["delta"].get("content", "")
|
||||
for chunk in chunks))
|
||||
|
||||
def test_max_rounds_does_not_execute_more_calls(self):
|
||||
self.hub.settings["max_rounds"] = 1
|
||||
self.start(call_script("fake__echo", text="known"), call_script("fake__echo", text="must not run"))
|
||||
code, text = self.post({"strata_mcp": True})
|
||||
self.assertEqual(code, 200, text)
|
||||
self.assertEqual(len(self.engine.prompts), 2)
|
||||
mcp = [chunk["strata_mcp"] for chunk in self.chunks(text) if "strata_mcp" in chunk]
|
||||
self.assertEqual(sum(event["event"] == "call" for event in mcp), 1)
|
||||
self.assertNotIn("must not run", Path(self.log.name).read_text())
|
||||
|
||||
def test_stop_during_a_tool(self):
|
||||
"""Closing the connection while a slow tool runs stops the tool (notifications/cancelled) and the loop."""
|
||||
|
||||
+1
-1
@@ -910,7 +910,7 @@ async function send() {
|
||||
for (const t of m.tools || []) if (t.state === "writing" || t.state === "running") { t.state = "skipped"; t.ms = null; }
|
||||
const ran = (m.tools || []).filter((t) => t.state === "done" || t.state === "error").length;
|
||||
if (ran) m.meta = `${m.meta ? `${m.meta} · ` : ""}${ran} tool call${ran > 1 ? "s" : ""}`;
|
||||
if (m.limit) m.meta = `${m.meta || ""} · stopped at the limit of ${m.limit} tool rounds (mcp.max_rounds)`;
|
||||
if (m.limit) m.meta = `${m.meta || ""} · tool limit reached (${m.limit} rounds, mcp.max_rounds)`;
|
||||
busy = null;
|
||||
setBusy(false);
|
||||
if (frame) cancelAnimationFrame(frame);
|
||||
|
||||
Reference in new issue
Block a user