youssefleb commited on
Commit
6a017c5
·
verified ·
1 Parent(s): 87a36dc

Update mcp_servers.py

Browse files
Files changed (1) hide show
  1. mcp_servers.py +9 -13
mcp_servers.py CHANGED
@@ -1,4 +1,4 @@
1
- # mcp_servers.py (FIXED: Robust 'str' handling)
2
  import asyncio
3
  import json
4
  import re
@@ -15,15 +15,19 @@ EVALUATION_PROMPT_TEMPLATE = load_prompt(config.PROMPT_FILES["evaluator"])
15
  def extract_json(text: str) -> dict:
16
  """Robustly extracts JSON from text."""
17
  try:
 
18
  clean_text = text.strip()
19
  if "```json" in clean_text:
20
  clean_text = clean_text.split("```json")[1].split("```")[0].strip()
21
  elif "```" in clean_text:
22
  clean_text = clean_text.split("```")[1].split("```")[0].strip()
23
  return json.loads(clean_text)
24
- except json.JSONDecodeError:
 
25
  try:
26
- match = re.search(r'(\{.*\})', text, re.DOTALL)
 
 
27
  if match:
28
  return json.loads(match.group(1))
29
  except:
@@ -52,8 +56,7 @@ class BusinessSolutionEvaluator:
52
 
53
  v_fitness = extract_json(response.text)
54
 
55
- # --- FIX 1: Strict Type Checking ---
56
- # If LLM returns a string (e.g. "Error") instead of JSON object, raise error to trigger fallback.
57
  if not isinstance(v_fitness, (dict, list)):
58
  raise ValueError(f"Judge returned invalid type: {type(v_fitness)}")
59
 
@@ -62,7 +65,6 @@ class BusinessSolutionEvaluator:
62
 
63
  except Exception as e:
64
  print(f"ERROR: BusinessSolutionEvaluator failed: {e}")
65
- # Return a safe fallback dictionary so the agent doesn't crash
66
  return {
67
  "Novelty": {"score": 1, "justification": f"Error: {str(e)}"},
68
  "Usefulness_Feasibility": {"score": 1, "justification": f"Error: {str(e)}"},
@@ -120,22 +122,17 @@ class AgentCalibrator:
120
  continue
121
  metric = role_metrics[role]
122
 
123
- # --- FIX 2: Robust Dict Access ---
124
- # Safely get the score object
125
  raw_score_data = res.get("score", {})
126
 
127
- # If it's not a container (e.g. it's a string), reset it
128
  if not isinstance(raw_score_data, (dict, list)):
129
  raw_score_data = {}
130
 
131
- # If it's a list, unwrap it
132
  if isinstance(raw_score_data, list):
133
  raw_score_data = raw_score_data[0] if len(raw_score_data) > 0 else {}
134
 
135
- # Now get the specific metric
136
  metric_data = raw_score_data.get(metric, {})
137
 
138
- # Repeat safety checks
139
  if not isinstance(metric_data, (dict, list)):
140
  metric_data = {}
141
 
@@ -143,7 +140,6 @@ class AgentCalibrator:
143
  metric_data = metric_data[0] if len(metric_data) > 0 else {}
144
 
145
  score = metric_data.get("score", 0)
146
- # ---------------------------------
147
 
148
  if score > best_score:
149
  best_score = score
 
1
+ # mcp_servers.py (FIXED: Robust 'str' handling & Enhanced Extraction)
2
  import asyncio
3
  import json
4
  import re
 
15
  def extract_json(text: str) -> dict:
16
  """Robustly extracts JSON from text."""
17
  try:
18
+ # 1. Try simple block extraction first
19
  clean_text = text.strip()
20
  if "```json" in clean_text:
21
  clean_text = clean_text.split("```json")[1].split("```")[0].strip()
22
  elif "```" in clean_text:
23
  clean_text = clean_text.split("```")[1].split("```")[0].strip()
24
  return json.loads(clean_text)
25
+ except (json.JSONDecodeError, IndexError):
26
+ # 2. Fallback: Regex to find the outermost JSON object
27
  try:
28
+ # Looks for { ... } but matching closest brackets is hard with regex.
29
+ # This regex grabs from the first { to the last }
30
+ match = re.search(r'(\{[\s\S]*\})', text)
31
  if match:
32
  return json.loads(match.group(1))
33
  except:
 
56
 
57
  v_fitness = extract_json(response.text)
58
 
59
+ # Strict Type Checking
 
60
  if not isinstance(v_fitness, (dict, list)):
61
  raise ValueError(f"Judge returned invalid type: {type(v_fitness)}")
62
 
 
65
 
66
  except Exception as e:
67
  print(f"ERROR: BusinessSolutionEvaluator failed: {e}")
 
68
  return {
69
  "Novelty": {"score": 1, "justification": f"Error: {str(e)}"},
70
  "Usefulness_Feasibility": {"score": 1, "justification": f"Error: {str(e)}"},
 
122
  continue
123
  metric = role_metrics[role]
124
 
125
+ # Robust Dict Access
 
126
  raw_score_data = res.get("score", {})
127
 
 
128
  if not isinstance(raw_score_data, (dict, list)):
129
  raw_score_data = {}
130
 
 
131
  if isinstance(raw_score_data, list):
132
  raw_score_data = raw_score_data[0] if len(raw_score_data) > 0 else {}
133
 
 
134
  metric_data = raw_score_data.get(metric, {})
135
 
 
136
  if not isinstance(metric_data, (dict, list)):
137
  metric_data = {}
138
 
 
140
  metric_data = metric_data[0] if len(metric_data) > 0 else {}
141
 
142
  score = metric_data.get("score", 0)
 
143
 
144
  if score > best_score:
145
  best_score = score