repo-contract
An MCP server that enforces repository governance rules for AI coding agents, providing tools to validate plans, diffs, and scan for architectural and safety violations.
README
repo-contract — AI Agent Governance Layer
repo-contract is a lightweight, portable Python package and command-line tool that acts as an enforceable governance layer for AI coding agents (such as Claude Code, Cursor, Codex, Windsurf, Aider, etc.).
It allows repositories to declare machine-checkable architectural, safety, and workflow rules. The engine deterministically validates proposed agent plans (pre-edit) and git diff patches (post-edit) to ensure compliance before code is modified or merged.
Why Governance?
AI agents are excellent at generating edits, but often fail to respect repository-specific constraints:
- Scope Creep: Editing unrelated components or refactoring large sub-systems.
- Architectural Regressions: Importing internal database handlers directly into view controllers.
- Workflow Breaches: Editing crucial functionality without adding corresponding tests.
- Safety Violations: Reading/modifying forbidden files (
.env,secrets/) or sensitive directories.
Rather than trying to help agents "understand more code" or storing synthetic meaning, repo-contract focuses on enforcing negative constraints, which empirical studies show is the most effective way to shape safe agent workflows.
Key Features
- YAML/TOML Contracts: Declarative policy schema containing architecture layers, safety paths, agent limits, and test mappings.
- Python Module Inspector: AST-based import resolution (both absolute and relative package paths).
- Plan Validation: Pre-checks an agent's text/markdown plan block, extracting planned files and verifying safety boundaries before edits begin.
- Diff Validation: Analyzes git patches/diffs to catch forbidden imports, path violations, test gaps, and file counts.
- Dependency Guard: Warns when a diff adds imports of external packages that are not declared in
pyproject.toml/requirements.txt/setup.py— a common AI-agent failure mode. - Unrelated Refactor Detection: Identifies whether modifications span disjoint, unconnected modules in the import graph (connected components analysis).
- Deterministic Engine: 100% deterministic rules with clear, human-readable explanations and JSON-RPC outputs.
- MCP Server Integration: Stdio-based Model Context Protocol (MCP) server wrapper that exposes validators directly as tools for AI assistants.
Installation
pip install repo-contract
Requires Python 3.11 or newer. This installs the repo-contract command along with the MCP server.
<details> <summary>Installing from source (for development)</summary>
git clone https://github.com/dager23/RepoContract.git
cd RepoContract
pip install -e ".[dev]"
pytest
</details>
Quick Start
1. Initialize the Contract
Create a default configuration file in your repository:
repo-contract init
# Generates repo-contract.yaml
2. Configure Rules
Modify repo-contract.yaml (or TOML equivalent) to define your rules:
version: 1
project:
name: my-app
src_root: "my_app" # Directory containing python package modules
architecture:
layers:
- name: ui
paths: ["my_app/ui/**"]
# Entries may be layer names ("db"), dotted module prefixes ("my_app.db"),
# or path prefixes ("my_app/db") — all three are equivalent.
cannot_import: ["my_app/db"] # UI layer cannot import DB layer directly
- name: db
paths: ["my_app/db/**"]
cannot_import: ["my_app/ui"]
workflow:
required_tests:
# Require corresponding test file to be modified when source is modified
- when_paths_match: ["my_app/**/*.py"]
require: ["tests/**/test_*.py"]
safety:
forbidden_paths:
- ".env"
- "secrets/**"
approval_required_for:
- "my_app/auth/**"
agent:
max_files_per_change: 6
forbid_unrelated_refactors: true
dependencies:
# Warn when a diff adds imports of packages not declared in the
# project's dependency files (pyproject.toml / requirements.txt / setup.py)
check_undeclared: true
3. Run Validation
Scan Codebase
Scan the entire repository for static layer boundary violations or missing tests:
repo-contract scan
Validate Agent Plan (Pre-edit)
Pass the agent's proposed plan (as raw text or from a file) to ensure they aren't touching forbidden files or planning changes that are too broad:
repo-contract check-plan "I will edit my_app/auth/login.py and .env to configure authentication."
# Fails because .env is forbidden and auth/ requires approval.
# Or validate a plan saved to a file:
repo-contract check-plan plan.md
If the argument is a path to an existing file it is read as a plan file; otherwise it is treated as raw plan text (use --text to force raw-text interpretation).
Validate Git Diff (Post-edit / CI Gate)
Check uncommitted working tree edits, a specific git commit range, or a patch file:
# Validate local uncommitted edits
repo-contract check-diff
# Validate staging changes
repo-contract check-diff --staged
# Validate range (e.g. CI run)
repo-contract check-diff HEAD~1..HEAD
# Validate a patch file
repo-contract check-diff my-change.patch
Model Context Protocol (MCP) Integration
You can run repo-contract as a stdio-based MCP Server in your favorite editor (like Cursor, Claude Desktop, or Windsurf) by adding this command to your configuration:
{
"mcpServers": {
"repo-contract": {
"command": "python",
"args": ["-m", "repo_contract.mcp.server"],
"cwd": "/path/to/your/project"
}
}
}
Exposed Tools
scan_repository: Runs structural checks on the current workspace.check_plan: Analyzes the agent's textual/markdown plan, extracting planned files and validating bounds before edits begin.check_diff: Checks current uncommitted working directory edits.
License
MIT © Yash Shah
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。