check_license_header
Verify Python files in a git repository contain required license headers in the first five lines, checking for Copyright or SPDX-License-Identifier text without network access or file modifications.
Instructions
Deterministic, network-free, read-only Tier 1 guardian.
Checks .py files tracked by git to ensure the first 5 lines contain "Copyright" OR "SPDX-License-Identifier".
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes |
Implementation Reference
- The implementation of the check_license_header tool which checks if .py files tracked by git contain a copyright notice or SPDX-License-Identifier in the first 5 lines.
def check_license_header(repo_path: str) -> Dict[str, Any]: """ Deterministic, network-free, read-only Tier 1 guardian. Checks .py files tracked by git to ensure the first 5 lines contain "Copyright" OR "SPDX-License-Identifier". """ if not _repo_path_is_valid(repo_path): return _fail_closed("fail-closed: invalid_repo_path", repo_path) abs_repo = os.path.abspath(repo_path) try: tracked = _run_git_ls_files(abs_repo) except RuntimeError as e: return _fail_closed(f"fail-closed: {str(e)}", repo_path, output={"files_missing_header": [], "notes": ["git ls-files failed"]}) py_files = [p for p in tracked if p.endswith(".py")] missing: List[str] = [] for rel in py_files: full = os.path.join(abs_repo, rel) if not _first_n_lines_contains_required_marker(full, n=5): missing.append(rel) missing.sort() output = { "files_missing_header": missing, "notes": [ "scope: tracked .py files only", "rule: first 5 lines contain Copyright OR SPDX-License-Identifier", "listing: git ls-files", ], } if missing: return { "tool": "check_license_header", "repo_path": repo_path, "ok": False, "fail_closed": True, "details": "fail-closed: missing_license_header", "output": output, } return { "tool": "check_license_header", "repo_path": repo_path, "ok": True, "fail_closed": False, "details": "ok", "output": output, } - src/mcp_license_header_guardian/server.py:74-74 (registration)MCP tool registration for check_license_header.
@mcp.tool() - Helper function that checks if the first N lines of a file contain the required license markers.
def _first_n_lines_contains_required_marker(path: str, n: int = 5) -> bool: try: with open(path, "rb") as f: raw = f.read(8192) except Exception: return False text = raw.decode("utf-8", errors="replace") lines = text.splitlines()[:n] for line in lines: if "Copyright" in line or "SPDX-License-Identifier" in line: return True