Harden MCP article and weight workflows

This commit is contained in:
2026-09-25 16:34:49 +02:00
parent 1169df96a8
commit 580e9caa5e
6 changed files with 181 additions and 12 deletions
+58 -1
View File
@@ -374,6 +374,63 @@ def get_article(
return repository.resolve(article_number, article_name_hint)
def calculate_material_weight(
*,
roll_length_m: Any,
width_m: Any,
area_weight_g_m2: Any,
) -> dict[str, Any]:
"""Calculate material-only roll weight without a core or diameter assumption."""
values = {
"roll_length_m": roll_length_m,
"width_m": width_m,
"area_weight_g_m2": area_weight_g_m2,
}
invalid = []
normalized = {}
for field, value in values.items():
if isinstance(value, bool) or not isinstance(value, (int, float)):
invalid.append({"field": field, "message": "must be a number"})
continue
number = float(value)
if not math.isfinite(number) or number <= 0:
invalid.append(
{"field": field, "message": "must be greater than zero"}
)
continue
normalized[field] = number
if invalid:
return {"status": "invalid_parameter", "invalid": invalid}
material_weight = _material_weight_kg(
normalized["roll_length_m"],
normalized["width_m"],
normalized["area_weight_g_m2"],
)
if not math.isfinite(material_weight) or material_weight <= 0:
return {
"status": "invalid_parameter",
"invalid": [
{
"field": "request",
"message": "values are outside the supported calculation range",
}
],
}
return {
"status": "success",
"weight_scope": "material_only",
"inputs": normalized,
"material_weight_kg": material_weight,
}
def _material_weight_kg(
roll_length_m: float, width_m: float, area_weight_g_m2: float
) -> float:
return area_weight_g_m2 * roll_length_m * width_m / 1000
def _canonical_name(value: str) -> str:
value = value.casefold().replace("×", " x ")
tokens = re.findall(r"\d+(?:[.,]\d+)?|[^\W\d_]+", value)
@@ -746,7 +803,7 @@ def calculate_roll(
)
roll_weight = None
if request.include_roll_weight:
roll_weight = area_weight * length * width / 1000
roll_weight = _material_weight_kg(length, width, area_weight)
except OverflowError:
return _issue_result(
"invalid_parameter",