refactor(mcp)!: derive a surface's presence from its peers

enabled and shared were hand-maintained in 24 roles and wrong in both
directions. Both now resolve from the mcp_consumer edges already declared
in meta/services.yml: a server takes the consumers it admits, a client
takes the servers that admit it, and the pair renders as "'peer' in
group_names or ...", or False when no peer is admitted at all. setdefault
keeps meta/mcp.yml and meta/variants.yml as the override channel, so
pinning either key still wins.

The resolved block replaces the mapping instead of being written into it.
_build_role_base_config parks the YAML cache's own object under mcp, so an
in-place setdefault injected the derived keys into every later raw read and
made test_mcp_schema pass alone but fail in a directory run.

The derivation keys on peer presence, never on a peer's own value: a
re-entrant read under _RENDER_GUARD returns the unrendered dict.

Four lints now ask the resolver instead of reading meta/mcp.yml, which they
could not do while the key was hand-written. That is what exposed the eleven
missing hostile-path specs the previous commit closed.

Carries one change that belongs to the WordPress probe but shares a file
with this one: the tool allowlist moves to the hyphenated names the adapter
actually serves.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-09-01 16:13:01 +02:00
1 parent c94a0a0b5d
commit ffdef28da3
61 files changed
+292 -322

No files matched your search

+134
View File
@@ -19,9 +19,11 @@ from pathlib import Path
from utils.cache import _reset_cache_for_tests
from utils.cache import applications as cache_apps
from utils.cache.yaml import load_yaml_any
from utils.roles.mapping import (
ROLE_DIR_META_ADDONS,
ROLE_FILE_META_MAIN,
ROLE_FILE_META_MCP,
ROLE_FILE_META_SERVICES,
ROLE_FILE_META_USERS,
ROLE_FILE_META_VARIANTS,
@@ -449,5 +451,137 @@ class TestApplicationsImportableWithoutAnsible(unittest.TestCase):
self.assertIn("OK", result.stdout)
class TestDeriveMcpPresence(unittest.TestCase):
def setUp(self) -> None:
_reset_cache_for_tests()
@staticmethod
def _seed_pair(tmp: Path) -> Path:
roles = tmp / "roles"
_write(
roles / "web-app-prov" / ROLE_FILE_META_SERVICES,
"""
prov:
image: prov
version: latest
cons:
mcp_consumer: true
""",
)
_write(
roles / "web-app-prov" / ROLE_FILE_META_MCP,
"""
direction: server
""",
)
_write(
roles / "web-app-cons" / ROLE_FILE_META_SERVICES,
"""
cons:
image: cons
version: latest
mcp_consumer: true
""",
)
_write(
roles / "web-app-cons" / ROLE_FILE_META_MCP,
"""
direction: client
""",
)
return roles
def test_provider_resolves_to_its_admitted_consumers(self):
with tempfile.TemporaryDirectory() as tmp:
roles = self._seed_pair(Path(tmp))
defaults = cache_apps.get_application_defaults(roles_dir=roles)
self.assertEqual(
defaults["web-app-prov"]["mcp"]["enabled"],
"{{ 'web-app-cons' in group_names }}",
)
self.assertEqual(
defaults["web-app-prov"]["mcp"]["shared"],
defaults["web-app-prov"]["mcp"]["enabled"],
)
def test_provider_that_excludes_its_only_consumer_resolves_to_false(self):
with tempfile.TemporaryDirectory() as tmp:
roles = self._seed_pair(Path(tmp))
_write(
roles / "web-app-prov" / ROLE_FILE_META_SERVICES,
"""
prov:
image: prov
version: latest
cons:
mcp_consumer: false
""",
)
defaults = cache_apps.get_application_defaults(roles_dir=roles)
self.assertIs(defaults["web-app-prov"]["mcp"]["enabled"], False)
def test_configured_value_wins_over_the_derivation(self):
with tempfile.TemporaryDirectory() as tmp:
roles = self._seed_pair(Path(tmp))
_write(
roles / "web-app-prov" / ROLE_FILE_META_MCP,
"""
direction: server
enabled: false
shared: false
""",
)
defaults = cache_apps.get_application_defaults(roles_dir=roles)
self.assertIs(defaults["web-app-prov"]["mcp"]["enabled"], False)
def test_every_variant_carries_the_derived_value(self):
with tempfile.TemporaryDirectory() as tmp:
roles = self._seed_pair(Path(tmp))
_write(
roles / "web-app-prov" / ROLE_FILE_META_VARIANTS,
"""
- {}
- services:
prov:
image: prov-alt
""",
)
variants = cache_apps.get_variants(roles_dir=roles)["web-app-prov"]
self.assertEqual(len(variants), 2)
for variant in variants:
self.assertEqual(
variant["mcp"]["enabled"],
"{{ 'web-app-cons' in group_names }}",
)
def test_derivation_leaves_the_yaml_cache_untouched(self):
with tempfile.TemporaryDirectory() as tmp:
roles = self._seed_pair(Path(tmp))
mcp_path = roles / "web-app-prov" / ROLE_FILE_META_MCP
cache_apps.get_application_defaults(roles_dir=roles)
raw = load_yaml_any(str(mcp_path), default_if_missing={})
self.assertNotIn("enabled", raw)
self.assertNotIn("shared", raw)
def test_variant_override_wins_over_the_derivation(self):
with tempfile.TemporaryDirectory() as tmp:
roles = self._seed_pair(Path(tmp))
_write(
roles / "web-app-prov" / ROLE_FILE_META_VARIANTS,
"""
- {}
- mcp:
enabled: false
shared: false
""",
)
variants = cache_apps.get_variants(roles_dir=roles)["web-app-prov"]
self.assertEqual(
variants[0]["mcp"]["enabled"],
"{{ 'web-app-cons' in group_names }}",
)
self.assertIs(variants[1]["mcp"]["enabled"], False)
if __name__ == "__main__":
unittest.main()