Files
claude-config/bin/repo_checks/test_headless_mcp.py
T

40 lines
2.1 KiB
Python

import ast
import pathlib
import subprocess
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from repo_checks import ROOT
def denies_tools_without_strict_mcp(source: str) -> bool:
"""Whether the source passes "--tools" more often than "--strict-mcp-config", so a call that denies tools would load the MCP servers of the user's or the project's config."""
strings = [node.value for node in ast.walk(ast.parse(source)) if isinstance(node, ast.Constant) and isinstance(node.value, str)]
return strings.count("--tools") > strings.count("--strict-mcp-config")
class HeadlessMcp(unittest.TestCase):
def test_each_file_that_passes_tools_passes_strict_mcp_config_too(self):
# Untracked files count too, so a new call is checked before its first commit.
listed = subprocess.run(["git", "ls-files", "--cached", "--others", "--exclude-standard", "*.py"], capture_output=True, check=True, cwd=ROOT, text=True).stdout.split()
self.assertIn("reviewer-merge-trials/analyze.py", listed)
self.assertIn("bin/agent_cycle/judge.py", listed)
files = [name for name in listed if not pathlib.PurePath(name).name.startswith("test_")]
lacking = {name for name in files if denies_tools_without_strict_mcp((ROOT / name).read_text(encoding="utf-8"))}
self.assertEqual(lacking, set(), "add --strict-mcp-config beside --tools, so no MCP server loads where the prompt holds text an outsider can write")
def test_a_file_with_tools_and_no_strict_mcp_config_is_found(self):
self.assertTrue(denies_tools_without_strict_mcp('args = ["--tools", ""]'))
self.assertFalse(denies_tools_without_strict_mcp('args = ["--tools", "", "--strict-mcp-config"]'))
self.assertFalse(denies_tools_without_strict_mcp('args = ["-p"]'))
def test_a_second_call_without_the_flag_is_found_in_a_file_where_another_call_has_it(self):
self.assertTrue(denies_tools_without_strict_mcp('a = ["--tools", "", "--strict-mcp-config"]\nb = ["--tools", ""]'))
self.assertFalse(denies_tools_without_strict_mcp('a = ["--tools", "", "--strict-mcp-config"]\nb = ["--tools", "", "--strict-mcp-config"]'))
if __name__ == "__main__":
unittest.main()