add 15s timeout to MCP tool calls

This commit is contained in:
2026-04-14 11:18:08 -03:00
parent 318e8ab03e
commit ffde483c71
2 changed files with 18 additions and 4 deletions

View File

@@ -74,13 +74,20 @@ async def run_fce(
origin = flight_status.get("origin", "") origin = flight_status.get("origin", "")
destination = flight_status.get("destination", "") destination = flight_status.get("destination", "")
async def _call(server, tool, args, is_live=False): async def _call(server, tool, args, is_live=False, timeout=15.0):
t = time.time() t = time.time()
try: try:
result = await mcp.call_tool(server, tool, args) result = await asyncio.wait_for(
mcp.call_tool(server, tool, args), timeout=timeout,
)
lat = int((time.time() - t) * 1000) lat = int((time.time() - t) * 1000)
await emit("tool_call_end", tool=tool, latency_ms=lat, is_live=is_live) await emit("tool_call_end", tool=tool, latency_ms=lat, is_live=is_live)
return result return result
except asyncio.TimeoutError:
lat = int((time.time() - t) * 1000)
await emit("tool_call_error", tool=tool, error="timeout", latency_ms=lat)
errors.append(f"{tool}: timeout after {timeout}s")
return None
except Exception as e: except Exception as e:
lat = int((time.time() - t) * 1000) lat = int((time.time() - t) * 1000)
await emit("tool_call_error", tool=tool, error=str(e), latency_ms=lat) await emit("tool_call_error", tool=tool, error=str(e), latency_ms=lat)

View File

@@ -52,13 +52,20 @@ async def run_handover(
await emit("node_enter", node="gather_all") await emit("node_enter", node="gather_all")
async def _call(server, tool, args, is_live=False): async def _call(server, tool, args, is_live=False, timeout=15.0):
t = time.time() t = time.time()
try: try:
result = await mcp.call_tool(server, tool, args) result = await asyncio.wait_for(
mcp.call_tool(server, tool, args), timeout=timeout,
)
lat = int((time.time() - t) * 1000) lat = int((time.time() - t) * 1000)
await emit("tool_call_end", tool=tool, latency_ms=lat, is_live=is_live) await emit("tool_call_end", tool=tool, latency_ms=lat, is_live=is_live)
return result return result
except asyncio.TimeoutError:
lat = int((time.time() - t) * 1000)
await emit("tool_call_error", tool=tool, error="timeout", latency_ms=lat)
errors.append(f"{tool}: timeout after {timeout}s")
return None
except Exception as e: except Exception as e:
lat = int((time.time() - t) * 1000) lat = int((time.time() - t) * 1000)
await emit("tool_call_error", tool=tool, error=str(e), latency_ms=lat) await emit("tool_call_error", tool=tool, error=str(e), latency_ms=lat)