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 <function= tag's equals sign ends up in the name), namespace_member (one underscore dropped) and the bare member. Each is now an alias of the declared tool when exactly one declared tool produces it and no declared name is the same; a name that matches nothing stays text (#3075), two namespaces that share a member are not guessed, and a declared flat tool always wins. Requests without namespaced tools are unchanged. serve/test_tool_name_alias.py (10 tests, 5 of the first 9 fail without the change; the tenth pins that a flat tool named like the underscore alias wins), docs/DETAILS.md names the spellings. Ported from PR #3972.
This commit is contained in:
1 parent
f8ff3420d9
commit
fd8d37e93d
3 files changed
+209
-6
No files matched your search
+5
-3
@@ -481,9 +481,11 @@ applies at the next start. Network, key, MCP and program keys are not editable t
|
||||
- With `"stream": true` everything arrives as it is made: thinking, answer and tool calls (the tool's name first, then its
|
||||
arguments piece by piece). While the model reads a long prompt the stream sends keep-alives. The server window prints progress
|
||||
every 15 s and `GET /status` says what it is doing. Closing the connection or pressing stop in your app stops the model.
|
||||
- A tool call counts only when it names a tool the request declared. 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>`, `<function:NAME>`
|
||||
or `<NAME>` instead of `<function=NAME>`) 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>`,
|
||||
`<function:NAME>` or `<NAME>` instead of `<function=NAME>`) 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
|
||||
|
||||
+7
-3
@@ -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 <function= tag's equals sign into the name),
|
||||
# namespace_member (one underscore dropped), and the bare member when exactly one
|
||||
# declared tool has it. Only an unambiguous alias counts, and an exactly declared
|
||||
# name always wins over an alias.
|
||||
aliases = {}
|
||||
for name in self.schemas:
|
||||
if isinstance(name, str) and "." in name:
|
||||
namespace, member = name.rsplit(".", 1)
|
||||
aliases.setdefault(f"{namespace}__{member}", set()).add(name)
|
||||
for alias in (f"{namespace}__{member}", f"{namespace}={member}",
|
||||
f"{namespace}_{member}", member):
|
||||
aliases.setdefault(alias, set()).add(name)
|
||||
self.aliases = {alias: next(iter(names)) for alias, names in aliases.items()
|
||||
if len(names) == 1 and alias not in self.schemas}
|
||||
self.unread = 0 # #3441: `<tool_call>` blocks that opened like a call and were returned as text
|
||||
|
||||
@@ -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 <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])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user