Skip to content

Commit 78d0292

Browse files
committed
Introduce REQUEST_TIMEOUT_SECONDS constant: replace hardcoded timeouts and improve error message clarity for timeout scenarios
1 parent 31f5cfe commit 78d0292

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/core/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
"""Core components for CodeAlive MCP server."""
22

33
from .client import CodeAliveContext, get_api_key_from_context, codealive_lifespan
4-
from .config import Config
4+
from .config import Config, REQUEST_TIMEOUT_SECONDS
55
from .logging import setup_debug_logging, log_api_request, log_api_response
66

77
__all__ = [
88
'CodeAliveContext',
99
'get_api_key_from_context',
1010
'codealive_lifespan',
1111
'Config',
12+
'REQUEST_TIMEOUT_SECONDS',
1213
'setup_debug_logging',
1314
'log_api_request',
1415
'log_api_response',

src/core/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from fastmcp import Context, FastMCP
1010
from fastmcp.server.dependencies import get_http_headers
1111

12-
from .config import Config
12+
from .config import Config, REQUEST_TIMEOUT_SECONDS
1313

1414

1515
@dataclass
@@ -68,7 +68,7 @@ async def codealive_lifespan(server: FastMCP) -> AsyncIterator[CodeAliveContext]
6868
"Authorization": f"Bearer {config.api_key}",
6969
"Content-Type": "application/json",
7070
},
71-
timeout=300.0,
71+
timeout=REQUEST_TIMEOUT_SECONDS,
7272
verify=config.verify_ssl,
7373
)
7474
else:
@@ -78,7 +78,7 @@ async def codealive_lifespan(server: FastMCP) -> AsyncIterator[CodeAliveContext]
7878
headers={
7979
"Content-Type": "application/json",
8080
},
81-
timeout=300.0,
81+
timeout=REQUEST_TIMEOUT_SECONDS,
8282
verify=config.verify_ssl,
8383
)
8484

src/core/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from dataclasses import dataclass
55
from typing import Optional
66

7+
# Request timeout in seconds (5 minutes)
8+
REQUEST_TIMEOUT_SECONDS = 300.0
9+
710

811
@dataclass
912
class Config:

src/utils/errors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import httpx
55
from fastmcp import Context
66

7+
from core.config import REQUEST_TIMEOUT_SECONDS
8+
79

810
async def handle_api_error(
911
ctx: Context,
@@ -21,6 +23,16 @@ async def handle_api_error(
2123
Returns:
2224
User-friendly error message string
2325
"""
26+
# Handle timeout errors first
27+
if isinstance(error, httpx.TimeoutException):
28+
timeout_minutes = int(REQUEST_TIMEOUT_SECONDS // 60)
29+
error_msg = (
30+
f"Request timeout during {operation}: The CodeAlive service did not respond within {timeout_minutes} minutes. "
31+
"This may happen due to temporarily overloaded LLMs. Please try again later."
32+
)
33+
await ctx.error(error_msg)
34+
return f"Error: {error_msg}"
35+
2436
if isinstance(error, httpx.HTTPStatusError):
2537
error_code = error.response.status_code
2638
error_detail = error.response.text

0 commit comments

Comments
 (0)