okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0212-tools (ea765a571): tool-call fixes in serve/ and the native gateway, with their tests and docs311926416

main
3.3 KiB73 linesraw
1"""serve/test_tool_name_mcp_member.py - #5281: a namespaced MCP tool called by its last segment is that tool.
2
3Seen live (Qwen3.8-Flash-Next Q2_0 under DeepSeek Harness, stream): the request declared `mcp__assets__get_asset_info` and
4`mcp__assets__get_maintenance_plan`; the model wrote `<function=get_asset_info>` and `<function=get_maintenance_plan>`.  Neither name was
5declared, so each block went back in `content` as XML, the harness got `stop` and the lookups never ran.  The member after the last `__`
6is the tool when exactly one declared tool ends that way; a declared name always wins, and two tools with the same ending are not guessed.
7
8    python -m unittest serve.test_tool_name_mcp_member -v
9"""
10import sys
11import unittest
12from pathlib import Path
13
14sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
15
16from serve.frontend import OutputParser  # noqa: E402
17
18def tool(name, prop="id"):
19    return {"name": name, "parameters": {"properties": {prop: {"type": "integer"}}}}
20
21TOOLS = [tool("read_skill_resource", "path"), tool("mcp__assets__get_asset_info"), tool("mcp__assets__get_maintenance_plan")]
22
23
24def call(name, arg="id", value="101"):
25    return f"<tool_call>\n<function={name}>\n<parameter={arg}>\n{value}\n</parameter>\n</function>\n</tool_call>"
26
27
28def run(text, tools=TOOLS, stream_tools=True, step=5, recover=True):
29    p = OutputParser(thinking=False, tools=tools, stream_tools=stream_tools, recover=recover, declared_only=True)
30    evs = []
31    for i in range(0, len(text), step):
32        evs += p.feed(text[i:i + step])
33    evs += p.finish("stop")
34    return evs
35
36
37def calls(evs):
38    return [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"]
39
40
41class McpMember(unittest.TestCase):
42    def test_the_last_segment_is_the_tool(self):
43        for stream in (False, True):
44            for step in (1, 5, 1000):
45                evs = run(call("get_asset_info") + "\n" + call("get_maintenance_plan"), stream_tools=stream, step=step)
46                self.assertEqual(calls(evs), [("mcp__assets__get_asset_info", {"id": 101}),
47                                              ("mcp__assets__get_maintenance_plan", {"id": 101})], (stream, step))
48                self.assertFalse([e for e in evs if e.kind == "content" and e.text.strip()])
49
50    def test_the_full_name_is_unchanged(self):
51        self.assertEqual(calls(run(call("mcp__assets__get_asset_info"))), [("mcp__assets__get_asset_info", {"id": 101})])
52
53    def test_two_tools_with_the_same_ending_are_not_guessed(self):
54        tools = [tool("mcp__a__search"), tool("mcp__b__search")]
55        evs = run(call("search"), tools)
56        self.assertEqual(calls(evs), [])
57        self.assertIn("<function=search>", "".join(e.text or "" for e in evs if e.kind == "content"))
58
59    def test_a_declared_name_wins(self):
60        tools = [tool("get_asset_info"), tool("mcp__assets__get_asset_info")]
61        self.assertEqual(calls(run(call("get_asset_info"), tools)), [("get_asset_info", {"id": 101})])
62
63    def test_an_unrelated_name_stays_text(self):
64        evs = run(call("get_asset"))
65        self.assertEqual(calls(evs), [])
66
67    def test_the_dotted_member_rule_is_unchanged(self):
68        tools = [tool("mcp__fs.write_file", "path")]
69        self.assertEqual(calls(run(call("write_file", "path", "x"), tools)), [("mcp__fs.write_file", {"path": "x"})])
70
71
72if __name__ == "__main__":
73    unittest.main()