okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1"""Codex MCP names that are not the declared namespace.member form. 2 3Observed with Qwen3.8-Flash-Next through Codex CLI against Strata v0.2.2. The model 4wrote <function=mcp__filesystem=write_file>, <function=mcp__filesystem_edit_file>, 5and later a bare <function=write_file>. The declared tool was 6mcp__filesystem.write_file. Stock main already accepts namespace__member. The 7equals sign, the dropped underscore, and a unique bare member have to as well. 8A name that matches nothing stays text (#3075). Two tools that share a member 9are not guessed. 10 11python -m unittest serve.test_tool_name_alias -v 12""" 13import json 14import sys 15import unittest 16from pathlib import Path 17 18sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 19 20from serve .frontend import OutputParser # noqa: E402 21from serve .responses import Assembler ,request_tools # noqa: E402 22 23REQ = { 24"tools" : [{ 25"type" :"namespace" , 26"name" :"mcp__filesystem" , 27"tools" : [ 28 {"type" :"function" ,"name" :"write_file" ,"parameters" : { 29"type" :"object" , 30"properties" : {"path" : {"type" :"string" },"content" : {"type" :"string" }}, 31"required" : ["path" ,"content" ]}}, 32 {"type" :"function" ,"name" :"edit_file" ,"parameters" : { 33"type" :"object" , 34"properties" : {"path" : {"type" :"string" },"edits" : {"type" :"string" }}, 35"required" : ["path" ,"edits" ]}}, 36 {"type" :"function" ,"name" :"list_allowed_directories" ,"parameters" : { 37"type" :"object" ,"properties" : {}}}, 38 ], 39 }], 40} 41 42 43def events (text ,tools ,thinking = False ): 44parser = OutputParser (thinking = thinking ,tools = tools ,stream_tools = True ) 45evs = parser .feed (text ) 46evs += parser .finish ("stop" ) 47return evs 48 49 50def text_of (evs ): 51return "" .join (e .text or "" for e in evs if e .kind in ("content" ,"reasoning" )) 52 53 54def response_items (text ,tools ,names ,req = REQ ): 55asm = Assembler (req ,"qwen" ,10 ,names ,encrypted = False ) 56out = [] 57parser_events = events (text ,tools ) 58for ev in parser_events : 59out += asm .feed (ev ) 60out += asm .finish ({"finish" :"stop" ,"completion_tokens" :1 ,"prompt_tokens" :10 }) 61items = [ev ["item" ]for ev in out if ev .get ("type" )== "response.output_item.done" ] 62return items ,parser_events 63 64 65class DriftedMcpNames (unittest .TestCase ): 66def setUp (self ): 67self .tools ,self .names ,_skipped = request_tools (REQ ) 68 69def _call (self ,text ,tools = None ,names = None ): 70items ,evs = response_items (text ,tools or self .tools ,names or self .names ) 71calls = [i for i in items if i ["type" ]== "function_call" ] 72return calls ,evs 73 74def test_equals_write_is_the_namespace_tool (self ): 75raw = ( 76"<tool_call>\n<function=mcp__filesystem=write_file>\n<parameter=path>\n/tmp/x.py\n</parameter>\n" 77"<parameter=content>\nprint(\"not used\")\n</parameter>\n</function>\n</tool_call>" 78 ) 79calls ,evs = self ._call (raw ) 80self .assertEqual (text_of (evs ).strip (),"" ) 81self .assertEqual (len (calls ),1 ) 82self .assertEqual ((calls [0 ]["namespace" ],calls [0 ]["name" ]), ("mcp__filesystem" ,"write_file" )) 83args = json .loads (calls [0 ]["arguments" ]) 84self .assertEqual (args ["path" ],"/tmp/x.py" ) 85self .assertIn ('print("not used")' ,args ["content" ]) 86 87def test_single_underscore_edit_keeps_a_string_argument (self ): 88raw = ( 89"<tool_call>\n<function=mcp__filesystem_edit_file>\n<parameter=path>\n/tmp/a.txt\n</parameter>\n" 90"<parameter=edits>\nsay \"hi\"\n</parameter>\n</function>\n</tool_call>" 91 ) 92calls ,evs = self ._call (raw ) 93self .assertEqual (text_of (evs ).strip (),"" ) 94self .assertEqual ((calls [0 ]["namespace" ],calls [0 ]["name" ]), ("mcp__filesystem" ,"edit_file" )) 95args = json .loads (calls [0 ]["arguments" ]) 96self .assertEqual (args ["path" ],"/tmp/a.txt" ) 97self .assertIsInstance (args ["edits" ],str ) 98self .assertIn ('"hi"' ,args ["edits" ]) 99 100def test_bare_write_file_is_the_one_namespace_tool (self ): 101raw = ( 102"<tool_call>\n<function=write_file>\n<parameter=path>\n/tmp/patch-tencut.py\n</parameter>\n" 103"<parameter=content>\nprint(\"ten\")\n</parameter>\n</function>\n</tool_call>" 104 ) 105calls ,evs = self ._call (raw ) 106self .assertEqual (text_of (evs ).strip (),"" ) 107self .assertEqual ((calls [0 ]["namespace" ],calls [0 ]["name" ]), ("mcp__filesystem" ,"write_file" )) 108args = json .loads (calls [0 ]["arguments" ]) 109self .assertEqual (args ["path" ],"/tmp/patch-tencut.py" ) 110self .assertEqual (args ["content" ],'print("ten")' ) 111 112def test_bare_list_allowed_directories_is_a_call (self ): 113raw = "<tool_call>\n<function=list_allowed_directories>\n</function>\n</tool_call>" 114calls ,evs = self ._call (raw ) 115self .assertEqual (text_of (evs ).strip (),"" ) 116self .assertEqual ((calls [0 ]["namespace" ],calls [0 ]["name" ]), 117 ("mcp__filesystem" ,"list_allowed_directories" )) 118self .assertEqual (json .loads (calls [0 ]["arguments" ]), {}) 119 120def test_double_underscore_still_maps (self ): 121raw = ( 122"<tool_call>\n<function=mcp__filesystem__write_file>\n<parameter=path>\n/tmp/z\n</parameter>\n" 123"<parameter=content>\nok\n</parameter>\n</function>\n</tool_call>" 124 ) 125calls ,_evs = self ._call (raw ) 126self .assertEqual ((calls [0 ]["namespace" ],calls [0 ]["name" ]), ("mcp__filesystem" ,"write_file" )) 127self .assertEqual (json .loads (calls [0 ]["arguments" ])["content" ],"ok" ) 128 129def test_equals_inside_thinking_is_still_a_call (self ): 130raw = ( 131"<tool_call>\n<function=mcp__filesystem=write_file>\n<parameter=path>\n/tmp/x.py\n</parameter>\n" 132"<parameter=content>\nok\n</parameter>\n</function>\n</tool_call>" 133 ) 134evs = events (raw + "</think>\n" ,self .tools ,thinking = True ) 135found = [e for e in evs if e .kind == "tool_call" ] 136self .assertEqual (len (found ),1 ) 137self .assertEqual (found [0 ].call .arguments ["path" ],"/tmp/x.py" ) 138 139def test_an_unknown_name_stays_text (self ): 140raw = "<tool_call>\n<function=mcp__filesystem=not_a_tool>\n</function>\n</tool_call>" 141calls ,evs = self ._call (raw ) 142self .assertEqual (calls , []) 143self .assertIn ("not_a_tool" ,text_of (evs )) 144 145def test_two_namespaces_sharing_a_member_stay_text (self ): 146req = {"tools" : [ 147 {"type" :"namespace" ,"name" :"mcp__filesystem" ,"tools" : [ 148 {"type" :"function" ,"name" :"write_file" ,"parameters" : { 149"type" :"object" ,"properties" : {"path" : {"type" :"string" }}}}]}, 150 {"type" :"namespace" ,"name" :"mcp__other" ,"tools" : [ 151 {"type" :"function" ,"name" :"write_file" ,"parameters" : { 152"type" :"object" ,"properties" : {"path" : {"type" :"string" }}}}]}, 153 ]} 154tools ,names ,_ = request_tools (req ) 155raw = "<tool_call>\n<function=write_file>\n<parameter=path>\n/tmp/x\n</parameter>\n</function>\n</tool_call>" 156calls ,evs = self ._call (raw ,tools ,names ) 157self .assertEqual (calls , []) 158self .assertIn ("write_file" ,text_of (evs )) 159 160def test_a_flat_tool_with_the_member_name_wins (self ): 161req = {"tools" : [ 162 {"type" :"function" ,"name" :"write_file" ,"parameters" : { 163"type" :"object" ,"properties" : {"path" : {"type" :"string" }}}}, 164 {"type" :"namespace" ,"name" :"mcp__filesystem" ,"tools" : [ 165 {"type" :"function" ,"name" :"write_file" ,"parameters" : { 166"type" :"object" ,"properties" : {"path" : {"type" :"string" }, 167"content" : {"type" :"string" }}}}]}, 168 ]} 169tools ,names ,_ = request_tools (req ) 170self .assertEqual (names ["write_file" ], (None ,"write_file" ,"function" )) 171raw = "<tool_call>\n<function=write_file>\n<parameter=path>\n/tmp/x\n</parameter>\n</function>\n</tool_call>" 172calls ,evs = self ._call (raw ,tools ,names ) 173self .assertEqual (text_of (evs ).strip (),"" ) 174self .assertEqual (calls [0 ]["name" ],"write_file" ) 175self .assertNotIn ("namespace" ,calls [0 ]) 176 177 178def test_a_flat_tool_named_like_the_underscore_alias_wins (self ): 179req = {"tools" : [ 180 {"type" :"function" ,"name" :"mcp__filesystem_write_file" ,"parameters" : { 181"type" :"object" ,"properties" : {"path" : {"type" :"string" }}}}, 182 {"type" :"namespace" ,"name" :"mcp__filesystem" ,"tools" : [ 183 {"type" :"function" ,"name" :"write_file" ,"parameters" : { 184"type" :"object" ,"properties" : {"path" : {"type" :"string" }, 185"content" : {"type" :"string" }}}}]}, 186 ]} 187tools ,names ,_ = request_tools (req ) 188raw = ("<tool_call>\n<function=mcp__filesystem_write_file>\n<parameter=path>\n/tmp/x\n</parameter>\n</function>" 189"\n</tool_call>" ) 190calls ,evs = self ._call (raw ,tools ,names ) 191self .assertEqual (text_of (evs ).strip (),"" ) 192self .assertEqual (calls [0 ]["name" ],"mcp__filesystem_write_file" ) 193self .assertNotIn ("namespace" ,calls [0 ]) 194 195 196def plain_tool (name ): 197return {"name" :name ,"parameters" : {"type" :"object" ,"properties" : {"url" : {"type" :"string" }}}} 198 199 200def call_text (name ,param = "url" ,value = "https://example.com" ): 201return f"<tool_call>\n<function= { name } >\n<parameter= { param } >\n { value } \n</parameter>\n</function>\n</tool_call>" 202 203 204def run_parser (text ,tools ,width = 0 ): 205parser = OutputParser (thinking = False ,tools = tools ,stream_tools = True ,declared_only = True ) 206evs = [] 207step = width or len (text )or 1 208for k in range (0 ,len (text ),step ): 209evs += parser .feed (text [k :k + step ]) 210evs += parser .finish ("stop" ) 211return evs ,parser 212 213 214class CollapsedUnderscoreNames (unittest .TestCase ): 215"""#3796: `mcp__server__tool` written as `mcp_server_tool` is a spelling of the declared tool (the same class of 216difference as namespace__member). A name that is only similar stays text, and is named for the client.""" 217MCP = [plain_tool ("mcp__playwright__browser_navigate" ),plain_tool ("mcp__playwright__browser_snapshot" ), 218plain_tool ("mcp__playwright__browser_tabs" ),plain_tool ("todo_write" )] 219 220def test_the_collapsed_spelling_is_the_declared_tool (self ): 221for name in ("browser_navigate" ,"browser_snapshot" ,"browser_tabs" ): 222for width in (0 ,1 ,5 ): 223with self .subTest (name = name ,width = width ): 224evs ,parser = run_parser ("Opening it.\n" + call_text ("mcp_playwright_" + name ),self .MCP ,width ) 225found = [e .call for e in evs if e .kind == "tool_call" ] 226self .assertEqual ([c .name for c in found ], ["mcp__playwright__" + name ])# the client gets the name it declared 227self .assertEqual (found [0 ].arguments , {"url" :"https://example.com" }) 228self .assertEqual (text_of (evs ).strip (),"Opening it." ) 229self .assertEqual ((parser .unread ,parser .unread_names ), (0 , [])) 230 231def test_the_declared_spelling_and_a_plain_tool_are_unchanged (self ): 232evs ,_ = run_parser (call_text ("mcp__playwright__browser_tabs" )+ "\n" + call_text ("todo_write" ),self .MCP ) 233self .assertEqual ([e .call .name for e in evs if e .kind == "tool_call" ], ["mcp__playwright__browser_tabs" ,"todo_write" ]) 234 235def test_a_tool_declared_in_the_collapsed_spelling_wins (self ): 236tools = self .MCP + [plain_tool ("mcp_playwright_browser_tabs" )] 237evs ,_ = run_parser (call_text ("mcp_playwright_browser_tabs" ),tools ) 238self .assertEqual ([e .call .name for e in evs if e .kind == "tool_call" ], ["mcp_playwright_browser_tabs" ]) 239 240def test_two_tools_with_the_same_collapsed_spelling_are_not_guessed (self ): 241tools = [plain_tool ("a__b_c" ),plain_tool ("a_b__c" )] 242evs ,parser = run_parser (call_text ("a_b_c" ),tools ) 243self .assertEqual ([e for e in evs if e .kind == "tool_call" ], []) 244self .assertIn ("a_b_c" ,text_of (evs )) 245self .assertEqual (parser .unread_names , ["a_b_c" ]) 246 247def test_a_reordered_or_renamed_tool_stays_text_and_is_named (self ): 248"""The LibreChat case (`server1_label_values` for `label_values_mcp_server1`) and the bash/shell case: no guessing.""" 249for declared ,written in (("label_values_mcp_server1" ,"server1_label_values" ), ("shell" ,"bash" ), 250 ("godot_execute_gdscript" ,"execute_gdscript" )): 251for width in (0 ,3 ): 252with self .subTest (written = written ,width = width ): 253raw = "Done.\n" + call_text (written ) 254evs ,parser = run_parser (raw , [plain_tool (declared )],width ) 255self .assertEqual ([e for e in evs if e .kind == "tool_call" ], []) 256self .assertEqual (text_of (evs ),raw )# the text is exactly what the model wrote 257self .assertEqual ((parser .unread ,parser .unread_names ), (1 , [written ])) 258 259 260if __name__ == "__main__" : 261unittest .main ()