Cursor is an AI-first IDE. This guide shows how to use Invar with Cursor via MCP and rule files.
uv add --dev invar-tools invar-runtimecd your-project
uv run invar initThis creates:
CLAUDE.md— Agent guidance (can be referenced by any agent)INVAR.md— Protocol reference.pre-commit-config.yaml— Verification hook
Create .cursorrules in your project root for Cursor-specific guidance:
# Invar Development Protocol
## Critical Rules
| Always | Remember |
|--------|----------|
| **Verify** | Use invar_guard MCP tool — NOT pytest, NOT crosshair |
| **Core** | @pre/@post + doctests, NO I/O imports in core/ |
| **Shell** | Returns Result[T, E] from returns library |
| **Flow** | Contracts first → Build → Validate |
## Project Structure
src/{project}/ ├── core/ # Pure logic (@pre/@post, doctests, no I/O) └── shell/ # I/O operations (Result[T, E] return type)
## Workflow
### 1. SPECIFY
- Write @pre/@post BEFORE implementation
- Add doctests for expected behavior
### 2. BUILD
- Follow the contracts from SPECIFY
- Run invar_guard frequently
### 3. VALIDATE
- Run invar_guard (full verification)
- Ensure all requirements met
## Verification
ALWAYS use invar_guard instead of pytest:
invar_guard(changed=true)
pytest ... crosshair ...
## Contract Example
```python
from invar_runtime import pre, post
@pre(lambda x: x > 0, "x must be positive")
@post(lambda result: result >= 0)
def calculate(x: int) -> int:
"""
>>> calculate(10)
100
"""
return x * x
### 4. Configure MCP
In Cursor settings, add MCP server configuration:
**Option A: Using project environment (recommended)**
```json
{
"mcpServers": {
"invar": {
"command": "uv",
"args": ["run", "invar", "mcp"]
}
}
}
Option B: Using project venv
{
"mcpServers": {
"invar": {
"command": "${workspaceFolder}/.venv/bin/python",
"args": ["-m", "invar.mcp"]
}
}
}Cursor supports .cursor/rules/ directory:
.cursor/
└── rules/
├── invar-core.mdc
└── invar-verification.mdc
---
description: Invar core development rules
globs: ["src/**/core/**/*.py"]
alwaysApply: true
---
# Core Module Rules
This is a **core** module. Follow these rules:
1. **Pure functions only** - No I/O operations
2. **Contracts required** - Every public function needs @pre/@post
3. **Doctests required** - Include usage examples
4. **No forbidden imports** - No pathlib, os.path, requests, etc.
## Example
```python
from invar_runtime import pre, post
@pre(lambda x: x > 0)
@post(lambda result: result >= 0)
def process(x: int) -> int:
"""
>>> process(5)
25
"""
return x * x
### `invar-verification.mdc`
```markdown
---
description: Verification rules
globs: ["**/*.py"]
alwaysApply: true
---
# Verification
**NEVER** use pytest or crosshair directly.
**ALWAYS** use the invar_guard MCP tool.
invar_guard(changed=true)
pytest ...
| Invar Feature | Cursor Support |
|---|---|
| Guard Verification | ✅ Via MCP |
| Sig/Map Tools | ✅ Via MCP |
| Core/Shell Rules | ✅ Via rules |
| Claude Code | Cursor Alternative |
|---|---|
| CLAUDE.md | .cursorrules or .cursor/rules/ |
| MCP tools | Same MCP protocol |
Historical note: Pre-DX-91 versions supported hooks, skills, and USBV workflow ceremony. These were removed per DX-91 in favor of guard-enforced outcomes over process ceremony.
your-project/
├── .cursorrules # Simple rules (or use .cursor/rules/)
├── .cursor/ # Optional: rules directory
│ └── rules/
│ ├── invar-core.mdc
│ └── invar-verification.mdc
├── CLAUDE.md # Invar guidance (from init)
├── INVAR.md # Protocol reference (from init)
├── src/
│ └── your_package/
│ ├── core/ # Pure logic
│ └── shell/ # I/O operations
└── .pre-commit-config.yaml
- Check Cursor MCP settings
- Verify installation:
pip show invar-tools - Test directly:
uv run invar mcp
- Use
.cursor/rules/*.mdcformat (recommended) - Check
globspatterns match your files - Verify
alwaysApplysetting
Add explicit instruction to .cursorrules:
## IMPORTANT
NEVER run pytest or crosshair directly.
ALWAYS use the invar_guard MCP tool for verification.- Use MDC rules - More organized than single .cursorrules
- Be explicit - "Run invar_guard" not "run tests"
- Glob patterns - Target rules to specific directories