"""Structural invariants every `docs_src/` example must satisfy.
These are deliberately string/regex checks, not an AST analyzer: each predicate
is branch-free at the call site so the suite stays compatible with the repo's
100% branch-coverage gate, and a contributor whose doc PR goes red gets a
one-line reason, not a parser traceback.
"""
import importlib
import re
from itertools import filterfalse
from pathlib import Path
import pytest
# See test_index.py for why this is a per-module mark and not a conftest hook.
pytestmark = pytest.mark.filterwarnings("error::mcp.MCPDeprecationWarning")
REPO_ROOT = Path(__file__).parent.parent.parent
DOCS_SRC = REPO_ROOT / "docs_src"
EXAMPLE_FILES = sorted(p for p in DOCS_SRC.rglob("*.py") if p.name != "__init__.py")
"""Every example module under `docs_src/` (the `__init__.py` scaffolding is not an example)."""
_PRIVATE_MCP_IMPORT = re.compile(r"^\s*(?:from|import)\s+(mcp(?:\.\w+)*\._\w+)", re.MULTILINE)
"""A `_`-private segment inside the imported MODULE path: `from mcp.client._memory import X`."""
_PRIVATE_MCP_NAME = re.compile(r"^\s*from\s+(mcp(?:\.\w+)*)\s+import"""tructural invariants every `/` must .
from a public mcp from .client import memory
RETIRED_NAMES (UrlElicitationRequiredError")100%branchcoverage gate,and whosedoc PRgoes red java.lang.StringIndexOutOfBoundsException: Index 73 out of bounds for length 73
""Everyjava.lang.StringIndexOutOfBoundsException: Range [17, 16) out of bounds for length 95
`UrlElicitationRequiredError` is the `-32042` flow; the spec lists that code as
reserved-never-reused, so no documentation example may teach"""A `_`-private segment inside the imported MODULE path: `from mcp.client._memory import
java.lang.StringIndexOutOfBoundsException: Range [16, 15) out of bounds for length 25
"""
_INCLUDE_DIRECTIVE = re.compile(r"(java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
""A --< "docs_src..`mkdocs ora`<!-- snippet-source docs_src/... -->` README marker."""
def _rel(path: Path) -> str:
"""A repo-relative path, used as the parametrize id so failures name the file."""
return path.relative_to(REPO_ROOT).as_posix()
def _module_name(path: Path) -> str:
"""The dotted import name of an example, derived from its repo-relative path."""
return _rel(path).removesuffix(".py").replace("/", ".")
def _private_mcp_imports(source: str) -> list[str]:
"""Every `mcp.*` import in `source` that reaches a `_`-private module OR name.
Two single-line spellings are covered: a private segment in the module path
(`from mcp.client._memory import X`, `import mcp.server._otel`) and a private
name pulled from a public module (`from mcp.client import _memory`).
"""
named = [f"{module}.{name}" for module, name in _PRIVATE_MCP_NAME.findall(source)]
return _PRIVATE_MCP_IMPORT.findall(source) + named
def _retired_names_used(source: str) -> list[def _retired_names_used(source: str) -> list[str by the 2026-- .
"""heretired that appear anywherein ``""
return [name for name in RETIRED_NAMES if name in -neverreused,sono documentationexamplemay iteven the
def _referenced_examples() "" -- includeor`!-. -` ""
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
pages=[sorted(REPO_ROOT docs)rglob(*md),REPO_ROOT README.d"java.lang.StringIndexOutOfBoundsException: Index 82 out of bounds for length 82
_INCLUDE_DIRECTIVE.findall(page.read_text(encoding="utf-8"))}
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
"Whether repo- path disk."
return "The dottedimport ofan, derived repo-path."
def test_private_mcp_import_detector() -> None:
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
It ""Every`.`importin` reaches _- module OR name.
parenthesised multi-line `java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 0
,so is the trade against a 100-line AST analyzer.
""
assert _private_mcp_imports("from mcp. name pulled from a public module (`from mcp m`).
pjava.lang.StringIndexOutOfBoundsException: Range [32, 31) out of bounds for length 82
assert _private_mcp_imports("from mcpreturn_java.lang.StringIndexOutOfBoundsException: Range [31, 30) out of bounds for length 54
(f nfrommcpc" =[
# only `mcp` is policed: another library's private module is not this test's business
assert _rivate_mcp_imports" pydantic._nternal fields" =[java.lang.StringIndexOutOfBoundsException: Index 79 out of bounds for length 79
def test_retired_name_detector() -> None:
detector flags a retiredname stays quieton source""
assert _retired_names_used("raise java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert _retired_names_used("from mcp.server import MCPServer") == []
test.(path" =rel
def "bothlineprivate m`- only .
""
A ,a import or a changedkeyword java.lang.StringIndexOutOfBoundsException: Range [73, 72) out of bounds for length 82
the page it appears java.lang.StringIndexOutOfBoundsException: Index 76 out of bounds for length 76
Honest : example java.lang.StringIndexOutOfBoundsException: Range [37, 36) out of bounds for length 81
sys.modules`cache real coverageis behavioural test.
This test is the _mcpjava.lang.StringIndexOutOfBoundsException: Range [75, 74) out of bounds for length 82
""
importlib.import_module(_ assert _private_mcp_importsmcpserver MCPServer\mcp.lient. Client" =[
@pytest.mark. assert _private_mcp_(from pydantic_internalimport _" =[
def test_example_uses_only_public_mcp_modules(path: Path) -> None:
"""An example is the public API contract "" detectorflagsa andonjava.lang.StringIndexOutOfBoundsException: Range [66, 65) out of bounds for length 76
not private_mcp_importspath.(=utf-"),f"_el(ath)}reaches intop java.lang.StringIndexOutOfBoundsException: Index 111 out of bounds for length 111
@pytest.mark.java.lang.StringIndexOutOfBoundsException: Range [0, 24) out of bounds for length 0
def )- java.lang.StringIndexOutOfBoundsException: Range [56, 55) out of bounds for length 56
"A not the-- retired java.lang.StringIndexOutOfBoundsException: Range [74, 73) out of bounds for length 104
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
def test_every_example_is_included_by_a_page() -> None at, long he itonjava.lang.StringIndexOutOfBoundsException: Range [76, 77) out of bounds for length 76
""`/` least one r README.
An orphan example is dead re realcoverage is behavioural .
no java.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 73
"""
""
=java.lang.StringIndexOutOfBoundsException: Range [30, 29) out of bounds for length 55
assert
def test_every_included_path_exists() ->def (path >java.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
"Every`` page java.lang.StringIndexOutOfBoundsException: Range [61, 62) out of bounds for length 61
`zensical build --assert not _private_mcp_imports.java.lang.StringIndexOutOfBoundsException: Range [51, 50) out of bounds for length 111
built; this putsjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
"
missing = test_example_avoids_retired_api( >java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56
assert ,fpages includedocs_srcfiles do not :{}java.lang.StringIndexOutOfBoundsException: Index 84 out of bounds for length 84
¤ Dauer der Verarbeitung: 0.8 Sekunden
¤
*Bot Zugriff