From 30e586bfcbeae8161296a48dcb5240c049e976dd Mon Sep 17 00:00:00 2001 From: takshit12 <162030143+takshit12@users.noreply.github.com> Date: Sun, 9 Mar 2025 17:39:14 +0530 Subject: [PATCH] feat: Add timeout handling to MCP server requests - Add 5-minute timeout, proper error handling, and improved logging (#27) Co-authored-by: Takshit Mathur --- mcp/mcp_server.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/mcp/mcp_server.py b/mcp/mcp_server.py index f7b7ed72..10b8ce59 100644 --- a/mcp/mcp_server.py +++ b/mcp/mcp_server.py @@ -57,17 +57,25 @@ async def create_thread() -> str: def _make_request(thread_id: str, user_input: str, config: dict) -> str: """Make synchronous request to graph service""" - response = requests.post( - f"{GRAPH_SERVICE_URL}/invoke", - json={ - "message": user_input, - "thread_id": thread_id, - "is_first_message": not active_threads[thread_id], - "config": config - } - ) - response.raise_for_status() - return response.json() + try: + response = requests.post( + f"{GRAPH_SERVICE_URL}/invoke", + json={ + "message": user_input, + "thread_id": thread_id, + "is_first_message": not active_threads[thread_id], + "config": config + }, + timeout=300 # 5 minute timeout for long-running operations + ) + response.raise_for_status() + return response.json() + except requests.exceptions.Timeout: + write_to_log(f"Request timed out for thread {thread_id}") + raise TimeoutError("Request to graph service timed out. The operation took longer than expected.") + except requests.exceptions.RequestException as e: + write_to_log(f"Request failed for thread {thread_id}: {str(e)}") + raise @mcp.tool()