WhoDat55 commited on
Commit
8c3c873
Β·
verified Β·
1 Parent(s): da23e7c

Update core_mcp.py

Browse files
Files changed (1) hide show
  1. core_mcp.py +39 -25
core_mcp.py CHANGED
@@ -276,22 +276,6 @@ def execute_tool(tool_name: str, params: Dict[str, str]) -> Dict[str, any]:
276
  return result
277
 
278
  def execute_gradio_space(tool_name: str, tool_config: Dict, params: Dict[str, str], transaction_id: str, timestamp: str) -> Dict[str, any]:
279
- try:
280
- from gradio_client import Client
281
- except ImportError:
282
- return {
283
- 'transaction_id': transaction_id,
284
- 'timestamp': timestamp,
285
- 'tool_name': tool_name,
286
- 'tool_type': 'gradio_space',
287
- 'endpoint': tool_config['space_name'],
288
- 'request_payload': params,
289
- 'response_payload': None,
290
- 'status': 'FAILURE',
291
- 'latency': 0.0,
292
- 'error_message': 'gradio_client not installed',
293
- 'summary': '❌ gradio_client not installed'
294
- }
295
  space_name = tool_config['space_name']
296
  if not space_name or 'PLACEHOLDER' in space_name:
297
  return {
@@ -307,14 +291,23 @@ def execute_gradio_space(tool_name: str, tool_config: Dict, params: Dict[str, st
307
  'error_message': 'Tool space name not configured',
308
  'summary': '❌ Space not configured'
309
  }
 
 
 
 
 
 
 
 
 
310
  try:
311
- hf_token = tool_config.get('hf_token', None)
312
- client = Client(space_name, hf_token=hf_token) if hf_token else Client(space_name)
313
- param_order = tool_config.get('param_order', tool_config['required_params'])
314
- param_values = [params.get(param, '') for param in param_order]
315
- api_name = tool_config.get('api_name', '/predict')
316
- result = client.predict(*param_values, api_name=api_name)
317
  summary = result.get('message', f"βœ… Executed {tool_name}") if isinstance(result, dict) else f"βœ… Executed {tool_name}"
 
318
  return {
319
  'transaction_id': transaction_id,
320
  'timestamp': timestamp,
@@ -328,7 +321,25 @@ def execute_gradio_space(tool_name: str, tool_config: Dict, params: Dict[str, st
328
  'error_message': None,
329
  'summary': summary
330
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  except Exception as e:
 
332
  return {
333
  'transaction_id': transaction_id,
334
  'timestamp': timestamp,
@@ -339,8 +350,8 @@ def execute_gradio_space(tool_name: str, tool_config: Dict, params: Dict[str, st
339
  'response_payload': None,
340
  'status': 'FAILURE',
341
  'latency': 0.0,
342
- 'error_message': str(e),
343
- 'summary': f"❌ Execution failed: {str(e)[:50]}..."
344
  }
345
 
346
  def execute_http_tool(tool_name: str, tool_config: Dict, params: Dict[str, str], transaction_id: str, timestamp: str) -> Dict[str, any]:
@@ -419,7 +430,10 @@ def call_ai_api(full_url: str, api_key: str, messages: List[Dict[str, str]]) ->
419
  else:
420
  return f"ERROR: API malformed response: {data}"
421
  except Exception as e:
422
- return f"ERROR: API call failed: {str(e)}"
 
 
 
423
 
424
  def resolve_model(selected_label: str, model_registry: Dict[str, Dict], fallback_label: str, agent_name: str) -> Tuple[str, str, str]:
425
  if selected_label and selected_label in model_registry:
 
276
  return result
277
 
278
  def execute_gradio_space(tool_name: str, tool_config: Dict, params: Dict[str, str], transaction_id: str, timestamp: str) -> Dict[str, any]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  space_name = tool_config['space_name']
280
  if not space_name or 'PLACEHOLDER' in space_name:
281
  return {
 
291
  'error_message': 'Tool space name not configured',
292
  'summary': '❌ Space not configured'
293
  }
294
+
295
+ api_endpoint = f"https://huggingface.co/spaces/{space_name}/api/predict"
296
+
297
+ param_order = tool_config.get('param_order', tool_config['required_params'])
298
+ data_array = [params.get(param, '') for param in param_order]
299
+
300
+ payload = {"data": data_array}
301
+ headers = {'Content-Type': 'application/json'}
302
+
303
  try:
304
+ response = requests.post(api_endpoint, headers=headers, json=payload, timeout=120)
305
+ response.raise_for_status()
306
+ result_data = response.json()
307
+
308
+ result = result_data.get('data', [{}])[0] if 'data' in result_data else result_data
 
309
  summary = result.get('message', f"βœ… Executed {tool_name}") if isinstance(result, dict) else f"βœ… Executed {tool_name}"
310
+
311
  return {
312
  'transaction_id': transaction_id,
313
  'timestamp': timestamp,
 
321
  'error_message': None,
322
  'summary': summary
323
  }
324
+ except requests.exceptions.HTTPError as e:
325
+ error_msg = f"HTTP {e.response.status_code}: {e.response.reason}"
326
+ if e.response.status_code == 503:
327
+ error_msg = "Space unavailable (503) - may be cold-starting or overloaded"
328
+ return {
329
+ 'transaction_id': transaction_id,
330
+ 'timestamp': timestamp,
331
+ 'tool_name': tool_name,
332
+ 'tool_type': 'gradio_space',
333
+ 'endpoint': space_name,
334
+ 'request_payload': params,
335
+ 'response_payload': None,
336
+ 'status': 'FAILURE',
337
+ 'latency': 0.0,
338
+ 'error_message': error_msg,
339
+ 'summary': f"❌ {error_msg}"
340
+ }
341
  except Exception as e:
342
+ sanitized_error = sanitize_error_message(str(e))
343
  return {
344
  'transaction_id': transaction_id,
345
  'timestamp': timestamp,
 
350
  'response_payload': None,
351
  'status': 'FAILURE',
352
  'latency': 0.0,
353
+ 'error_message': sanitized_error,
354
+ 'summary': f"❌ Execution failed: {sanitized_error[:50]}..."
355
  }
356
 
357
  def execute_http_tool(tool_name: str, tool_config: Dict, params: Dict[str, str], transaction_id: str, timestamp: str) -> Dict[str, any]:
 
430
  else:
431
  return f"ERROR: API malformed response: {data}"
432
  except Exception as e:
433
+ # SANITIZE the error message to remove API keys
434
+ raw_error = str(e)
435
+ sanitized_error = sanitize_error_message(raw_error)
436
+ return f"ERROR: API call failed: {sanitized_error}"
437
 
438
  def resolve_model(selected_label: str, model_registry: Dict[str, Dict], fallback_label: str, agent_name: str) -> Tuple[str, str, str]:
439
  if selected_label and selected_label in model_registry: