1010import static org .opensearch .ml .common .CommonValue .MCP_CONNECTORS_FIELD ;
1111import static org .opensearch .ml .common .CommonValue .MCP_CONNECTOR_ID_FIELD ;
1212import static org .opensearch .ml .common .CommonValue .ML_CONNECTOR_INDEX ;
13- import static org .opensearch .ml .common .CommonValue .TENANT_ID_FIELD ;
1413import static org .opensearch .ml .common .utils .StringUtils .getParameterMap ;
1514import static org .opensearch .ml .common .utils .StringUtils .gson ;
1615import static org .opensearch .ml .common .utils .StringUtils .isJson ;
2928import static org .opensearch .ml .engine .algorithms .agent .MLChatAgentRunner .TOOL_NAMES ;
3029import static org .opensearch .ml .engine .algorithms .agent .MLPlanExecuteAndReflectAgentRunner .RESPONSE_FIELD ;
3130import static org .opensearch .ml .engine .memory .ConversationIndexMemory .LAST_N_INTERACTIONS ;
31+ import static org .opensearch .ml .engine .tools .ToolUtils .getToolName ;
3232
3333import java .io .IOException ;
3434import java .lang .reflect .Type ;
8181import org .opensearch .ml .engine .encryptor .Encryptor ;
8282import org .opensearch .ml .engine .function_calling .FunctionCalling ;
8383import org .opensearch .ml .engine .tools .McpSseTool ;
84+ import org .opensearch .ml .engine .tools .ToolUtils ;
8485import org .opensearch .remote .metadata .client .GetDataObjectRequest ;
8586import org .opensearch .remote .metadata .client .SdkClient ;
8687import org .opensearch .remote .metadata .common .SdkClientUtils ;
@@ -646,10 +647,6 @@ public static int getMessageHistoryLimit(Map<String, String> params) {
646647 return messageHistoryLimitStr != null ? Integer .parseInt (messageHistoryLimitStr ) : LAST_N_INTERACTIONS ;
647648 }
648649
649- public static String getToolName (MLToolSpec toolSpec ) {
650- return toolSpec .getName () != null ? toolSpec .getName () : toolSpec .getType ();
651- }
652-
653650 public static List <MLToolSpec > getMlToolSpecs (MLAgent mlAgent , Map <String , String > params ) {
654651 String selectedToolsStr = params .get (SELECTED_TOOLS );
655652 List <MLToolSpec > toolSpecs = new ArrayList <>();
@@ -841,7 +838,8 @@ public static void createTools(
841838 return ;
842839 }
843840 for (MLToolSpec toolSpec : toolSpecs ) {
844- Tool tool = createTool (toolFactories , params , toolSpec , mlAgent .getTenantId ());
841+ Map <String , String > toolParams = ToolUtils .buildToolParameters (params , toolSpec , mlAgent .getTenantId ());
842+ Tool tool = ToolUtils .createTool (toolFactories , toolParams , toolSpec );
845843 tools .put (tool .getName (), tool );
846844 if (toolSpec .getAttributes () != null ) {
847845 if (tool .getAttributes () == null ) {
@@ -856,55 +854,6 @@ public static void createTools(
856854 }
857855 }
858856
859- public static Tool createTool (
860- Map <String , Tool .Factory > toolFactories ,
861- Map <String , String > params ,
862- MLToolSpec toolSpec ,
863- String tenantId
864- ) {
865- if (!toolFactories .containsKey (toolSpec .getType ())) {
866- throw new IllegalArgumentException ("Tool not found: " + toolSpec .getType ());
867- }
868- Map <String , String > executeParams = new HashMap <>();
869- if (toolSpec .getParameters () != null ) {
870- executeParams .putAll (toolSpec .getParameters ());
871- }
872- executeParams .put (TENANT_ID_FIELD , tenantId );
873- for (String key : params .keySet ()) {
874- String toolNamePrefix = getToolName (toolSpec ) + "." ;
875- if (key .startsWith (toolNamePrefix )) {
876- executeParams .put (key .replace (toolNamePrefix , "" ), params .get (key ));
877- }
878- }
879- Map <String , Object > toolParams = new HashMap <>();
880- toolParams .putAll (executeParams );
881- Map <String , Object > runtimeResources = toolSpec .getRuntimeResources ();
882- if (runtimeResources != null ) {
883- toolParams .putAll (runtimeResources );
884- }
885- Tool tool = toolFactories .get (toolSpec .getType ()).create (toolParams );
886- String toolName = getToolName (toolSpec );
887- tool .setName (toolName );
888-
889- if (toolSpec .getDescription () != null ) {
890- tool .setDescription (toolSpec .getDescription ());
891- }
892- if (params .containsKey (toolName + ".description" )) {
893- tool .setDescription (params .get (toolName + ".description" ));
894- }
895-
896- return tool ;
897- }
898-
899- public static List <String > getToolNames (Map <String , Tool > tools ) {
900- final List <String > inputTools = new ArrayList <>();
901- for (Map .Entry <String , Tool > entry : tools .entrySet ()) {
902- String toolName = entry .getValue ().getName ();
903- inputTools .add (toolName );
904- }
905- return inputTools ;
906- }
907-
908857 public static Map <String , String > constructToolParams (
909858 Map <String , Tool > tools ,
910859 Map <String , MLToolSpec > toolSpecMap ,
@@ -916,8 +865,15 @@ public static Map<String, String> constructToolParams(
916865 Map <String , String > toolParams = new HashMap <>();
917866 Map <String , String > toolSpecParams = toolSpecMap .get (action ).getParameters ();
918867 Map <String , String > toolSpecConfigMap = toolSpecMap .get (action ).getConfigMap ();
868+ MLToolSpec toolSpec = toolSpecMap .get (action );
919869 if (toolSpecParams != null ) {
920870 toolParams .putAll (toolSpecParams );
871+ for (String key : toolSpecParams .keySet ()) {
872+ String toolNamePrefix = getToolName (toolSpec ) + "." ;
873+ if (key .startsWith (toolNamePrefix )) {
874+ toolParams .put (key .replace (toolNamePrefix , "" ), toolSpecParams .get (key ));
875+ }
876+ }
921877 }
922878 if (toolSpecConfigMap != null ) {
923879 toolParams .putAll (toolSpecConfigMap );
0 commit comments