Policy framework for Claude Code and Cursor hooks
Project description
AI Agent Policies
Policies turn your Cursor Rules or CLAUDE.md into hard guardrails which an AI Agent cannot simply ignore, or forget. They handle what to do when an agent wants to make a decision, along with other hooks-supported events. Policies can yield both decisions and guidance.
This framework supports Claude Code. Support for Cursor is in beta.
Why Policies
Automating Decisions
Rule files can be forgotten or ignored completely by LLMs. Policies are unavoidable:
if re.match(r'^terraform\s+apply(?:\s|$)', command):
yield PolicyDecision(action=PolicyAction.DENY, reason="terraform apply is not allowed. Use `terraform plan` instead.")
if re.match(r'^terraform\s+(fmt|plan)(?:\s|$)', command):
yield PolicyDecision(action=PolicyAction.ALLOW)
[!WARNING]
Be aware when automatically allowing that Bash tools use strings can invole more than one underlying tool. Consider also commands such asfindhaving unsafe options like-exec.
Automating Guidance
Aside from denying and allowing automatically, policies can also provide guidance:
if re.match(r'^python\s+test_', input_data.command):
yield PolicyGuidance(content="Consider using pytest instead of running test files directly")
Usage
At DevLeaps we developed an internal policy set for AI Agents. To create your own, refer to the example server as a starting point The example server contains:
- A basic server setup demonstrating the use of policies and middleware.
- Rudimentary policies showcasing how to automatically deny, allow and provide guidance.
- Rudimentary middleware showcasing how multi-command tool use could be handled.
To run the example server:
devleaps-policy-example-server
This starts a minimal server running just our example policies.
Architecture
graph TB
subgraph "Developer Machine"
Editor[Claude Code / Cursor]
Client[devleaps-policy-client]
end
subgraph "Policy Server"
Server[HTTP API]
Policies[Your policies<br/>kubectl, terraform, git, python, etc.]
end
Editor -->|Hooks| Client
Client --> Server
Server -->|Events| Policies
Policies -->|Decision and Guidance| Server
Server --> Client
Client -->|Decision and Guidance| Editor
Quick Start
Installation
Update your local profile with;
# Add the bin directory to $PATH
export PATH="$PATH:/path/to/agent-policies/bin/"
Running an Example Server
devleaps-policy-example-server
The example server runs on port 8338 by default and serves endpoints for both Claude Code and Cursor.
Configure Claude Code
Add devleaps-policy-client to your Claude Code hooks configuration in ~/.claude/settings.json:
Click to expand Claude Code configuration
{
"hooks": {
"PreToolUse": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"PostToolUse": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"UserPromptSubmit": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"Stop": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"SubagentStop": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"Notification": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"PreCompact": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"SessionStart": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
],
"SessionEnd": [
{
"hooks": [
{
"matcher": "*",
"type": "command",
"command": "devleaps-policy-client claude-code"
}
]
}
]
}
}
Configure Cursor
Create or edit ~/.cursor/hooks.json:
Click to expand Cursor configuration
{
"version": 1,
"hooks": {
"beforeShellExecution": [
{ "command": "devleaps-policy-client cursor" }
],
"beforeMCPExecution": [
{ "command": "devleaps-policy-client cursor" }
],
"afterFileEdit": [
{ "command": "devleaps-policy-client cursor" }
],
"beforeReadFile": [
{ "command": "devleaps-policy-client cursor" }
],
"beforeSubmitPrompt": [
{ "command": "devleaps-policy-client cursor" }
],
"stop": [
{ "command": "devleaps-policy-client cursor" }
]
}
}
The devleaps-policy-client cursor command will forward hook events to the policy server running on localhost:8338.
Development
This project is built with uv.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file devleaps_agent_policies-1.0.4.tar.gz.
File metadata
- Download URL: devleaps_agent_policies-1.0.4.tar.gz
- Upload date:
- Size: 43.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4d2d501d056a85b325c47d2a0514f706eb7c3fe550a5a8f247733361a7379fe
|
|
| MD5 |
0627ebe7aa617f847fcc39139bdc8a3d
|
|
| BLAKE2b-256 |
eab2ca147757c270f720a468fe365b3c29d9ff00ae109981bc54af119b9634b8
|
File details
Details for the file devleaps_agent_policies-1.0.4-py3-none-any.whl.
File metadata
- Download URL: devleaps_agent_policies-1.0.4-py3-none-any.whl
- Upload date:
- Size: 28.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8253da2d73590eda692f98b95226eaf4c396384c2439cc7b3910b27d1e755f9
|
|
| MD5 |
2bcf57c5d1cfd17dd1e2ead264615aef
|
|
| BLAKE2b-256 |
dba58561d0c41311373ae643b4520a164aa0826b65459a13b5cc6928aaca8b71
|