From fd8d37e93db253ff7c6d7ce77c21f35d1bf062da Mon Sep 17 00:00:00 2001 From: Righteous-AI <347349792+Righteous-AI@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:12:20 +0100 Subject: [PATCH] serve: accept Codex spellings of a namespace tool (#3972) A declared namespace.member tool (Codex MCP: mcp__filesystem.write_file) was accepted only as namespace__member (#3556). Qwen3.8-Flash-Next also writes namespace=member (the `, `` - or `` instead of ``) is read as the call when NAME is a declared tool. +- A tool call counts only when it names a tool the request declared. A namespaced tool (`mcp__filesystem.write_file`) is + that call when the model writes `mcp__filesystem__write_file`, `mcp__filesystem=write_file`, `mcp__filesystem_write_file`, + or the bare member (`write_file`) and exactly one declared tool has that member. Any other call, or any call in a request + that declared no tools, comes back as the text it was written as. A call whose envelope drifted by a token (`<=NAME>`, + `` or `` instead of ``) is read as the call when NAME is a declared tool. - JSON output: `response_format: {"type":"json_object"}` or `{"type":"json_schema", ...}` on `/v1/chat/completions`. The schema must accept only JSON objects at its root (`"type":"object"`, or `anyOf`/`oneOf` of object schemas). Local `#` references work and remote ones are refused. This is schema prompting followed by server validation, not grammar-constrained diff --git a/serve/frontend.py b/serve/frontend.py index a70494f0..0d00ded4 100644 --- a/serve/frontend.py +++ b/serve/frontend.py @@ -923,14 +923,18 @@ class OutputParser: # the client has no tool to run). The Responses route leaves it off: a Codex compaction request keeps its # conversation's tools in the prompt only, and its calls come back as before. self.declared_only = declared_only - # PR #3556: the native namespace__member spelling of a declared namespace.member tool is accepted without advertising a - # duplicate tool. Only an unambiguous alias counts (two namespaces can end in the same member), and an exactly declared + # A declared namespace.member tool is also accepted as namespace__member (PR #3556), + # namespace=member (Qwen writes the ` blocks that opened like a call and were returned as text diff --git a/serve/test_tool_name_alias.py b/serve/test_tool_name_alias.py new file mode 100644 index 00000000..1ceda8b8 --- /dev/null +++ b/serve/test_tool_name_alias.py @@ -0,0 +1,197 @@ +"""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 , , +and later a bare . 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 = ( + "\n\n\n/tmp/x.py\n\n" + "\nprint(\"not used\")\n\n\n" + ) + 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 = ( + "\n\n\n/tmp/a.txt\n\n" + "\nsay \"hi\"\n\n\n" + ) + 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 = ( + "\n\n\n/tmp/patch-tencut.py\n\n" + "\nprint(\"ten\")\n\n\n" + ) + 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 = "\n\n\n" + 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 = ( + "\n\n\n/tmp/z\n\n" + "\nok\n\n\n" + ) + 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 = ( + "\n\n\n/tmp/x.py\n\n" + "\nok\n\n\n" + ) + evs = events(raw + "\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 = "\n\n\n" + 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 = "\n\n\n/tmp/x\n\n\n" + 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 = "\n\n\n/tmp/x\n\n\n" + 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 = ("\n\n\n/tmp/x\n\n" + "\n") + 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]) + + +if __name__ == "__main__": + unittest.main()