utils.py•1.27 kB
"""Utility helpers shared across the MCP server."""
from __future__ import annotations
from typing import Any, Mapping
class OccApiError(RuntimeError):
"""Raised when the OCC API returns an error we cannot recover from."""
def __init__(self, message: str, *, status_code: int | None = None, payload: Any | None = None) -> None:
super().__init__(message)
self.status_code = status_code
self.payload = payload
def to_dict(self) -> dict[str, Any]:
"""Provide a JSON-serializable representation of the error."""
data: dict[str, Any] = {"message": str(self)}
if self.status_code is not None:
data["status_code"] = self.status_code
if self.payload is not None:
data["payload"] = self.payload
return data
def coalesce_fields(fields: str | None, default: str) -> str:
"""Return the provided fields value if present, otherwise fall back to the default."""
if fields and fields.strip():
return fields.strip()
return default
def clean_params(params: Mapping[str, Any]) -> dict[str, Any]:
"""Remove keys whose values are ``None`` while preserving falsy values like 0."""
return {key: value for key, value in params.items() if value is not None}