@@ -62,13 +62,9 @@ func (m *MCPCategoryClassifier) Init(cfg *config.RouterConfig) error {
6262 if ! cfg .Classifier .MCPCategoryModel .Enabled {
6363 return fmt .Errorf ("MCP category classifier is not enabled" )
6464 }
65- if cfg .Classifier .MCPCategoryModel .ToolName == "" {
66- return fmt .Errorf ("MCP tool name is not specified" )
67- }
6865
69- // Store config and tool name
66+ // Store config
7067 m .config = cfg
71- m .toolName = cfg .Classifier .MCPCategoryModel .ToolName
7268
7369 // Create MCP client configuration
7470 mcpConfig := mcpclient.ClientConfig {
@@ -99,10 +95,69 @@ func (m *MCPCategoryClassifier) Init(cfg *config.RouterConfig) error {
9995 }
10096
10197 m .client = client
98+
99+ // Discover classification tool
100+ if err := m .discoverClassificationTool (); err != nil {
101+ client .Close ()
102+ return fmt .Errorf ("failed to discover classification tool: %w" , err )
103+ }
104+
102105 observability .Infof ("Successfully initialized MCP category classifier with tool '%s'" , m .toolName )
103106 return nil
104107}
105108
109+ // discoverClassificationTool finds the appropriate classification tool from available MCP tools
110+ func (m * MCPCategoryClassifier ) discoverClassificationTool () error {
111+ // If tool name is explicitly specified, use it
112+ if m .config .Classifier .MCPCategoryModel .ToolName != "" {
113+ m .toolName = m .config .Classifier .MCPCategoryModel .ToolName
114+ observability .Infof ("Using explicitly configured tool: %s" , m .toolName )
115+ return nil
116+ }
117+
118+ // Otherwise, auto-discover by listing available tools
119+ tools := m .client .GetTools ()
120+ if len (tools ) == 0 {
121+ return fmt .Errorf ("no tools available from MCP server" )
122+ }
123+
124+ // Look for classification-related tools by common names
125+ classificationToolNames := []string {
126+ "classify_text" ,
127+ "classify" ,
128+ "categorize" ,
129+ "categorize_text" ,
130+ }
131+
132+ for _ , toolName := range classificationToolNames {
133+ for _ , tool := range tools {
134+ if tool .Name == toolName {
135+ m .toolName = tool .Name
136+ observability .Infof ("Auto-discovered classification tool: %s - %s" , m .toolName , tool .Description )
137+ return nil
138+ }
139+ }
140+ }
141+
142+ // If no common name found, look for tools that mention "classif" in name or description
143+ for _ , tool := range tools {
144+ lowerName := strings .ToLower (tool .Name )
145+ lowerDesc := strings .ToLower (tool .Description )
146+ if strings .Contains (lowerName , "classif" ) || strings .Contains (lowerDesc , "classif" ) {
147+ m .toolName = tool .Name
148+ observability .Infof ("Auto-discovered classification tool by pattern match: %s - %s" , m .toolName , tool .Description )
149+ return nil
150+ }
151+ }
152+
153+ // Log available tools for debugging
154+ var toolNames []string
155+ for _ , tool := range tools {
156+ toolNames = append (toolNames , tool .Name )
157+ }
158+ return fmt .Errorf ("no classification tool found among available tools: %v" , toolNames )
159+ }
160+
106161// Close closes the MCP client connection
107162func (m * MCPCategoryClassifier ) Close () error {
108163 if m .client != nil {
0 commit comments