883. Intelligent routing decisions (model selection and reasoning control) 
99
1010The server implements two MCP tools: 
11- - 'list_categories': Returns available categories for dynamic loading  
11+ - 'list_categories': Returns available categories with per-category system prompts and descriptions  
1212- 'classify_text': Classifies text and returns routing recommendations 
1313
1414Protocol: 
15- - list_categories returns: {"categories": ["math", "science", "technology", ...]} 
15+ - list_categories returns: { 
16+     "categories": ["math", "science", "technology", ...], 
17+     "category_system_prompts": {  # optional, per-category system prompts 
18+       "math": "You are a mathematics expert. When answering math questions...", 
19+       "science": "You are a science expert. When answering science questions...", 
20+       "technology": "You are a technology expert. When answering tech questions..." 
21+     }, 
22+     "category_descriptions": {  # optional 
23+       "math": "Mathematical and computational queries", 
24+       "science": "Scientific concepts and queries" 
25+     } 
26+   } 
1627- classify_text returns: { 
1728    "class": 0, 
1829    "confidence": 0.85, 
4657)
4758logger  =  logging .getLogger (__name__ )
4859
49- # Define classification categories and their regex patterns 
60+ # Define classification categories with their regex patterns, descriptions, and system prompts 
61+ # Each category has its own system prompt for specialized context 
5062CATEGORIES  =  {
5163    "math" : {
5264        "patterns" : [
5668            r"\b(sin|cos|tan|log|sqrt|sum|average|mean)\b" ,
5769        ],
5870        "description" : "Mathematical and computational queries" ,
71+         "system_prompt" : """You are a mathematics expert. When answering math questions: 
72+ - Show step-by-step solutions with clear explanations 
73+ - Use proper mathematical notation and terminology 
74+ - Verify calculations and provide intermediate steps 
75+ - Explain the underlying concepts and principles 
76+ - Offer alternative approaches when applicable""" ,
5977    },
6078    "science" : {
6179        "patterns" : [
6583            r"\b(planet|star|galaxy|universe|ecosystem|organism)\b" ,
6684        ],
6785        "description" : "Scientific concepts and queries" ,
86+         "system_prompt" : """You are a science expert. When answering science questions: 
87+ - Provide evidence-based answers grounded in scientific research 
88+ - Explain relevant scientific concepts and principles 
89+ - Use appropriate scientific terminology 
90+ - Cite the scientific method and experimental evidence when relevant 
91+ - Distinguish between established facts and current theories""" ,
6892    },
6993    "technology" : {
7094        "patterns" : [
7498            r"\b(python|java|javascript|C\+\+|golang|rust)\b" ,
7599        ],
76100        "description" : "Technology and computing topics" ,
101+         "system_prompt" : """You are a technology expert. When answering tech questions: 
102+ - Include practical examples and code snippets when relevant 
103+ - Follow best practices and industry standards 
104+ - Explain both high-level concepts and implementation details 
105+ - Consider security, performance, and maintainability 
106+ - Recommend appropriate tools and technologies for the use case""" ,
77107    },
78108    "history" : {
79109        "patterns" : [
83113            r"\b(BCE|CE|AD|BC|\d{4})\b.*\b(year|century|ago)\b" ,
84114        ],
85115        "description" : "Historical events and topics" ,
116+         "system_prompt" : """You are a history expert. When answering historical questions: 
117+ - Provide accurate dates, names, and historical context 
118+ - Cite time periods and geographical locations 
119+ - Explain the causes, events, and consequences 
120+ - Consider multiple perspectives and historical interpretations 
121+ - Connect historical events to their broader significance""" ,
86122    },
87123    "general" : {
88124        "patterns" : [r".*" ],  # Catch-all pattern 
89125        "description" : "General questions and topics" ,
126+         "system_prompt" : """You are a knowledgeable assistant. When answering general questions: 
127+ - Provide balanced, well-rounded responses 
128+ - Draw from multiple domains of knowledge when relevant 
129+ - Be clear, concise, and accurate 
130+ - Adapt your explanation to the complexity of the question 
131+ - Acknowledge limitations and uncertainties when appropriate""" ,
90132    },
91133}
92134
@@ -300,8 +342,9 @@ async def list_tools() -> list[Tool]:
300342        Tool (
301343            name = "list_categories" ,
302344            description = (
303-                 "List all available classification categories. " 
304-                 "Returns a simple array of category names that the router will use for dynamic category loading." 
345+                 "List all available classification categories with per-category system prompts and descriptions. " 
346+                 "Returns: categories (array), category_system_prompts (object), category_descriptions (object). " 
347+                 "Each category can have its own system prompt that the router injects for category-specific LLM context." 
305348            ),
306349            inputSchema = {"type" : "object" , "properties" : {}},
307350        ),
@@ -328,9 +371,27 @@ async def call_tool(name: str, arguments: Any) -> list[TextContent]:
328371            return  [TextContent (type = "text" , text = json .dumps ({"error" : str (e )}))]
329372
330373    elif  name  ==  "list_categories" :
331-         # Return simple list of category names as expected by semantic router 
332-         categories_response  =  {"categories" : CATEGORY_NAMES }
333-         logger .info (f"Returning { len (CATEGORY_NAMES )}   categories: { CATEGORY_NAMES }  " )
374+         # Return category information including per-category system prompts and descriptions 
375+         # This allows the router to get category-specific instructions from the MCP server 
376+         category_descriptions  =  {
377+             name : CATEGORIES [name ]["description" ] for  name  in  CATEGORY_NAMES 
378+         }
379+ 
380+         category_system_prompts  =  {
381+             name : CATEGORIES [name ]["system_prompt" ]
382+             for  name  in  CATEGORY_NAMES 
383+             if  "system_prompt"  in  CATEGORIES [name ]
384+         }
385+ 
386+         categories_response  =  {
387+             "categories" : CATEGORY_NAMES ,
388+             "category_system_prompts" : category_system_prompts ,
389+             "category_descriptions" : category_descriptions ,
390+         }
391+ 
392+         logger .info (
393+             f"Returning { len (CATEGORY_NAMES )}   categories with { len (category_system_prompts )}   system prompts: { CATEGORY_NAMES }  " 
394+         )
334395        return  [TextContent (type = "text" , text = json .dumps (categories_response ))]
335396
336397    else :
0 commit comments