INNER CODE UNIT · Python

_safe

tiliondev/fortress · mcp/server.py:46

def _safe(fn):
    """Error boundary for a tool: enforce a wall-clock timeout AND on any failure
    return a STRUCTURED error the agent can reason about ({status:'error', error})
    instead of an opaque protocol error, so a bad/slow call never crashes or wedges
    the server. Preserves the signature so FastMCP introspects it."""
    @functools.wraps(fn)
    async def wrap(*args, **kwargs):
        try:
            return await asyncio.wait_for(fn(*args, **kwargs), timeout=_TOOL_TIMEOUT)
        except asyncio.TimeoutError:
            return {"status": "error",
                    "error": f"tool timed out after {_TOOL_TIMEOUT:.0f}s "
                             f"(raise TILION_MCP_TOOL_TIMEOUT for slow crawls)"}
        except Exception as exc:  # noqa: BLE001 - deliberate catch-all at the tool boundary
            return {"status": "error", "error": f"{type(exc).__name__}: {exc}"}
    return wrap

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…