diff --git a/docs_mcp/server.py b/docs_mcp/server.py index 0371f43..07a2aa3 100644 --- a/docs_mcp/server.py +++ b/docs_mcp/server.py @@ -810,6 +810,21 @@ def weekly_digest( return "\n".join(lines) +def _pub_sort_key(pub: str) -> tuple[int, int]: + """Sortable (year, month) for an HPE "Published" value like "July 2026". + + Plain string comparison ranks "March 2026" above "July 2026". + Unparseable values sort last. + """ + for fmt in ("%B %Y", "%b %Y", "%B %d, %Y", "%Y-%m-%d"): + try: + d = _dt.datetime.strptime(pub.strip(), fmt) + return (d.year, d.month) + except ValueError: + continue + return (0, 0) + + @mcp.tool() def corpus_status() -> str: """Freshness + size of the knowledge base. @@ -831,7 +846,7 @@ def corpus_status() -> str: for slug, b in cat.items(): pub = (b.get("dates") or {}).get("Published") if pub: - if latest_pub is None or pub > latest_pub: + if latest_pub is None or _pub_sort_key(pub) > _pub_sort_key(latest_pub): latest_pub = pub per_bundle.append((slug, pub)) if latest_pub: @@ -850,7 +865,7 @@ def corpus_status() -> str: "", ] if per_bundle: - per_bundle.sort(key=lambda kv: kv[1], reverse=True) + per_bundle.sort(key=lambda kv: _pub_sort_key(kv[1]), reverse=True) lines.append("## Most-recently-edited bundles (by HPE)") for slug, when in per_bundle[:5]: b = cat.get(slug, {})