Skip to content

Commit 5adb109

Browse files
committed
feat(docs[_ext]): Add safety badges to homepage section headings
why: The homepage "What you can do" section had parenthesized tier names like "Inspect (readonly)" — should use colored badges like tools/index. what: - Extend _add_section_badges to run on both tools/index and index pages - Match parenthesized headings ("Inspect (readonly)") and replace the parenthesized text with a colored badge - Add 2 tests: parenthesized variant, homepage page scoping
1 parent 989c41a commit 5adb109

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

docs/_ext/fastmcp_autodoc.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -798,29 +798,51 @@ def _register_tool_labels(app: Sphinx, doctree: nodes.document) -> None:
798798
domain.labels[section_id] = (docname, section_id, tool_name)
799799

800800

801+
_SECTION_BADGE_PAGES: set[str] = {"tools/index", "index"}
802+
803+
801804
def _add_section_badges(
802805
app: Sphinx,
803806
doctree: nodes.document,
804807
fromdocname: str,
805808
) -> None:
806-
"""Append safety badges to Inspect/Act/Destroy section headings.
809+
"""Replace parenthesized tier names with colored badges in headings.
810+
811+
Matches both bare headings (``Inspect``) and parenthesized variants
812+
(``Inspect (readonly)``). The parenthesized text is stripped and
813+
replaced with a badge node.
807814
808-
Only applied to the tools index page — individual tool pages already
809-
have per-tool badges, making section-level badges redundant.
815+
Only applied to pages in ``_SECTION_BADGE_PAGES`` — individual tool
816+
pages already have per-tool badges, making section-level badges
817+
redundant.
810818
811819
Runs at ``doctree-resolved`` — section IDs are already frozen, so
812-
appending nodes to the title doesn't affect anchors or cross-refs.
820+
modifying the title doesn't affect anchors or cross-refs.
813821
"""
814-
if fromdocname != "tools/index":
822+
if fromdocname not in _SECTION_BADGE_PAGES:
815823
return
816824
for section in doctree.findall(nodes.section):
817825
if not section.children or not isinstance(section[0], nodes.title):
818826
continue
819827
title_text = section[0].astext().strip()
828+
829+
# Try exact match first ("Inspect")
820830
safety = SECTION_BADGE_MAP.get(title_text)
821831
if safety is not None:
822832
section[0] += nodes.Text(" ")
823833
section[0] += _safety_badge(safety)
834+
continue
835+
836+
# Try parenthesized match ("Inspect (readonly)")
837+
m = re.match(r"^(\w+)\s*\((\w+)\)$", title_text)
838+
if m:
839+
heading, tier = m.group(1), m.group(2)
840+
if heading in SECTION_BADGE_MAP and tier == SECTION_BADGE_MAP[heading]:
841+
# Replace title children: strip parenthesized text, add badge
842+
title_node = section[0]
843+
title_node.clear()
844+
title_node += nodes.Text(heading + " ")
845+
title_node += _safety_badge(tier)
824846

825847

826848
class _tool_ref_placeholder(nodes.General, nodes.Inline, nodes.Element):

tests/docs/_ext/test_fastmcp_autodoc.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,35 @@ def test_add_section_badges_ignores_non_matching() -> None:
573573
assert title.astext() == "Overview"
574574

575575

576+
def test_add_section_badges_parenthesized_on_index() -> None:
577+
"""_add_section_badges replaces 'Inspect (readonly)' with badge on index."""
578+
doc, _section, title = _make_doc_with_section(
579+
"inspect-readonly", "Inspect (readonly)"
580+
)
581+
582+
fastmcp_autodoc._add_section_badges(None, doc, "index")
583+
584+
# Title should be: Text("Inspect "), inline(badge)
585+
assert len(title.children) == 2
586+
assert title.children[0].astext() == "Inspect "
587+
badge = title.children[1]
588+
assert isinstance(badge, nodes.inline)
589+
assert "sd-bg-success" in badge["classes"]
590+
assert badge.astext() == "readonly"
591+
592+
593+
def test_add_section_badges_works_on_homepage() -> None:
594+
"""_add_section_badges runs on the homepage (fromdocname='index')."""
595+
doc, _section, title = _make_doc_with_section("act", "Act")
596+
597+
fastmcp_autodoc._add_section_badges(None, doc, "index")
598+
599+
assert len(title.children) == 3
600+
badge = title.children[2]
601+
assert isinstance(badge, nodes.inline)
602+
assert "sd-bg-warning" in badge["classes"]
603+
604+
576605
# ---------------------------------------------------------------------------
577606
# Integration: collect real tools
578607
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)