INNER CODE UNIT · Python
on_call_tool
ASCIT31/Dark-Moon · mcp/src/server.py:235
async def on_call_tool(self, context, call_next):
args = getattr(context.message, "arguments", None)
session_id = args.get("session_id") if isinstance(args, dict) else None
try:
result = await call_next(context)
except ToolError as exc:
raise ToolError(_sanitize_for_model(str(exc), session_id)) from None
except Exception as exc: # noqa: BLE001 — any tool exception must be scrubbed
raise ToolError(
_sanitize_for_model(f"{type(exc).__name__}: {exc}", session_id)
) from None
try:
content = getattr(result, "content", None)
if isinstance(content, list):
for block in content:
if getattr(block, "type", None) == "text" and isinstance(
getattr(block, "text", None), str
):