Problem: a request declares tools; the model writes a complete call for a name that is not among them (bash for shell, a dropped prefix, an MCP name with every "__" written as "_"). Since #3075 such a call stays text, which is the rule we keep, but the client saw an ordinary answer with finish_reason stop and nothing else, so the agent turn ended with raw XML and the model never learned of its mistake. Reporters on OpenCode, Pithagoras, OpenClaw, LibreChat, Open WebUI and Pi hit it. What changed (declared-only stays: nothing undeclared is ever returned as a call, finish_reason is unchanged): - serve/frontend.py OutputParser: a declared name that contains "__" is also read when written with every "__" as "_" (mcp_playwright_browser_navigate for mcp__playwright__browser_navigate); the call goes to the client under the declared name. Same rules as the other aliases: only when unambiguous, and a tool declared under that spelling wins. A reordered or renamed tool (server1_label_values for label_values_mcp_server1, bash for shell) is not guessed. - serve/server.py: when a block went back as text for an undeclared name, the reply carries "strata_rejected_tool_calls": [{"name": ..., "reason": "undeclared"}] (the names the parser already collected, up to five): at the top level of a Chat Completions reply and in its last streamed chunk, and in the message_delta event and the message of /v1/messages. It is absent when nothing was rejected, so every other reply is unchanged. The text itself is untouched. - The server window line says the response carries the names; the count goes to totals["tool_calls_unread"] and to /metrics/prometheus as strata:tool_calls_unread_total. - docs/DETAILS.md: the alias, the marker, the counter. Tests: serve/test_tool_name_alias.py (collapsed spelling at several chunk widths, a declared spelling and a plain tool unchanged, a tool declared in the collapsed spelling wins, two tools with one collapsed spelling are not guessed, reordered/renamed names stay text and are named), serve/test_server.py ForcedToolChoice (the field in Chat Completions whole and streamed and in /v1/messages, absent for an ordinary answer and a declared call, the collapsed name is a call; the counter). The whole serve/ suite: 1055 tests, two failures that also fail on the base tree (test_fatal_recovery on this PC, RestartWindow.test_not_alive_until_ready under load). Not covered: returning an undeclared call as a real tool call (the owner keeps declared-only); a block for a DECLARED name that came back as text because it was malformed (counted, no name to list); the /v1/messages field with a real client; a stderr or log-file copy of the warning.
262 lines
13 KiB
Python
262 lines
13 KiB
Python
"""Codex MCP names that are not the declared namespace.member form.
|
|
|
|
Observed with Qwen3.8-Flash-Next through Codex CLI against Strata v0.2.2. The model
|
|
wrote <function=mcp__filesystem=write_file>, <function=mcp__filesystem_edit_file>,
|
|
and later a bare <function=write_file>. The declared tool was
|
|
mcp__filesystem.write_file. Stock main already accepts namespace__member. The
|
|
equals sign, the dropped underscore, and a unique bare member have to as well.
|
|
A name that matches nothing stays text (#3075). Two tools that share a member
|
|
are not guessed.
|
|
|
|
python -m unittest serve.test_tool_name_alias -v
|
|
"""
|
|
import json
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
from serve.frontend import OutputParser # noqa: E402
|
|
from serve.responses import Assembler, request_tools # noqa: E402
|
|
|
|
REQ = {
|
|
"tools": [{
|
|
"type": "namespace",
|
|
"name": "mcp__filesystem",
|
|
"tools": [
|
|
{"type": "function", "name": "write_file", "parameters": {
|
|
"type": "object",
|
|
"properties": {"path": {"type": "string"}, "content": {"type": "string"}},
|
|
"required": ["path", "content"]}},
|
|
{"type": "function", "name": "edit_file", "parameters": {
|
|
"type": "object",
|
|
"properties": {"path": {"type": "string"}, "edits": {"type": "string"}},
|
|
"required": ["path", "edits"]}},
|
|
{"type": "function", "name": "list_allowed_directories", "parameters": {
|
|
"type": "object", "properties": {}}},
|
|
],
|
|
}],
|
|
}
|
|
|
|
|
|
def events(text, tools, thinking=False):
|
|
parser = OutputParser(thinking=thinking, tools=tools, stream_tools=True)
|
|
evs = parser.feed(text)
|
|
evs += parser.finish("stop")
|
|
return evs
|
|
|
|
|
|
def text_of(evs):
|
|
return "".join(e.text or "" for e in evs if e.kind in ("content", "reasoning"))
|
|
|
|
|
|
def response_items(text, tools, names, req=REQ):
|
|
asm = Assembler(req, "qwen", 10, names, encrypted=False)
|
|
out = []
|
|
parser_events = events(text, tools)
|
|
for ev in parser_events:
|
|
out += asm.feed(ev)
|
|
out += asm.finish({"finish": "stop", "completion_tokens": 1, "prompt_tokens": 10})
|
|
items = [ev["item"] for ev in out if ev.get("type") == "response.output_item.done"]
|
|
return items, parser_events
|
|
|
|
|
|
class DriftedMcpNames(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tools, self.names, _skipped = request_tools(REQ)
|
|
|
|
def _call(self, text, tools=None, names=None):
|
|
items, evs = response_items(text, tools or self.tools, names or self.names)
|
|
calls = [i for i in items if i["type"] == "function_call"]
|
|
return calls, evs
|
|
|
|
def test_equals_write_is_the_namespace_tool(self):
|
|
raw = (
|
|
"<tool_call>\n<function=mcp__filesystem=write_file>\n<parameter=path>\n/tmp/x.py\n</parameter>\n"
|
|
"<parameter=content>\nprint(\"not used\")\n</parameter>\n</function>\n</tool_call>"
|
|
)
|
|
calls, evs = self._call(raw)
|
|
self.assertEqual(text_of(evs).strip(), "")
|
|
self.assertEqual(len(calls), 1)
|
|
self.assertEqual((calls[0]["namespace"], calls[0]["name"]), ("mcp__filesystem", "write_file"))
|
|
args = json.loads(calls[0]["arguments"])
|
|
self.assertEqual(args["path"], "/tmp/x.py")
|
|
self.assertIn('print("not used")', args["content"])
|
|
|
|
def test_single_underscore_edit_keeps_a_string_argument(self):
|
|
raw = (
|
|
"<tool_call>\n<function=mcp__filesystem_edit_file>\n<parameter=path>\n/tmp/a.txt\n</parameter>\n"
|
|
"<parameter=edits>\nsay \"hi\"\n</parameter>\n</function>\n</tool_call>"
|
|
)
|
|
calls, evs = self._call(raw)
|
|
self.assertEqual(text_of(evs).strip(), "")
|
|
self.assertEqual((calls[0]["namespace"], calls[0]["name"]), ("mcp__filesystem", "edit_file"))
|
|
args = json.loads(calls[0]["arguments"])
|
|
self.assertEqual(args["path"], "/tmp/a.txt")
|
|
self.assertIsInstance(args["edits"], str)
|
|
self.assertIn('"hi"', args["edits"])
|
|
|
|
def test_bare_write_file_is_the_one_namespace_tool(self):
|
|
raw = (
|
|
"<tool_call>\n<function=write_file>\n<parameter=path>\n/tmp/patch-tencut.py\n</parameter>\n"
|
|
"<parameter=content>\nprint(\"ten\")\n</parameter>\n</function>\n</tool_call>"
|
|
)
|
|
calls, evs = self._call(raw)
|
|
self.assertEqual(text_of(evs).strip(), "")
|
|
self.assertEqual((calls[0]["namespace"], calls[0]["name"]), ("mcp__filesystem", "write_file"))
|
|
args = json.loads(calls[0]["arguments"])
|
|
self.assertEqual(args["path"], "/tmp/patch-tencut.py")
|
|
self.assertEqual(args["content"], 'print("ten")')
|
|
|
|
def test_bare_list_allowed_directories_is_a_call(self):
|
|
raw = "<tool_call>\n<function=list_allowed_directories>\n</function>\n</tool_call>"
|
|
calls, evs = self._call(raw)
|
|
self.assertEqual(text_of(evs).strip(), "")
|
|
self.assertEqual((calls[0]["namespace"], calls[0]["name"]),
|
|
("mcp__filesystem", "list_allowed_directories"))
|
|
self.assertEqual(json.loads(calls[0]["arguments"]), {})
|
|
|
|
def test_double_underscore_still_maps(self):
|
|
raw = (
|
|
"<tool_call>\n<function=mcp__filesystem__write_file>\n<parameter=path>\n/tmp/z\n</parameter>\n"
|
|
"<parameter=content>\nok\n</parameter>\n</function>\n</tool_call>"
|
|
)
|
|
calls, _evs = self._call(raw)
|
|
self.assertEqual((calls[0]["namespace"], calls[0]["name"]), ("mcp__filesystem", "write_file"))
|
|
self.assertEqual(json.loads(calls[0]["arguments"])["content"], "ok")
|
|
|
|
def test_equals_inside_thinking_is_still_a_call(self):
|
|
raw = (
|
|
"<tool_call>\n<function=mcp__filesystem=write_file>\n<parameter=path>\n/tmp/x.py\n</parameter>\n"
|
|
"<parameter=content>\nok\n</parameter>\n</function>\n</tool_call>"
|
|
)
|
|
evs = events(raw + "</think>\n", self.tools, thinking=True)
|
|
found = [e for e in evs if e.kind == "tool_call"]
|
|
self.assertEqual(len(found), 1)
|
|
self.assertEqual(found[0].call.arguments["path"], "/tmp/x.py")
|
|
|
|
def test_an_unknown_name_stays_text(self):
|
|
raw = "<tool_call>\n<function=mcp__filesystem=not_a_tool>\n</function>\n</tool_call>"
|
|
calls, evs = self._call(raw)
|
|
self.assertEqual(calls, [])
|
|
self.assertIn("not_a_tool", text_of(evs))
|
|
|
|
def test_two_namespaces_sharing_a_member_stay_text(self):
|
|
req = {"tools": [
|
|
{"type": "namespace", "name": "mcp__filesystem", "tools": [
|
|
{"type": "function", "name": "write_file", "parameters": {
|
|
"type": "object", "properties": {"path": {"type": "string"}}}}]},
|
|
{"type": "namespace", "name": "mcp__other", "tools": [
|
|
{"type": "function", "name": "write_file", "parameters": {
|
|
"type": "object", "properties": {"path": {"type": "string"}}}}]},
|
|
]}
|
|
tools, names, _ = request_tools(req)
|
|
raw = "<tool_call>\n<function=write_file>\n<parameter=path>\n/tmp/x\n</parameter>\n</function>\n</tool_call>"
|
|
calls, evs = self._call(raw, tools, names)
|
|
self.assertEqual(calls, [])
|
|
self.assertIn("write_file", text_of(evs))
|
|
|
|
def test_a_flat_tool_with_the_member_name_wins(self):
|
|
req = {"tools": [
|
|
{"type": "function", "name": "write_file", "parameters": {
|
|
"type": "object", "properties": {"path": {"type": "string"}}}},
|
|
{"type": "namespace", "name": "mcp__filesystem", "tools": [
|
|
{"type": "function", "name": "write_file", "parameters": {
|
|
"type": "object", "properties": {"path": {"type": "string"},
|
|
"content": {"type": "string"}}}}]},
|
|
]}
|
|
tools, names, _ = request_tools(req)
|
|
self.assertEqual(names["write_file"], (None, "write_file", "function"))
|
|
raw = "<tool_call>\n<function=write_file>\n<parameter=path>\n/tmp/x\n</parameter>\n</function>\n</tool_call>"
|
|
calls, evs = self._call(raw, tools, names)
|
|
self.assertEqual(text_of(evs).strip(), "")
|
|
self.assertEqual(calls[0]["name"], "write_file")
|
|
self.assertNotIn("namespace", calls[0])
|
|
|
|
|
|
def test_a_flat_tool_named_like_the_underscore_alias_wins(self):
|
|
req = {"tools": [
|
|
{"type": "function", "name": "mcp__filesystem_write_file", "parameters": {
|
|
"type": "object", "properties": {"path": {"type": "string"}}}},
|
|
{"type": "namespace", "name": "mcp__filesystem", "tools": [
|
|
{"type": "function", "name": "write_file", "parameters": {
|
|
"type": "object", "properties": {"path": {"type": "string"},
|
|
"content": {"type": "string"}}}}]},
|
|
]}
|
|
tools, names, _ = request_tools(req)
|
|
raw = ("<tool_call>\n<function=mcp__filesystem_write_file>\n<parameter=path>\n/tmp/x\n</parameter>\n</function>"
|
|
"\n</tool_call>")
|
|
calls, evs = self._call(raw, tools, names)
|
|
self.assertEqual(text_of(evs).strip(), "")
|
|
self.assertEqual(calls[0]["name"], "mcp__filesystem_write_file")
|
|
self.assertNotIn("namespace", calls[0])
|
|
|
|
|
|
def plain_tool(name):
|
|
return {"name": name, "parameters": {"type": "object", "properties": {"url": {"type": "string"}}}}
|
|
|
|
|
|
def call_text(name, param="url", value="https://example.com"):
|
|
return f"<tool_call>\n<function={name}>\n<parameter={param}>\n{value}\n</parameter>\n</function>\n</tool_call>"
|
|
|
|
|
|
def run_parser(text, tools, width=0):
|
|
parser = OutputParser(thinking=False, tools=tools, stream_tools=True, declared_only=True)
|
|
evs = []
|
|
step = width or len(text) or 1
|
|
for k in range(0, len(text), step):
|
|
evs += parser.feed(text[k:k + step])
|
|
evs += parser.finish("stop")
|
|
return evs, parser
|
|
|
|
|
|
class CollapsedUnderscoreNames(unittest.TestCase):
|
|
"""#3796: `mcp__server__tool` written as `mcp_server_tool` is a spelling of the declared tool (the same class of
|
|
difference as namespace__member). A name that is only similar stays text, and is named for the client."""
|
|
MCP = [plain_tool("mcp__playwright__browser_navigate"), plain_tool("mcp__playwright__browser_snapshot"),
|
|
plain_tool("mcp__playwright__browser_tabs"), plain_tool("todo_write")]
|
|
|
|
def test_the_collapsed_spelling_is_the_declared_tool(self):
|
|
for name in ("browser_navigate", "browser_snapshot", "browser_tabs"):
|
|
for width in (0, 1, 5):
|
|
with self.subTest(name=name, width=width):
|
|
evs, parser = run_parser("Opening it.\n" + call_text("mcp_playwright_" + name), self.MCP, width)
|
|
found = [e.call for e in evs if e.kind == "tool_call"]
|
|
self.assertEqual([c.name for c in found], ["mcp__playwright__" + name]) # the client gets the name it declared
|
|
self.assertEqual(found[0].arguments, {"url": "https://example.com"})
|
|
self.assertEqual(text_of(evs).strip(), "Opening it.")
|
|
self.assertEqual((parser.unread, parser.unread_names), (0, []))
|
|
|
|
def test_the_declared_spelling_and_a_plain_tool_are_unchanged(self):
|
|
evs, _ = run_parser(call_text("mcp__playwright__browser_tabs") + "\n" + call_text("todo_write"), self.MCP)
|
|
self.assertEqual([e.call.name for e in evs if e.kind == "tool_call"], ["mcp__playwright__browser_tabs", "todo_write"])
|
|
|
|
def test_a_tool_declared_in_the_collapsed_spelling_wins(self):
|
|
tools = self.MCP + [plain_tool("mcp_playwright_browser_tabs")]
|
|
evs, _ = run_parser(call_text("mcp_playwright_browser_tabs"), tools)
|
|
self.assertEqual([e.call.name for e in evs if e.kind == "tool_call"], ["mcp_playwright_browser_tabs"])
|
|
|
|
def test_two_tools_with_the_same_collapsed_spelling_are_not_guessed(self):
|
|
tools = [plain_tool("a__b_c"), plain_tool("a_b__c")]
|
|
evs, parser = run_parser(call_text("a_b_c"), tools)
|
|
self.assertEqual([e for e in evs if e.kind == "tool_call"], [])
|
|
self.assertIn("a_b_c", text_of(evs))
|
|
self.assertEqual(parser.unread_names, ["a_b_c"])
|
|
|
|
def test_a_reordered_or_renamed_tool_stays_text_and_is_named(self):
|
|
"""The LibreChat case (`server1_label_values` for `label_values_mcp_server1`) and the bash/shell case: no guessing."""
|
|
for declared, written in (("label_values_mcp_server1", "server1_label_values"), ("shell", "bash"),
|
|
("godot_execute_gdscript", "execute_gdscript")):
|
|
for width in (0, 3):
|
|
with self.subTest(written=written, width=width):
|
|
raw = "Done.\n" + call_text(written)
|
|
evs, parser = run_parser(raw, [plain_tool(declared)], width)
|
|
self.assertEqual([e for e in evs if e.kind == "tool_call"], [])
|
|
self.assertEqual(text_of(evs), raw) # the text is exactly what the model wrote
|
|
self.assertEqual((parser.unread, parser.unread_names), (1, [written]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|