Spaces:
Runtime error
Runtime error
Update core_mcp.py
Browse files- 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 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 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':
|
| 343 |
-
'summary': f"β Execution failed: {
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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:
|