@@ -465,12 +465,47 @@ def _run_tool(
465465 mcp_tool = self ._get_mcp_tools ().get (tool_use_block_param .name )
466466 if mcp_tool :
467467 return self ._run_mcp_tool (tool_use_block_param )
468+ # Fallback: try prefix matching (for cached trajectories with different UUIDs)
469+ tool = self ._find_tool_by_prefix (tool_use_block_param .name )
470+ if tool :
471+ return self ._run_regular_tool (tool_use_block_param , tool )
472+ msg = f"no matching tool found with name { tool_use_block_param .name } "
473+ logger .error (msg )
468474 return ToolResultBlockParam (
469475 content = f"Tool not found: { tool_use_block_param .name } " ,
470476 is_error = True ,
471477 tool_use_id = tool_use_block_param .id ,
472478 )
473479
480+ def _find_tool_by_prefix (self , cached_name : str ) -> Tool | None :
481+ """Find a tool by matching name prefix (without UUID suffix).
482+
483+ Tool names have format: {base_name}_tags_{tags}_{uuid} or {base_name}_{uuid}
484+ This method strips the UUID suffix and matches by the remaining prefix.
485+
486+ Args:
487+ cached_name: Tool name from cached trajectory (may have different UUID)
488+
489+ Returns:
490+ Matching Tool if found, None otherwise
491+ """
492+ # Extract prefix by removing trailing UUID (pattern: _xxxxxxxx-xxxx-...)
493+ # UUIDs start with 8 hex chars after an underscore
494+ uuid_pattern = re .compile (r"_[0-9a-f]{8}-[0-9a-f]{4}.*$" , re .IGNORECASE )
495+ cached_prefix = uuid_pattern .sub ("" , cached_name )
496+
497+ if not cached_prefix or cached_prefix == cached_name :
498+ # No UUID found or name unchanged, can't match by prefix
499+ return None
500+
501+ # Find a tool whose name starts with the same prefix
502+ for tool_name , tool in self .tool_map .items ():
503+ tool_prefix = uuid_pattern .sub ("" , tool_name )
504+ if tool_prefix == cached_prefix :
505+ return tool
506+
507+ return None
508+
474509 async def _list_mcp_tools (self , mcp_client : McpClientProtocol ) -> list [McpTool ]:
475510 async with mcp_client :
476511 return await mcp_client .list_tools ()
0 commit comments