Skip to content

Commit 5725fe3

Browse files
committed
feat: added in basic retry logic to make LLM use workflow context if errors occur [2025-07-30]
1 parent a527512 commit 5725fe3

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/mcp_service/os_service.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,18 @@ async def wrapper(*args, **kwargs):
280280

281281
return wrapper
282282

283+
# TODO: This is a bit of a hack - we need to improve the error handling and retry logic
284+
# TODO: Could we actually spawn a seperate AI agent to handle the retry logic and return the result to the main agent?
285+
def _add_retry_context(self, response_data: dict, tool_name: str) -> dict:
286+
"""Add retry guidance to tool responses"""
287+
if "error" in response_data:
288+
response_data["retry_guidance"] = {
289+
"tool": tool_name,
290+
"suggestion": "Review the error message and try again with corrected parameters",
291+
"MANDATORY_INSTRUCTION": "YOU MUST call get_workflow_context() if you need to see available options again"
292+
}
293+
return response_data
294+
283295
async def hello_world(self, name: str) -> str:
284296
"""Simple hello world tool for testing"""
285297
return f"Hello, {name}! 👋"
@@ -314,8 +326,8 @@ async def list_collections(
314326

315327
return json.dumps({"collections": collections})
316328
except Exception as e:
317-
logger.error("Error listing collections")
318-
return json.dumps({"error": str(e)})
329+
error_response = {"error": str(e)}
330+
return json.dumps(self._add_retry_context(error_response, "list_collections"))
319331

320332
async def get_collection_info(
321333
self,
@@ -337,7 +349,8 @@ async def get_collection_info(
337349

338350
return json.dumps(data)
339351
except Exception as e:
340-
return json.dumps({"error": str(e)})
352+
error_response = {"error": str(e)}
353+
return json.dumps(self._add_retry_context(error_response, "get_collection_info"))
341354

342355
async def get_collection_queryables(
343356
self,
@@ -359,7 +372,8 @@ async def get_collection_queryables(
359372

360373
return json.dumps(data)
361374
except Exception as e:
362-
return json.dumps({"error": str(e)})
375+
error_response = {"error": str(e)}
376+
return json.dumps(self._add_retry_context(error_response, "get_collection_queryables"))
363377

364378
async def search_features(
365379
self,
@@ -434,7 +448,8 @@ async def search_features(
434448

435449
return json.dumps(data)
436450
except Exception as e:
437-
return json.dumps({"error": str(e)})
451+
error_response = {"error": str(e)}
452+
return json.dumps(self._add_retry_context(error_response, "search_features"))
438453

439454
async def get_feature(
440455
self,
@@ -466,7 +481,8 @@ async def get_feature(
466481

467482
return json.dumps(data)
468483
except Exception as e:
469-
return json.dumps({"error": f"Error getting feature: {str(e)}"})
484+
error_response = {"error": f"Error getting feature: {str(e)}"}
485+
return json.dumps(self._add_retry_context(error_response, "get_feature"))
470486

471487
async def get_linked_identifiers(
472488
self,
@@ -500,7 +516,8 @@ async def get_linked_identifiers(
500516

501517
return json.dumps(data)
502518
except Exception as e:
503-
return json.dumps({"error": str(e)})
519+
error_response = {"error": str(e)}
520+
return json.dumps(self._add_retry_context(error_response, "get_linked_identifiers"))
504521

505522
async def get_bulk_features(
506523
self,
@@ -540,7 +557,8 @@ async def get_bulk_features(
540557

541558
return json.dumps({"results": parsed_results})
542559
except Exception as e:
543-
return json.dumps({"error": str(e)})
560+
error_response = {"error": str(e)}
561+
return json.dumps(self._add_retry_context(error_response, "get_bulk_features"))
544562

545563
async def get_bulk_linked_features(
546564
self,
@@ -571,7 +589,8 @@ async def get_bulk_linked_features(
571589

572590
return json.dumps({"results": parsed_results})
573591
except Exception as e:
574-
return json.dumps({"error": str(e)})
592+
error_response = {"error": str(e)}
593+
return json.dumps(self._add_retry_context(error_response, "get_bulk_linked_features"))
575594

576595
async def get_prompt_templates(
577596
self,

0 commit comments

Comments
 (0)